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

    <resultMap type="com.kmall.api.entity.MngChangeVo" id="mngChangeMap">
        <result property="id" column="id"/>
        <result property="changeNum" column="change_num"/>
        <result property="originalNum" column="original_num"/>
        <result property="validNum" column="valid_num"/>
        <result property="merchSn" column="merch_sn"/>
        <result property="thirdPartyMerchCode" column="third_party_merch_code"/>
        <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.api.entity.MngChangeVo">
		select
			`id`,
			`change_num`,
			`original_num`,
			`valid_num`,
			`merch_sn`,
			`third_party_merch_code`,
			`goods_id`,
			`is_valid`,
			`change_type`,
			`change_reason`,
			`creater_sn`,
			`create_time`,
			`moder_sn`,
			`mod_time`,
			`tstm`
		from mall_mng_change
		where id = #{id}
	</select>

	<select id="queryList" resultType="com.kmall.api.entity.MngChangeVo">
		select
    		`id`,
    		`change_num`,
    		`original_num`,
    		`valid_num`,
    		`merch_sn`,
    		`third_party_merch_code`,
    		`goods_id`,
    		`is_valid`,
    		`change_type`,
    		`change_reason`,
    		`creater_sn`,
    		`create_time`,
    		`moder_sn`,
    		`mod_time`,
    		`tstm`
		from mall_mng_change c left join mall_merch m on c.merch_sn = m.merch_sn
		left join third_merchant_biz mb on c.third_party_merch_code = mb.third_party_merch_code
		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>
        <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_mng_change
		WHERE 1=1
        <if test="name != null and name.trim() != ''">
            AND name LIKE concat('%',#{name},'%')
        </if>
	</select>
	 
	<insert id="save" parameterType="com.kmall.api.entity.MngChangeVo">
		insert into mall_mng_change(
			`change_num`,
			`original_num`,
			`valid_num`,
			`merch_sn`,
			`third_party_merch_code`,
			`goods_id`,
			`is_valid`,
			`change_type`,
			`change_reason`,
			`creater_sn`,
			`create_time`,
			`moder_sn`,
			`mod_time`,
			`tstm`)
		values(
			#{changeNum},
			#{originalNum},
			#{validNum},
			#{merchSn},
			#{thirdPartyMerchCode},
			#{goodsId},
			#{isValid},
			#{changeType},
			#{changeReason},
			#{createrSn},
			#{createTime},
			#{moderSn},
			#{modTime},
			#{tstm})
	</insert>
	 
	<update id="update" parameterType="com.kmall.api.entity.MngChangeVo">
		update mall_mng_change 
		<set>
			<if test="changeNum != null">`change_num` = #{changeNum}, </if>
			<if test="originalNum != null">`original_num` = #{originalNum}, </if>
			<if test="validNum != null">`valid_num` = #{validNum}, </if>
			<if test="merchSn != null">`merch_sn` = #{merchSn}, </if>
			<if test="thirdPartyMerchCode != null">`third_party_merch_code` = #{thirdPartyMerchCode}, </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_mng_change where id = #{value}
	</delete>
	
	<delete id="deleteBatch">
		delete from mall_mng_change where id in 
		<foreach item="id" collection="array" open="(" separator="," close=")">
			#{id}
		</foreach>
	</delete>

</mapper>