<?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.MkStoreTicketGoodsTempDao">

    <resultMap type="com.kmall.admin.entity.mk.store.MkStoreTicketGoodsTempEntity" id="mkStoreTicketGoodsTempMap">
        <result property="discGoodTempId" column="disc_good_temp_id"/>
		<result property="guid" column="guid"/>
        <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"/>
		<result property="isSubmit" column="is_submit"/>
		<result property="isButtonCannel" column="is_button_cannel"/>
		<result property="storeTopicId" column="store_topic_id"/>
    </resultMap>

	<select id="queryObject" resultType="com.kmall.admin.entity.mk.store.MkStoreTicketGoodsTempEntity">
		select
			`disc_good_temp_id`,
			guid,
			is_submit,
			`apply_type`,
			`store_rela_id`,
			`is_valid`,
			`note`,
			`creater_sn`,
			`create_time`,
			`moder_sn`,
			`mod_time`,
			`tstm`,
			is_button_cannel,
			store_topic_id
		from mk_store_topic_goods_temp
		where disc_good_temp_id = #{id}
	</select>

	<select id="queryGoodsTempByRealId" resultType="com.kmall.admin.entity.mk.store.MkStoreTicketGoodsTempEntity">
		select
		`disc_good_temp_id`,
		guid,is_submit,
		`apply_type`,
		`store_rela_id`,
		`is_valid`,
		`note`,
		`creater_sn`,
		`create_time`,
		`moder_sn`,
		`mod_time`,
		`tstm`,
		is_button_cannel,
		store_topic_id
		from mk_store_topic_goods_temp
		where store_rela_id = #{storeRelaId} and guid = #{guid}
	</select>

	<select id="queryGoodsTempByGuid" resultType="com.kmall.admin.entity.mk.store.MkStoreTicketGoodsTempEntity">
		select
		`disc_good_temp_id`,guid,is_submit,
		`apply_type`,
		`store_rela_id`,
		`is_valid`,
		`note`,
		`creater_sn`,
		`create_time`,
		`moder_sn`,
		`mod_time`,
		`tstm`,is_button_cannel,store_topic_id
		from mk_store_topic_goods_temp
		where guid = #{guid}
	</select>

	<select id="queryGoodsTempByIsValid" resultType="com.kmall.admin.entity.mk.store.MkStoreTicketGoodsTempEntity">
		select
		s.`disc_good_temp_id`,
		s.guid,
		s.is_submit,
		s.`apply_type`,
		s.`store_rela_id`,
		s.`is_valid`,
		is_button_cannel,
		s.store_topic_id
		from mk_store_topic_goods_temp s LEFT JOIN mall_product_store_rela r on s.store_rela_id=r.id
		where 1=1
		<if test="guid != null">
			AND guid = #{guid}
		</if>
		<if test="isValid != null">
			AND s.is_valid = #{isValid}
		</if>
		<if test="isButtonCannel != null">
			AND s.is_button_cannel = #{isButtonCannel}
		</if>
		<if test="storeId != null">
			AND r.store_id = #{storeId}
		</if>
		<if test="applyType != null and applyType.trim() != ''">
			AND apply_type = #{applyType}
		</if>
		<if test="storeTopicId != null">
			AND s.store_topic_id = #{storeTopicId}
		</if>
	</select>

	<!--暂时无用-->
	<select id="queryGoodsTempByGuidAndIsSubmit" resultType="com.kmall.admin.entity.mk.store.MkStoreTicketGoodsTempEntity">
		select
		`disc_good_temp_id`,
		guid,is_submit,
		`apply_type`,
		`store_rela_id`,
		store_topic_id
		from mk_store_topic_goods_temp s LEFT JOIN mall_product_store_rela r on s.store_rela_id=r.id
		where  1=1
		<if test="guid != null">
			AND guid = #{guid}
		</if>
		<if test="storeTopicId != null">
			AND s.store_topic_id = #{storeTopicId}
		</if>
		<if test="isSubmit != null and isSubmit.trim() != ''">
			AND is_submit = #{isSubmit}
		</if>
		<if test="storeId != null">
			AND r.store_id = #{storeId}
		</if>
		<if test="applyType != null and applyType.trim() != ''">
			AND apply_type = #{applyType}
		</if>

	</select>

	<select id="queryList" resultType="com.kmall.admin.entity.mk.store.MkStoreTicketGoodsTempEntity">
		select
    		`disc_good_temp_id`,guid,is_submit,
    		`apply_type`,
    		`store_rela_id`,
    		`is_valid`,
    		`note`,
    		`creater_sn`,
    		`create_time`,
    		`moder_sn`,
    		`mod_time`,
    		`tstm`,
		store_topic_id
		from mk_store_topic_goods_temp
		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 disc_good_temp_id desc
			</otherwise>
        </choose>
		<if test="offset != null and limit != null">
			limit #{offset}, #{limit}
		</if>
	</select>

	<select id="queryAllDiscountByStoreId" resultType="com.kmall.admin.entity.mk.store.MkStoreTicketGoodsTempEntity">
		SELECT
			s.id,
			s.goods_id 'goodsId',
			s.retail_price,
			g.store_rela_id 'isCheck',
			g.apply_type ,
			gs.name 'goodsName',
			g.disc_good_temp_id,g.guid,
			g.store_topic_id
		FROM
			mall_product_store_rela s
			INNER JOIN mk_store_topic_goods_temp g ON s.id = g.store_rela_id
			inner join mall_goods gs on s.goods_id = gs.id
		WHERE 1=1 and gs.is_delete = 0
			and s.store_id = #{storeId}
			and g.guid = #{guid}
			and g.is_valid = 0 AND g.is_button_cannel = 0
		<if test="applyType != null">
			and g.apply_type = #{applyType}
		</if>
		<if test="offset != null and limit != null">
			limit #{offset}, #{limit}
		</if>
	</select>

	<select id="queryTotallDiscountByStoreId" resultType="int">
		select count(*)
		FROM
		mall_product_store_rela s
		INNER JOIN mk_store_topic_goods_temp g ON s.id = g.store_rela_id
		inner join mall_goods gs on s.goods_id = gs.id
		WHERE 1=1 and gs.is_delete = 0
		and s.store_id = #{storeId}
		and g.guid = #{guid}
		and g.is_valid = 0 AND g.is_button_cannel = 0
		<if test="applyType != null">
			and g.apply_type = #{applyType}
		</if>
	</select>
	
 	<select id="queryTotal" resultType="int">
		select count(*) from mk_store_topic_goods_temp
		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.MkStoreTicketGoodsTempEntity" useGeneratedKeys="true" keyProperty="discGoodTempId">
		insert into mk_store_topic_goods_temp(
			`apply_type`,guid,
			`store_rela_id`,
			`is_valid`,is_submit,is_button_cannel,
			store_topic_id,
			`note`,
			`creater_sn`,
			`create_time`,
			`moder_sn`,
			`mod_time`,
			`tstm`)
		values(
			#{applyType},#{guid},
			#{storeRelaId},
			#{isValid},#{isSubmit},#{isButtonCannel},
			#{storeTopicId},
			#{note},
			#{createrSn},
			#{createTime},
			#{moderSn},
			#{modTime},
			#{tstm})
	</insert>
	 
	<update id="update" parameterType="com.kmall.admin.entity.mk.store.MkStoreTicketGoodsTempEntity">
		update mk_store_topic_goods_temp
		<set>
			<if test="applyType != null">`apply_type` = #{applyType}, </if>
			<if test="guid != null">guid = #{guid},</if>
			<if test="storeRelaId != null">`store_rela_id` = #{storeRelaId}, </if>
			<if test="isValid != null">`is_valid` = #{isValid}, </if>
			<if test="isSubmit != null">`is_submit` = #{isSubmit}, </if>
			<if test="isButtonCannel != null">`is_button_cannel` = #{isButtonCannel}, </if>
			<if test="storeTopicId != null">`store_topic_id` = #{storeTopicId}, </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 disc_good_temp_id = #{discGoodTempId}
	</update>
	
	<delete id="delete">
		delete from mk_store_topic_goods_temp where disc_good_temp_id = #{value}
	</delete>
	
	<delete id="deleteBatch">
		delete from mk_store_topic_goods_temp where disc_good_temp_id in
		<foreach item="discGoodTempId" collection="array" open="(" separator="," close=")">
			#{discGoodTempId}
		</foreach>
	</delete>

	<delete id="deleteByGuid">
		delete from mk_store_topic_goods_temp where guid = #{guid}
	</delete>

	<select id="queryGoodsTempIdBy" resultType="com.kmall.admin.entity.mk.store.MkStoreTicketGoodsTempEntity">
		SELECT
		g.disc_good_temp_id
		FROM
		mall_product_store_rela s
		INNER JOIN mk_store_topic_goods_temp g ON s.id = g.store_rela_id
		INNER JOIN mall_goods gs ON s.goods_id = gs.id
		WHERE
		1 = 1
		AND gs.is_delete = 0
		AND s.store_id = #{storeId}
		AND g.guid = #{guid}
		<if test="isValid != null">
			AND g.is_valid = #{isValid}
		</if>
		<if test="isButtonCannel != null">
			AND g.is_button_cannel = #{isButtonCannel}
		</if>
		<if test="storeRelaIdList != null">
			AND g.store_rela_id NOT IN
			<foreach collection="storeRelaIdList" item="listItem" open="(" separator="," close=")">
				#{listItem}
			</foreach></if>
	</select>

	<select id="queryGoodsTempByRealIdAndTopicId" resultType="com.kmall.admin.entity.mk.store.MkStoreTicketGoodsTempEntity">
		select
		`disc_good_temp_id`,
		guid,is_submit,
		`apply_type`,
		`store_rela_id`,
		`is_valid`,
		`note`,
		`creater_sn`,
		`create_time`,
		`moder_sn`,
		`mod_time`,
		`tstm`,
		is_button_cannel,
		store_topic_id
		from mk_store_topic_goods_temp
		where store_rela_id = #{storeRelaId} and store_topic_id = #{storeTopicId}
	</select>

	<select id="queryGoodsTempByTopicId" resultType="com.kmall.admin.entity.mk.store.MkStoreTicketGoodsTempEntity">
		select
		`disc_good_temp_id`,
		guid,is_submit,
		`apply_type`,
		`store_rela_id`,
		`is_valid`,
		`note`,
		`creater_sn`,
		`create_time`,
		`moder_sn`,
		`mod_time`,
		`tstm`,
		is_button_cannel,
		store_topic_id
		from mk_store_topic_goods_temp
		where store_topic_id = #{storeTopicId}
	</select>

	<delete id="deleteByStoreTopicIdAndRealId">
		delete from mk_store_topic_goods_temp where store_topic_id = #{storeTopicId} and store_rela_id = #{storeRelaId}
	</delete>

	<delete id="deleteByStoreTopicId">
		delete from mk_store_topic_goods_temp where store_topic_id = #{storeTopicId}
	</delete>

</mapper>