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

    <resultMap type="com.kmall.admin.entity.StoreMngChangeEntity" id="storeMngChangeMap">
        <result property="id" column="id"/>
        <result property="storeChangeNum" column="store_change_num"/>
        <result property="storeOriginalNum" column="store_original_num"/>
        <result property="storeValidNum" column="store_valid_num"/>
        <result property="merchSn" column="merch_sn"/>
        <result property="storeId" column="store_id"/>
        <result property="goodsId" column="goods_id"/>
        <result property="isValid" column="is_valid"/>
        <result property="changeType" column="change_type"/>
        <result property="changeReason" column="change_reason"/>
        <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.StoreMngChangeEntity">
		select
			`id`,
			`store_change_num`,
			`store_original_num`,
			`store_valid_num`,
			`merch_sn`,
			`store_id`,
			`goods_id`,
			`is_valid`,
			`change_type`,
			`change_reason`,
			`creater_sn`,
			`create_time`,
			`moder_sn`,
			`mod_time`,
			`tstm`
		from mall_store_mng_change
		where id = #{id}
	</select>

	<select id="queryObjectByGoodsIdAndType" resultType="com.kmall.admin.entity.StoreMngChangeEntity">
		select
		`id`,
		`store_change_num`,
		`store_original_num`,
		`store_valid_num`,
		`merch_sn`,
		`store_id`,
		`goods_id`,
		`is_valid`,
		`change_type`,
		`change_reason`,
		`creater_sn`,
		`create_time`,
		`moder_sn`,
		`mod_time`,
		`tstm`
		from mall_store_mng_change
		where change_type = #{changeType} and goods_id = #{goodsId}
	</select>

	<select id="queryList" resultType="com.kmall.admin.entity.StoreMngChangeEntity">
		select
			c.`id`,
			c.`store_change_num`,
			c.`store_original_num`,
			c.`store_valid_num`,
			c.`merch_sn`,
			c.`store_id`,
			c.`goods_id`,
			c.`is_valid`,
			c.`change_type`,
			c.`change_reason`,
			c.`creater_sn`,
			c.`create_time`,
			c.`moder_sn`,
    		c.`mod_time`,
			s.store_name storeName,
			g.name goodsName,
			m.merch_name merchName
		from mall_store_mng_change c left join mall_merch m on c.merch_sn = m.merch_sn
		left join mall_store s on s.id = c.store_id
		left join mall_goods g on c.goods_id = g.id
		WHERE 1=1
		<if test="goodsId != null and goodsId != ''">
			AND c.goods_id = #{goodsId}
		</if>
		<if test="storeId != null and storeId != ''">
			AND c.store_id = #{storeId}
		</if>
		<if test="changeType != null and changeType != ''">
			AND c.change_type = #{changeType}
		</if>
		<if test="thirdPartyMerchCode != null and thirdPartyMerchCode.trim() != ''">
			AND s.third_party_merch_code = #{thirdPartyMerchCode}
		</if>
        <choose>
            <when test="sidx != null and sidx.trim() != ''">
                order by ${sidx} ${order}
            </when>
			<otherwise>
                order by id desc
			</otherwise>
        </choose>
		<if test="offset != null and limit != null">
			limit #{offset}, #{limit}
		</if>
	</select>
	
 	<select id="queryTotal" resultType="int">
		select count(*) from mall_store_mng_change c left join mall_merch m on c.merch_sn = m.merch_sn
		left join mall_store s on s.id = c.store_id
		left join mall_goods g on c.goods_id = g.id
		WHERE 1=1
		<if test="goodsId != null and goodsId != ''">
			AND c.goods_id = #{goodsId}
		</if>
		<if test="storeId != null and storeId != ''">
			AND c.store_id = #{storeId}
		</if>
		<if test="changeType != null and changeType != ''">
			AND c.change_type = #{changeType}
		</if>
		<if test="thirdPartyMerchCode != null and thirdPartyMerchCode.trim() != ''">
			AND s.third_party_merch_code = #{thirdPartyMerchCode}
		</if>
	</select>
	 
	<insert id="save" parameterType="com.kmall.admin.entity.StoreMngChangeEntity">
		insert into mall_store_mng_change(
			`store_change_num`,
			`store_original_num`,
			`store_valid_num`,
			`merch_sn`,
			`store_id`,
			`goods_id`,
			`is_valid`,
			`change_type`,
			`change_reason`,
			`creater_sn`,
			`create_time`,
			`moder_sn`,
			`mod_time`,
			`tstm`)
		values(
			#{storeChangeNum},
			#{storeOriginalNum},
			#{storeValidNum},
			#{merchSn},
			#{storeId},
			#{goodsId},
			#{isValid},
			#{changeType},
			#{changeReason},
			#{createrSn},
			#{createTime},
			#{moderSn},
			#{modTime},
			#{tstm})
	</insert>
	 
	<update id="update" parameterType="com.kmall.admin.entity.StoreMngChangeEntity">
		update mall_store_mng_change 
		<set>
			<if test="storeChangeNum != null">`store_change_num` = #{storeChangeNum}, </if>
			<if test="storeOriginalNum != null">`store_original_num` = #{storeOriginalNum}, </if>
			<if test="storeValidNum != null">`store_valid_num` = #{storeValidNum}, </if>
			<if test="merchSn != null">`merch_sn` = #{merchSn}, </if>
			<if test="storeId != null">`store_id` = #{storeId}, </if>
			<if test="goodsId != null">`goods_id` = #{goodsId}, </if>
			<if test="isValid != null">`is_valid` = #{isValid}, </if>
			<if test="changeType != null">`change_type` = #{changeType}, </if>
			<if test="changeReason != null">`change_reason` = #{changeReason}, </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 id = #{id}
	</update>
	
	<delete id="delete">
		delete from mall_store_mng_change where id = #{value}
	</delete>
	
	<delete id="deleteBatch">
		delete from mall_store_mng_change where id in 
		<foreach item="id" collection="array" open="(" separator="," close=")">
			#{id}
		</foreach>
	</delete>

</mapper>