<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.czg.service.market.mapper.MkPointsGoodsMapper">

    <select id="getPointsGoodsPageByUser" resultType="com.czg.market.entity.MkPointsGoods">
        select goods.*, count(record.id) as boughtCount
        from mk_points_goods goods
        left join mk_points_goods_record record
        on record.points_goods_id = goods.id and record.user_id = #{userId} and record.status != '已退款' and
        record.is_del = 0
        where goods.shop_id = #{shopId}
        and goods.del_flag = 0
        and goods.status = 1
        <if test="goodsCategory != null and goodsCategory !=''">
            and goods.goods_category = #{goodsCategory}
        </if>
        GROUP BY goods.id
        HAVING goods.id IS NOT NULL
        order by goods.sort desc, goods.id desc
    </select>
</mapper>
