<?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.qy.worksheetsystem.model.mapper.CTWORAttendanceTypeMapper">
    <select id="selectIDName" resultType="java.util.Map">
    SELECT
    FID AS id,
	FName_l2 AS name
    FROM
	CT_WOR_AttendanceType order by fnumber;
   </select>
    <select id="getAttendanceType" resultType="java.util.Map" parameterType="String">

        SELECT
            aType.FID AS id,
            (aType.FName_l2 +' '+ isnull(CONVERT(varchar,CAST(t.remainLimit as decimal(20,2)))+'h','')) AS name,
            isnull(CAST(t.remainLimit as decimal(20,2)),0) remainLimit
        FROM
            CT_WOR_AttendanceType aType
                LEFT JOIN (
                SELECT SUM
                           ( isnull( t.FRemainLimit, 0 )  ) AS remainLimit,
                       h.FHolidayTypeID,
                       p.fnumber
                FROM
                    T_HR_ATS_HolidayLimit t
                        INNER JOIN T_HR_ATS_HolidayPolicy h ON t.FHOLIDAYPOLICYID = h.FID
                        INNER JOIN T_HR_ATS_HolidayType y ON y.FID = h.FHolidayTypeID
                        INNER JOIN T_BD_PERSON p ON p.FID = t.FProposerID
                WHERE
                    p.fnumber= #{number}
                  AND t.FHolidayPolicyID= #{HolidayPolicy}
                  AND t.FDelayDate >=getDate()
                GROUP BY
                    h.FHolidayTypeID,
                    p.fnumber
            ) t ON t.FHolidayTypeID= aType.CFAttendanceID
        ORDER BY
            aType.fnumber
    </select>

    <select id="getAttendanceTypeV2" resultType="java.util.Map" parameterType="String">
    SELECT
    aType.FID AS id,
    (
    aType.FName_l2 + ' ' + isnull( CONVERT ( VARCHAR, CAST ( (t.remainLimit-isnull(t2.workinghours,0)) AS DECIMAL ( 20, 2 )))+'h', '') ) AS name,
    isnull( CAST ((t.remainLimit-isnull(t2.workinghours,0)) AS DECIMAL ( 20, 2 )), 0 ) remainLimit
    ,t2.workinghours
    FROM
    CT_WOR_AttendanceType aType
    LEFT JOIN (
    SELECT SUM
    ( isnull( t.FRemainLimit, 0 )  ) AS remainLimit,
    h.FHolidayTypeID,
    p.fnumber
    FROM
    T_HR_ATS_HolidayLimit t
    INNER JOIN T_HR_ATS_HolidayPolicy h ON t.FHOLIDAYPOLICYID = h.FID
    INNER JOIN T_HR_ATS_HolidayType y ON y.FID = h.FHolidayTypeID
    INNER JOIN T_BD_PERSON p ON p.FID = t.FProposerID
    WHERE
    p.fnumber= #{number}
    AND t.FHolidayPolicyID = #{HolidayPolicy}
    AND t.FDelayDate >= getDate()
    GROUP BY
    h.FHolidayTypeID,
    p.fnumber
    ) t ON t.FHolidayTypeID= aType.CFAttendanceID
    LEFT JOIN (
    SELECT SUM
    ( workinghours ) workinghours,
    fnumber,CFAttendanceID
    FROM
    (
    SELECT
    item.CFPersonID,
    p.fnumber,
    item.CFReportDate, item.CFAttendanceID,
    SUM (
    CONVERT ( INT, item.cfworkinghours )) workinghours
    FROM
    CT_WOR_JobBookingEntry item
    LEFT JOIN t_bd_person p ON p.fid= item.CFPersonID
    left join (
        select attType.fid,
               t.FProposerID
        from CT_WOR_AttendanceType attType
                 INNER JOIN T_HR_ATS_HolidayPolicy h ON h.FHolidayTypeID = attType.CFAttendanceID
                 INNER join (
            select FHolidayPolicyID,
                   FProposerID,
                   count(*) lenNum
            from T_HR_ATS_HolidayLimit
            where FDelayDate >= getDate() and FProposerID= #{number}
            group by FHolidayPolicyID,
                     FProposerID
        ) t on t.FHolidayPolicyID = h.fid
    ) t2 on t2.fid = item.CFAttendanceID
        and t2.FProposerID = p.fid
    WHERE
    p.fnumber= #{number}
    and t2.fid is not null
    AND item.CFReportDate &lt;= #{reportDate}
    AND (item.CFStatus = #{state} or (item.CFStatus = '3' and item.FSourceBillID is null))
    AND item.
    GROUP BY item.CFAttendanceID,
    item.CFReportDate,
    item.CFPersonID,
    p.fnumber
    ) t
    GROUP BY	t.CFAttendanceID,
    t.CFPersonID,
    t.fnumber
    ) t2 ON t2.fnumber = #{number} and t2.CFAttendanceID=aType.fid
    ORDER BY
    aType.fnumber
    </select>
    <select id="getHolidayLimit" resultType="java.util.Map" parameterType="String">
        SELECT SUM
                   ( isnull( t.FRemainLimit, 0 )  ) AS remainLimit,
               h.FHolidayTypeID,
               p.fnumber number
        FROM
            T_HR_ATS_HolidayLimit t
                INNER JOIN T_HR_ATS_HolidayPolicy h ON t.FHOLIDAYPOLICYID = h.FID
                INNER JOIN T_HR_ATS_HolidayType y ON y.FID = h.FHolidayTypeID
                INNER JOIN T_BD_PERSON p ON p.FID = t.FProposerID
        WHERE
            p.fnumber= #{number}
          AND t.FHolidayPolicyID= #{HolidayPolicy}
          AND t.FDelayDate >=getDate()
        GROUP BY
            h.FHolidayTypeID,
            p.fnumber
    </select>
</mapper>