MerchantPathMapper.xml 7.2 KB
<?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="cn.com.glsx.merchants.mapper.MerchantPathMapper">
    <resultMap id="BaseResultMap" type="cn.com.glsx.merchants.entity.MerchantPath">
        <!--
          WARNING - @mbg.generated
        -->
        <id column="id" jdbcType="INTEGER" property="id"/>
        <result column="superior_id" jdbcType="INTEGER" property="superiorId"/>
        <result column="sub_id" jdbcType="INTEGER" property="subId"/>
        <result column="depth" jdbcType="INTEGER" property="depth"/>
        <result column="del_flag" jdbcType="TINYINT" property="delFlag"/>
        <result column="created_date" jdbcType="TIMESTAMP" property="createdDate"/>
        <result column="created_by" jdbcType="INTEGER" property="createdBy"/>
        <result column="updated_date" jdbcType="TIMESTAMP" property="updatedDate"/>
        <result column="updated_by" jdbcType="INTEGER" property="updatedBy"/>
        <result column="subName" jdbcType="VARCHAR" property="subName"/>
        <result column="status" jdbcType="VARCHAR" property="status"/>
        <result column="entmicro_name" jdbcType="VARCHAR" property="entmicroName"/>
        <result column="merchant_account_level" jdbcType="INTEGER" property="merchantAccountLevel"/>
    </resultMap>
    <sql id="Base_Column_List">
        <!--
          WARNING - @mbg.generated
        -->
        id, superior_id, sub_id, `depth`, del_flag, created_date, created_by, updated_date,
        updated_by
    </sql>
    <insert id="insertPath" parameterType="cn.com.glsx.merchants.entity.MerchantPath">
      INSERT INTO platform_merchant_path(superior_id,sub_id,depth)
    SELECT t.superior_id,#{merchantPath.subId},t.depth+1
    FROM platform_merchant_path AS t
    WHERE t.sub_id = #{merchantPath.superiorId}
    UNION ALL
    SELECT #{merchantPath.subId},#{merchantPath.subId},0
    </insert>
    <insert id="insertPathByMerchantPathRequest" parameterType="cn.com.glsx.merchants.model.request.MerchantPathRequest">
        INSERT INTO platform_merchant_path(superior_id,sub_id,depth,created_date)
        select super.superior_id,
        sub.sub_id ,
        super.depth + sub.depth + 1,
        NOW()
        from
        platform_merchant_path super
        cross join
        platform_merchant_path sub
        where super.sub_id = #{request.targetId}
        and sub.superior_id =  #{request.sourceId}
    </insert>
    <update id="updatePathDelFlagByMerchantPathRequest" parameterType="cn.com.glsx.merchants.model.request.MerchantPathRequest">
    UPDATE platform_merchant_path
        SET del_flag = -1
        WHERE
        sub_id IN (SELECT * FROM(SELECT sub_id FROM platform_merchant_path WHERE superior_id = #{request.sourceId}) AS t1)
        AND
        superior_id IN (SELECT * FROM(SELECT superior_id FROM platform_merchant_path WHERE sub_id = #{request.sourceId} AND superior_id != #{request.sourceId}) AS t2);

    </update>
    <delete id="deletePathByMerchantPathRequest" parameterType="cn.com.glsx.merchants.model.request.MerchantPathRequest">
    DELETE from platform_merchant_path
        where
        sub_id in (select * from (select sub_id from platform_merchant_path where superior_id = #{request.sourceId}) as t1)
        and
        superior_id in (select * from (select superior_id from platform_merchant_path where sub_id = #{request.sourceId} and superior_id != #{request.sourceId}) as t2)
		and del_flag = -1;
    </delete>
    <select id="findMerchantTree" resultType="cn.com.glsx.merchants.entity.MerchantPath"
            parameterType="cn.com.glsx.merchants.model.request.MerchantRequest">
      select t.superior_id as superiorId,t.sub_id as subId,t1.merchant_name as subName,t1.merchantNo as merchantNo,t1.merchant_account_level as merchantAccountLevel,t1.entmicro_name as entmicroName  from 	(select superior_id,sub_id from platform_merchant_path where superior_id in (select sub_id from platform_merchant_path  where superior_id=#{merchantRequest.id}) and depth=1
	UNION ALL select null,#{merchantRequest.id})t left join platform_merchant t1 on t.sub_id=t1.id where t1.status=2
    </select>

    <select id="findAllMerchantTree" resultType="cn.com.glsx.merchants.entity.MerchantPath"
            parameterType="cn.com.glsx.merchants.model.request.MerchantRequest">
      select t.superior_id as superiorId,t.sub_id as subId,t1.status as status,t1.merchant_name as subName,t1.merchantNo as merchantNo,t1.merchant_account_level as merchantAccountLevel,t1.entmicro_name as entmicroName  from 	(select superior_id,sub_id from platform_merchant_path where superior_id in (select sub_id from platform_merchant_path  where superior_id=#{merchantRequest.id}) and depth=1
	UNION ALL select null,#{merchantRequest.id})t left join platform_merchant t1 on t.sub_id=t1.id
    </select>
    <select id="findMerchantAllTree" resultType="cn.com.glsx.merchants.entity.MerchantPath"
            parameterType="cn.com.glsx.merchants.model.request.MerchantRequest">
      SELECT
	t.superior_id AS superiorId,
	t.sub_id AS subId,
	t1.merchant_name AS subName,
	t1.merchantNo AS merchantNo,
	t1.merchant_account_level AS merchantAccountLevel,
	t1.entmicro_name AS entmicroName
    FROM
	(
    SELECT
	superior_id,
	sub_id
    FROM
	platform_merchant_path
    WHERE
	superior_id IN ( SELECT sub_id FROM platform_merchant_path WHERE superior_id IN (SELECT t1.id FROM platform_merchant t1 WHERE t1.merchant_account_level = 1)
	)AND depth =1
	union all
	select null,t2.id
	FROM platform_merchant t2 WHERE t2.merchant_account_level = 1
	)t
	left join platform_merchant t1 on t.sub_id=t1.id
    </select>

    <select id="findSubordinateMerchant" resultMap="BaseResultMap"
            parameterType="java.lang.Integer">
        select pp.*,pm.status,pm.merchant_account_level from platform_merchant_path pp
        left join platform_merchant pm on pp.sub_id=pm.id
        where pp.del_flag=0 and superior_id=#{merchantId}
    </select>


    <select id="findDirectSubordinateMerchant" resultMap="BaseResultMap"
            parameterType="java.lang.Integer">
         select pp.* from platform_merchant_path pp
        left join platform_merchant t1 on pp.sub_id=t1.id
        where pp.del_flag=0 and pp.superior_id=#{merchantId}
			 and (pp.depth=0
			or (t1.merchant_account_level=4 and pp.depth=1))
    </select>
    <select id="findMerchantByIds" resultMap="BaseResultMap"
    >
        select pp.*,t1.merchant_name as subName from platform_merchant_path pp
        left join platform_merchant t1 on pp.sub_id=t1.id
        WHERE pp.depth=1 and pp.sub_id in
        <foreach collection="list" index="index" item="item"
                 open="(" close=")" separator=",">
            #{item}
        </foreach>
    </select>
    <select id="findMerchantTreeToIds" resultType="java.lang.Integer">
        select t.sub_id from (select superior_id,sub_id from platform_merchant_path where superior_id in (select sub_id from platform_merchant_path  where superior_id=#{id}) and depth=1
	UNION ALL select null,#{id})t left join platform_merchant t1 on t.sub_id=t1.id
    </select>

    <delete id="deleteSuperiorId">
        DELETE FROM platform_merchant_path WHERE  depth != 0 AND sub_id = (SELECT id FROM platform_merchant WHERE merchantNo= #{merchantNo})
    </delete>
</mapper>