<?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.PersonWorkMapper">
    <select id="selectBillKey" resultType="String">
        select newbosid('CF9DCEB1') as id  from dual
    </select>
    <select id="selectKeyByBosTyoe" resultType="String">
        select newbosid(#{bosTyoe}) as id  from dual
    </select>
    <select id="getAvailableDateInfo" resultType="java.util.Map">
        SELECT
        item.fid as "id",
        TO_CHAR(NVL( item.FDate, ''), 'YYYY-MM-DD')  "date",
        (item.FDayType) as "dateType",
        item.FWEEK "week",hre.CFEmployeeID "employeeID",hre.workHours as "workHours"
        FROM
        T_HR_ATS_WorkCalendarItem item
        left join T_HR_ATS_WorkCalendar w on w.fid=item.FCALENDARGROUPID
        left join (
        select whre.CFEmployeeID,whre.CFdata,sum(whre.CFWorkingHours) workHours
        from
        CT_PRO_WorkHoursReportEntry whre
        where whre.CFEmployeeID=#{employeeID}
        group by whre.CFData,whre.CFEmployeeID) hre on hre.CFdata=item.FDate
        WHERE
        item.FDate BETWEEN to_date(#{startDate},'YYYY-MM-DD')
        AND  to_date(#{endDate},'YYYY-MM-DD')
        AND w.FISDEFAULT=1
        order by item.FDate
    </select>
    <!---->
    <select id="getPersonWorkList" resultType="java.util.Map">
        select p.fnumber "personNumber",p.fname_l2 "personName",
        whr.CFAffiliatedProjec "projectID", pm.CFProjectName "projectName",
        hre.CFClockTime "clockTime",to_char(hre.CfData,'YYYY-MM-DD') "data",hre.CFWorkingHours "WorkingHours",
        whr.fbillState "state",hre.fid "id"
        from CT_PRO_WorkHoursReportEntry hre
        left join CT_PRO_WorkHoursReport whr on whr.fid=hre.FParentID
        left join CT_PRO_ProjectManagement pm on pm.FID=whr.CFAffiliatedProjec
        left join t_bd_person p on hre.CFEmployeeID=p.FID
        where p.fnumber=#{number}
        and  hre.CfData BETWEEN to_date(#{beginTime},'YYYY-MM-DD')
        AND  to_date(#{endTime},'YYYY-MM-DD')
        <if test="state==true">
            and whr.fbillState!=0
        </if>
        <if test="state==false">
            and whr.fbillState=0
        </if>
    </select>
    <select id="getPersonWorkSumInfo" resultType="java.util.Map">
        select whr.fbillState "state",sum(hre.CFWorkingHours) "WorkingHours"
        from CT_PRO_WorkHoursReportEntry hre
        left join CT_PRO_WorkHoursReport whr on whr.fid=hre.FParentID
        left join CT_PRO_ProjectManagement pm on pm.FID=whr.CFAffiliatedProjec
        left join t_bd_person p on hre.CFEmployeeID=p.FID
        where p.fnumber=#{number}
        and  hre.CfData BETWEEN to_date(#{beginTime},'YYYY-MM-DD')
        AND  to_date(#{endTime},'YYYY-MM-DD')
        <if test="state==true">
            and whr.fbillState!=0
        </if>
        <if test="state==false">
            and whr.fbillState=0
        </if>
        group by whr.fbillState
    </select>
    <insert id="insertBill" parameterType="java.util.List" >
        insert
        into CT_PRO_WorkHoursReport(
        CFAffiliatedProjec,FAdminOrgUnitId,FHROrgUnitID,
        FBillState,FNumber,FBizDate,
        FCreatorID,FCreateTime,FLastUpdateUserID,
        FLastUpdateTime, FControlUnitID,FID,
        CFDataSource,CFFillingStartDate,CFFillingEndDate
        )
        <foreach collection="billList" item="item" index="index" separator="UNION ALL">
            SELECT
            #{item.affiliatedProject}, #{item.adminOrg}, #{item.hrOrgUnit},
            #{item.billState}, #{item.number}, #{item.bizDate},
            #{item.creator}, SYSDATE, #{item.lastUpdateUser},
            SYSDATE, #{item.CU}, #{item.id},
            'employeeReport', #{item.fillingStartDate}, #{item.fillingEndDate}
            FROM dual
        </foreach>

    </insert>

    <insert id="insertWork" parameterType="java.util.List">
        INSERT into CT_PRO_WorkHoursReportEntry(FParentID,CFEmployeeID,CFClockTime,
        CFClockLocation,CFData,CFWorkingHours,
        CFNotes,FSeq,Fid)
        <foreach collection="workList" item="item" index="index" separator="UNION ALL">
            SELECT
            #{item.parent},#{item.employee},
            #{item.clockTime},#{item.clockLocation},to_date(#{item.clockTime},'YYYY-MM-DD'),
            #{item.WorkingHours},#{item.notes},
            #{item.seq}, newbosid('22DCD041')
            FROM dual
        </foreach>
    </insert>
    <resultMap id="JobBookingEntry" type="com.qy.worksheetsystem.model.entity.JobBooking" >
        <id column="id" property="id"/>
        <result column="startDate" property="beginTime"/>
        <result column="endDate" property="endTime"/>
        <result column="state" property="state"/>
        <result column="projectName" property="projectName"/>
        <result column="projectId" property="projectId"/>
        <association property="person" javaType="com.qy.worksheetsystem.model.entity.JobBookPerson">
            <id property="personId" column="personId" />
            <result column="personName" property="personName" />
            <result column="personNumber" property="personNumber" />
        </association>
        <collection property="entries" ofType="com.qy.worksheetsystem.model.entity.JobBookingEntry">
            <id column="entryId" property="entryId"/>
            <result column="seq" property="seq"/>
            <result column="notes" property="notes"/>
            <result column="workingHours" property="workingHours"/>
            <result column="workingDate" property="workingDate"/>
        </collection>
    </resultMap>
    <select id="getPersonWorkInfoById" resultMap="JobBookingEntry">
        select whr.fid "id",whr.CFFILLINGSTARTDATE "startDate",
        whr.CFFILLINGENDDATE "endDate" , whr.FBILLSTATE "state",
        p.fid "personId",p.fname_l2 "personName",p.fnumber "personNumber",
        pm.CFPROJECTNAME "projectName",pm.fid "projectId",
        hre.CFDATA "workingDate",hre.CFWORKINGHOURS "workingHours",
        hre.CFNOTES "notes",hre.FSEQ "seq",
        hre.fid "entryId"
        from
        CT_PRO_WorkHoursReportEntry hre
        left join CT_PRO_WorkHoursReport whr on whr.fid=hre.FParentID
        left join CT_PRO_ProjectManagement pm on pm.FID=whr.CFAffiliatedProjec
        left join t_bd_person p on hre.CFEmployeeID=p.FID
        where whr.fid =#{id}
    </select>
    <delete id="deleteWork" parameterType="String">
        delete CT_PRO_WorkHoursReport where fid =#{id}
    </delete>
    <delete id="deleteWorkEntry" parameterType="String">
        delete CT_PRO_WorkHoursReportEntry where FPARENTID =#{id}
    </delete>

</mapper>