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

    <resultMap type="com.kmall.admin.entity.mk.dist.MkDistSellStatisEntity" id="mkDistSellStatisMap">
        <result property="id" column="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="goodsPrice" column="goods_price"/>
        <result property="allocType" column="alloc_type"/>
		<result property="sellAllocHistoryId" column="sell_alloc_history_id"/>
        <result property="expendDistId" column="expend_dist_id"/>
        <result property="expendDistPrice" column="expend_dist_price"/>
        <result property="expendDistLevel" column="expend_dist_level"/>
        <result property="expendSource" column="expend_source"/>
        <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="orderSn" column="order_sn"/>
		<result property="actualPrice" column="actual_price"/>
    </resultMap>

	<select id="queryObject" resultType="com.kmall.admin.entity.mk.dist.MkDistSellStatisEntity">
		select
			`id`,
			`dist_flag`,
			`third_merch_sn`,
			`supplier_id`,
			`dist_id`,
			`goods_id`,
			`goods_price`,
			`sell_alloc_history_id`,
			`expend_dist_id`,
			`expend_dist_price`,
			`expend_dist_level`,
			`expend_source`,
			`is_valid`,
			`note`,
			`creater_sn`,
			`create_time`,
			`moder_sn`,
			`mod_time`,
			`tstm`,order_sn
		from mk_dist_sell_statis
		where id = #{id}
	</select>

	<select id="queryList" resultType="com.kmall.admin.entity.mk.dist.MkDistSellStatisEntity">
		SELECT * FROM (
		select
			a.`id`,
			a.`dist_flag`,
			a.`third_merch_sn`,
			a.`supplier_id`,
			a.`dist_id`,
			a.`goods_id`,
			a.`goods_price`,
			a.`sell_alloc_history_id`,
			a.`expend_dist_id`,
			a.`expend_dist_price`,
			a.`expend_dist_level`,
			a.`expend_source`,
			a.`is_valid`,
			a.`note`,
			a.`creater_sn`,
			a.`create_time`,
			a.`moder_sn`,
			a.`mod_time`,
    		a.`tstm`,o.order_sn,
			d.dist_name goodsDistName,d2.dist_name expendDistName,g.name goodsName,
			t.third_party_merch_name thirdPartyMerchName,ss.child_supplier_name childSupplierName,
			sa.expend_dist_ids expendDistIds,sa.expend_dist_lines expendDistLines,sa.alloc_type,s1.store_name storeName,s2.store_name childStoreName
		from mk_dist_sell_statis  a
		left join mk_dist_sell_allocation_history sa on a.sell_alloc_history_id = sa.sell_alloc_history_id
		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 mk_dist d2 on a.expend_dist_id = d2.dist_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
		left join mall_order o on o.order_sn = a.order_sn
		left join mall_store s1 on d.store_id = s1.id
		left join mall_store s2 on d2.store_id = s2.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>
		<if test="startTime != null and startTime != ''">
			AND a.create_time <![CDATA[ >  ]]> #{startTime}
		</if>
		<if test="endTime != null and endTime != ''">
			AND a.create_time <![CDATA[ <  ]]> #{endTime}
		</if>

		<choose>
            <when test="sidx != null and sidx.trim() != ''">
                order by ${sidx} ${order}
            </when>
			<otherwise>
                order by a.id desc
			</otherwise>
        </choose>
		)a
		GROUP BY
		a.dist_id ,a.expend_dist_level
		<if test="offset != null and limit != null">
			limit #{offset}, #{limit}
		</if>
	</select>
	
 	<select id="queryTotal" resultType="int">
		select count(*) from mk_dist_sell_statis  a
		left join mk_dist_sell_allocation_history sa on a.sell_alloc_history_id = sa.sell_alloc_history_id
		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 mk_dist d2 on a.expend_dist_id = d2.dist_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
		left join mall_order o on o.order_sn = a.order_sn
		left join mall_store s1 on d.store_id = s1.id
		left join mall_store s2 on d2.store_id = s2.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>
		<if test="startTime != null and startTime != ''">
			AND a.create_time <![CDATA[ >  ]]> #{startTime}
		</if>
		<if test="endTime != null and endTime != ''">
			AND a.create_time <![CDATA[ <  ]]> #{endTime}
		</if>
	</select>


	<select id="queryStatisExportList" resultType="com.kmall.admin.entity.mk.dist.MkDistSellStatisEntity">
		SELECT * FROM (
		select
		a.`id`,
		a.`dist_flag`,
		a.`third_merch_sn`,
		a.`supplier_id`,
		a.`dist_id`,
		a.`goods_id`,
		a.`goods_price`,
		a.`sell_alloc_history_id`,
		a.`expend_dist_id`,
		a.`expend_dist_price`,
		a.`expend_dist_level`,
		a.`expend_source`,
		a.`is_valid`,
		a.`note`,
		a.`creater_sn`,
		a.`create_time`,
		a.`moder_sn`,
		a.`mod_time`,
		a.`tstm`,o.order_sn,o.actual_price,
		d.dist_name goodsDistName,d2.dist_name expendDistName,g.name goodsName,
		t.third_party_merch_name thirdPartyMerchName,ss.child_supplier_name childSupplierName,
		sa.expend_dist_ids expendDistIds,sa.expend_dist_lines expendDistLines,sa.alloc_type,s1.store_name storeName,s2.store_name childStoreName
		from mk_dist_sell_statis  a
		left join mk_dist_sell_allocation_history sa on a.sell_alloc_history_id = sa.sell_alloc_history_id
		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 mk_dist d2 on a.expend_dist_id = d2.dist_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
		left join mall_order o on o.order_sn = a.order_sn
		left join mall_store s1 on d.store_id = s1.id
		left join mall_store s2 on d2.store_id = s2.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>
		<if test="startTime != null and startTime != ''">
			AND a.create_time <![CDATA[ >  ]]> #{startTime}
		</if>
		<if test="endTime != null and endTime != ''">
			AND a.create_time <![CDATA[ <  ]]> #{endTime}
		</if>
		ORDER BY a.id
		)a
		GROUP BY
		a.dist_id ,a.expend_dist_level
	</select>

	<insert id="save" parameterType="com.kmall.admin.entity.mk.dist.MkDistSellStatisEntity" useGeneratedKeys="true" keyProperty="id">
		insert into mk_dist_sell_statis(
			`dist_flag`,
			`third_merch_sn`,
			`supplier_id`,
			`dist_id`,
			order_sn,
			`goods_id`,
			`goods_price`,
			`sell_alloc_history_id`,
			`expend_dist_id`,
			`expend_dist_price`,
			`expend_dist_level`,
			`expend_source`,
			`is_valid`,
			`note`,
			`creater_sn`,
			`create_time`,
			`moder_sn`,
			`mod_time`,
			`tstm`)
		values(
			#{distFlag},
			#{thirdMerchSn},
			#{supplierId},
			#{distId},
			#{orderSn},
			#{goodsId},
			#{goodsPrice},
			#{sellAllocHistoryId},
			#{expendDistId},
			#{expendDistPrice},
			#{expendDistLevel},
			#{expendSource},
			#{isValid},
			#{note},
			#{createrSn},
			#{createTime},
			#{moderSn},
			#{modTime},
			#{tstm})
	</insert>

	<update id="update" parameterType="com.kmall.admin.entity.mk.dist.MkDistSellStatisEntity">
		update mk_dist_sell_statis 
		<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="orderSn != null">`order_sn` = #{orderSn}, </if>
			<if test="goodsId != null">`goods_id` = #{goodsId}, </if>
			<if test="goodsPrice != null">`goods_price` = #{goodsPrice}, </if>
			<if test="sellAllocHistoryId != null">`sell_alloc_history_id` = #{sellAllocHistoryId}, </if>
			<if test="expendDistId != null">`expend_dist_id` = #{expendDistId}, </if>
			<if test="expendDistPrice != null">`expend_dist_price` = #{expendDistPrice}, </if>
			<if test="expendDistLevel != null">`expend_dist_level` = #{expendDistLevel}, </if>
			<if test="expendSource != null">`expend_source` = #{expendSource}, </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 id = #{id}
	</update>
	
	<delete id="delete">
		delete from mk_dist_sell_statis where id = #{value}
	</delete>
	
	<delete id="deleteBatch">
		delete from mk_dist_sell_statis where id in 
		<foreach item="id" collection="array" open="(" separator="," close=")">
			#{id}
		</foreach>
	</delete>

</mapper>