<?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.hzs.system.config.mapper.BdVertexMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.hzs.common.domain.vertex.BdVertex">
        <result column="CREATION_TIME" property="creationTime"/>
        <result column="MODIFIED_TIME" property="modifiedTime"/>
        <result column="DEL_FLAG" property="delFlag"/>
        <result column="PK_COUNTRY" property="pkCountry"/>
        <result column="PK_CREATOR" property="pkCreator"/>
        <result column="PK_MODIFIED" property="pkModified"/>
        <result column="PK_ID" property="pkId"/>
        <result column="PK_MEMBER" property="pkMember"/>
        <result column="VERTEX_NAME" property="vertexName"/>
        <result column="PK_PARENT" property="pkParent"/>
        <result column="SORT" property="sort"/>
        <result column="SYSTEM_TYPE" property="systemType"/>
    </resultMap>

    <resultMap id="childResultMap" type="com.hzs.common.domain.vertex.ext.BdVertexExt">
        <result column="PK_ID" property="pkId"/>
        <result column="PK_MEMBER" property="pkMember"/>
        <result column="VERTEX_NAME" property="vertexName"/>
        <collection property="childList" ofType="com.hzs.common.domain.vertex.BdVertex">
            <result column="CHILD_PK_ID" property="pkId"/>
            <result column="CHILD_PK_PARENT" property="pkParent"/>
            <result column="CHILD_PK_MEMBER" property="pkMember"/>
            <result column="CHILD_VERTEX_NAME" property="vertexName"/>
        </collection>
    </resultMap>

    <update id="updateBdVertexById">
        update cu_member
        set pk_vertex = #{pkVertex}
        where pk_id in (
            SELECT pk_id
            FROM cu_member t
            WHERE t.del_flag = 0
        START WITH t.pk_id = #{pkMember}
        CONNECT BY PRIOR t.pk_id = t.pk_parent)
    </update>

    <!-- 更新体系（新） -->
    <update id="updateVertexNew">
        update cu_member
        set pk_vertex = #{pkVertex}
        where pk_id in (SELECT cm.pk_id
                        FROM cu_member cm
                        WHERE cm.del_flag = 0
        START WITH cm.pk_id = #{pkMember}
        CONNECT BY PRIOR cm.pk_id = cm.pk_parent
               and cm.pk_id not in
                 (select bv.pk_member
            from bd_vertex bv
            where bv.del_flag = 0
               and bv.pk_country = #{pkCountry}
               and bv.pk_member != #{pkMember}))
    </update>

    <update id="updateBdVertexNullById">
        update cu_member
        set pk_vertex = null
        where pk_vertex = #{pkVertex}
    </update>

    <select id="selectByList" resultType="com.hzs.system.config.vo.VertexVo">
        select bv.*,cm.MEMBER_CODE memberCode, brv.name as regionVertexName
        from BD_VERTEX bv
        left join CU_MEMBER cm on cm.PK_ID = bv.PK_MEMBER
        left join BD_REGION_VERTEX brv on brv.pk_id = bv.REGION_VERTEX_PK_ID
        where bv.DEL_FLAG = 0
        <if test="createStartDate != null">
            and bv.CREATION_TIME &gt;= #{createStartDate}
        </if>
        <if test="createEndDate != null">
            and bv.CREATION_TIME &lt;= #{createEndDate}
        </if>
        <if test="pkCountry != null">
            and bv.PK_COUNTRY = #{pkCountry}
        </if>
        <if test="systemType != null">
            and bv.system_type = #{systemType}
        </if>
        <if test="vertexName != null and vertexName !=''">
            and bv.VERTEX_NAME like #{vertexName} || '%'
        </if>
        <if test="memberCode != null and memberCode !=''">
            and cm.MEMBER_CODE like #{memberCode} || '%'
        </if>
        <if test="regionVertexPkId != null">
            and bv.REGION_VERTEX_PK_ID = #{regionVertexPkId}
        </if>
        order by bv.creation_time
    </select>

    <!-- 根据管理员ID查询体系列表 -->
    <select id="queryListByUserId" resultMap="BaseResultMap">
        select bv.pk_id, bv.vertex_name
        from sys_user_vertex suv
                 left join BD_VERTEX bv
                           on bv.pk_id = suv.pk_vertex
                               and bv.del_flag = 0
        where suv.user_id = #{userId}
        order by bv.creation_time
    </select>

    <!-- 查询国家下体系层级关系 -->
    <select id="queryBdVertexChild" resultMap="childResultMap">
        select bv.pk_id,
        bv.pk_member,
        bv.vertex_name,
        bvc.pk_id child_pk_id,
        bvc.pk_parent child_pk_parent,
        bvc.pk_member child_pk_member,
        bvc.vertex_name child_vertex_name
        from bd_vertex bv
        left join bd_vertex bvc
        on (bvc.pk_parent = bv.pk_id or bvc.pk_id = bv.pk_id)
        and bvc.del_flag = 0
        where bv.del_flag = 0
        and bv.pk_country = #{pkCountry}
        <if test="systemType != null">
            and bv.system_type = #{systemType}
        </if>
        and bv.pk_parent = 0
    </select>

    <!-- 查询体系以及会员信息 -->
    <select id="queryVertexAndMember" resultType="com.hzs.common.domain.vertex.ext.BdVertexExt">
        select bv.*, cm.member_code
        from bd_vertex bv
                 left join cu_member cm
                           on cm.pk_id = bv.pk_member
        where bv.del_flag = 0
          and bv.pk_id = #{pkId}
    </select>

    <!-- 查询刷新体系数据 -->
    <select id="queryFlushVertexList" resultMap="BaseResultMap">
        select bv.pk_id, bv.pk_member
        from (select level lev, cm.pk_id, cm.pk_vertex
              from cu_member cm
              where cm.del_flag = 0
                  start
              with cm.member_code = #{memberCode}
              connect by cm.pk_parent = prior cm.pk_id
              order by level) cm
                 left join bd_vertex bv
                           on bv.pk_member = cm.pk_id
                               and bv.del_flag = 0
                               and bv.pk_country = #{pkCountry}
        where bv.pk_member is not null
        order by cm.lev, bv.pk_member
    </select>

</mapper>
