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

    <resultMap type="com.kmall.admin.entity.mk.dist.MkDistHierEntity" id="mkDistHierMap">
        <result property="distHierId" column="dist_hier_id"/>
        <result property="distChnlId" column="dist_chnl_id"/>
        <result property="pdistId" column="pdist_id"/>
        <result property="distId" column="dist_id"/>
        <result property="distHierName" column="dist_hier_name"/>
        <result property="distHierNname" column="dist_hier_nname"/>
        <result property="hierLvl" column="hier_lvl"/>
        <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="pdDistName" column="pdDistName"/>
		<result property="distName" column="distName"/>
		<result property="distFlag" column="distFlag"/>
		<result property="childSupplierName" column="childSupplierName"/>
    </resultMap>

	<select id="queryObject" resultType="com.kmall.admin.entity.mk.dist.MkDistHierEntity">
		select
			`dist_hier_id`,
			`dist_chnl_id`,
			`pdist_id`,
			`dist_id`,
			`dist_hier_name`,
			`dist_hier_nname`,
			`hier_lvl`,
			`supplier_id`,
			`sort_order`,
			`is_valid`,
			`note`,
			`creater_sn`,
			`create_time`,
			`moder_sn`,
			`mod_time`,
			`tstm`
		from mk_dist_hier
		where dist_hier_id = #{id}
	</select>

	<select id="queryObjectByName" resultType="com.kmall.admin.entity.mk.dist.MkDistHierEntity">
		select
		`dist_hier_id`,
		`dist_chnl_id`,
		`pdist_id`,
		`dist_id`,
		`dist_hier_name`,
		`dist_hier_nname`,
		`hier_lvl`,
		`supplier_id`,
		`sort_order`,
		`is_valid`,
		`note`,
		`creater_sn`,
		`create_time`,
		`moder_sn`,
		`mod_time`,
		`tstm`
		from mk_dist_hier
		where dist_hier_name = #{distHierName}
	</select>

	<select id="queryObjectByPdistIdAndChnlId" resultType="com.kmall.admin.entity.mk.dist.MkDistHierEntity">
		select
		h.`dist_hier_id`,
		h.`dist_chnl_id`,
		h.`pdist_id`,
		h.`dist_id`,
		h.`dist_hier_name`,
		h.`dist_hier_nname`,
		h.`hier_lvl`,
		h.`supplier_id`,
		h.`sort_order`,
		h.`is_valid`,
		h.`note`,
		h.`creater_sn`,
		h.`create_time`,
		h.`moder_sn`,
		h.`mod_time`,
		h.`tstm`,cc.dist_flag,d.dist_name
		from mk_dist_hier h
		left join mk_dist_chnl cc on h.dist_chnl_id = cc.dist_chnl_id
		left join mk_dist d on h.pdist_id = d.dist_id
		where 1=1
		<if test="pdistId != null and pdistId != ''">
			and h.pdist_id = #{pdistId}
		</if>
		<if test="distChnlId != null and distChnlId != ''">
			and h.dist_chnl_id = #{distChnlId}
		</if>
		order by h.mod_time desc
	</select>

	<select id="queryList" resultType="com.kmall.admin.entity.mk.dist.MkDistHierEntity">
		select
		h.`dist_hier_id`,
		h.`dist_chnl_id`,
		h.`pdist_id` 'parentId',
		h.`dist_id`,
		h.`dist_hier_name`,
		h.`dist_hier_nname`,
		h.`hier_lvl`,
		h.`supplier_id`,
		h.`sort_order`,
		h.`is_valid`,
		h.`note`,
		h.`creater_sn`,
		h.`create_time`,
		h.`moder_sn`,
		h.`mod_time`,
		h.`tstm`,d1.dist_name 'parentName',d2.dist_name 'name',dc.dist_flag 'distFlag',s.child_supplier_name 'childSupplierName'
		from mk_dist_hier h
		left join mk_dist_chnl dc on h.dist_chnl_id = dc.dist_chnl_id
		left join mk_dist d1 on h.pdist_id = d1.dist_id
		left join mk_dist d2 on h.dist_id = d2.dist_id
		left join mall_supplier s on h.supplier_id = s.id
		left join third_merchant_biz t on dc.third_merch_sn = t.third_merch_sn
		LEFT JOIN mall_store st ON d1.store_id = st.id
		LEFT JOIN mall_store st2 ON d2.store_id = st2.id
		WHERE 1=1
		<if test="distHierName != null and distHierName.trim() != ''">
			AND h.dist_hier_name LIKE concat('%',#{distHierName},'%')
		</if>
		<if test="pdistId != null and pdistId != ''">
			AND h.pdist_id = #{pdistId}
		</if>
		<if test="distId != null and distId != ''">
			AND h.dist_id = #{distId}
		</if>
		<if test="thirdMerchSn != null and thirdMerchSn != ''">
			AND dc.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 st2.id = #{storeId}
		</if>

        <choose>
            <when test="sidx != null and sidx.trim() != ''">
                order by ${sidx} ${order}
            </when>
			<otherwise>
                order by h.dist_hier_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_hier h
		left join mk_dist_chnl dc on h.dist_chnl_id = dc.dist_chnl_id
		left join mk_dist d1 on h.pdist_id = d1.dist_id
		left join mk_dist d2 on h.dist_id = d2.dist_id
		left join mall_supplier s on h.supplier_id = s.id
		left join third_merchant_biz t on dc.third_merch_sn = t.third_merch_sn
		LEFT JOIN mall_store st ON d1.store_id = st.id
		LEFT JOIN mall_store st2 ON d2.store_id = st2.id
		WHERE 1=1
		<if test="distHierName != null and distHierName.trim() != ''">
			AND h.dist_hier_name LIKE concat('%',#{distHierName},'%')
		</if>
		<if test="pdistId != null">
			AND h.pdist_id = #{pdistId}
		</if>
		<if test="thirdMerchSn != null and thirdMerchSn != ''">
			AND dc.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 st2.id = #{storeId}
		</if>
	</select>

	<select id="queryListByTree" resultType="com.kmall.admin.entity.mk.dist.MkDistHierEntity">
		SELECT
		h.`dist_id`,
		h.`pdist_id`,
		d2.dist_name NAME,
		d1.dist_name parentName,
		h.hier_lvl,
		h.dist_chnl_id
		FROM
		mk_dist_hier h
		LEFT JOIN mk_dist_chnl dc ON h.dist_chnl_id = dc.dist_chnl_id
		LEFT JOIN mk_dist d1 ON h.pdist_id = d1.dist_id
		LEFT JOIN mk_dist d2 ON h.dist_id = d2.dist_id
		WHERE
		1 = 1
		<if test="pdistId != null and pdistId != ''">
			AND h.pdist_id = ( SELECT pdist_id FROM mk_dist_hier WHERE 1 = 1 and dist_id = #{pdistId})
		</if>
		<if test="distId != null and distId != ''">
			AND h.dist_id = #{distId}
		</if>
		<if test="hierLvl != null and hierLvl != ''">
			AND h.hier_lvl = #{hierLvl}
		</if>
		<choose>
			<when test="sidx != null and sidx.trim() != ''">
				order by ${sidx} ${order}
			</when>
		</choose>
	</select>
<!--
	<select id="queryListByTree" resultType="com.kmall.admin.entity.mk.MkDistHierEntity">
		&lt;!&ndash;select * from (&ndash;&gt;
		select
		h.`dist_id`,
		h.`pdist_id`,
		d2.dist_name name,
		d1.dist_name parentName,
		h.hier_lvl
		from mk_dist_hier h
		left join mk_dist_chnl dc on h.dist_chnl_id = dc.dist_chnl_id
		left join mk_dist d1 on h.pdist_id = d1.dist_id
		left join mk_dist d2 on h.dist_id = d2.dist_id
		WHERE 1=1
		<if test="pdistId != null">
			AND h.pdist_id = #{pdistId}
		</if>
		&lt;!&ndash;UNION all&ndash;&gt;
		&lt;!&ndash;select d1.`dist_id` dist_id,&ndash;&gt;
		&lt;!&ndash;d1.is_valid pdist_id,&ndash;&gt;
		&lt;!&ndash;d1.dist_name parentName,&ndash;&gt;
		&lt;!&ndash;d1.dist_name name,&ndash;&gt;
		&lt;!&ndash;0 'hier_lvl' from mk_dist d1&ndash;&gt;
		&lt;!&ndash;WHERE 1=1&ndash;&gt;
		&lt;!&ndash;<if test="pdistId != null">&ndash;&gt;
			&lt;!&ndash;AND d1.dist_id = #{pdistId}&ndash;&gt;
		&lt;!&ndash;</if>&ndash;&gt;
		&lt;!&ndash;)s&ndash;&gt;
		<choose>
			<when test="sidx != null and sidx.trim() != ''">
				order by ${sidx} ${order}
			</when>
		</choose>
	</select>-->
	<select id="selectDistHierByChnlIdAndOneLvl" resultType="com.kmall.admin.entity.mk.dist.MkDistHierEntity">
		SELECT * FROM `mk_dist_hier` where 1=1 and dist_chnl_id = #{distChnlId} and hier_lvl = 1
		<if test="distHierId != null and distHierId != ''">
			and dist_hier_id != #{distHierId}
		</if>
	</select>
	<select id="selectDistHierByDistId" resultType="com.kmall.admin.entity.mk.dist.MkDistHierEntity">
		SELECT * FROM `mk_dist_hier` where 1=1 and dist_id = #{distId}
	</select>
	<select id="selectDistHierByDistChnlId" resultType="com.kmall.admin.entity.mk.dist.MkDistHierEntity">
		SELECT * FROM `mk_dist_hier` where 1=1 and dist_chnl_id = #{distChnlId}
	</select>

	<select id="queryListByPdistIdAndChnlIdAndDistId" resultType="com.kmall.admin.entity.mk.dist.MkDistHierEntity">
		SELECT h.*
		FROM mk_dist_hier h
		WHERE 1=1
		<if test="pdistId != null">
			AND h.pdist_id = #{pdistId}
		</if>
		<if test="distChnlId != null">
			AND h.dist_chnl_id = #{distChnlId}
		</if>
		<if test="distId != null">
			AND h.dist_id = #{distId}
		</if>
	</select>
	<select id="queryObjectByDistIdAndPdistId" resultType="com.kmall.admin.entity.mk.dist.MkDistHierEntity">
		SELECT h.*
		FROM mk_dist_hier h
		WHERE 1=1
		<if test="pdistId != null">
			AND h.pdist_id = #{pdistId}
		</if>
		<if test="distId != null">
			AND h.dist_id = #{distId}
		</if>
		<if test="distHierId != null">
			AND h.dist_hier_id != #{distHierId}
		</if>

	</select>

	<select id="queryObjectByHierMap" resultType="com.kmall.admin.entity.mk.dist.MkDistHierEntity">
		select
		h.`dist_hier_id`,
		h.`dist_chnl_id`,
		h.`pdist_id`,
		h.`dist_id`,
		h.`dist_hier_name`,
		h.`dist_hier_nname`,
		h.`hier_lvl`,
		h.`supplier_id`,
		h.`sort_order`,
		h.`is_valid`,
		h.`note`,
		h.`creater_sn`,
		h.`create_time`,
		h.`moder_sn`,
		h.`mod_time`,
		h.`tstm`,cc.dist_flag,d.dist_name
		from mk_dist_hier h
		left join mk_dist_chnl cc on h.dist_chnl_id = cc.dist_chnl_id
		left join mk_dist d on h.pdist_id = d.dist_id
		where 1=1
		<if test="pdistId != null and pdistId != ''">
			and h.pdist_id = #{pdistId}
		</if>
		<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="hierLvl != null and hierLvl != ''">
			and h.hier_lvl = #{hierLvl}
		</if>
	</select>

	<select id="querySysConfigByKey" resultType="map">
		select s.value from sys_config s where s.key = #{key}
	</select>

	<insert id="save" parameterType="com.kmall.admin.entity.mk.dist.MkDistHierEntity" useGeneratedKeys="true" keyProperty="distHierId">
		insert into mk_dist_hier(
			`dist_chnl_id`,
			`pdist_id`,
			`dist_id`,
			`dist_hier_name`,
			`dist_hier_nname`,
			`hier_lvl`,
			`supplier_id`,
			`sort_order`,
			`is_valid`,
			`note`,
			`creater_sn`,
			`create_time`,
			`moder_sn`,
			`mod_time`,
			`tstm`)
		values(
			#{distChnlId},
			#{pdistId},
			#{distId},
			#{distHierName},
			#{distHierNname},
			#{hierLvl},
			#{supplierId},
			#{sortOrder},
			#{isValid},
			#{note},
			#{createrSn},
			#{createTime},
			#{moderSn},
			#{modTime},
			#{tstm})
	</insert>
	 
	<update id="update" parameterType="com.kmall.admin.entity.mk.dist.MkDistHierEntity">
		update mk_dist_hier 
		<set>
			<if test="distChnlId != null">`dist_chnl_id` = #{distChnlId}, </if>
			<if test="pdistId != null">`pdist_id` = #{pdistId}, </if>
			<if test="distId != null">`dist_id` = #{distId}, </if>
			<if test="distHierName != null">`dist_hier_name` = #{distHierName}, </if>
			<if test="distHierNname != null">`dist_hier_nname` = #{distHierNname}, </if>
			<if test="hierLvl != null">`hier_lvl` = #{hierLvl}, </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_hier_id = #{distHierId}
	</update>
	
	<delete id="delete">
		delete from mk_dist_hier where dist_hier_id = #{value}
	</delete>
	
	<delete id="deleteBatch">
		delete from mk_dist_hier where dist_hier_id in 
		<foreach item="distHierId" collection="array" open="(" separator="," close=")">
			#{distHierId}
		</foreach>
	</delete>

</mapper>