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

    <!-- 查询加班规则列表 -->
    <select id="selectOvertimeRulePage" resultType="jnpf.model.attendance.vo.attendance.OvertimeRulePageVo">
        SELECT
        r.F_Id AS id, r.F_RuleName AS ruleName, r.F_Enabled AS enabled,
        COALESCE(d.workRule, 0) AS workRule,
        COALESCE(d.restRule, 0) AS restRule,
        COALESCE(d.holidayRule, 0) AS holidayRule,
        CASE
        WHEN s.allCount > 0 THEN '全部'
        ELSE CONCAT(
        CASE
        WHEN COALESCE(s.orgCount, 0) > 0
        THEN CONCAT('【', s.orgCount, '个组织】')
        ELSE ''
        END,
        CASE
        WHEN COALESCE(s.orgCount, 0) > 0 AND COALESCE(s.userCount, 0) > 0
        THEN '、'
        ELSE ''
        END,
        CASE
        WHEN COALESCE(s.userCount, 0) > 0
        THEN CONCAT('【', s.userCount, '位成员】')
        ELSE ''
        END
        )
        END AS scope
        FROM ftb_attendance_overtime_rule r
        LEFT JOIN (
        SELECT
        F_RuleId,
        MAX(CASE WHEN F_OvertimeType = 1 AND F_Enabled = 1 THEN F_CalcMethod ELSE 0 END) AS workRule,
        MAX(CASE WHEN F_OvertimeType = 2 AND F_Enabled = 1 THEN F_CalcMethod ELSE 0 END) AS restRule,
        MAX(CASE WHEN F_OvertimeType = 3 AND F_Enabled = 1 THEN F_CalcMethod ELSE 0 END) AS holidayRule
        FROM ftb_attendance_overtime_rule_detail
        GROUP BY F_RuleId
        ) d ON r.F_Id = d.F_RuleId
        LEFT JOIN (
        SELECT
        F_RuleId,
        SUM(CASE WHEN F_ScopeType = 3 THEN 1 ELSE 0 END) AS allCount,
        SUM(CASE WHEN F_ScopeType = 2 THEN 1 ELSE 0 END) AS orgCount,
        SUM(CASE WHEN F_ScopeType = 1 THEN 1 ELSE 0 END) AS userCount
        FROM ftb_attendance_rule_scope
        GROUP BY F_RuleId
        ) s ON r.F_Id = s.F_RuleId
        WHERE r.F_DeleteMark = 0
        <if test="null != ruleName and '' != ruleName">
          AND INSTR(r.F_RuleName, #{ruleName}) > 0
        </if>
    </select>
</mapper>