<?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="jnpf.attendance.mapper.PublicHolidayRulesMapper" >

    <insert id="insert">
        INSERT INTO ftb_attendance_public_holiday_rules (
            F_Id, F_Name,   F_Type,  F_DayNum,  F_TransformationType,
            F_LifespanType,   F_FixedDay,  F_SpecifyDay,  F_ScopeOfAdaptation,
            F_State,   F_CreatorTime,   F_CreatorUserId,  F_LastModifyTime,  F_LastModifyUserId
        )
        VALUES (
            #{publicHolidayRulesDto.id},  #{publicHolidayRulesDto.name},    #{publicHolidayRulesDto.type},   #{publicHolidayRulesDto.dayNum},   #{publicHolidayRulesDto.transformationType},
            #{publicHolidayRulesDto.lifespanType},   #{publicHolidayRulesDto.fixedDay},  #{publicHolidayRulesDto.specifyDay},  #{publicHolidayRulesDto.scopeOfAdaptation},
            0, now(),  #{userId}, now(), #{userId}
        )
    </insert>

    <update id="update">
        UPDATE ftb_attendance_public_holiday_rules
        SET
        F_Name = #{publicHolidayRulesDto.name},
        F_Type = #{publicHolidayRulesDto.type},
        F_DayNum = #{publicHolidayRulesDto.dayNum},
        F_TransformationType = #{publicHolidayRulesDto.transformationType},
        F_LifespanType = #{publicHolidayRulesDto.lifespanType},
        F_FixedDay = #{publicHolidayRulesDto.fixedDay},
        F_SpecifyDay = #{publicHolidayRulesDto.specifyDay},
        F_ScopeOfAdaptation = #{publicHolidayRulesDto.scopeOfAdaptation},
        F_LastModifyTime = now(),
        F_LastModifyUserId = #{userId}
        WHERE F_Id = #{publicHolidayRulesDto.id}
    </update>

    <update id="updateState">
        UPDATE ftb_attendance_public_holiday_rules
        SET
        F_State = #{state},
        F_LastModifyTime = now(),
        F_LastModifyUserId = #{userId}
        WHERE F_Id = #{id}
    </update>
    <update id="updateScopeOfAdaptation">
        UPDATE ftb_attendance_public_holiday_rules
        SET F_ScopeOfAdaptation = -1
    </update>
    <delete id="deletePublicHolidayBalance">
        DELETE FROM ftb_attendance_public_holiday_balance
        WHERE F_YearMonth = #{yearMonth} AND F_UserId IN
        <foreach collection="userIds" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
    </delete>

    <select id="list" resultType="jnpf.model.attendance.vo.attendance.AttendancePublicHolidayRulesVo">
        SELECT
        F_Id AS id,
        F_Name AS name,
        F_Type AS type,
        F_DayNum AS dayNum,
        F_TransformationType AS transformationType,
        F_ScopeOfAdaptation AS scopeOfAdaptation,
        F_State AS state
        FROM  ftb_attendance_public_holiday_rules
        WHERE   1=1
        <if test="iText != null and iText != ''">
            AND F_Name LIKE CONCAT('%',#{iText},'%')
        </if>
        ORDER BY  F_LastModifyTime DESC
    </select>

    <select id="getBalanceList"
            resultType="jnpf.model.attendance.vo.attendance.AttendancePublicHolidayBalance">
        SELECT  F_UserId userId,  F_Total total
        FROM ftb_attendance_public_holiday_balance
        WHERE F_YearMonth  = #{yearMonth} AND F_UserId IN
        <foreach collection="userIds" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
    </select>
    <select id="getPublicHolidayBalanceList"
            resultType="jnpf.model.attendance.vo.attendance.AttendancePublicHolidayBalance">
        SELECT  F_UserId userId, F_YearMonth yearMonth,  F_Balance balance,  F_Total total ,F_TransformationType transformationType
        FROM ftb_attendance_public_holiday_balance
        WHERE F_YearMonth  = #{yearMonth} AND F_UserId IN
        <foreach collection="userIds" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
    </select>

    <insert id="batchAddPublicHolidayBalance">
        INSERT INTO ftb_attendance_public_holiday_balance (
            F_Id, F_UserId, F_Balance, F_Total, F_YearMonth,F_TransformationType,F_AttendanceRatio
        )
        VALUES
        <foreach collection="addList" item="item" separator=",">
            (
            #{item.id}, #{item.userId}, #{item.balance}, #{item.total}, #{yearMonth},#{item.transformationType},#{item.attendanceRatio}
            )
        </foreach>
    </insert>

    <select id="getPublicHolidayTransferList" resultType="jnpf.model.attendance.vo.attendance.PublicHolidayTransferListVo">
        SELECT
        F_UserId userId,
        F_YearMonth yearMonth,
        F_Balance surplusDays,
        F_AttendanceRatio attendanceRatio
        FROM ftb_attendance_public_holiday_balance
        WHERE F_YearMonth = #{yearMonth}
        AND F_TransformationType = 0
        AND F_UserId IN
        <foreach collection="userIds" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
    </select>
    <select id="selectPublicHolidayBalanceList" resultType="java.lang.String">
        SELECT F_UserId
        FROM ftb_attendance_public_holiday_balance
        WHERE F_YearMonth = #{yearMonth} AND F_UserId IN
        <foreach collection="userIds" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
    </select>
</mapper>
