<?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.kmall.api.dao.mk.ApiMkStoreCampMinusGoodsMapper">

    <resultMap type="com.kmall.api.entity.mk.MkStoreCampMinusGoodsVo" id="mkStoreCampMinusGoodsMap">
        <result property="minusGoodId" column="minus_good_id"/>
        <result property="campMinusId" column="camp_minus_id"/>
        <result property="applyType" column="apply_type"/>
        <result property="storeRelaId" column="store_rela_id"/>
        <result property="isValid" column="is_valid"/>
        <result property="note" column="note"/>
        <result property="createrSn" column="creater_sn"/>
        <result property="createTime" column="create_time"/>
        <result property="moderSn" column="moder_sn"/>
        <result property="modTime" column="mod_time"/>
        <result property="tstm" column="tstm"/>
    </resultMap>

	<!-- 可根据自己的需求，是否要使用 -->
	<resultMap type="com.kmall.api.entity.GoodsVo" id="goodsMap">
		<result property="id" column="id"/>
		<result property="category_id" column="category_id"/>
		<result property="goods_sn" column="goods_sn"/>
		<result property="name" column="name"/>
		<result property="brand_id" column="brand_id"/>
		<result property="goods_number" column="goods_number"/>
		<result property="keywords" column="keywords"/>
		<result property="goods_brief" column="goods_brief"/>
		<result property="goods_desc" column="goods_desc"/>
		<result property="is_on_sale" column="is_on_sale"/>
		<result property="add_time" column="add_time"/>
		<result property="sort_order" column="sort_order"/>
		<result property="is_delete" column="is_delete"/>
		<result property="attribute_category" column="attribute_category"/>
		<result property="counter_price" column="counter_price"/>
		<result property="extra_price" column="extra_price"/>
		<result property="is_new" column="is_new"/>
		<result property="goods_unit" column="goods_unit"/>
		<result property="primary_pic_url" column="primary_pic_url"/>
		<result property="list_pic_url" column="list_pic_url"/>
		<result property="retail_price" column="retail_price"/>
		<result property="market_price" column="market_price"/>
		<result property="sell_volume" column="sell_volume"/>
		<result property="primary_product_id" column="primary_product_id"/>
		<result property="unit_price" column="unit_price"/>
		<result property="promotion_desc" column="promotion_desc"/>
		<result property="promotion_tag" column="promotion_tag"/>
		<result property="app_exclusive_price" column="app_exclusive_price"/>
		<result property="is_app_exclusive" column="is_app_exclusive"/>
		<result property="is_limited" column="is_limited"/>
		<result property="is_hot" column="is_hot"/>
		<result property="product_id" column="product_id"/>
		<result property="sku" column="sku"/>
		<result property="goodsBizType" column="goods_biz_type"/>
		<result column="creater_sn" property="createrSn" jdbcType="VARCHAR" />
		<result column="create_time" property="createTime" jdbcType="TIMESTAMP" />
		<result column="moder_sn" property="moderSn" jdbcType="VARCHAR" />
		<result column="mod_time" property="modTime" jdbcType="TIMESTAMP" />
		<result column="tstm" property="tstm" jdbcType="TIMESTAMP" />
		<result property="goodsRate" column="goods_rate"/>
		<result property="stockNum" column="stock_num"/>
		<result property="storeId" column="store_id"/>
		<result property="merchSn" column="merch_sn"/>
		<result property="merchName" column="merch_name"/>
		<result column="third_party_merch_code" property="thirdPartyMerchCode" />
		<result column="isStockShare" property="isStockShare" />
		<result property="goods_sell_volume" column="goods_sell_volume"/>
	</resultMap>


	<select id="queryObject" resultType="com.kmall.api.entity.mk.MkStoreCampMinusGoodsVo">
		select
			`minus_good_id`,
			`camp_minus_id`,
			`apply_type`,
			`store_rela_id`,
			`is_valid`,
			`note`,
			`creater_sn`,
			`create_time`,
			`moder_sn`,
			`mod_time`,
			`tstm`
		from mk_store_camp_minus_goods
		where minus_good_id = #{id}
	</select>

	<select id="queryList" resultType="com.kmall.api.entity.mk.MkStoreCampMinusGoodsVo">
		select
    		`minus_good_id`,
    		`camp_minus_id`,
    		`apply_type`,
    		`store_rela_id`,
    		`is_valid`,
    		`note`,
    		`creater_sn`,
    		`create_time`,
    		`moder_sn`,
    		`mod_time`,
    		`tstm`
		from mk_store_camp_minus_goods
		WHERE 1=1
		<if test="name != null and name.trim() != ''">
			AND name LIKE concat('%',#{name},'%')
		</if>
        <choose>
            <when test="sidx != null and sidx.trim() != ''">
                order by ${sidx} ${order}
            </when>
			<otherwise>
                order by minus_good_id desc
			</otherwise>
        </choose>
		<if test="offset != null and limit != null">
			limit #{offset}, #{limit}
		</if>
	</select>
	
 	<select id="queryTotal" resultType="int">
		select count(*) from mk_store_camp_minus_goods
		WHERE 1=1
        <if test="name != null and name.trim() != ''">
            AND name LIKE concat('%',#{name},'%')
        </if>
	</select>

	<select id="queryCampMinusGoodsList" resultMap="goodsMap">
		select
		<if test="fields != null and fields != ''">
			${fields}
		</if>
		<if test="fields == null or fields == ''">
			a.*,b.id as product_id,psr1.category_id,psr1.brand_id,psr1.attribute_category
		</if>
		,a.goods_biz_type,
		a.goods_number,
		a.third_party_merch_code third_party_merch_code,
		mb.is_stock_share isStockShare,psr1.store_id,
		( SELECT sum( number ) FROM mall_cart WHERE goods_id = a.id AND store_id = psr1.store_id ) cart_num
		FROM
		mall_goods a
		LEFT JOIN mall_product_store_rela psr1 ON a.id = psr1.goods_id
		AND a.merch_sn = psr1.merch_sn
		LEFT JOIN mk_store_camp_minus_goods g ON psr1.id = g.store_rela_id
		<if test="campMinusId != null and campMinusId != ''">
			and g.camp_minus_id = #{campMinusId}
		</if>
		LEFT JOIN third_merchant_biz mb ON mb.third_party_merch_code = a.third_party_merch_code
		AND mb.merch_sn = a.merch_sn
		LEFT JOIN mall_product b ON b.id = psr1.product_id
		LEFT JOIN mall_category c ON psr1.category_id = c.id
		WHERE 1 = 1
		<if test="applyType != null and applyType == '01'">
			<if test="campMinusId != null and campMinusId != ''">
				and g.camp_minus_id = #{campMinusId}
			</if>
		</if>
		<if test="applyType != null and applyType == '02'">
			and psr1.id not in(SELECT store_rela_id FROM mk_store_camp_minus_goods WHERE camp_minus_id = #{campMinusId})
		</if>
		<if test="isStockShare == 1">
			and (a.goods_number > 0  or psr1.stock_num > 0 )
		</if>
		<if test="isStockShare == 0">
			and psr1.stock_num > 0
		</if>
		and b.id is not null and a.is_delete != 1 and psr1.retail_price is not null and a.is_on_sale = 1
		<if test="is_new != null and is_new != ''">
			and a.is_new = #{is_new}
		</if>
		<if test="brand_id != null and brand_id != ''">
			and psr1.brand_id = #{brand_id}
		</if>
		<if test="category_id != null and category_id != ''">
			and psr1.category_id = #{category_id}
		</if>
		<if test="keyword != null">
			and a.keywords like concat('%',#{keyword},'%')
		</if>
		<if test="categoryIds != null">
			and psr1.category_id in
			<foreach item="item" collection="categoryIds" open="(" separator="," close=")">
				#{item}
			</foreach>
		</if>
		<if test="category_parent_id != null and category_parent_id != null and category_parent_id != 0">
			and FIND_IN_SET(psr1.category_id, getCategoryChildLst(#{category_parent_id}))
		</if>
		<if test="goodsId != null and goodsId != ''">
			and a.id = #{goodsId}
		</if>
		<if test="store_id != null and store_id != ''">
			and psr1.store_id = #{store_id}
		</if>
		<if test="goodsBizType != null and goodsBizType != ''">
			and a.goods_biz_type = #{goodsBizType}
		</if>
		<choose>
			<when test="sidx != null and sidx.trim() != ''">
				order by ${sidx} ${order}
			</when>
			<otherwise>
				order by a.id desc
			</otherwise>
		</choose>
		<if test="offset != null and limit != null">
			limit #{offset}, #{limit}
		</if>
	</select>

	<select id="queryCampMinusGoodsTotal" resultType="int">
		select count(*)
		FROM
		mall_goods a
		LEFT JOIN mall_product_store_rela psr1 ON a.id = psr1.goods_id
		AND a.merch_sn = psr1.merch_sn
		LEFT JOIN mk_store_camp_minus_goods g ON psr1.id = g.store_rela_id
		<if test="campMinusId != null and campMinusId != ''">
			and g.camp_minus_id = #{campMinusId}
		</if>
		LEFT JOIN third_merchant_biz mb ON mb.third_party_merch_code = a.third_party_merch_code
		AND mb.merch_sn = a.merch_sn
		LEFT JOIN mall_product b ON b.id = psr1.product_id
		LEFT JOIN mall_category c ON psr1.category_id = c.id
		WHERE 1 = 1
		<if test="applyType != null and applyType == '01'">
			<if test="campMinusId != null and campMinusId != ''">
				and g.camp_minus_id = #{campMinusId}
			</if>
		</if>
		<if test="applyType != null and applyType == '02'">
			and psr1.id not in(SELECT store_rela_id FROM mk_store_camp_minus_goods WHERE camp_minus_id = #{campMinusId})
		</if>
		<if test="isStockShare == 1">
			and (a.goods_number > 0  or psr1.stock_num > 0 )
		</if>
		<if test="isStockShare == 0">
			and psr1.stock_num > 0
		</if>
		and b.id is not null and a.is_delete != 1 and psr1.retail_price is not null and a.is_on_sale = 1
		<if test="is_new != null and is_new != ''">
			and a.is_new = #{is_new}
		</if>
		<if test="brand_id != null and brand_id != ''">
			and psr1.brand_id = #{brand_id}
		</if>
		<if test="category_id != null and category_id != ''">
			and psr1.category_id = #{category_id}
		</if>
		<if test="keyword != null">
			and a.keywords like concat('%',#{keyword},'%')
		</if>
		<if test="categoryIds != null">
			and psr1.category_id in
			<foreach item="item" collection="categoryIds" open="(" separator="," close=")">
				#{item}
			</foreach>
		</if>
		<if test="category_parent_id != null and category_parent_id != null and category_parent_id != 0">
			and FIND_IN_SET(psr1.category_id, getCategoryChildLst(#{category_parent_id}))
		</if>
		<if test="goodsId != null and goodsId != ''">
			and a.id = #{goodsId}
		</if>
		<if test="store_id != null and store_id != ''">
			and psr1.store_id = #{store_id}
		</if>
		<if test="goodsBizType != null and goodsBizType != ''">
			and a.goods_biz_type = #{goodsBizType}
		</if>
	</select>

</mapper>