<?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.admin.dao.mk.store.MkStoreCampMinusGoodsDao">

    <resultMap type="com.kmall.admin.entity.mk.store.MkStoreCampMinusGoodsEntity" 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>

	<select id="queryObject" resultType="com.kmall.admin.entity.mk.store.MkStoreCampMinusGoodsEntity">
		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.admin.entity.mk.store.MkStoreCampMinusGoodsEntity">
		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>
	 
	<insert id="save" parameterType="com.kmall.admin.entity.mk.store.MkStoreCampMinusGoodsEntity" useGeneratedKeys="true" keyProperty="minusGoodId">
		insert into mk_store_camp_minus_goods(
			`camp_minus_id`,
			`apply_type`,
			`store_rela_id`,
			`is_valid`,
			`note`,
			`creater_sn`,
			`create_time`,
			`moder_sn`,
			`mod_time`,
			`tstm`)
		values(
			#{campMinusId},
			#{applyType},
			#{storeRelaId},
			#{isValid},
			#{note},
			#{createrSn},
			#{createTime},
			#{moderSn},
			#{modTime},
			#{tstm})
	</insert>
	 
	<update id="update" parameterType="com.kmall.admin.entity.mk.store.MkStoreCampMinusGoodsEntity">
		update mk_store_camp_minus_goods 
		<set>
			<if test="campMinusId != null">`camp_minus_id` = #{campMinusId}, </if>
			<if test="applyType != null">`apply_type` = #{applyType}, </if>
			<if test="storeRelaId != null">`store_rela_id` = #{storeRelaId}, </if>
			<if test="isValid != null">`is_valid` = #{isValid}, </if>
			<if test="note != null">`note` = #{note}, </if>
			<if test="createrSn != null">`creater_sn` = #{createrSn}, </if>
			<if test="createTime != null">`create_time` = #{createTime}, </if>
			<if test="moderSn != null">`moder_sn` = #{moderSn}, </if>
			<if test="modTime != null">`mod_time` = #{modTime}, </if>
			<if test="tstm != null">`tstm` = #{tstm}</if>
		</set>
		where minus_good_id = #{minusGoodId}
	</update>
	
	<delete id="delete">
		delete from mk_store_camp_minus_goods where minus_good_id = #{value}
	</delete>
	
	<delete id="deleteBatch">
		delete from mk_store_camp_minus_goods where minus_good_id in 
		<foreach item="minusGoodId" collection="array" open="(" separator="," close=")">
			#{minusGoodId}
		</foreach>
	</delete>

	<select id="queryCampMinusGoodsByIdList" resultType="com.kmall.admin.entity.mk.store.MkStoreCampMinusGoodsEntity">
		SELECT
			g.NAME 'goodsName',
			s.store_name 'storeName',
			r.retail_price 'retailPrice',
			r.market_price 'marketPrice',
			g.is_on_sale 'isOnSale',
			m.minus_good_id,
			(
		SELECT
			sum( o.actual_price )
		FROM
			mall_order o
			INNER JOIN mall_order_goods og ON o.id = og.order_id
		WHERE
			og.goods_id = r.goods_id
			AND o.store_id = cm.store_id
			AND o.activity_id = cm.store_topic_id
			) 'actualTotalPrice'
		FROM
			mk_store_camp_minus_goods m
			LEFT JOIN mk_store_camp_minus cm ON m.camp_minus_id = cm.camp_minus_id
			LEFT JOIN mall_product_store_rela r ON m.store_rela_id = r.id
			LEFT JOIN mall_store s ON r.store_id = s.id
			LEFT JOIN mall_goods g ON r.goods_id = g.id
		WHERE 1=1
		<if test="goodsName != null and goodsName.trim() != ''">
			AND g.NAME LIKE concat('%',#{goodsName},'%')
		</if>
		<if test="campMinusId != null and campMinusId != ''">
			AND m.camp_minus_id = #{campMinusId}
		</if>
		<if test="offset != null and limit != null">
			limit #{offset}, #{limit}
		</if>
	</select>

	<select id="queryCampMinusGoodsByIdTotal" resultType="int">
		select count(*) FROM
		mk_store_camp_minus_goods m
		LEFT JOIN mk_store_camp_minus cm ON m.camp_minus_id = cm.camp_minus_id
		LEFT JOIN mall_product_store_rela r ON m.store_rela_id = r.id
		LEFT JOIN mall_store s ON r.store_id = s.id
		LEFT JOIN mall_goods g ON r.goods_id = g.id
		WHERE 1=1
		<if test="goodsName != null and goodsName.trim() != ''">
			AND g.NAME LIKE concat('%',#{goodsName},'%')
		</if>
		<if test="campMinusId != null and campMinusId != ''">
			AND m.camp_minus_id = #{campMinusId}
		</if>
	</select>

	<select id="queryCampMinusStoreTopicById" resultType="com.kmall.admin.entity.mk.store.MkStoreCampMinusGoodsEntity">
		select
		g.`minus_good_id`,
		g.`camp_minus_id`,
		g.`apply_type`,
		g.`store_rela_id`,
		g.`is_valid`,
		g.`note`,
		g.`creater_sn`,
		g.`create_time`,
		g.`moder_sn`,
		g.`mod_time`,
		g.`tstm`,
		m.store_topic_id 'storeTopicId'
		FROM
		`mk_store_camp_minus_goods` g
		INNER JOIN mk_store_camp_minus m ON g.camp_minus_id = m.camp_minus_id
		where minus_good_id = #{minusGoodId}
	</select>

	<delete id="deleteMinusGoodsByMinusId">
		delete from mk_store_camp_minus_goods where camp_minus_id = #{campMinusId}
	</delete>

</mapper>