<?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.ruoyi.oa.mapper.OaSalaryMapper">

    <resultMap type="com.ruoyi.oa.domain.vo.OaSalaryVo" id="OaSalaryResult">
        <result property="salaryId" column="salary_id"/>
        <result property="userId" column="user_id"/>
        <result property="realSalary" column="real_salary"/>
        <result property="payTime" column="pay_time"/>
        <result property="baseSalary" column="base_salary"/>
        <result property="createTime" column="create_time"/>
        <result property="createBy" column="create_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="delFlag" column="del_flag"/>
        <result property="remark" column="remark"/>
        <result property="nickName" column="nick_name"/>
        <result property="punishSalary" column="punish_salary"/>
        <result property="insure" column="insure"/>
    </resultMap>


    <delete id="delByPayTime" parameterType="Date">
            DELETE FROM oa_salary
            WHERE YEAR(pay_time) = YEAR(#{payTime})
              AND MONTH(pay_time) = MONTH(#{payTime})
    </delete>
    <delete id="delItemByPayYearMonth" parameterType="Date">
        DELETE FROM oa_salary_item
        WHERE YEAR(sign_time) = YEAR(#{payTime})
          AND MONTH(sign_time) = MONTH(#{payTime})
    </delete>


    <select id="selectStaffVoPage" resultMap="OaSalaryResult">
        SELECT
            os.salary_id,
            os.user_id,
            os.pay_time,
            su.nick_name,
            os.base_salary,
            os.insure,
            COALESCE(
                    SUM(
                            CASE
                                WHEN osi.flag = 1 THEN osi.price
                                WHEN osi.flag = 0 THEN -osi.price
                                ELSE 0
                                END
                    ),
                    0
            ) AS punish_salary
        FROM oa_salary os
                 LEFT JOIN oa_salary_item osi
                           ON osi.salary_id = os.salary_id
                               AND YEAR(osi.sign_time) = YEAR(#{payTime})
                               AND MONTH(osi.sign_time) = MONTH(#{payTime})
                               AND osi.del_flag = '0'
                 LEFT JOIN sys_user su
                           ON os.user_id = su.user_id
            ${ew.customSqlSegment}
            AND YEAR(os.pay_time) = YEAR(#{payTime})
            AND MONTH(os.pay_time) = MONTH(#{payTime})
        GROUP BY
            os.salary_id,
            os.user_id,
            os.pay_time,
            su.nick_name,
            os.base_salary

    </select>


    <select id="selectVoByUserId" resultType="com.ruoyi.oa.domain.OaSalary">
        select * from oa_salary
                 where user_id = #{userId}
        and YEAR(pay_time) = YEAR(#{yearMonth})
        and MONTH(pay_time) = MONTH(#{yearMonth})
    </select>

    <select id="selectVoAndItemVoById" resultType="com.ruoyi.oa.domain.vo.OaSalaryVo">
        SELECT
            os.salary_id,
            os.user_id,
            os.pay_time,
            os.base_salary,
            (
              DAY(LAST_DAY(#{payTime}))
                    + COALESCE(SUM(
                                       CASE
                                           WHEN osi.flag = 1 THEN osi.price
                                           WHEN osi.flag = 0 THEN -osi.price
                                           ELSE 0
                                           END), 0)
                ) AS real_salary,
            osi.salary_item_id,
            osi.reason,
            osi.price,
            osi.sign_time,
            os.insure,
            su.nick_name
        FROM oa_salary os
                 LEFT JOIN oa_salary_item osi
                           ON osi.salary_id = os.salary_id
                               AND YEAR(osi.sign_time) = YEAR(#{payTime})
                               AND MONTH(osi.sign_time) = MONTH(#{payTime})
                 LEFT JOIN sys_user su ON os.user_id = su.user_id
           where os.salary_id = #{salaryId}
            and
                YEAR(os.pay_time) = YEAR(#{payTime})
            AND MONTH(os.pay_time) = MONTH(#{payTime})
        GROUP BY
            os.salary_id,
            os.user_id,
            os.pay_time,
            os.base_salary,
            osi.salary_item_id,
            osi.type,
            osi.reason,
            osi.price,
            osi.sign_time
    </select>


</mapper>
