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

    <!-- 查询用户生效中的规则 -->
    <select id="selectUserEffectList" resultType="jnpf.entity.attendance.AttendanceRuleScope">
        SELECT t.F_Id id, t.F_BizType bizType, t.F_RuleId ruleId, t.F_ScopeType scopeType, t.F_ScopeValue scopeValue, t.F_Priority priority
        FROM (
          SELECT *,
          DENSE_RANK() OVER (ORDER BY F_ScopeType ASC) AS rn
          FROM ftb_attendance_rule_scope
          WHERE (F_ScopeType = 3
          OR (F_ScopeType = 2 AND F_ScopeValue = #{organizeId})
          OR (F_ScopeType = 1 AND F_ScopeValue = #{userId}))
          <if test="null != bizType and '' != bizType">
              AND F_BizType = #{bizType}
          </if>
        ) t
        WHERE t.rn = 1
    </select>
    <!-- 查询用户生效中的规则[批量] -->
    <select id="selectUserEffectListBatch" resultType="jnpf.entity.attendance.AttendanceRuleScope">
        SELECT * FROM (
            SELECT
            t.F_Id         AS id,
            t.F_BizType    AS bizType,
            t.F_RuleId     AS ruleId,
            t.F_ScopeType  AS scopeType,
            t.F_ScopeValue AS scopeValue,
            t.F_Priority   AS priority,
            t.F_Expand AS expand,
            uo.userId
            <if test="1 == priority">
                , ROW_NUMBER() OVER (PARTITION BY uo.userId,t.F_Expand ORDER BY t.F_ScopeType ASC) AS rn
            </if>
            FROM ftb_attendance_rule_scope t
            INNER JOIN (
            <foreach collection="list" item="item" separator="UNION ALL">
                SELECT
                #{item.userId} AS userId,
                #{item.organizeId} AS organizeId
            </foreach>
            ) AS uo
            ON (t.F_ScopeType = 3)
            OR (t.F_ScopeType = 2 AND t.F_ScopeValue = uo.organizeId)
            OR (t.F_ScopeType = 1 AND t.F_ScopeValue = uo.userId)
        <where>
            <if test="null != bizType and '' != bizType">
                t.F_BizType = #{bizType}
            </if>
            <if test="null != leaveTypeIds and leaveTypeIds.size>0">
               and t.F_Expand in
                <foreach collection="leaveTypeIds" item="item" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
        </where>
        ) tt
        <if test="1 == priority">
          WHERE tt.rn = 1
        </if>
    </select>
    <select id="selectOrgEffectListBatch" resultType="jnpf.entity.attendance.AttendanceRuleScope">
            SELECT
            t.F_Id         AS id,
            t.F_BizType    AS bizType,
            t.F_RuleId     AS ruleId,
            t.F_ScopeType  AS scopeType,
            t.F_ScopeValue AS scopeValue
            FROM ftb_attendance_rule_scope t
            WHERE   t.F_BizType = #{bizType} AND t.F_ScopeType = 2 AND t.F_ScopeValue = #{organizeId}

    </select>
</mapper>