<?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.personnels.mapper.FtbPersonnelsRewardsPunishmentsMapper">
    <!--查询指定类型的排序最大值-->
    <select id="queryTheMaximumValueOfSorting" resultType="java.lang.Integer">
        SELECT IFNULL(MAX(F_Sort),0) FROM ftb_personnels_rewards_punishments WHERE F_EnableMark = 0
    </select>
    <!--更新排序值-->
    <update id="updateSortValue">
        UPDATE ftb_personnels_rewards_punishments SET F_Sort = F_Sort-1 WHERE F_Sort > #{sortValue}
    </update>
    <!--查询奖励和处罚信息-->
    <select id="listQuery"
            resultType="jnpf.model.personnels.vo.rewardspunishments.FtbPersonnelsRewardsPunishmentQueryVO">
        SELECT
        a.F_Id as id,
        a.F_RegularName as regularName,
        a.F_Money as money,
        a.F_Status as status,
        a.F_Sort as sort,
        a.F_LastModifyTime as lastModifyTime,
        a.F_LastModifyUserId as lastModifyUserId,
        a.F_IsRecorded as isRecorded,
        a.F_Type as type
        FROM
        ftb_personnels_rewards_punishments a LEFT joIN ftb_personnels_staff_roster b ON a.F_LastModifyUserId = b.F_UserId
        WHERE
        a.F_EnableMark = 0
        <if test="params.type != null">
            AND a.F_Type = #{params.type}
        </if>
        <if test="params.status != null">
            AND a.F_Status = #{params.status}
        </if>
        <if test="params.regularName != null and params.regularName != ''">
            AND (a.F_RegularName LIKE CONCAT('%',#{params.regularName},'%') or b.F_Name LIKE CONCAT('%',#{params.regularName},'%'))
        </if>
        <if test="params.startTime != null and params.endTime != null">
            AND DATE_FORMAT(a.F_LastModifyTime, '%Y-%m-%d') &gt;= #{params.startTime}
            AND DATE_FORMAT(a.F_LastModifyTime, '%Y-%m-%d') &lt;= #{params.endTime}
        </if>
        order by a.F_Sort
    </select>
    <!--向下更新排序范围-->
    <update id="updateSortDownward">
        UPDATE ftb_personnels_rewards_punishments SET F_Sort = F_Sort+1
        WHERE F_Id != #{id} and F_Sort >= #{minSort} and F_Sort &lt;= #{maxSort}
    </update>
    <!--向下更新排序范围-->
    <update id="updateSortDownwardS">
        UPDATE ftb_personnels_rewards_punishments SET F_Sort = F_Sort-1
        where F_Id != #{id} and F_Sort >= #{minSort} and F_Sort &lt;= #{maxSort}
    </update>
    <!--查询总工资和奖励信息-->
    <select id="queryTheTotalSalaryAndRewards"
            resultType="jnpf.model.personnels.vo.rewardspunishments.FtbPersonnelSalaryRewardVO">
        SELECT
        b.F_UserId AS userId,
        SUM(a.F_Amount) AS rewardMoney
        FROM
        ftb_reward_approval a JOIN ftb_reward_user b ON a.F_Id=b.F_ApprovalId
        WHERE
        a.F_Status = 1 AND
        DATE_FORMAT( a.F_Day, '%Y-%m' ) = #{params.monthInfo}
        and b.F_UserId in
        <foreach collection="params.userIds" item="userId" open="(" separator="," close=")" >
            #{userId}
        </foreach>
        GROUP BY b.F_UserId
    </select>
    <!--查询总工资和处罚信息-->
    <select id="queryTheTotalSalaryAndPenaltys"
            resultType="jnpf.model.personnels.vo.rewardspunishments.FtbPersonnelSalaryRewardVO">
        SELECT
        b.F_UserId AS userId,
        SUM(a.F_Amount) AS rewardMoney
        FROM
        ftb_punishments_approval a join ftb_punishments_user b on a.F_Id=b.F_PunishmentsId
        WHERE
        a.F_Status = 1 AND
        DATE_FORMAT( a.F_Day, '%Y-%m' ) = #{params.monthInfo}
        and b.F_UserId in
        <foreach collection="params.userIds" item="userId" open="(" separator="," close=")" >
            #{userId}
        </foreach>
        GROUP BY b.F_UserId
    </select>

    <select id="salaryMetaDataQuery" parameterType="jnpf.model.personnels.dto.salary.FtbSalaryMetaDataQueryDto" resultType="java.math.BigDecimal">
        SELECT
            SUM(g.rewardMoney)
        FROM
            (
                SELECT
                    1 AS type,
                    COALESCE(a.F_UserId, b.F_UserId) AS userId,
                    c.F_Name AS userName,
                    a.F_TypeName AS rewardTypeName,
                    a.F_Amount AS rewardMoney,
                    a.F_Day AS operateTime
                FROM
                    ftb_punishments_approval a
                        LEFT JOIN ftb_punishments_user b ON a.F_Id = b.F_PunishmentsId
                        JOIN ftb_personnels_staff_roster c ON (a.F_UserId = c.F_UserId OR b.F_UserId = c.F_UserId )
                WHERE
                    a.F_Status IN(1,4)
                UNION
                SELECT
                    0 AS type,
                    COALESCE(a.F_UserId, b.F_UserId) AS userId,
                    c.F_Name AS userName,
                    a.F_TypeName AS rewardTypeName,
                    a.F_Amount AS rewardMoney,
                    a.F_Day AS operateTime
                FROM
                    ftb_reward_approval a
                    LEFT JOIN ftb_reward_user b ON a.F_Id = b.F_ApprovalId
                        JOIN ftb_personnels_staff_roster c ON (a.F_UserId = c.F_UserId OR b.F_UserId = c.F_UserId )
                WHERE
                    a.F_Status IN(1,4)
            ) AS g
        WHERE
            g.rewardTypeName IS NOT NULL
          AND g.userId = #{userId}
          <if test="type != null">
              AND g.type = #{type}
          </if>
        <if test="typeName != null and typeName!= ''">
            AND g.rewardTypeName =#{typeName}
        </if>
          AND DATE_FORMAT( g.operateTime, '%Y-%m-%d' ) BETWEEN DATE_FORMAT( #{startTime}, '%Y-%m-%d' )
            AND DATE_FORMAT(#{endTime}, '%Y-%m-%d' )
    </select>


    <select id="salaryBatchMetaDataQuery" parameterType="jnpf.model.personnels.dto.salary.FtbSalaryMetaDataQueryDto" resultType="jnpf.model.personnels.vo.rewardspunishments.FtbXcEmployeeRewardRecordsVO">
        SELECT
        g.userId AS userId,
        g.type AS rewardType,
        g.rewardTypeName AS rewardTypeName,
        SUM(g.rewardMoney) AS rewardMoney
        FROM
        (
        SELECT
        1 AS type,
        COALESCE(a.F_UserId, b.F_UserId) AS userId,
        c.F_Name AS userName,
        a.F_TypeName AS rewardTypeName,
        a.F_Amount AS rewardMoney,
        a.F_Day AS operateTime
        FROM
        ftb_punishments_approval a
        LEFT JOIN ftb_punishments_user b ON a.F_Id = b.F_PunishmentsId
        JOIN ftb_personnels_staff_roster c ON (a.F_UserId = c.F_UserId OR b.F_UserId = c.F_UserId )
        WHERE
        a.F_Status IN(1,4)
        UNION
        SELECT
        0 AS type,
        COALESCE(a.F_UserId, b.F_UserId) AS userId,
        c.F_Name AS userName,
        a.F_TypeName AS rewardTypeName,
        a.F_Amount AS rewardMoney,
        a.F_Day AS operateTime
        FROM
        ftb_reward_approval a
        LEFT JOIN ftb_reward_user b ON a.F_Id = b.F_ApprovalId
        JOIN ftb_personnels_staff_roster c ON (a.F_UserId = c.F_UserId OR b.F_UserId = c.F_UserId )
        WHERE
        a.F_Status IN(1,4)
        ) AS g
        WHERE
        g.rewardTypeName IS NOT NULL
        <if test="userIds != null and userIds.size()>0">
            AND g.userId in
            <foreach collection="userIds" item="x" open="(" close=")" separator=",">
                #{x}
            </foreach>
        </if>
        AND DATE_FORMAT( g.operateTime, '%Y-%m-%d' ) BETWEEN DATE_FORMAT( #{startTime}, '%Y-%m-%d' )
        AND DATE_FORMAT(#{endTime}, '%Y-%m-%d' )
        GROUP BY g.userId,g.type,g.rewardTypeName
    </select>

    <!--员工奖惩记录分页查询-->
    <select id="employeeRewardAndPunishmentRecords"
            resultType="jnpf.model.personnels.vo.rewardspunishments.FtbEmployeeRewardRecordsVO">
        SELECT
        *
        FROM
        (
        SELECT
        a.F_Id AS id,
        1 AS type,
        if(a.F_Status = 4,a.F_UserId,b.F_UserId) as userId,
        c.F_SystemWokerId AS userCustomerId,
        c.F_Name AS userName,
        a.F_TypeName AS rewardTypeName,
        a.f_flowid AS taskId,
        a.F_Amount AS rewardMoney,
        a.F_RewardReason AS rewardReason,
        a.F_CreatorTime AS operateTime,
        a.F_CreatorUserId AS operator,
        IFNULL(a.F_OrgName ,b.F_OrgName) as orgName,
        IFNULL(a.F_PostName ,b.F_PostName) as jobName,
        a.F_Status AS `status`
        FROM
        ftb_punishments_approval a
        LEFT JOIN ftb_punishments_user b ON a.F_Id = b.F_PunishmentsId
        JOIN ftb_personnels_staff_roster c ON ( a.F_UserId = c.F_UserId OR b.F_UserId = c.F_UserId )
        UNION
        SELECT
        a.F_Id AS id,
        0 AS type,
        if(a.F_Status = 4,a.F_UserId,b.F_UserId) as userId,
        c.F_SystemWokerId AS userCustomerId,
        c.F_Name AS userName,
        a.F_TypeName AS rewardTypeName,
        a.f_flowid AS taskId,
        a.F_Amount AS rewardMoney,
        a.F_RewardReason AS rewardReason,
        a.F_CreatorTime AS operateTime,
        a.F_CreatorUserId AS operator,
        IFNULL(a.F_OrgName ,b.F_OrgName) as orgName,
        IFNULL(a.F_PostName ,b.F_PostName) as jobName,
        a.F_Status AS `status`
        FROM
        ftb_reward_approval a
        LEFT JOIN ftb_reward_user b ON a.F_Id = b.F_ApprovalId
        JOIN ftb_personnels_staff_roster c ON ( a.F_UserId = c.F_UserId OR b.F_UserId = c.F_UserId )
        ) AS g
        <where>
            <if test="params.type != null ">
                g.type = #{params.type}
            </if>
            <if test="params.keyWords != null ">
                and (g.userName like concat('%',#{params.keyWords},'%') or g.rewardReason like concat('%',#{params.keyWords},'%'))
            </if>

            <if test="params.startTime != null and params.endTime != null">
                and DATE_FORMAT(g.operateTime, '%Y-%m-%d') between #{params.startTime} and #{params.endTime}
            </if>
            <if test="params.status != null ">
                and g.status = #{params.status}
            </if>
            <if test="params.userIds != null and params.userIds.size() > 0">
                and g.userId in
                <foreach collection="params.userIds" item="userId" open="(" separator="," close=")" >
                    #{userId}
                </foreach>
            </if>
        </where>
    </select>

    <select id="queryDetail" resultType="jnpf.model.personnels.vo.rewardspunishments.FtbEmployeeRewardRecordsVO">
        SELECT
        *
        FROM
        (
        SELECT
        a.F_Id as id,
        1 AS type,
        a.F_Day as day,
        b.F_UserId AS userId,
        c.F_SystemWokerId AS userCustomerId,
        c.F_Name AS userName,
        a.F_TypeName AS rewardTypeName,
        a.f_flowid AS taskId,
        a.F_Amount AS rewardMoney,
        a.F_RewardReason AS rewardReason,
        a.F_CreatorTime AS operateTime,
        a.F_CreatorUserId AS operator,
        IFNULL(a.F_OrgName ,b.F_OrgName) as orgName,
        IFNULL(a.F_PostName ,b.F_PostName) as jobName,
        a.F_Status AS `status`
        FROM
        ftb_punishments_approval a
        LEFT JOIN ftb_punishments_user b ON a.F_Id = b.F_PunishmentsId
        JOIN ftb_personnels_staff_roster c ON ( a.F_UserId = c.F_UserId OR b.F_UserId = c.F_UserId ) UNION
        SELECT
        a.F_Id as id,
        0 AS type,
        a.F_Day as day,
        b.F_UserId AS userId,
        c.F_SystemWokerId AS userCustomerId,
        c.F_Name AS userName,
        a.F_TypeName AS rewardTypeName,
        a.f_flowid AS taskId,
        a.F_Amount AS rewardMoney,
        a.F_RewardReason AS rewardReason,
        a.F_CreatorTime AS operateTime,
        a.F_CreatorUserId AS operator,
        IFNULL(a.F_OrgName ,b.F_OrgName) as orgName,
        IFNULL(a.F_PostName ,b.F_PostName) as jobName,
        a.F_Status AS `status`
        FROM
        ftb_reward_approval a
        LEFT JOIN ftb_reward_user b ON a.F_Id = b.F_ApprovalId
        JOIN ftb_personnels_staff_roster c ON ( a.F_UserId = c.F_UserId OR b.F_UserId = c.F_UserId )
        ) AS g
        where g.id = #{id}
    </select>
</mapper>
