<?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="org.springblade.modules.yjxt.base.servicePerson.mapper.YjxtServicePersonMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="servicePersonResultMap" type="org.springblade.modules.yjxt.base.servicePerson.entity.YjxtServicePersonEntity">
        <result column="user_id" property="userId"/>
        <result column="ad_code" property="adCode"/>
    </resultMap>

    <resultMap id="servicePersonVOResultMap" type="org.springblade.modules.yjxt.base.servicePerson.vo.YjxtServicePersonVO">
        <result column="user_id" property="userId"/>
        <result column="ad_code" property="adCode"/>
        <result column="real_name" property="userName"/>
        <result column="account" property="userAccount"/>
        <result column="phone" property="phone"/>
    </resultMap>

    <select id="selectPage" resultMap="servicePersonVOResultMap">
        SELECT
        p.*,
        u.real_name,u.account,u.phone
        FROM
        yjxt_base_service_person p
        LEFT JOIN blade_user u ON u.id = p.user_id and u.is_deleted = 0
        LEFT JOIN base_data_ad_cd a ON a.adcd = p.ad_code and a.is_deleted=0
        WHERE
        p.is_deleted = 0
        <if test="dto.userName!=null">
            and u.real_name like concat(concat('%', #{dto.userName}), '%')
        </if>
        <if test="dto.deptId!=null">
            and (u.dept_id =#{dto.deptId} or  FIND_IN_SET(#{dto.deptId} ,u.dept_id) >0 )
        </if>
        <if test="dto.adCode!=null and dto.adCode!=''">
            and (a.adcd =#{dto.adCode} or  FIND_IN_SET(#{dto.adCode} ,a.ancestors) >0 )
        </if>
        ORDER BY
        u.real_name
    </select>

</mapper>
