<?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.jeecg.modules.basedata.mapper.FdPersonnelMapper">
    <select id="getCount" resultType="java.lang.Long">
        SELECT COUNT(1) FROM fd_personnel where del_flag=0
    </select>

    <select id="getAllPage" resultType="org.jeecg.modules.basedata.entity.FdPersonnel">
        <bind name="pagecurr" value="(current-1)*size"></bind>
        SELECT
        p.id,
        d.pk_personnel as pk_personnelId ,
        p. CODE,
        p. NAME,
        p.en_name,
        p.gender,
        p.category,
        p.pk_depart,
        p.pk_org,
        p.mobile,
        p.tel,
        p.email,
        p. STATUS,
        p. ENABLE,
        d.depart_name as departName,
        c.depart_name as orgName
        FROM
        fd_personnel p
        LEFT JOIN sys_depart d ON p.pk_depart=d.id
        LEFT JOIN sys_depart c ON p.pk_org=c.id
        where
          1=1 and p.del_flag=0
            <if test="fdPersonnel.code!=null and fdPersonnel.code!=''">
                and p.code = #{fdPersonnel.code}
            </if>
            <if test="fdPersonnel.name!=null and fdPersonnel.name!=''">
                and p.name LIKE concat(concat('%',#{fdPersonnel.name}),'%')
            </if>
            <if test="fdPersonnel.enName!=null and fdPersonnel.enName!=''">
                and p.en_name = #{fdPersonnel.enName}
            </if>
            <if test="fdPersonnel.gender!=null and fdPersonnel.gender!=''">
                and p.gender = #{fdPersonnel.gender}
            </if>
            <if test="fdPersonnel.category!=null and fdPersonnel.category!=''">
                and p.category = #{fdPersonnel.category}
            </if>
            <if test="fdPersonnel.pkDepart!=null and fdPersonnel.pkDepart!=''">
                and p.pk_depart = #{fdPersonnel.pkDepart}
            </if>
            <if test="fdPersonnel.tel!=null and fdPersonnel.tel!=''">
                and p.tel = #{fdPersonnel.tel}
            </if>
            <if test="fdPersonnel.email!=null and fdPersonnel.email!=''">
                and p.email = #{fdPersonnel.email}
            </if>
            <if test="fdPersonnel.status!=null and fdPersonnel.status!=''">
                and p.status = #{fdPersonnel.status}
            </if>
            <if test="fdPersonnel.enable!=null and fdPersonnel.enable!=''">
                and p.enable = #{fdPersonnel.enable}
            </if>
        LIMIT #{pagecurr},#{size}
    </select>

    <select id="findByPage" resultType="org.jeecg.modules.basedata.entity.FdPersonnel">
         SELECT
            p.id,
            d.pk_personnel as pk_personnelId ,
            p. CODE,
            p. NAME,
            p.en_name,
            p.gender,
            p.category,
            p.pk_depart,
            p.pk_org,
            p.mobile,
            p.tel,
            p.email,
            p. STATUS,
            p. ENABLE,
            d.depart_name as departName,
            c.depart_name as orgName,
            p2.name as namePersonnel
        FROM
          fd_personnel p
        LEFT JOIN sys_depart d ON p.pk_depart=d.id
        LEFT JOIN sys_depart c ON p.pk_org=c.id
        left JOIN fd_personnel p2 on p.pk_personnel=p2.id
        ${ew.customSqlSegment}
    </select>

    <select id="getByIdPersonnel" resultType="org.jeecg.modules.basedata.entity.FdPersonnel">
        select * from fd_personnel where id=#{id}
    </select>

    <!--<select id="queryApprover" resultType="org.jeecg.modules.basedata.entity.FdPersonnel">-->
        <!--SELECT-->
            <!--a.id AS id,-->
            <!--a.NAME AS NAME,-->
            <!--a.pk_depart AS parentId,-->
            <!--su.username AS CODE-->
        <!--FROM-->
            <!--fd_personnel a-->
            <!--LEFT JOIN sys_user su on su.personnel_id=a.id-->
        <!--WHERE-->
            <!--a.del_flag = 0-->
            <!--AND TRIM( a.id )!= '' UNION-->
        <!--SELECT-->
            <!--sd.id AS id,-->
            <!--sd.depart_name AS NAME,-->
            <!--sd.parent_id AS parentId,-->
            <!--sd.org_code AS CODE-->
        <!--FROM-->
            <!--fd_personnel a-->
            <!--LEFT JOIN sys_depart sd ON sd.id = a.pk_depart-->
        <!--WHERE-->
            <!--a.del_flag = 0-->
            <!--AND TRIM( sd.id )!= '' UNION-->
        <!--SELECT-->
            <!--sd.id AS id,-->
            <!--sd.depart_name AS NAME,-->
            <!--sd.parent_id AS parentId,-->
            <!--sd.org_code AS CODE-->
        <!--FROM-->
            <!--fd_personnel a-->
            <!--LEFT JOIN sys_depart sd ON a.pk_org = sd.id-->
        <!--WHERE-->
            <!--a.del_flag = 0-->
            <!--AND a.pk_org !='fe33687fb0a645ccb71a2873460a3d7d'-->
            <!--AND TRIM( sd.id )!= ''-->
    <!--</select>-->

    <select id="queryApprover" resultType="org.jeecg.modules.basedata.entity.FdPersonnel">
        SELECT
            *
        FROM
            (
        SELECT
            a.id AS id,
            a.NAME AS NAME,
            a.pk_depart AS parentId,
            su.username AS CODE
        FROM
            fd_personnel a
            LEFT JOIN sys_user su ON su.personnel_id = a.id
        WHERE
            a.del_flag = 0
            AND TRIM( a.id ) != '' UNION
        SELECT
            sd.id AS id,
            sd.depart_name AS NAME,
            sd.parent_id AS parentId,
            sd.org_code AS CODE
        FROM
            fd_personnel a
            LEFT JOIN sys_depart sd ON sd.id = a.pk_depart
        WHERE
            a.del_flag = 0
            AND TRIM( sd.id ) != '' UNION
        SELECT
            sd.id AS id,
            sd.depart_name AS NAME,
            sd.parent_id AS parentId,
            sd.org_code AS CODE
        FROM
            fd_personnel a
            LEFT JOIN sys_depart sd ON a.pk_org = sd.id
        WHERE
            a.del_flag = 0
            AND a.pk_org != 'fe33687fb0a645ccb71a2873460a3d7d'
            AND TRIM( sd.id ) != ''
            ) t1
        ORDER BY
            CONVERT ( NAME USING gbk )
    </select>

    <!--根据所需角色查询人员-->
    <select id="getPersonnelByRole" resultType="org.jeecg.modules.basedata.entity.FdPersonnel">
        SELECT
            p.id,
            p.NAME,
            p.pk_depart,
            r.id as roleId,
            r.role_name as roleName
        FROM
            sys_user_role ur
            LEFT JOIN sys_user u ON u.id = ur.user_id
            LEFT JOIN sys_role r ON r.id = ur.role_id
            LEFT JOIN fd_personnel p ON p.id = u.personnel_id
        WHERE
            u.personnel_id != ''
            AND p.del_flag = '0'
            <if test="roleId!=null and roleId!=''">
                and r.id=#{roleId}
            </if>
    </select>

    <!--根据多个所需角色查询人员-->
    <select id="getPersonnelByRoleLists" resultType="org.jeecg.modules.basedata.entity.FdPersonnel">
        SELECT
        p.id,
        p.NAME,
        p.pk_depart,
        r.id as roleId,
        r.role_name as roleName
        FROM
        sys_user_role ur
        LEFT JOIN sys_user u ON u.id = ur.user_id
        LEFT JOIN sys_role r ON r.id = ur.role_id
        LEFT JOIN fd_personnel p ON p.id = u.personnel_id
        WHERE
        u.personnel_id != ''
        AND p.del_flag = '0'
        and r.id in
        <foreach collection="list" index="index" item="item" separator="," open="(" close=")">
            #{item}
        </foreach>
        GROUP BY CONVERT(p.NAME USING gbk)
    </select>

    <select id="getRoleBypersonnelList" resultType="org.jeecg.modules.system.entity.SysRole">
        SELECT
            *,
            u.personnel_id as personnelId
        FROM
            sys_role r
            LEFT JOIN sys_user_role ur ON ur.role_id = r.id
            LEFT JOIN sys_user u ON u.id = user_id
        WHERE
            u.del_flag='0'
    </select>

</mapper>