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

    <resultMap type="com.kmall.admin.entity.mk.dist.MkDistEntity" id="mkDistMap">
        <result property="distId" column="dist_id"/>
        <result property="distName" column="dist_name"/>
        <result property="distEname" column="dist_ename"/>
        <result property="distFlag" column="dist_flag"/>
        <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"/>
		<result property="thirdPartyMerchName" column="thirdPartyMerchName"/>
		<result property="childSupplierName" column="childSupplierName"/>
		<result property="storeId" column="store_id"/>
    </resultMap>

	<select id="queryObject" resultType="com.kmall.admin.entity.mk.dist.MkDistEntity">
		select
			`dist_id`,
			`dist_name`,
			`dist_ename`,
			`dist_flag`,
			`third_merch_sn`,
			`supplier_id`,
			`sort_order`,
			`is_valid`,
			`note`,
			`creater_sn`,
			`create_time`,
			`moder_sn`,
			`mod_time`,
			`tstm`,store_id
		from mk_dist
		where dist_id = #{id}
	</select>
	<select id="queryDistByGoodsId" resultType="com.kmall.admin.entity.mk.dist.MkDistEntity">
		SELECT
			d.*
		FROM
			mk_dist d
			LEFT JOIN mall_store s ON d.store_id = s.id
			LEFT JOIN mall_product_store_rela r ON s.id = r.store_id
		WHERE
			r.goods_id = #{goodsId} and r.store_id = #{storeId}
	</select>
	<select id="queryObjectByDistName" resultType="com.kmall.admin.entity.mk.dist.MkDistEntity">
		select
		`dist_id`,
		`dist_name`,
		`dist_ename`,
		`dist_flag`,
		`third_merch_sn`,
		`supplier_id`,
		`sort_order`,
		`is_valid`,
		`note`,
		`creater_sn`,
		`create_time`,
		`moder_sn`,
		`mod_time`,
		`tstm`,store_id
		from mk_dist
		where dist_name = #{distName}
	</select>

	<select id="queryDistByStoreId" resultType="com.kmall.admin.entity.mk.dist.MkDistEntity">
		select
		`dist_id`,
		`dist_name`,
		`dist_ename`,
		`dist_flag`,
		`third_merch_sn`,
		`supplier_id`,
		`sort_order`,
		`is_valid`,
		`note`,
		`creater_sn`,
		`create_time`,
		`moder_sn`,
		`mod_time`,
		`tstm`,store_id
		from mk_dist
		where store_id = #{storeId}
	</select>

	<select id="queryList" resultType="com.kmall.admin.entity.mk.dist.MkDistEntity">
		select
		d.`dist_id`,
		d.`dist_name`,
		d.`dist_ename`,
		d.`dist_flag`,
		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,st.store_name storeName
		from mk_dist 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
		left join mall_store st on d.store_id = st.id
		WHERE 1=1
		<if test="distName != null and distName.trim() != ''">
			AND dist_name LIKE concat('%',#{distName},'%')
		</if>
		<if test="distId != null and distId != ''">
			and d.dist_id = #{distId}
		</if>
		<if test="thirdMerchSn != null and thirdMerchSn != ''">
			and d.third_merch_sn = #{thirdMerchSn}
		</if>
		<if test="storeId != null and storeId != ''">
			AND d.store_id = #{storeId}
		</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="childDistId != null and childDistId != ''">
			AND d.dist_id != #{childDistId}
		</if>

        <choose>
            <when test="sidx != null and sidx.trim() != ''">
                order by ${sidx} ${order}
            </when>
			<otherwise>
                order by d.dist_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 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
		left join mall_store st on d.store_id = st.id
		WHERE 1=1
		<if test="distName != null and distName.trim() != ''">
			AND dist_name LIKE concat('%',#{distName},'%')
		</if>
		<if test="distId != null and distId != ''">
			and d.dist_id = #{distId}
		</if>
		<if test="thirdMerchSn != null and thirdMerchSn != ''">
			and d.third_merch_sn = #{thirdMerchSn}
		</if>
		<if test="storeId != null and storeId != ''">
			AND d.store_id = #{storeId}
		</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="childDistId != null and childDistId != ''">
			AND d.dist_id != #{childDistId}
		</if>
	</select>

	<select id="queryAllByHier" resultType="com.kmall.admin.entity.mk.dist.MkDistEntity">
		select
		d.`dist_id`,
		d.`dist_name`,
		d.`dist_ename`,
		d.`dist_flag`,
		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,st.store_name storeName
		from mk_dist 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
		left join mall_store st on d.store_id = st.id
		WHERE 1=1
		and dist_id in (select dist_id from mk_dist_hier where 1=1
		<if test="hierLvl != null and hierLvl != ''">
			AND hier_lvl = #{hierLvl}
		</if>)
		<if test="distName != null and distName.trim() != ''">
			AND dist_name LIKE concat('%',#{distName},'%')
		</if>
		<if test="distId != null and distId != ''">
			and d.dist_id = #{distId}
		</if>
		<if test="thirdMerchSn != null and thirdMerchSn != ''">
			and d.third_merch_sn = #{thirdMerchSn}
		</if>
		<if test="storeId != null and storeId != ''">
			AND d.store_id = #{storeId}
		</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="childDistId != null and childDistId != ''">
			AND d.dist_id != #{childDistId}
		</if>
	</select>


	<insert id="save" parameterType="com.kmall.admin.entity.mk.dist.MkDistEntity" useGeneratedKeys="true" keyProperty="distId">
		insert into mk_dist(
			`dist_name`,
			`dist_ename`,
			`dist_flag`,
			`third_merch_sn`,
			`supplier_id`,
			store_id,
			`sort_order`,
			`is_valid`,
			`note`,
			`creater_sn`,
			`create_time`,
			`moder_sn`,
			`mod_time`,
			`tstm`)
		values(
			#{distName},
			#{distEname},
			#{distFlag},
			#{thirdMerchSn},
			#{supplierId},
			#{storeId},
			#{sortOrder},
			#{isValid},
			#{note},
			#{createrSn},
			#{createTime},
			#{moderSn},
			#{modTime},
			#{tstm})
	</insert>

	<update id="update" parameterType="com.kmall.admin.entity.mk.dist.MkDistEntity">
		update mk_dist 
		<set>
			<if test="distName != null">`dist_name` = #{distName}, </if>
			<if test="distEname != null">`dist_ename` = #{distEname}, </if>
			<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="storeId != null">`store_id` = #{storeId}, </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_id = #{distId}
	</update>
	
	<delete id="delete">
		delete from mk_dist where dist_id = #{value}
	</delete>
	
	<delete id="deleteBatch">
		delete from mk_dist where dist_id in 
		<foreach item="distId" collection="array" open="(" separator="," close=")">
			#{distId}
		</foreach>
	</delete>

</mapper>