<?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.bonus.system.basic.dao.SysUserMapper">
    <insert id="insertUser" keyColumn="user_id" useGeneratedKeys="true" keyProperty="userId">
        insert into sys_user(
        <if test="userName != null and userName != ''">user_name,</if>
        <if test="phone != null and phone != ''">phone,</if>
        <if test="idNumber != null and idNumber != ''">id_number,</if>
        <if test="password != null and password != ''">password,</if>
        <if test="createBy != null and createBy != ''">create_user_id,</if>
        <if test="isCadre != null and isCadre != ''">is_cadre,</if>
        create_time
        )values(
        <if test="userName != null and userName != ''">#{userName},</if>
        <if test="phone != null and phone != ''">#{phone},</if>
        <if test="idNumber != null and idNumber != ''">#{idNumber},</if>
        <if test="password != null and password != ''">#{password},</if>
        <if test="createBy != null and createBy != ''">#{createBy},</if>
        <if test="isCadre != null and isCadre != ''">#{isCadre},</if>
        sysdate()
        )
    </insert>

    <insert id="insertUserPost">
        insert into sys_user_post(user_id, post_id) values
        <foreach collection="list" item="item" separator=",">
            (#{item.userId},#{item.postId})
        </foreach>
    </insert>
    <insert id="insertUserRole">
        insert into sys_user_role(user_id, role_id) values
        <foreach collection="list" item="item" separator=",">
            (#{item.userId},#{item.roleId})
        </foreach>
    </insert>
    <insert id="insertUserOrg">
        insert into sys_user_org(user_id, org_id) values
        <foreach collection="list" item="item" separator=",">
            (#{item.userId},#{item.orgId})
        </foreach>
    </insert>
    <delete id="deleteUserOrgByUserId">
        delete
        from sys_user_org
        where user_id = #{userId}
    </delete>
    <delete id="deleteUserPostByUserId">
        delete
        from sys_user_post
        where user_id = #{userId}
    </delete>
    <delete id="deleteUserRoleByUserId">
        delete
        from sys_user_role
        where user_id = #{userId}
    </delete>
    <!--删除人员数据后，同步删除考勤组数据-->
    <delete id="delAttGroupPersonRelation">
        UPDATE att_group_person_relation SET is_active = '0' WHERE user_id = #{userId}
    </delete>
    <update id="deleteUserById">
        update sys_user
        set is_active = 0
        where user_id = #{userId}
    </update>
    <update id="updateUser">
        update sys_user
        <set>
            <if test="userName != null and userName != ''">user_name = #{userName},</if>
            <if test="phone != null and phone != ''">phone = #{phone},</if>
            <if test="idNumber != null and idNumber != ''">id_number = #{idNumber},</if>
            <if test="password != null and password != ''">password = #{password},</if>
            <if test="updateBy != null and updateBy != ''">update_user_id = #{updateBy},</if>
            <if test="isCadre != null and isCadre != ''">is_cadre = #{isCadre},</if>
            update_time = sysdate()
        </set>
        where user_id = #{userId}
    </update>

    <update id="updateIsFace">
        update sys_user
        set is_face = #{isFace}
        where user_id = #{userId}
    </update>

    <!--	改两次sys_user是为了第一次录入人脸时多次录入需要审核但又未更新时间的问题-->
    <update id="checkPersonAssignment">
        UPDATE sys_user_face SET
        `is_check` = #{isCheck},
        `examine_opinion` = #{examineOpinion}
        WHERE `user_id` = #{userId};
        <if test='isCheck == 1'>
            update sys_user set is_face = 1 where user_id = #{userId};
            update sys_user set is_face = 0 where user_id = #{userId};
        </if>
    </update>
    <update id="updateAttOrgByUserId">
        update att_group_person_relation
        set org_id = #{newOrgId}
        where user_id = #{userId}
          and org_id = #{oldOrgId};
    </update>
    <update id="updateOrgIdByUserId">
        UPDATE leave_apply SET org_id = #{orgId},org_name = #{orgName}
        WHERE user_id = #{userId}
    </update>
    <update id="updateOrgIdByUserIdWithOutSame">
        UPDATE leave_apply SET org_id = #{user.orgId},org_name = #{user.orgName}
        WHERE user_id = #{user.userId}
        <if test='user.orgListIds.size != null and user.orgListIds.size > 0'>
            and org_id not in
            <foreach item="id" index="index" collection="user.orgListIds"
                     open="(" separator="," close=")">
                #{id}
            </foreach>
        </if>
    </update>

    <select id="selectUserByUserName" resultType="com.bonus.system.api.domain.SysUser">
        select *
        from sys_user su
        where su.is_active = 1
          and su.phone = #{userName}
    </select>

    <select id="selectUserById" resultType="com.bonus.system.api.domain.SysUser">
        select *
        from sys_user su
        where su.user_id = #{userId}
    </select>
    <select id="getOrgByUserId" resultType="com.bonus.system.api.domain.MapVo">
        SELECT id,
               `org_name` AS `name`
        FROM sys_organization
        WHERE id IN (SELECT org_id
                     FROM sys_user_org
                     WHERE user_id = #{userId})
    </select>
    <select id="selectUserList" resultType="com.bonus.system.api.domain.SysUser">
        SELECT
        su.user_id AS userId,
        su.user_name as userName,
        su.id_number as idNumber,
        GROUP_CONCAT(DISTINCT so.org_name ORDER BY so.id SEPARATOR ', ') as orgName,
        GROUP_CONCAT(DISTINCT sdd.dict_label ORDER BY sdd.dict_code SEPARATOR ', ') as postName,
        GROUP_CONCAT(DISTINCT sr.role_name ORDER BY sr.role_id SEPARATOR ', ') as roleName,
        su.phone,su.is_cadre as isCadre,
        su.is_face as isFace,
        case when suf.new_face is not null then suf.new_face else suf.applied_face end new_face,
        suf.is_check,
        suf.examine_opinion as examineOpinion,
        suf.collection_time,
        if(agpr.user_id is null, false, true) as isAttend
        FROM
        sys_user su
        LEFT JOIN sys_user_post sup on sup.user_id=su.user_id and sup.is_active=1
        LEFT JOIN sys_dict_data sdd on sdd.dict_code=sup.post_id and sdd.`status`=0
        LEFT JOIN sys_user_role sur on sur.user_id=su.user_id and sur.is_active=1
        LEFT JOIN sys_role sr on sr.role_id=sur.role_id and sr.del_flag=0
        LEFT JOIN sys_user_org suo on suo.user_id=su.user_id and suo.is_active=1
        LEFT JOIN sys_organization so on so.id=suo.org_id and so.is_active=1
        LEFT JOIN sys_user_face suf ON suf.user_id = su.user_id
        LEFT JOIN att_group_person_relation agpr on agpr.user_id = su.user_id and agpr.is_active = 1
        WHERE
        su.is_active =1
        <if test="params.userName != null and params.userName != ''">
            AND su.user_name like concat('%', #{params.userName}, '%')
        </if>
        <if test="params.orgId != null and params.orgId != ''">
            AND suo.org_id = #{params.orgId}
        </if>
        <if test="params.phone != null and params.phone != ''">
            AND su.phone = #{params.phone}
        </if>
        <if test="params.userId != null">
            AND su.user_id = #{params.userId}
        </if>
        <if test='params.orgIdList != null and params.orgIdList.size() > 0'>
            and suo.org_id in (
            <foreach collection="params.orgIdList" item="item" separator=",">
                #{item}
            </foreach>
            )
        </if>
<!--        <if test="roleType =='2' || roleType == 2 ">-->
<!--            and suo.org_id in (#{orgListId})-->
<!--        </if>-->
        <if test="params.isAttend =='1' || params.isAttend == 1 ">
            and agpr.user_id is not null
        </if>
        <if test="params.isAttend =='0' || params.isAttend == 0 ">
            and agpr.user_id is null
        </if>

        GROUP BY su.user_id
        order by su.update_time desc
    </select>
    <select id="getSelectRole" resultType="com.bonus.system.api.domain.SysRole">
        SELECT role_id,
               role_name
        FROM sys_role
        WHERE del_flag = '0'
          and status = '0'
    </select>
    <select id="getSelectPost" resultType="com.bonus.system.api.domain.SysDictData">
        SELECT dict_code,
               dict_label
        FROM sys_dict_data
        WHERE dict_type = 'post_list'
          and status = '0'
    </select>
    <select id="getRoleIdsByUserId">
        SELECT role_id as roleId
        FROM sys_user_role
        WHERE user_id = #{userId}
          and is_active = 1
    </select>
    <select id="getOrgIdsByUserId">
        SELECT org_id as orgId
        FROM sys_user_org
        WHERE user_id = #{userId}
          and is_active = 1
    </select>
    <select id="getPostIdsByUserId">
        SELECT sdd.dict_value as postId
        FROM sys_dict_data sdd
                 LEFT JOIN sys_user_post sup on sup.post_id = sdd.dict_code and sup.is_active = 1
        WHERE sup.user_id = #{userId}
          and sdd.`status` = 0
    </select>
    <select id="checkPhoneUnique" resultType="com.bonus.system.api.domain.SysUser">
        select user_id as userId, phone
        from sys_user
        where phone = #{phone}
          and is_active = '1' limit 1
    </select>
    <select id="selectPostIdById" resultType="java.lang.String">
        select dict_code as postId
        from sys_dict_data
        where dict_value = #{postId}
          and dict_type = 'post_list'
          and status = '0' limit 1
    </select>
    <select id="getOrgList" resultType="com.bonus.system.holiday.entity.HolidayKeyBean">
        SELECT id,
               org_name as `name`
        FROM `sys_organization`
        WHERE is_active = 1
          and org_name is not null
        GROUP BY org_name
    </select>
    <select id="getRoleList" resultType="com.bonus.system.holiday.entity.HolidayKeyBean">
        SELECT role_id   as id,
               role_name as `name`
        FROM sys_role
        WHERE del_flag = '0'
          and status = '0'
          and role_name is not null
        GROUP BY role_name
    </select>
    <select id="getPostList" resultType="com.bonus.system.holiday.entity.HolidayKeyBean">
        SELECT dict_code  as id,
               dict_label as `name`
        FROM sys_dict_data
        WHERE dict_type = 'post_list'
          and status = '0'
          and dict_label is not null
        GROUP BY dict_label
    </select>
    <select id="getSelectUser" resultType="com.bonus.system.api.domain.SysUser">
        SELECT user_id   AS userIds,
               user_name AS userName,
               id_number AS idNumber,
               phone
        FROM sys_user
        WHERE is_active = 1
          AND user_name IS NOT NULL
    </select>
    <select id="checkIdNumberUnique" resultType="com.bonus.system.api.domain.SysUser">
        select user_id as userId, id_number as idNumber
        from sys_user
        where id_number = #{idNumber}
          and is_active = '1' limit 1
    </select>
    <select id="selectUserByUserNames" resultType="com.bonus.system.api.domain.SysUser">
        SELECT suo.org_id AS orgId
        FROM sys_user su
                 LEFT JOIN sys_user_org suo ON suo.user_id = su.user_id
            AND suo.is_active = 1
        WHERE su.is_active = 1
          AND su.user_name = #{userName}
    </select>

    <select id="getSelectUserCurrent" resultType="com.bonus.system.api.domain.SysUser">
        SELECT user_id   AS userIds,
               user_name AS userName,
               id_number AS idNumber,
               phone
        FROM sys_user
        WHERE is_active = 1
          AND user_name IS NOT NULL
    </select>
    <select id="checkUserNameUnique" resultType="com.bonus.system.api.domain.SysUser">
        select user_id as userId, id_number as idNumber
        from sys_user
        where user_name = #{userName}
          and is_active = '1' limit 1
    </select>

    <select id="getAttGroupByUserId" resultType="com.bonus.system.api.domain.MapVo">
        SELECT ag.id,
               ag.group_name AS `name`
        FROM att_group_person_relation agpr
                 LEFT JOIN att_group ag ON agpr.group_id = ag.id
            AND ag.is_active = '1'
        WHERE agpr.user_id = #{userId}
          and agpr.is_active = '1'
          and ag.id is not null LIMIT 1
    </select>
    <select id="getUserAttGroupByUserId" resultType="java.lang.Long">
        select org_id
        from att_group_person_relation
        where user_id = #{userId}
          and is_active = 1 limit 1

    </select>
    <select id="getRoleListById" resultType="java.lang.String">
        select role_id
        from sys_user_role
        where user_id = #{userId}
    </select>

    <select id="getOrg" resultType="java.lang.String">
        SELECT GROUP_CONCAT(suo.org_id SEPARATOR ', ') AS orgName
        FROM `sys_user` su
			 LEFT JOIN sys_user_org suo on suo.user_id = su.user_id and suo.is_active = 1
        WHERE su.is_active = 1
          AND su.user_id = #{userId}
    </select>

    <select id="getUserIdList" resultType="java.lang.String">
        SELECT GROUP_CONCAT(su.user_id SEPARATOR ',') AS orgName
        FROM `sys_user` su
                 LEFT JOIN sys_user_org suo on suo.user_id = su.user_id and suo.is_active = 1
        WHERE su.is_active = 1
          AND suo.org_id in (#{userIdList})
    </select>

    <select id="getManagerList" resultType="com.bonus.system.api.domain.SysRole">
        SELECT *
        FROM sys_organization_head
        WHERE is_active = '1'
          AND org_id in (
            SELECT org_id FROM sys_organization_head
            WHERE is_active = '1'
            AND user_id = #{userId}
        )
    </select>
    <select id="findOrgByOrgId" resultType="com.bonus.system.basic.domain.SysOrg">
        SELECT org_name as orgName
        FROM sys_organization
        WHERE id = #{orgId}
    </select>
</mapper>