<?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.dist.MkDistSellAllocationDao">

    <resultMap type="com.kmall.admin.entity.mk.dist.MkDistSellAllocationEntity" id="mkDistSellAllocationMap">
        <result property="sellAllocId" column="sell_alloc_id"/>
		<result property="distChnlId" column="dist_chnl_id"/>
        <result property="distFlag" column="dist_flag"/>
        <result property="thirdMerchSn" column="third_merch_sn"/>
        <result property="supplierId" column="supplier_id"/>
        <result property="distId" column="dist_id"/>
        <result property="goodsId" column="goods_id"/>
        <result property="allocType" column="alloc_type"/>
        <result property="expendDistIds" column="expend_dist_ids"/>
        <result property="expendDistLines" column="expend_dist_lines"/>
        <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="storeId" column="store_id"/>
		<result property="distName" column="distName"/>
		<result property="storeName" column="storeName"/>
		<result property="goodsName" column="goodsName"/>
		<result property="sellAllocHistoryId" column="sell_alloc_history_id"/>
    </resultMap>

	<select id="queryObject" resultType="com.kmall.admin.entity.mk.dist.MkDistSellAllocationEntity">
		select
			`sell_alloc_id`,
			`dist_flag`,
			`third_merch_sn`,
			`supplier_id`,
			`dist_id`,
			`goods_id`,
			`alloc_type`,
			`expend_dist_ids`,
			`expend_dist_lines`,
			`is_valid`,
			`note`,
			`creater_sn`,
			`create_time`,
			`moder_sn`,
			`mod_time`,
			`tstm`,
			store_id,dist_chnl_id,sell_alloc_history_id
		from mk_dist_sell_allocation
		where sell_alloc_id = #{id}
	</select>

	<select id="queryList" resultType="com.kmall.admin.entity.mk.dist.MkDistSellAllocationEntity">
		select
		a.`sell_alloc_id`,
		c.`dist_flag`,
		a.`third_merch_sn`,
		a.`supplier_id`,
		a.`dist_id`,
		a.`goods_id`,
		a.`alloc_type`,
		a.`expend_dist_ids`,
		a.`expend_dist_lines`,
		a.`is_valid`,
		a.`note`,
		a.`creater_sn`,
		a.`create_time`,
		a.`moder_sn`,
		a.`mod_time`,
		a.`tstm`,a.store_id,a.dist_chnl_id,a.sell_alloc_history_id,
		d.dist_name distName,s.store_name storeName,g.name goodsName,
		t.third_party_merch_name thirdPartyMerchName,ss.child_supplier_name childSupplierName
		from mk_dist_sell_allocation a
		left join mk_dist_chnl c on a.dist_chnl_id = c.dist_chnl_id
		left join mk_dist d on a.dist_id = d.dist_id
		left join mall_store s on a.store_id = s.id
		left join mall_goods g on a.goods_id = g.id
		left join third_merchant_biz t on a.third_merch_sn = t.third_merch_sn
		left join mall_supplier ss on a.supplier_id = ss.id
		WHERE 1=1
		<if test="distChnlId != null and distChnlId != ''">
			AND a.dist_chnl_id = #{distChnlId}
		</if>
		<if test="storeId != null and storeId != ''">
			AND a.store_id = #{storeId}
		</if>
		<if test="distId != null and distId != ''">
			AND a.dist_id = #{distId}
		</if>
		<if test="goodsId != null and goodsId != ''">
			AND a.goods_id = #{goodsId}
		</if>
		<if test="thirdMerchSn != null and thirdMerchSn != ''">
			AND a.third_merch_sn = #{thirdMerchSn}
		</if>
		<if test="merchSn != null and merchSn != ''">
			AND t.merch_sn = #{merchSn}
		</if>
		<if test="thirdPartyMerchCode != null and thirdPartyMerchCode != ''">
			AND t.third_party_merch_code = #{thirdPartyMerchCode}
		</if>
        <choose>
            <when test="sidx != null and sidx.trim() != ''">
                order by ${sidx} ${order}
            </when>
			<otherwise>
                order by a.sell_alloc_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_dist_sell_allocation a
		left join mk_dist_chnl c on a.dist_chnl_id = c.dist_chnl_id
		left join mk_dist d on a.dist_id = d.dist_id
		left join mall_store s on a.store_id = s.id
		left join mall_goods g on a.goods_id = g.id
		left join third_merchant_biz t on a.third_merch_sn = t.third_merch_sn
		left join mall_supplier ss on a.supplier_id = ss.id
		WHERE 1=1
		<if test="distChnlId != null and distChnlId != ''">
			AND a.dist_chnl_id = #{distChnlId}
		</if>
		<if test="storeId != null and storeId != ''">
			AND a.store_id = #{storeId}
		</if>
		<if test="distId != null and distId != ''">
			AND a.dist_id = #{distId}
		</if>
		<if test="goodsId != null and goodsId != ''">
			AND a.goods_id = #{goodsId}
		</if>
		<if test="thirdMerchSn != null and thirdMerchSn != ''">
			AND a.third_merch_sn = #{thirdMerchSn}
		</if>
		<if test="merchSn != null and merchSn != ''">
			AND t.merch_sn = #{merchSn}
		</if>
		<if test="thirdPartyMerchCode != null and thirdPartyMerchCode != ''">
			AND t.third_party_merch_code = #{thirdPartyMerchCode}
		</if>
	</select>

	<select id="queryAllocationByMap"  resultType="com.kmall.admin.entity.mk.dist.MkDistSellAllocationEntity">
		SELECT
			a.*,
			h.pdist_id
		FROM
		mk_dist_sell_allocation a
		LEFT JOIN mk_dist_hier h ON a.dist_id = h.dist_id
		AND a.dist_chnl_id = h.dist_chnl_id

		WHERE 1=1
		<if test="distId != null and distId != ''">
			AND h.dist_id = #{distId}
		</if>
		<if test="distChnlId != null and distChnlId != ''">
			AND h.dist_chnl_id = #{distChnlId}
		</if>
		<if test="goodsId != null and goodsId != ''">
			AND a.goods_id = #{goodsId}
		</if>
		<if test="storeId != null and storeId != ''">
			AND a.store_id = #{storeId}
		</if>
		<if test="sellAllocId != null and sellAllocId != ''">
			AND a.sell_alloc_id != #{sellAllocId}
		</if>
	</select>
	 
	<insert id="save" parameterType="com.kmall.admin.entity.mk.dist.MkDistSellAllocationEntity" useGeneratedKeys="true" keyProperty="sellAllocId">
		insert into mk_dist_sell_allocation(
			`dist_flag`,
			`third_merch_sn`,
			`supplier_id`,
			`dist_id`,
			dist_chnl_id,
			store_id,
			`goods_id`,
			`alloc_type`,
			`expend_dist_ids`,
			`expend_dist_lines`,
			`is_valid`,
			sell_alloc_history_id,
			`note`,
			`creater_sn`,
			`create_time`,
			`moder_sn`,
			`mod_time`,
			`tstm`)
		values(
			#{distFlag},
			#{thirdMerchSn},
			#{supplierId},
			#{distId},
			#{distChnlId},
			#{storeId},
			#{goodsId},
			#{allocType},
			#{expendDistIds},
			#{expendDistLines},
			#{isValid},
			#{sellAllocHistoryId},
			#{note},
			#{createrSn},
			#{createTime},
			#{moderSn},
			#{modTime},
			#{tstm})
	</insert>
	 
	<update id="update" parameterType="com.kmall.admin.entity.mk.dist.MkDistSellAllocationEntity">
		update mk_dist_sell_allocation 
		<set>
			<if test="distFlag != null">`dist_flag` = #{distFlag}, </if>
			<if test="thirdMerchSn != null">`third_merch_sn` = #{thirdMerchSn}, </if>
			<if test="supplierId != null">`supplier_id` = #{supplierId}, </if>
			<if test="distId != null">`dist_id` = #{distId}, </if>
			<if test="distChnlId != null">`dist_chnl_id` = #{distChnlId}, </if>
			<if test="storeId != null">`store_id` = #{storeId}, </if>
			<if test="goodsId != null">`goods_id` = #{goodsId}, </if>
			<if test="allocType != null">`alloc_type` = #{allocType}, </if>
			<if test="expendDistIds != null">`expend_dist_ids` = #{expendDistIds}, </if>
			<if test="expendDistLines != null">`expend_dist_lines` = #{expendDistLines}, </if>
			<if test="sellAllocHistoryId != null">`sell_alloc_history_id` = #{sellAllocHistoryId}, </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 sell_alloc_id = #{sellAllocId}
	</update>
	
	<delete id="delete">
		delete from mk_dist_sell_allocation where sell_alloc_id = #{value}
	</delete>
	
	<delete id="deleteBatch">
		delete from mk_dist_sell_allocation where sell_alloc_id in 
		<foreach item="sellAllocId" collection="array" open="(" separator="," close=")">
			#{sellAllocId}
		</foreach>
	</delete>

</mapper>