<?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.att.dao.AttGroupDao">

    <select id="selectAttGroupList" resultType="com.bonus.system.att.entity.AttGroupBean">
        select ags.*, ag.id as groupId, ag.group_name, ag.att_type
        from att_group ag
        left join att_group_setting ags on ags.group_id = ag.id and ags.is_active = 1
        left join att_group_person_relation agpr on agpr.group_id = ag.id and agpr.is_active = 1
        where ag.is_active = 1
        <if test="groupName != null and groupName != '' ">
            and ag.group_name = #{groupName}
        </if>
        GROUP BY ag.id
    </select>

    <select id="selectOrgList" resultType="com.bonus.system.att.entity.AttGroupCheckOrgBean">
        select org.id                           as orgId,
               org.org_name                     as orgName,
               su.user_id,
               su.user_name,
               IF(agpr.is_active is null, 0, 1) as isBind
        from sys_organization org
                 left join sys_user_org suo on suo.org_id = org.id and org.is_active = 1
                 left join sys_user su on su.user_id = suo.user_id and su.is_active = 1
                 left join att_group_person_relation agpr on agpr.user_id = su.user_id and agpr.is_active = 1
        where org.is_active = 1
        GROUP BY org.id, su.user_id
    </select>

    <select id="selectAttGroupById" resultType="com.bonus.system.att.entity.AttGroupBean">
        select ag.id as groupId, ag.*, ags.*
        from att_group ag
                 left join att_group_setting ags on ags.group_id = ag.id and ags.is_active = 1
        where ag.is_active = 1
          and ag.id = #{groupId}
    </select>

    <select id="selectAttGroupUserById" resultType="com.bonus.system.att.entity.AttGroupCheckOrgBean">
        select user_id AS userId
        from att_group_person_relation
        where group_id = #{groupId}
          and is_active = 1
    </select>

    <select id="getAttGroupPersonIsBind" resultType="java.lang.String">
        SELECT IFNULL((SELECT is_active
                       FROM att_group_person_relation
                       WHERE user_id = #{id}
                         AND is_active = 1), 0) as idBind
        FROM DUAL
    </select>

    <select id="getAttGroupCheckPerson" resultType="com.bonus.system.basic.domain.SysTree">
        select CONCAT('org', '|', id) AS id, org_name as name, CONCAT('org', '|', parent_id) AS parent_id,'' as userId, id as orgId,false AS isChecked
        from sys_organization
        where is_active = 1
        union
        select CONCAT('user', '|',su.user_id)      as id,
               user_name                               as name,
               CONCAT('org', '|', suo.org_id )  as parnet_id,
               su.user_id,
               suo.org_id,
               IF(agpr.user_id IS NULL,false,true) AS isChecked
        from sys_user su
        left join sys_user_org suo on suo.user_id = su.user_id and suo.is_active = 1
        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="groupId==null">
            AND agpr.group_id IS NULL
        </if>
        <if test="groupId!=null">
            AND (agpr.group_id = #{groupId} OR agpr.group_id IS NULL)
        </if>
    </select>

    <select id="getUserByOrg" resultType="com.bonus.system.att.entity.AttGroupCheckOrgBean">
        SELECT
        su.user_id,
        suo.org_id
        FROM
        sys_user su
        LEFT JOIN sys_user_org suo ON suo.user_id = su.user_id
        AND suo.is_active = 1
        LEFT JOIN att_group_person_relation agpr ON agpr.user_id = su.user_id
        <if test="groupId != null and groupId != ''">
            and agpr.group_id != #{groupId}
        </if>
        AND agpr.is_active = 1
        WHERE
        su.is_active = 1
        <if test="idList != null and idList.size() > 0">
            AND suo.org_id IN
            <foreach item="id" collection="idList" separator="," open="(" close=")" index="">
                #{id}
            </foreach>
        </if>
        -- AND agpr.user_id IS NULL
    </select>

    <delete id="deleteAttGroup">
        update att_group
        set is_active = 0
        where id = #{groupId};
        update att_group_setting
        set is_active = 0
        where group_id = #{groupId};
        update att_group_person_relation
        set is_active = 0,expiring_time = now()
        where group_id = #{groupId};
    </delete>

    <delete id="deleteAttGroupRelationPerson">
        update att_group_person_relation
        set is_active = 0
        where group_id = #{groupId};
    </delete>

    <delete id="deleteAttGroupPersonByUserId">
        <foreach item="params" collection="list" separator=";">
        update att_group_person_relation
        set is_active = 0,expiring_time = now()
        where user_id = #{params.userId}
        </foreach>
    </delete>

    <insert id="insertAttGroup" useGeneratedKeys="true" keyColumn="id" keyProperty="groupId">
        insert into att_group(group_name, att_type, create_user_id)
        values (#{groupName}, #{attType}, #{createUserId});
    </insert>

    <insert id="insertAttGroupSetting">
        insert into att_group_setting(group_id, att_day
        <if test="toWorkTime != null and toWorkTime != ''">,to_work_time</if>
        <if test="offWorkTime != null and offWorkTime != ''">,off_work_time</if>
        <if test="breakStartTime != null and breakStartTime != ''">,break_start_time</if>
        <if test="breakEndTime != null and breakEndTime != ''">,break_end_time</if>
        <if test="attRange != null and attRange != ''">,att_range</if>
        <if test="lateMinute != null ">,late_minute</if>
        <if test="leaveMinute != null ">,leave_minute</if>
        <if test="absenteeismLateMinute != null ">,absenteeism_late_minute</if>
        <if test="absenteeismLeaveMinute != null ">,absenteeism_leave_minute</if>
        <if test="entryAbnormalMinute != null ">,entry_abnormal_minute</if>
        <if test="todayClockNum != null ">,today_clock_num</if>
        <if test="attendanceDuration != null ">,attendance_duration</if>
        <if test="isHaveHoliday!= null ">,is_have_holiday</if>
        )values( #{groupId}, #{attDay}
        <if test="toWorkTime != null and toWorkTime != ''">,#{toWorkTime}</if>
        <if test="offWorkTime != null and offWorkTime != ''">,#{offWorkTime}</if>
        <if test="breakStartTime != null and breakStartTime != ''">,#{breakStartTime}</if>
        <if test="breakEndTime != null and breakEndTime != ''">,#{breakEndTime}</if>
        <if test="attRange != null and attRange != ''">,#{attRange}</if>
        <if test="lateMinute != null ">,#{lateMinute}</if>
        <if test="leaveMinute != null ">,#{leaveMinute}</if>
        <if test="absenteeismLateMinute != null ">,#{absenteeismLateMinute}</if>
        <if test="absenteeismLeaveMinute != null ">,#{absenteeismLeaveMinute}</if>
        <if test="entryAbnormalMinute != null ">,#{entryAbnormalMinute}</if>
        <if test="todayClockNum != null ">,#{todayClockNum}</if>
        <if test="attendanceDuration != null ">,#{attendanceDuration}</if>
        <if test="isHaveHoliday != null ">,#{isHaveHoliday}</if>
        )
    </insert>

    <insert id="insertAttGroupPerson">
        insert into att_group_person_relation(group_id, user_id, effective_time)
        values
        <foreach item="params" collection="list" separator=",">
            (#{params.groupId}, #{params.userId}, now())
        </foreach>
    </insert>

    <update id="updateAttGroup">
        update att_group
        set group_name     = #{groupName},
            att_type       = #{attType},
            update_user_id = #{updateUserId}
        where id = #{groupId}
    </update>

    <update id="updateAttGroupSetting">
        update att_group_setting set att_day = #{attDay}
        <if test="toWorkTime != null and toWorkTime != ''">,to_work_time = #{toWorkTime}</if>
        <if test="offWorkTime != null and offWorkTime != ''">,off_work_time = #{offWorkTime}</if>
        <if test="breakStartTime != null and breakStartTime != ''">,break_start_time = #{breakStartTime}</if>
        <if test="breakEndTime != null and breakEndTime != ''">,break_end_time = #{breakEndTime}</if>
        <if test="attRange != null and attRange != ''">,att_range = #{attRange}</if>
        <if test="lateMinute != null ">,late_minute = #{lateMinute}</if>
        <if test="leaveMinute != null ">,leave_minute = #{leaveMinute}</if>
        <if test="absenteeismLateMinute != null ">,absenteeism_late_minute =
            #{absenteeismLateMinute}
        </if>
        <if test="absenteeismLeaveMinute != null ">,absenteeism_leave_minute =
            #{absenteeismLeaveMinute}
        </if>
        <if test="entryAbnormalMinute != null ">,entry_abnormal_minute =
            #{entryAbnormalMinute}
        </if>
        <if test="todayClockNum != null ">,today_clock_num = #{todayClockNum}</if>
        <if test="attendanceDuration != null ">,attendance_duration =
            #{attendanceDuration}
        </if>
        <if test="isHaveHoliday != null ">,is_have_holiday =
            #{isHaveHoliday}
        </if>
        where group_id = #{groupId}
    </update>
</mapper>