<?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.OrgChangeDao">

    <select id="selectOrgChangeList" resultType="com.bonus.system.att.entity.OrgChangeBean">
        SELECT
        su.user_name,
        so.org_name AS oldOrgName,
        so2.org_name AS newOrgName,
        ag.group_name AS oldAttGroupName,
        ag2.group_name AS newAttGroupName,
        oc.*
        FROM
        org_change oc
        LEFT JOIN sys_user su ON oc.user_id = su.user_id
        AND su.is_active = '1'
        LEFT JOIN sys_organization so ON oc.old_org_id = so.id
        AND so.is_active = '1'
        LEFT JOIN sys_organization so2 ON oc.new_org_id = so2.id
        AND so2.is_active = '1'
        LEFT JOIN att_group ag ON oc.old_att_group = ag.id
        AND ag.is_active = '1'
        LEFT JOIN att_group ag2 ON oc.new_att_group = ag2.id
        AND ag2.is_active = '1'
        WHERE
        oc.is_active = '1'
        <if test="userId != null and userId != '' ">
            and oc.user_id = #{userId}
        </if>
        <if test="userName != null and userName != '' ">
            and locate(#{userName},su.user_name)
        </if>
        <if test="startTime != null and startTime != '' ">
            and oc.create_time between #{startTime} and #{endTime}
        </if>
        <if test="isCheck != null and isCheck != '' ">
          <if test="isCheck == '-1'">
            and oc.is_check in (0,1,2)
          </if>
            <if test="isCheck != '-1'">
                and oc.is_check = #{isCheck}
            </if>
        </if>
        order by oc.is_check ASC
    </select>

    <select id="selectOrgChangeById" resultType="com.bonus.system.att.entity.OrgChangeBean">
        SELECT
            su.user_name,
            so.org_name AS oldOrgName,
            so2.org_name AS newOrgName,
            ag.group_name AS oldAttGroupName,
            ag2.group_name AS newAttGroupName,
            oc.*
        FROM
            org_change oc
                LEFT JOIN sys_user su ON oc.user_id = su.user_id
                AND su.is_active = '1'
                LEFT JOIN sys_organization so ON oc.old_org_id = so.id
                AND so.is_active = '1'
                LEFT JOIN sys_organization so2 ON oc.new_org_id = so2.id
                AND so2.is_active = '1'
                LEFT JOIN att_group ag ON oc.old_att_group = ag.id
                AND ag.is_active = '1'
                LEFT JOIN att_group ag2 ON oc.new_att_group = ag2.id
                AND ag2.is_active = '1'
        WHERE
            oc.is_active = '1'
            and oc.id = #{id}
    </select>
    <select id="getAttGroupList" resultType="com.bonus.system.api.domain.MapVo">
        SELECT
            id,
            group_name AS `name`
        FROM
            att_group
        WHERE
            is_active = '1'
    </select>

    <delete id="deleteAttGroup">
        update org_change
        set is_active = 0
        where id = #{id}
    </delete>

    <insert id="insertOrgChange">
        insert into org_change(user_id, old_org_id, new_org_id,is_change_att_group, change_effective_date, is_check,remark
        <if test="oldAttGroup != null and oldAttGroup != ''">,old_att_group</if>
        <if test="newAttGroup != null and newAttGroup != ''">,new_att_group</if>
        )
        values (#{userId},#{oldOrgId},#{newOrgId},#{isChangeAttGroup},#{changeEffectiveDate},#{isCheck},#{remark}
        <if test="oldAttGroup != null and oldAttGroup != ''">,#{oldAttGroup}</if>
        <if test="newAttGroup != null and newAttGroup != ''">,#{newAttGroup}</if>
        )
    </insert>


    <update id="updateOrgChange">
        update org_change set user_id = #{userId}, old_org_id = #{oldOrgId}, new_org_id = #{newOrgId},
                              is_change_att_group = #{isChangeAttGroup}, change_effective_date = #{changeEffectiveDate},
                              is_check = #{isCheck}, remark = #{remark}
        <if test="oldAttGroup != null and oldAttGroup != ''">,old_att_group = #{oldAttGroup}</if>
        <if test="newAttGroup != null and newAttGroup != ''">,new_att_group = #{newAttGroup}</if>
        where id = #{id}
    </update>
    <update id="orgChangeCheck">
        update org_change
        <set>
            <if test="checkUserId != 0L">
                check_opinion = #{checkOpinion},
                check_user_id = #{checkUserId},
                check_time = #{checkTime},
            </if>
            is_check = #{isCheck}
        </set>
        where id = #{id}
    </update>
    <update id="updateOrgByUserId">
        update sys_user_org set org_id = #{newOrgId} where user_id = #{userId} and org_id = #{oldOrgId} and is_active = 1;
        update att_group_person_relation set org_id = #{newOrgId} where user_id = #{userId} and org_id = #{oldOrgId};
    </update>
    <update id="updateAttGroupByUserId">
        update att_group_person_relation set group_id = #{newAttGroup},org_id = #{newOrgId} where user_id = #{userId} and group_id = #{oldAttGroup}
    </update>
    <update id="updateOrgChangeIsApply">
        update org_change set is_apply = #{isApply} where id = #{id}
    </update>
    <update id="updateOrgByUserIdAndOldOrgId">
        UPDATE leave_apply SET org_id = #{newOrgId},org_name = #{newOrgName}
        WHERE user_id = #{userId} and org_id = #{oldOrgId}
    </update>
</mapper>