<?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.EmployeeReportingMapper">
    <select id="selectKey" resultType="String">
        select dbo.newbosid('BF63FCE1') as id
    </select>
    <delete id="deleteSheet" parameterType="String" >
        delete from CT_WOR_JobBookingEntry where FParentID=#{id};
        delete from CT_WOR_JobBooking where fid=#{id};
    </delete>
    <insert id="insertBill" parameterType="java.util.Map" >
        insert
        into CT_WOR_JobBooking(fid, FCreatorID, FCreateTime,
                               FLastUpdateUserID, FLastUpdateTime, FControlUnitID,
                               FDescription, CFPersonID, CFOrgID,
                               CFYearAndMonth, CFAttendanceID, CFWorkingHours,
                               CFStatus, CFTitle, CFBeginDate,
                               CFEndDate, CFPersonNumber, CFNameAndNumber,CFAuditorAndNumber
                             )
        values (#{billId}, #{FCreatorID}, GETDATE(),
                #{FLastUpdateUserID}, GETDATE(), '',
                #{FDescription}, #{CFPersonID}, #{CFOrgID},
                #{CFYearAndMonth}, #{CFAttendanceID}, #{CFWorkingHours},
                #{CFStatus}, #{CFTitle}, #{CFBeginDate},
                #{CFEndDate}, #{CFPersonNumber}, #{CFNameAndNumber}, #{CFAuditorAndNumber}
              );
    </insert>
    <insert id="insertEntry" parameterType="java.util.List">
        INSERT
        into CT_WOR_JobBookingEntry(FSeq,FID,FParentID,
                                    CFPersonID,CFOrgID,CFReportDate,
                                    CFYearAndMonth,CFAttendanceID,
                                    CFWorkingHours,CFStatus,
                                    CFCalendarType,CFPersonNumber,
                                    CFNameAndNumber,CFYsName,
                                    CFYsNumber,CFPersonDeptID,CFApproverID,
                                    CFAuditorAndNumber
        )
        values
        <foreach collection="list" item="item" index="index" separator=",">
            (
               #{item.Fseq},dbo.newbosid('EFAC0011'),#{item.FParentID},
               #{item.CFPersonID},#{item.CFOrgID},#{item.CFReportDate},
               #{item.CFYearAndMonth},#{item.CFAttendanceID},
               #{item.CFWorkingHours},#{item.CFStatus},
               #{item.CFCalendarType},#{item.CFPersonNumber},
               #{item.CFNameAndNumber},#{item.CFYsName},
               #{item.CFYsNumber},#{item.CFPersonDeptID},#{item.CFApproverID}
               ,#{item.CFAuditorAndNumber}
              )
        </foreach>
    </insert>
    <select id="getShrPersonInfo" resultType="java.util.Map" parameterType="String">
        select p.fid personId,p.fnumber personNumber,p.fname_l2 personName,
               org.fname_l2 orgName,org.fnumber orgNumber,org.fid orgId,post.fid postId,
               post.fname_l2 postName,post.fnumber postNumber,u.fid userid,
               org.FEffectDate,org.FLEFFDT,isnull(pp2.FEnterDate,pp.FEnterDate) enterDate,  CONVERT(varchar(10) ,isnull(pp2.FEnterDate,''),120) hiredate,
               0 isAuditor,
               btype.fname_l2 btypeName,btype.fid btypeFid
        from t_bd_person p
        left join t_pm_user u on u.fpersonid=p.fid
        left join T_HR_PERSONPOSITION pp on pp.fpersonid=p.fid
        left join T_HR_EMPLABORRELATION pp2 on pp2.FPersonID=p.fid
        left join T_HR_BDEmployeeType btype on btype.fid=pp2.FLaborRelationStateID
        left join t_org_admin org on org.fid=pp.FPERSONDEP
        left join t_org_position post on post.fid=pp.FPRIMARYPOSITIONID


        where p.fnumber=#{number}
    </select>
    <select id="getShrAdminInfo" resultType="java.util.Map" parameterType="String">
        select
               org.fname_l2 orgName,org.fnumber orgNumber,org.fid orgId,
               pt.fname_l2 ptName,pt.fnumber ptNumber,pt.fid ptId
        from t_org_admin org
                 left join t_org_admin pt on pt.fid=org.FParentID
              where org.fid=#{fid}
    </select>

    <select id="getPersonWorkDetail" resultType="java.util.Map" parameterType="String">
        select CFStatus,count(fid) num
        from
            CT_WOR_JobBookingEntry
        where CFPersonID=#{personId}
        and CFReportDate &gt;=#{beginDate}
        and CFReportDate &lt;=#{endDate}
        group by CFStatus
    </select>
    <select id="getPPWorkDetail" resultType="java.util.Map" parameterType="String">
        select CONVERT(varchar(10) ,CFReportDate, 120 ) CFReportDate,cfcalendarType,
               CFYsName,CFNameAndNumber,CFYearAndMonth,sum(convert(int,CFWorkingHours)) workingHours
        from
            CT_WOR_JobBookingEntry
        where CFPersonID=#{personId}
          and CFReportDate &gt;=#{beginDate}
          and CFReportDate &lt;=#{endDate}
          and CFOrgID =#{orgId}
        group by CFReportDate,CFPersonID,CFYearAndMonth,CFNameAndNumber,CFYsName,cfcalendarType
        order by CFReportDate asc
    </select>
    <select id="getTimeJobBooking" resultMap="JobBooking" parameterType="String">
        select child.fid projectId,(isnull(parent.fname_l2,'') + isnull(child.fname_l2,'')) projectName,
               child.fnumber childNumber,count(pp.FPersonID) projectPersonNum,
               '' month,'' hourTotal,
               p.fname_l2 empName,
               p.fnumber empNumber,
               '50%' monthRate,p.fid pId
        from T_ORG_Admin adcount
                 left join T_ORG_Admin child on (charindex((adcount.flongnumber+'!'),child.flongnumber) = 1 or adcount.flongnumber = child.flongnumber)
                 left join T_ORG_Admin parent on child.fid=parent.FParentID
                 left join T_HR_EmpOrgRelation pp on  pp.FADMINORGID = child.fid and pp.FLEFFDT='2199-12-31'
                 left join t_bd_person p on p.fid=pp.FPersonID
                 left join CT_WK_ManagerInfo mg on mg.CFPersonID=p.fid
        where
            adcount.fid=#{orgId} and child.fid=#{orgId}
          and mg.CFManagerID=#{personId}
        group by child.fid,child.fnumber,child.fname_l2,p.fname_l2,p.fnumber,p.fid,parent.fname_l2

    </select>
    <select id="getTimeJobBookingEntry" resultMap="JobBookingEntry" parameterType="String">
        select jbk.FID timeSheetId,jbk.CFTitle title,
               jbk.CFAttendanceID jbkAttendanceType,aType.fname_l2 jbkAttendanceTypeName,
               jbk.cfworkinghours hourTotal,jbke.FID detailId,
               jbke.cfworkinghours workingHours,jbke.cfcalendarType workCalendarType,
               jbke.CFAttendanceID jbkeAttendanceType,jbk.CFStatus jbkStatus,
               CONVERT(varchar(10) ,jbke.cfreportDate, 120 ) reportDate,
               CONVERT(varchar(10) ,jbk.CFBeginDate, 120 ) beginDate,
               CONVERT(varchar(10) ,jbk.CFendDate, 120 ) endDate,
               aType2.fname_l2 jbkeAttendanceTypeName,jbke.CFStatus jbkeStatus
        from CT_WOR_JobBooking jbk
                 left join CT_WOR_JobBookingEntry jbke on jbk.FID=jbke.FParentID
                 left join t_bd_person p on p.fid=jbke.CFPersonID
                 left join CT_WOR_AttendanceType aType on aType.fid=jbk.CFAttendanceID
                 left join CT_WOR_AttendanceType aType2 on aType2.fid=jbke.CFAttendanceID
        where
             jbk.CFPersonID=#{id} and jbk.cfyearAndMonth=#{month} and jbk.CFOrgID=#{OrgID}
        <if test='state != null and state != "" and state == "0"'>
            and jbk.CFStatus in('2')
        </if>
        <if test='state != null and state != "" and state == "1"'>
            and jbk.CFStatus in('3')
        </if>
            order by jbk.CFBeginDate asc
    </select>
    <resultMap id="JobBooking" type="com.qy.worksheetsystem.entity.Poject" >
        <id column="projectId" property="projectId"/>
        <!-- property对应实体类的属性名称，column为数据库字段名 -->
        <result column="projectName" property="projectName" />
        <result column="projectPersonNum" property="projectPersonNum" />
        <result column="month" property="month" />
        <result column="hourTotal" property="hourTotal" />
        <collection property="detail" ofType="com.qy.worksheetsystem.entity.JobBook">
            <id column="pId" property="pId"/>
            <result column="empName" property="empName"/>
            <result column="empNumber" property="empNumber"/>
            <result column="monthRate" property="monthRate"/>
        </collection>
    </resultMap>
    <resultMap id="JobBookingEntry" type="com.qy.worksheetsystem.entity.JobBooking" >
        <id column="timeSheetId" property="timeSheetId"/>
        <result column="title" property="title"/>
        <result column="jbkAttendanceType" property="attendanceType"/>
        <result column="jbkAttendanceTypeName" property="attendanceTypeName"/>
        <result column="jbkStatus" property="status"/>
        <result column="beginDate" property="beginDate"/>
        <result column="endDate" property="endDate"/>
        <result column="hourTotal" property="hourTotal"/>
        <collection property="details" ofType="com.qy.worksheetsystem.entity.JobBookingEntry">
            <id column="detailId" property="detailId"/>
            <result column="workingHours" property="workingHours"/>
            <result column="workCalendarType" property="workCalendarType"/>
            <result column="jbkeAttendanceType" property="attendanceType"/>
            <result column="jbkeAttendanceTypeName" property="attendanceTypeName"/>
            <result column="reportDate" property="reportDate"/>
            <result column="jbkeStatus" property="status"/>
        </collection>
    </resultMap>
    <select id="getMonthWorkSheet" resultType="java.util.Map" parameterType="String">
        select jbk.FID timeSheetId,jbk.CFTitle title,
               org.fid projectId,(parent.fname_l2+org.fname_l2) projectName,
               jbk.CFAttendanceID attendanceType,
               aType.fname_l2 attendanceTypeName,
               jbk.cfworkinghours workingHours,jbk.CFStatus status,'5*8h(工作日)' hourTotal
        from CT_WOR_JobBooking jbk
                 left join t_org_admin org on org.fid=jbk.CFOrgID
                 left join T_ORG_Admin parent on parent.fid=org.FParentID
                 left join CT_WOR_AttendanceType aType on aType.fid=jbk.CFAttendanceID
                 left join t_bd_person p on p.fid=jbk.CFPersonID
        where
            p.fnumber=#{number} and jbk.cfyearAndMonth=#{month}
        <if test='state != null and state != "" and state == "0"'>
            and jbk.CFStatus in('1','4')
        </if>
        <if test='state != null and state != "" and state == "1"'>
            and jbk.CFStatus in('2','3')
        </if>
        order by jbk.CFBeginDate asc
    </select>
    <select id="getWorkSheetById" resultType="java.util.Map" parameterType="String">
        select jbk.FID timeSheetId,jbk.CFTitle title,
        org.fid projectId,(parent.fname_l2+org.fname_l2) projectName,
        jbk.CFAttendanceID attendanceType,
        aType.fname_l2 attendanceTypeName,
        jbk.cfworkinghours workingHours,jbk.CFStatus status,'5*8h(工作日)' hourTotal
        from CT_WOR_JobBooking jbk
        left join t_org_admin org on org.fid=jbk.CFOrgID
        left join T_ORG_Admin parent on org.fid=parent.FParentID
        left join CT_WOR_AttendanceType aType on aType.fid=jbk.CFAttendanceID
        left join t_bd_person p on p.fid=jbk.CFPersonID
        where jbk.fid=#{id}

    </select>
    <select id="getMonthWorkSheetEntry" resultType="java.util.Map" parameterType="String">
        select fid detailId,cfworkingHours workingHours,
               cfCalendarType workCalendarType,CFAttendanceID attendanceType,
               CONVERT(varchar(10) ,cfreportDate, 120 ) reportDate,FParentID parentId
        From CT_WOR_JobBookingEntry
        where FParentID=#{id}
        order by cfreportDate asc
    </select>
    <select id="getMonthWorkSheetEntryV2" resultType="java.util.Map" parameterType="String">
        select  SUM(convert(int,cfworkinghours)) workingHours,
                cfCalendarType workCalendarType,
                CONVERT(varchar(10) ,cfreportDate, 120 ) reportDate
        From CT_WOR_JobBookingEntry
        where FParentID=#{id}
        group by cfreportDate,cfCalendarType
        order by cfreportDate asc
    </select>
    <select id="getHourTotal" resultType="java.util.Map" parameterType="String">
        select parentId,calendarType,COUNT(*) num,workHours,
        (CASE calendarType
        WHEN 0 THEN  convert(varchar,COUNT(*))+'*'+convert(varchar,workHours)+'h'
        WHEN 1 THEN convert(varchar,COUNT(*))+'*'+convert(varchar,workHours)+'h(休息日)'
        ELSE convert(varchar,COUNT(*))+'*'+convert(varchar,workHours)+'h(节假日)'
        end) hourTotal
        from (
        select
        fparentId parentId,cfcalendarType calendarType,
        cfreportDate reportDate,SUM(convert(int,cfworkinghours)) workHours
        from CT_WOR_JobBookingEntry
        where fparentId=#{id}
        <if test='calendarType != null and calendarType != ""'>
            and cfcalendarType=#{calendarType}
        </if>
        and cfcalendarType!=1
        group by cfcalendarType,fparentId,cfreportDate
        ) t
        group by calendarType,parentId,workHours
    </select>
    <select id="getHourTotalAll" resultType="java.util.Map" parameterType="String">
        select parentId,calendarType,COUNT(*) num,workHours,
        (CASE calendarType
        WHEN 0 THEN  convert(varchar,COUNT(*))+'*'+convert(varchar,workHours)+'h'
        WHEN 1 THEN convert(varchar,COUNT(*))+'*'+convert(varchar,workHours)+'h(休息日)'
        ELSE convert(varchar,COUNT(*))+'*'+convert(varchar,workHours)+'h(节假日)'
        end) hourTotal
        from (
        select
        fparentId parentId,cfcalendarType calendarType,
        cfreportDate reportDate,SUM(convert(int,cfworkinghours)) workHours
        from CT_WOR_JobBookingEntry
        where fparentId=#{id}
        group by cfcalendarType,fparentId,cfreportDate
        ) t
        group by calendarType,parentId,workHours
    </select>
    <select id="getHourTotalSum" resultType="java.util.Map">
        select calendarType,COUNT(*) num
        from (
                 select
                     cfcalendarType calendarType,
                     cfreportDate reportDate,SUM(convert(int,cfworkinghours)) workHours
                 from CT_WOR_JobBookingEntry
                 where fparentId in
                     <foreach item="id" index="index" collection="list" open="(" separator="," close=")">
                            #{id}
                     </foreach>
                    <if test='calendarType != null and calendarType != ""'>
                        and cfcalendarType=#{calendarType}
                    </if>
                 group by cfcalendarType,cfreportDate
             ) t
        group by calendarType
    </select>
    <select id="getHourTotalSumV2" resultType="java.util.Map">
        SELECT  count(DISTINCT cfreportDate) as num,cfcalendarType calendarType
        FROM CT_WOR_JobBookingEntry
        where 1=1
        and fparentId in
        <foreach item="id" index="index" collection="list" open="(" separator="," close=")">
            #{id}
        </foreach>
        <if test='calendarType != null and calendarType != ""'>
            and cfcalendarType=#{calendarType}
        </if>
        GROUP BY cfcalendarType
    </select>
    <select id="getOrgAndJobBookId" resultType="java.util.Map">
        select org.fid orgId,jbk.fid jbkId from
            T_ORG_Admin org
            left join CT_WOR_JobBooking jbk on jbk.CFOrgID=org.fid
        where
        jbk.CFYearAndMonth is not null
        and jbk.cfpersonid in
        <foreach item="id" index="index" collection="list" open="("
                 separator="," close=")">
            #{id}
        </foreach>
        <if test='audit != null and audit != "" and audit == "0"'>
            and jbk.CFStatus in('2')
        </if>
        <if test='audit != null and audit != "" and audit == "1"'>
            and jbk.CFStatus in('3')
        </if>
        and jbk.CFYearAndMonth is not null
        <if test='month != null and month != ""'>
            and jbk.CFYearAndMonth = #{month}
        </if>
    </select>
    <select id="getAudited" resultType="java.util.Map">
        select org.FID projectId,(parent.fname_l2+org.fname_l2) projectName,
        jbk.CFYearAndMonth yearAndMonth,jbkl.reportNum,
        isnull(SUM((CASE a.cfcalendarType WHEN 0 THEN isnull(a.dateTotal,0) end)),0) workHours,
        isnull(SUM((CASE a.cfcalendarType WHEN 2 THEN a.dateTotal end)),0) holidayHours
        from
        T_ORG_Admin org
        left join T_ORG_Admin parent on parent.fid=org.FParentID
        left join CT_WOR_JobBooking jbk on jbk.CFOrgID=org.fid
        left join (
        SELECT  count(DISTINCT cfreportDate) as dateTotal,CFPersonID,cfcalendarType,FParentID
        FROM CT_WOR_JobBookingEntry
        where 1=1
        <if test='audit != null and audit != "" and audit == "0"'>
            and CFStatus in('2')
        </if>
        <if test='audit != null and audit != "" and audit == "1"'>
            and CFStatus in('3')
        </if>
        <!--                 and CFPersonID in-->
        <!--                <foreach item="id" index="index" collection="list" open="("-->
        <!--                         separator="," close=")">-->
        <!--                    #{id}-->
        <!--                </foreach>-->
        GROUP BY FParentID,CFPersonID,cfcalendarType
        )  a on a.FParentID = jbk.FID and a.CFPersonID=jbk.CFPersonID
        left join (
        select CFOrgID,COUNT(CFPersonID) reportNum
        from(
        select CFOrgID,CFPersonID from
        CT_WOR_JobBooking
        where CFPersonID in
        <foreach item="id" index="index" collection="list" open="("
                 separator="," close=")">
            #{id}
        </foreach>
        group by CFOrgID,CFPersonID) t
        group by CFOrgID)
        jbkl on jbkl.CFOrgID=org.fid
        where jbk.cfpersonid in
        <foreach item="id" index="index" collection="list" open="("
                 separator="," close=")">
            #{id}
        </foreach>
        <if test='audit != null and audit != "" and audit == "0"'>
            and jbk.CFStatus in('2')
        </if>
        <if test='audit != null and audit != "" and audit == "1"'>
            and jbk.CFStatus in('3')
        </if>
        and jbk.CFYearAndMonth is not null
        <if test='month != null and month != ""'>
            and jbk.CFYearAndMonth = #{month}
        </if>
        group by org.FID,org.fname_l2,parent.fname_l2,jbk.CFYearAndMonth,jbkl.reportNum
    </select>
    <select id="getAuditedV9" resultType="java.util.Map">
        select org.FID projectId,(parent.fname_l2+org.fname_l2) projectName,
               jbk.CFYearAndMonth yearAndMonth,jbkl.reportNum,
        isnull(SUM((CASE a.cfcalendarType WHEN 0 THEN isnull(a.dateTotal,0) end)),0) workHours,
        isnull(SUM((CASE a.cfcalendarType WHEN 2 THEN a.dateTotal end)),0) holidayHours
        from
            T_ORG_Admin org
            left join T_ORG_Admin parent on parent.fid=org.FParentID
                left join CT_WOR_JobBooking jbk on jbk.CFOrgID=org.fid
                left join (
                SELECT  count(DISTINCT cfreportDate) as dateTotal,CFPersonID,cfcalendarType,FParentID
                FROM CT_WOR_JobBookingEntry
                where 1=1
                <if test='audit != null and audit != "" and audit == "0"'>
                    and CFStatus in('2')
                </if>
                <if test='audit != null and audit != "" and audit == "1"'>
                    and CFStatus in('3')
                </if>
<!--                and FParentID in-->
<!--                <foreach item="id" index="index" collection="list" open="("-->
<!--                         separator="," close=")">-->
<!--                    #{id}-->
<!--                </foreach>-->
                GROUP BY FParentID,CFPersonID,cfcalendarType
                )  a on a.FParentID = jbk.FID and a.CFPersonID=jbk.CFPersonID
                left join (
                    select CFOrgID,COUNT(CFPersonID) reportNum
                    from(
                         select CFOrgID,CFPersonID from
                         CT_WOR_JobBooking
                        where CFPersonID in
                        <foreach item="id" index="index" collection="pidList" open="("
                                 separator="," close=")">
                            #{id}
                        </foreach>
                         group by CFOrgID,CFPersonID) t
                     group by CFOrgID)
                jbkl on jbkl.CFOrgID=org.fid

        where jbk.fid in
        <foreach item="id" index="index" collection="list" open="("
                 separator="," close=")">
            #{id}
        </foreach>
        group by org.FID,org.fname_l2,parent.fname_l2,jbk.CFYearAndMonth,jbkl.reportNum
    </select>

    <select id="getReportDateWorkHoursByPerson" resultType="java.util.Map" parameterType="String">
        select jbk.*
        from CT_WOR_JobBookingEntry jbk
        where
            jbk.CFPersonID=#{personId} and jbk.CFReportDate = #{reportDate}
        order by jbk.FSeq
    </select>
    <select id="getJobDayByPerson" resultType="java.util.Map" parameterType="String">
        select SUM(workinghours) workinghours, fnumber number,count(*) num from (
        select item.CFPersonID,p.fnumber,item.CFReportDate,SUM(convert(int,item.cfworkinghours)) workinghours
        from CT_WOR_JobBookingEntry item
        left join t_bd_person p on p.fid=item.CFPersonID
        where
        p.fnumber=#{number}
        and item.CFReportDate &lt;=#{endDate}
        and item.CFReportDate &gt;=#{stratDate}
        <if test='attID != null and attID != ""'>
            and item.CFAttendanceID=#{attID}
        </if>
        <if test='orgid != null and orgid != ""'>
            and item.cforgid =#{orgid}
        </if>
        <if test='status != null and status != "" '>
            and item.CFStatus =#{status}
            and item.FSourceBillID is null
        </if>
        group by item.CFReportDate,item.CFPersonID,p.fnumber
        ) t
        group by t.CFPersonID,t.fnumber
    </select>
    <select id="getEmpAuditedV2Entry" resultMap="empAuditedEntry">
        select jbk.fid,jbkEny.fid detailId,
               jbkEny.CFCalendarType workCalendarType,jbkEny.CFWorkingHours workingHoursEny,
               jbkEny.CFAttendanceID jbkeAttendanceType,aTypeEny.fname_l2 jbkeAttendanceTypeName,
               CONVERT(varchar(10) ,jbkEny.cfreportDate, 120 ) reportDate
        from CT_WOR_JobBooking jbk
        left join CT_WOR_JobBookingEntry jbkEny on jbkEny.FParentID=jbk.fid
        left join CT_WOR_AttendanceType aTypeEny on aTypeEny.fid=jbkEny.CFAttendanceID
        where jbk.fid=#{timeSheetId}
        order by jbkEny.cfreportDate
    </select>

    <select id="getEmpAuditedV2" resultMap="empAuditedV2">
        select jbk.fid timeSheetId,jbk.CFTitle title,jbk.CFWorkingHours workingHours,
        jbk.cforgid projectId,org.fname_l2 projectName,jbk.CFstatus status,
        jbk.CFAttendanceID attendanceType,aType.fname_l2 attendanceTypeName,
        CONVERT(varchar(10) ,jbk.CFBeginDate, 120 ) beginDate,
        CONVERT(varchar(10) ,jbk.CFendDate, 120 ) endDate
        from CT_WOR_JobBooking jbk
        left join t_org_admin org on org.fid=jbk.cforgid
        left join CT_WOR_AttendanceType aType on aType.fid=jbk.CFAttendanceID

        where jbk.cfpersonid =  #{personId}

        <if test='audit != null and audit != "" and audit == "0"'>
            and jbk.CFStatus in('2')
        </if>
        <if test='audit != null and audit != "" and audit == "1"'>
            and jbk.CFStatus in('3')
        </if>

        <if test='month != null and month != ""'>
            and jbk.CFYearAndMonth = #{month}
        </if>
        order by jbk.CFBeginDate
    </select>

    <resultMap id="empAuditedEntry" type="com.qy.worksheetsystem.entity.JobBookingEntry">
        <id column="detailId" property="detailId"/>
        <result column="workingHoursEny" property="workingHours"/>
        <result column="workCalendarType" property="workCalendarType"/>
        <result column="jbkeAttendanceType" property="attendanceType"/>
        <result column="jbkeAttendanceTypeName" property="attendanceTypeName"/>
        <result column="reportDate" property="reportDate"/>
    </resultMap>
    <resultMap id="empAuditedV2" type="com.qy.worksheetsystem.entity.JobBooking" >
        <id column="timeSheetId" property="timeSheetId"/>
        <result column="title" property="title"/>
        <result column="projectId" property="projectId"/>
        <result column="projectName" property="projectName"/>
        <result column="attendanceType" property="attendanceType"/>
        <result column="attendanceTypeName" property="attendanceTypeName"/>
        <result column="status" property="status"/>
        <result column="beginDate" property="beginDate"/>
        <result column="endDate" property="endDate"/>
        <result column="workingHours" property="workingHours"/>
        <collection property="details" ofType="com.qy.worksheetsystem.entity.JobBookingEntry"
        javaType="java.util.List" select="getEmpAuditedV2Entry" column="timeSheetId"
        >
        </collection>
    </resultMap>

    <select id="getEmpAudited"  resultMap="empAudited"  >
    select jbk.fid timeSheetId,jbk.CFTitle title,jbk.CFWorkingHours workingHours,
           jbk.cforgid projectId,org.fname_l2 projectName,jbk.CFstatus status,
           jbk.CFAttendanceID attendanceType,aType.fname_l2 attendanceTypeName,jbkEny.fid detailId,
           jbkEny.CFCalendarType workCalendarType,jbkEny.CFWorkingHours workingHoursEny,
           jbkEny.CFAttendanceID jbkeAttendanceType,aTypeEny.fname_l2 jbkeAttendanceTypeName,
        CONVERT(varchar(10) ,jbkEny.cfreportDate, 120 ) reportDate,
        CONVERT(varchar(10) ,jbk.CFBeginDate, 120 ) beginDate,
        CONVERT(varchar(10) ,jbk.CFendDate, 120 ) endDate
    from CT_WOR_JobBooking jbk
    left join t_org_admin org on org.fid=jbk.cforgid
    left join CT_WOR_AttendanceType aType on aType.fid=jbk.CFAttendanceID
    left join CT_WOR_JobBookingEntry jbkEny on jbkEny.FParentID=jbk.fid
    left join CT_WOR_AttendanceType aTypeEny on aTypeEny.fid=jbkEny.CFAttendanceID
        where jbk.cfpersonid =  #{personId}

        <if test='audit != null and audit != "" and audit == "0"'>
            and jbk.CFStatus in('2')
        </if>
        <if test='audit != null and audit != "" and audit == "1"'>
            and jbk.CFStatus in('3')
        </if>

        <if test='month != null and month != ""'>
            and jbk.CFYearAndMonth = #{month}
        </if>
    </select>

    <resultMap id="empAudited" type="com.qy.worksheetsystem.entity.JobBooking" >
        <id column="timeSheetId" property="timeSheetId"/>
        <result column="title" property="title"/>
        <result column="projectId" property="projectId"/>
        <result column="projectName" property="projectName"/>
        <result column="attendanceType" property="attendanceType"/>
        <result column="attendanceTypeName" property="attendanceTypeName"/>
        <result column="status" property="status"/>
        <result column="beginDate" property="beginDate"/>
        <result column="endDate" property="endDate"/>
        <result column="workingHours" property="workingHours"/>
        <collection property="details" ofType="com.qy.worksheetsystem.entity.JobBookingEntry">
            <id column="detailId" property="detailId"/>
            <result column="workingHoursEny" property="workingHours"/>
            <result column="workCalendarType" property="workCalendarType"/>
            <result column="jbkeAttendanceType" property="attendanceType"/>
            <result column="jbkeAttendanceTypeName" property="attendanceTypeName"/>
            <result column="reportDate" property="reportDate"/>
        </collection>
    </resultMap>

    <select id="getEmpList" resultType="java.util.Map"  >
        select
        p.fname_l2 empName,p.fnumber empNumber
        from CT_WOR_JobBooking jbk
        left join t_bd_person p on p.fid=jbk.CFPersonID
        where jbk.cfpersonid in
        <foreach item="id" index="index" collection="list" open="("
                 separator="," close=")">
            #{id}
        </foreach>
        <if test='audit != null and audit != "" and audit == "0"'>
            and jbk.CFStatus in('2')
        </if>
        <if test='audit != null and audit != "" and audit == "1"'>
            and jbk.CFStatus in('3')
        </if>

        <if test='month != null and month != ""'>
            and jbk.CFYearAndMonth = #{month}
        </if>
        group by jbk.CFYearAndMonth,jbk.CFPersonID,p.fname_l2,p.fnumber
    </select>
</mapper>