<?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.baseinfo.servicePerson.mapper.ServicePersonMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="servicePersonResultMap" type="org.springblade.modules.baseinfo.servicePerson.entity.ServicePersonEntity">
        <result column="tenant_id" property="tenantId"/>
        <result column="user_id" property="userAccount"/>
        <result column="real_name" property="personName"/>
        <result column="email" property="mail"/>
        <result column="phone" property="contactPhone"/>
    </resultMap>

    <resultMap id="servicePersonVOResultMap" type="org.springblade.modules.baseinfo.servicePerson.vo.ServicePersonVO">
        <result column="tenant_id" property="tenantId"/>
        <result column="account" property="userAccount"/>
        <result column="real_name" property="personName"/>
        <result column="email" property="mail"/>
        <result column="phone" property="contactPhone"/>
        <result column="dept_name" property="deptName"/>
        <result column="role_name" property="roleName"/>
        <result column="post_name" property="postName"/>
        <result column="dict_value" property="postType"/>
        <result column="dept_id" property="deptId"/>
    </resultMap>

    <select id="selectPage" resultMap="servicePersonVOResultMap">
        SELECT
        u.*,r.role_name,d.dept_name,p.post_name,dict.dict_value
        FROM
        blade_user u
        LEFT JOIN blade_role r ON u.role_id = r.id and r.is_deleted = 0
        LEFT JOIN blade_dept d ON u.dept_id = d.id and d.is_deleted = 0
        LEFT JOIN blade_post p ON u.post_id = p.id and p.is_deleted = 0
        LEFT JOIN blade_dict dict ON dict.code ='post_category' and dict.dict_key=p.category and dict.is_deleted = 0
        WHERE
        u.tenant_id = #{dto.tenantId} and u.is_deleted = 0
        <if test="dto.personName!=null">
            and u.real_name like concat(concat('%', #{dto.personName}), '%')
        </if>
        <if test="dto.contactPhone!=null">
            and u.phone like concat(concat('%', #{dto.contactPhone}), '%')
        </if>
        <if test="dto.deptId!=null">
            and (u.dept_id =#{dto.deptId} or  FIND_IN_SET(#{dto.deptId},d.ancestors) > 0)
        </if>
        <if test="dto.category!=null">
            and p.category =#{dto.category}
        </if>
        <if test="dto.postId!=null">
            and  p.id =  #{dto.postId}
        </if>
        <if test="dto.roleId!=null">
            and u.role_id =#{dto.roleId}
        </if>
        ORDER BY u.account
    </select>


    <select id="selectServicePersonPage" resultMap="servicePersonVOResultMap">
        SELECT
        u.*,r.role_name,d.dept_name,p.post_name,dict.dict_value
        FROM
        blade_user u
        LEFT JOIN blade_role r ON u.role_id = r.id and r.is_deleted = 0
        LEFT JOIN blade_dept d ON u.dept_id = d.id and d.is_deleted = 0
        LEFT JOIN blade_post p ON u.post_id = p.id and p.is_deleted = 0
        LEFT JOIN blade_dict dict ON dict.code ='post_category' and dict.dict_key=p.category and dict.is_deleted = 0
        WHERE
        u.tenant_id = #{dto.tenantId} and u.is_deleted = 0
        <if test="dto.id!=null">
            and u.id != #{dto.id}
        </if>
        <if test="dto.personName!=null">
            and u.real_name like concat(concat('%', #{dto.personName}), '%')
        </if>
        <if test="dto.contactPhone!=null">
            and u.phone like concat(concat('%', #{dto.contactPhone}), '%')
        </if>
        <if test="dto.deptId!=null">
            and (u.dept_id =#{dto.deptId} or  FIND_IN_SET(#{dto.deptId},d.ancestors) > 0)
        </if>
        <if test="dto.category!=null">
            and p.category =#{dto.category}
        </if>
        <if test="dto.postId!=null">
            and  p.id =  #{dto.postId}
        </if>
        <if test="dto.roleId!=null">
            and u.role_id =#{dto.roleId}
        </if>
        ORDER BY u.account
    </select>

    <select id="getPersonServiceInfo" resultMap="servicePersonResultMap">
        SELECT
        u.*
        FROM
        blade_user u
        WHERE
        u.is_deleted = 0 and u.id = #{dto.id}
        limit 1
    </select>
</mapper>
