<?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.yd.csf.service.dao.PolicyBrokerMapper">

    <resultMap id="BaseResultMap" type="com.yd.csf.service.model.PolicyBroker">
            <id property="id" column="id" />
            <result property="policyBizId" column="policy_biz_id" />
            <result property="policyNo" column="policy_no" />
            <result property="brokerName" column="broker_name" />
            <result property="brokerBizId" column="broker_biz_id" />
            <result property="gender" column="gender" />
            <result property="internalCode" column="internal_code" />
            <result property="team" column="team" />
            <result property="teamBizId" column="team_biz_id" />
            <result property="brokerRatio" column="broker_ratio" />
            <result property="remark" column="remark" />
            <result property="isDeleted" column="is_deleted" />
            <result property="creatorId" column="creator_id" />
            <result property="updaterId" column="updater_id" />
            <result property="createTime" column="create_time" />
            <result property="updateTime" column="update_time" />
    </resultMap>
<!--    在PolicyBrokerMapper.xml里面写个SQL，通过传入的客户端用户ID字段，查询该客户端用户ID在三张关系表里面的，推荐人的客户端用户ID, 一级管理人客户端用户ID，二级管理人客户端用户ID，辅导人客户端用户ID，这些客户端用户ID整合返回List<String>客户端用户ID列表-->
    <sql id="Base_Column_List">
        id,policy_biz_id,policy_no,broker_name,broker_biz_id,gender,internal_code,
        team,broker_ratio,team_biz_id,remark,is_deleted,creator_id,updater_id,
        create_time,update_time
    </sql>

    <!-- 根据客户端用户ID查询关联的推荐人、管理人（一级、二级）、辅导人ID列表 -->
    <select id="selectRelatedBrokerBizIds" parameterType="java.lang.String" resultType="java.lang.String">
        SELECT DISTINCT related_user_id FROM (
            -- 推荐人
            SELECT referrer_id AS related_user_id
            FROM agent_referral_relation
            WHERE agent_id = #{clientUserId}
              AND is_deleted = 0
              AND is_active = 1

            UNION

            -- 一级管理人
            SELECT manage_id AS related_user_id
            FROM agent_manage_relation
            WHERE agent_id = #{clientUserId}
              AND is_deleted = 0
              AND is_active = 1

            UNION

            -- 二级管理人：通过一级管理人关联查询
            SELECT m2.manage_id AS related_user_id
            FROM agent_manage_relation m1
            JOIN agent_manage_relation m2 ON m1.manage_id = m2.agent_id
            WHERE m1.agent_id = #{clientUserId}
              AND m1.is_deleted = 0 AND m1.is_active = 1
              AND m2.is_deleted = 0 AND m2.is_active = 1

            UNION

            -- 辅导人
            SELECT tutor_id AS related_user_id
            FROM agent_tutoring_relation
            WHERE agent_id = #{clientUserId}
              AND is_deleted = 0
              AND is_active = 1
        ) AS t
        WHERE related_user_id IS NOT NULL
    </select>
</mapper>
