<?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.AttendanceConfirmMapper">
    <sql id="staticsWhere">
        <where>
            <if test=" year!= null">
                AND fac.F_Year = #{year}
            </if>
            <if test=" month!= null">
                AND fac.F_Month = #{month}
            </if>
            <if test="userIds!= null and userIds.size()>0">
                and fac.F_UserId in
                <foreach collection="userIds" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="type!= null">
                <choose>
                    <when test="type == 1">
                        AND fac.F_Status = 2
                    </when>
                    <when test="type == 2">
                        AND fac.F_Status in(0,1)
                    </when>
                    <when test="type == 3">
                        AND fac.F_Status = 0
                    </when>
                    <when test="type == 4">
                        AND fac.F_Status = 1
                    </when>
                    <otherwise>
                        and 1=1
                    </otherwise>
                </choose>
            </if>
        </where>
    </sql>
    <select id="getStatistics" resultType="jnpf.model.attendance.vo.attendance.ConfirmStatisticsVo">
        SELECT
        COUNT(fac.F_Id) total,
        COALESCE(SUM(IF(fac.F_Status = 2, 1, 0)),0) confirmed,
        COALESCE(SUM(IF(fac.F_Status = 0 or fac.F_Status = 1, 1, 0)),0) toDoConfirm,
        COALESCE(SUM(IF(fac.F_Status = 0 , 1, 0)),0) noViewed,
        COALESCE(SUM(IF(fac.F_Status = 1, 1, 0)),0) yesViewed
        FROM ftb_attendance_confirm fac
        <include refid="staticsWhere"/>
    </select>
    <select id="getPageList" resultType="jnpf.model.attendance.vo.attendance.ConfirmListQueryVo">
        SELECT
        fac.F_Id id,
        fac.F_UserId userId,
        fac.F_Year `year`,
        fac.F_Month `month`,
        fac.F_Status `status`
        FROM ftb_attendance_confirm fac
        left JOIN (
            SELECT
            fagu.F_UserId,
            fagu.F_GroupId
            FROM
            ftb_attendance_group_user fagu where fagu.F_DeleteMark = 0 and fagu.F_Type = 1
            GROUP BY fagu.F_UserId
        ) fag ON fac.F_UserId = fag.F_UserId
        <include refid="staticsWhere"/>
        order by fag.F_GroupId desc,fac.F_Status
    </select>
</mapper>