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

    <resultMap type="com.kmall.admin.entity.mk.dist.MkDistChnlEntity" id="mkDistChnlMap">
        <result property="distChnlId" column="dist_chnl_id"/>
        <result property="distFlag" column="dist_flag"/>
        <result property="partType" column="part_type"/>
        <result property="thirdMerchSn" column="third_merch_sn"/>
        <result property="supplierId" column="supplier_id"/>
        <result property="sortOrder" column="sort_order"/>
        <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"/>
    </resultMap>

	<select id="queryObject" resultType="com.kmall.admin.entity.mk.dist.MkDistChnlEntity">
		select
			`dist_chnl_id`,
			`dist_flag`,
			`part_type`,
			`third_merch_sn`,
			`supplier_id`,
			`sort_order`,
			`is_valid`,
			`note`,
			`creater_sn`,
			`create_time`,
			`moder_sn`,
			`mod_time`,
			`tstm`
		from mk_dist_chnl
		where dist_chnl_id = #{id}
	</select>

	<select id="queryObjectByDistFlag" resultType="com.kmall.admin.entity.mk.dist.MkDistChnlEntity">
		select
		`dist_chnl_id`,
		`dist_flag`,
		`part_type`,
		`third_merch_sn`,
		`supplier_id`,
		`sort_order`,
		`is_valid`,
		`note`,
		`creater_sn`,
		`create_time`,
		`moder_sn`,
		`mod_time`,
		`tstm`
		from mk_dist_chnl
		where dist_flag = #{distFlag}
	</select>
	<select id="queryList" resultType="com.kmall.admin.entity.mk.dist.MkDistChnlEntity">
		select
		d.`dist_chnl_id`,
		d.`dist_flag`,
		d.`part_type`,
		d.`third_merch_sn`,
		d.`supplier_id`,
		d.`sort_order`,
		d.`is_valid`,
		d.`note`,
		d.`creater_sn`,
		d.`create_time`,
		d.`moder_sn`,
		d.`mod_time`,
		d.`tstm`,t.third_party_merch_name thirdPartyMerchName,s.child_supplier_name childSupplierName
		from mk_dist_chnl d left join third_merchant_biz t on d.third_merch_sn = t.third_merch_sn
		left join mall_supplier s on d.supplier_id = s.id
		WHERE 1=1
		<if test="distFlag != null and distFlag.trim() != ''">
			AND d.dist_flag LIKE concat('%',#{distFlag},'%')
		</if>
		<if test="thirdMerchSn != null and thirdMerchSn != ''">
			AND d.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="storeId != null and storeId != ''">
			AND d.dist_chnl_id in (select dist_chnl_id from mk_dist_hier h left join mk_dist d
			on h.dist_id = d.dist_id left join mall_store s on d.store_id = s.id where s.id = #{storeId})
		</if>
        <choose>
            <when test="sidx != null and sidx.trim() != ''">
                order by ${sidx} ${order}
            </when>
			<otherwise>
                order by d.dist_chnl_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_chnl d left join third_merchant_biz t on d.third_merch_sn = t.third_merch_sn
		left join mall_supplier s on d.supplier_id = s.id
		WHERE 1=1
		<if test="distFlag != null and distFlag.trim() != ''">
			AND d.dist_flag LIKE concat('%',#{distFlag},'%')
		</if>
		<if test="thirdMerchSn != null and thirdMerchSn != ''">
			AND d.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="storeId != null and storeId != ''">
			AND d.dist_chnl_id in (select dist_chnl_id from mk_dist_hier h left join mk_dist d
			on h.dist_id = d.dist_id left join mall_store s on d.store_id = s.id where s.id = #{storeId})
		</if>
	</select>
	 
	<insert id="save" parameterType="com.kmall.admin.entity.mk.dist.MkDistChnlEntity" useGeneratedKeys="true" keyProperty="distChnlId">
		insert into mk_dist_chnl(
			`dist_flag`,
			`part_type`,
			`third_merch_sn`,
			`supplier_id`,
			`sort_order`,
			`is_valid`,
			`note`,
			`creater_sn`,
			`create_time`,
			`moder_sn`,
			`mod_time`,
			`tstm`)
		values(
			#{distFlag},
			#{partType},
			#{thirdMerchSn},
			#{supplierId},
			#{sortOrder},
			#{isValid},
			#{note},
			#{createrSn},
			#{createTime},
			#{moderSn},
			#{modTime},
			#{tstm})
	</insert>
	 
	<update id="update" parameterType="com.kmall.admin.entity.mk.dist.MkDistChnlEntity">
		update mk_dist_chnl 
		<set>
			<if test="distFlag != null">`dist_flag` = #{distFlag}, </if>
			<if test="partType != null">`part_type` = #{partType}, </if>
			<if test="thirdMerchSn != null">`third_merch_sn` = #{thirdMerchSn}, </if>
			<if test="supplierId != null">`supplier_id` = #{supplierId}, </if>
			<if test="sortOrder != null">`sort_order` = #{sortOrder}, </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 dist_chnl_id = #{distChnlId}
	</update>
	
	<delete id="delete">
		delete from mk_dist_chnl where dist_chnl_id = #{value}
	</delete>
	
	<delete id="deleteBatch">
		delete from mk_dist_chnl where dist_chnl_id in 
		<foreach item="distChnlId" collection="array" open="(" separator="," close=")">
			#{distChnlId}
		</foreach>
	</delete>

</mapper>