<?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.jeeplus.test.cw.projectReportArchive.mapper.CwProjectReportArchiveMapper">

    <resultMap id="BaseResultMap" type="com.jeeplus.test.cw.projectReportArchive.service.dto.CwProjectReportArchiveDTO">
            <id property="id" column="id" jdbcType="VARCHAR"/>
            <result property="createBy.id" column="create_by" jdbcType="VARCHAR"/>
            <result property="createDate" column="create_date" jdbcType="TIMESTAMP"/>
            <result property="updateBy.id" column="update_by" jdbcType="VARCHAR"/>
            <result property="updateDate" column="update_date" jdbcType="TIMESTAMP"/>
            <result property="delFlag" column="del_flag" jdbcType="INTEGER"/>
            <result property="remarks" column="remarks" jdbcType="VARCHAR"/>
            <result property="projectId" column="project_id" jdbcType="VARCHAR"/>
            <result property="name" column="name" jdbcType="VARCHAR"/>
            <result property="auditedUnits" column="audited_units" jdbcType="VARCHAR"/>
            <result property="reportNum" column="report_num" jdbcType="VARCHAR"/>
            <result property="papersNum" column="papers_num" jdbcType="VARCHAR"/>
            <result property="year" column="year" jdbcType="VARCHAR"/>
            <result property="number" column="number" jdbcType="VARCHAR"/>
            <result property="isNumber" column="is_number" jdbcType="VARCHAR"/>
            <result property="auditDate" column="audit_date" jdbcType="TIMESTAMP"/>
            <result property="procInsId" column="proc_ins_id" jdbcType="VARCHAR"/>
            <result property="processDefinitionId" column="process_definition_id" jdbcType="VARCHAR"/>
            <result property="status" column="status" jdbcType="VARCHAR"/>
            <result property="auditedUnitsName" column="audited_units_name" jdbcType="VARCHAR"/>
            <result property="createBy.name" column="user_name" jdbcType="VARCHAR"/>
            <result property="fileNumber" column="file_number" jdbcType="VARCHAR"/>
            <result property="projectMasterName" column="project_master_name" jdbcType="VARCHAR"/>
            <result property="projectName" column="project_name" jdbcType="VARCHAR"/>
            <result property="createName" column="user_name" jdbcType="VARCHAR"/>
            <result property="createDateT" column="create_date" jdbcType="VARCHAR"/>
            <result property="reportId" column="report_id" jdbcType="VARCHAR"/>
            <result property="reportNo" column="report_no" jdbcType="VARCHAR"/>
            <result property="reportRemarks" column="report_remarks" jdbcType="VARCHAR"/>
            <result property="reportDate" column="report_date" jdbcType="TIMESTAMP"/>
            <result property="reportCreateName" column="report_create_name" jdbcType="VARCHAR"/>
            <result property="signatureAnnotator1Name" column="signature_annotator1_name" jdbcType="VARCHAR"/>
            <result property="signatureAnnotator2Name" column="signature_annotator2_name" jdbcType="VARCHAR"/>
            <result property="taskId" column="task_id" jdbcType="VARCHAR"/>
        <association property="cwProjectRecordsDTO" column="project_id" select="getProject" javaType="com.jeeplus.test.cw.projectRecords.service.dto.CwProjectRecordsDTO"></association>
        <collection property="workAttachmentDtoList" ofType="com.jeeplus.test.workContract.service.dto.WorkAttachmentDto" column="id" select="getFileList"></collection>
    </resultMap>
    <resultMap id="AttachmentMap" type="com.jeeplus.test.cw.projectReportArchive.service.dto.WorkAttachmentArchiveDto">
        <id property="id" column="id" jdbcType="VARCHAR"/>
        <result property="createBy.id" column="create_by" jdbcType="VARCHAR"/>
        <result property="createDate" column="create_date" jdbcType="TIMESTAMP"/>
        <result property="updateBy.id" column="update_by" jdbcType="VARCHAR"/>
        <result property="createBy.name" column="create_name" jdbcType="VARCHAR"/>
        <result property="updateDate" column="update_date" jdbcType="TIMESTAMP"/>
        <result property="delFlag" column="del_flag" jdbcType="VARCHAR"/>
        <result property="url" column="url" jdbcType="VARCHAR"/>
        <result property="name" column="attachment_name" jdbcType="VARCHAR"/>
        <result property="size" column="file_size" jdbcType="VARCHAR"/>
        <result property="fileType" column="file_type" jdbcType="VARCHAR"/>
        <result property="remarks" column="cw_remarks" jdbcType="VARCHAR"/>
    </resultMap>
    <resultMap id="ProjectResultMap" type="com.jeeplus.test.cw.projectRecords.service.dto.CwProjectRecordsDTO">
        <id property="id" column="id" jdbcType="VARCHAR"/>
        <result property="createBy.id" column="create_by" jdbcType="VARCHAR"/>
        <result property="createDate" column="create_date" jdbcType="TIMESTAMP"/>
        <result property="updateBy.id" column="update_by" jdbcType="VARCHAR"/>
        <result property="updateDate" column="update_date" jdbcType="TIMESTAMP"/>
        <result property="delFlag" column="del_flag" jdbcType="INTEGER"/>
        <result property="remarks" column="remarks" jdbcType="VARCHAR"/>
        <result property="projectNumber" column="project_number" jdbcType="VARCHAR"/>
        <result property="projectName" column="project_name" jdbcType="VARCHAR"/>
        <result property="officeId" column="office_id" jdbcType="VARCHAR"/>
        <result property="projectMoney" column="project_money" jdbcType="VARCHAR"/>
        <result property="auditYear" column="audit_year" jdbcType="VARCHAR"/>
        <result property="planStartDate" column="plan_start_date" jdbcType="TIMESTAMP"/>
        <result property="planEndDate" column="plan_end_date" jdbcType="TIMESTAMP"/>
        <result property="projectMasterId" column="project_master_id" jdbcType="VARCHAR"/>
        <result property="projectLeaderId" column="project_leader_id" jdbcType="VARCHAR"/>
        <result property="procInsId" column="proc_ins_id" jdbcType="VARCHAR"/>
        <result property="processDefinitionId" column="process_definition_id" jdbcType="VARCHAR"/>
        <result property="status" column="status" jdbcType="VARCHAR"/>
        <result property="createBy.name" column="user_name" jdbcType="VARCHAR"/>
        <result property="projectMasterName" column="project_master_name" jdbcType="VARCHAR"/>
        <result property="contractId" column="contract_id" jdbcType="VARCHAR"/>
        <result property="contractName" column="contract_name" jdbcType="VARCHAR"/>
        <result property="contractAmount" column="contract_amount" jdbcType="VARCHAR"/>
        <result property="contractNum" column="contract_num" jdbcType="VARCHAR"/>
        <result property="payerSubject" column="payer_subject" jdbcType="VARCHAR"/>
        <result property="paymentMethod" column="payment_method" jdbcType="VARCHAR"/>
        <result property="realHeader" column="real_header" jdbcType="VARCHAR"/>
        <result property="realHeaderName" column="real_header_name" jdbcType="VARCHAR"/>
    </resultMap>

    <sql id="Base_Column_List">
        cw_pa.id,
        cw_pa.create_by,
        cw_pa.create_date,
        cw_pa.update_by,
        cw_pa.update_date,
        cw_pa.del_flag,
        cw_pa.remarks,
        cw_pa.project_id,
        cw_pa.name,
        cw_pa.audited_units,
        cw_pa.report_num,
        cw_pa.papers_num,
        cw_pa.year,
        cw_pa.number,
        cw_pa.is_number,
        cw_pa.audit_date,
        cw_pa.proc_ins_id,
        cw_pa.process_definition_id,
        cw_pa.status,
        cw_pa.file_number,
        cw_pa.report_id,
        cw_pa.report_remarks
    </sql>
    <sql id="Archive_File_Column_List">
        cw_paf.id,
        cw_paf.create_by,
        cw_paf.create_date,
        cw_paf.update_by,
        cw_paf.update_date,
        cw_paf.del_flag,
        cw_paf.remarks,
        cw_paf.work_attachment_id,
        cw_paf.file_type
    </sql>
    <sql id="Archive_Report_Column_List">
        cw_par.id,
        cw_par.create_by,
        cw_par.create_date,
        cw_par.update_by,
        cw_par.update_date,
        cw_par.del_flag,
        cw_par.remarks,
        cw_par.archive_id,
        cw_par.report_id
    </sql>
    <sql id="File_Column_List">
        wa.id,
        wa.create_by,
        wa.create_date,
        wa.update_by,
        wa.update_date,
        wa.remarks,
        wa.del_flag,
        wa.url,
        wa.type,
        wa.attachment_id,
        wa.attachment_name,
        wa.attachment_flag,
        wa.module_type,
        wa.attachment_type,
        wa.file_size,
        wa.sort,
        wa.description
    </sql>
    <sql id="Project_Column_List">
        cw_pr.id,
        cw_pr.create_by,
        cw_pr.create_date,
        cw_pr.update_by,
        cw_pr.update_date,
        cw_pr.del_flag,
        cw_pr.remarks,
        cw_pr.project_number,
        cw_pr.project_name,
        cw_pr.office_id,
        cw_pr.project_money,
        cw_pr.audit_year,
        cw_pr.plan_start_date,
        cw_pr.plan_end_date,
        cw_pr.project_master_id,
        cw_pr.project_leader_id,
        cw_pr.proc_ins_id,
        cw_pr.status,
        cw_pr.process_definition_id,
        cw_pr.contract_id,
        cw_pr.real_header
    </sql>

    <select id="getProject" resultMap="ProjectResultMap">
        select
        <include refid="Project_Column_List"></include>,
        cw_wci.contract_name,
        cw_wci.contract_amount,
        cw_wci.contract_num,
        cw_wci.payer_subject,
        cw_wci.payment_method,
        su.name as user_name,
        su2.name as project_master_name,
        su3.name as real_header_name
        from cw_project_records cw_pr
        left join cw_work_contract_info cw_wci on cw_wci.id = cw_pr.contract_id and cw_wci.del_flag = '0'
        left join sys_user su on su.id = cw_pr.create_by and su.del_flag = '0'
        left join sys_user su2 on su2.id = cw_pr.project_master_id and su2.del_flag = '0'
        left join sys_user su3 on su3.id = cw_pr.real_header and su3.del_flag = '0'
        where cw_pr.del_flag = '0' and cw_pr.id = #{id}
    </select>
    <select id="getReportList" resultType="com.jeeplus.test.cw.projectReportArchive.service.dto.CwProjectReportArchiveReportDTO">
        select
        <include refid="Archive_Report_Column_List"></include>,
        cw_pr.signature_annotator1,
        cw_pr.signature_annotator2,
        su1.name as signature_annotator1_name,
        su2.name as signature_annotator2_name,
        cw_prnl.report_date,
        cw_prnl.report_no,
        su3.name as report_create_name
        from cw_project_report_archive_report cw_par
        left join cw_project_report_new_line cw_prnl on cw_par.report_id = cw_prnl.id and cw_prnl.del_flag = '0'
        left join cw_project_report cw_pr on cw_pr.id = cw_prnl.report_id and cw_pr.del_flag = '0'
        left join sys_user su1 on su1.id = cw_pr.signature_annotator1 and su1.del_flag = '0'
        left join sys_user su2 on su2.id = cw_pr.signature_annotator2 and su2.del_flag = '0'
        left join sys_user su3 on su3.id = cw_prnl.create_by and su3.del_flag = '0'
        where cw_par.del_flag = '0' and cw_par.archive_id = #{id}
    </select>
    <select id="getFileList" resultMap="AttachmentMap">
        select
        <include refid="File_Column_List"></include>,
        su.name as create_name,
        cw_paf.file_type,
        cw_paf.remarks as cw_remarks
        from work_attachment wa
        left join cw_project_report_archive_file cw_paf on cw_paf.work_attachment_id = wa.id and cw_paf.del_flag = '0'
        left join sys_user su on su.id = wa.create_by and su.del_flag = '0'
        where wa.del_flag = '0' and wa.attachment_id = #{id}
    </select>

    <select id="findList" resultMap="BaseResultMap">
        select
        DISTINCT
        <include refid="Base_Column_List"></include>,
        cw_wcb.name as audited_units_name,
        su.name as user_name,
        su1.name as project_master_name,
        cw_pr.project_name as project_name,
        cw_prnl.report_no,
        date_format(cw_prnl.report_date,'%Y-%m-%d') as report_date,
        art.ID_ as task_id,
        (case when cw_pa.audit_date is null then timestampdiff(day,cw_prnl.report_date,now()) when cw_pa.audit_date is not null then timestampdiff(day,cw_prnl.report_date,cw_pa.audit_date) end) as archiveDay
        from cw_project_report_archive cw_pa
        left join cw_work_client_base cw_wcb on cw_pa.audited_units = cw_wcb.id and cw_wcb.del_flag = '0'
        left join sys_user su on su.id = cw_pa.create_by and su.del_flag = '0'
        left join sys_office so on so.id = su.office_id and so.del_flag ='0'
        left join cw_project_records cw_pr on cw_pr.id = cw_pa.project_id and cw_pr.del_flag = '0'
        left join sys_user su1 on cw_pr.project_master_id = su1.id and su1.del_flag = '0'
        left join cw_project_report_new_line cw_prnl on cw_prnl.report_id = cw_pa.report_id and cw_prnl.del_flag = '0'

        left join cw_project_members cpm on cw_pr.id = cpm.project_id
        left join sys_user sub on cpm.user_id = sub.id
        left join sys_user_manage_office sumo on sumo.office_id = sub.office_id

        left join act_ru_task art ON cw_pa.proc_ins_id = art.PROC_INST_ID_

        ${ew.customSqlSegment}
        and cw_prnl.report_number is not null and cw_prnl.report_date is not null
        <if test="sta == '超期未归档'">
            and cw_pa.audit_date is null and timestampdiff(day,cw_prnl.report_date,now())>60
        </if>
        <if test="sta == '暂时未归档'">
            and cw_pa.audit_date is null and timestampdiff(day,cw_prnl.report_date,now()) &lt;= 60
        </if>
        <if test="sta == '已超期归档'">
            and cw_pa.audit_date is not null and timestampdiff(day,cw_prnl.report_date,cw_pa.audit_date)>60
        </if>
        <if test="sta == '已按时归档'">
            and cw_pa.audit_date is not null and timestampdiff(day,cw_prnl.report_date,cw_pa.audit_date) &lt;= 60
        </if>

        ORDER BY cw_pa.create_date DESC
    </select>

    <sql id="prnl_column">
        cw_pr.id as report_id,
        cw_prnl.report_no,
        cw_prnl.report_date,
        cw_pr.signature_annotator1,
        cw_pr.signature_annotator2,
        cw_prnl.create_by,
        su.name as report_create_name,
        su1.name as signature_annotator1_name,
        su2.name as signature_annotator2_name
    </sql>
    <select id="queryById" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"></include>,
        cw_wcb.name as audited_units_name,
        su.name as user_name,
        b.report_date,
        b.report_create_name,
        b.signature_annotator1_name,
        b.signature_annotator2_name,
        b.report_no
        from cw_project_report_archive cw_pa
        left join cw_work_client_base cw_wcb on cw_pa.audited_units = cw_wcb.id and cw_wcb.del_flag = '0'
        left join sys_user su on su.id = cw_pa.create_by and su.del_flag = '0'
        left join(
            select
                <include refid="prnl_column"></include>
            from cw_project_report_new_line cw_prnl
            left join cw_project_report cw_pr on cw_pr.id = cw_prnl.report_id and cw_pr.del_flag = '0'
            left join sys_user su on su.id = cw_prnl.create_by and su.del_flag = '0'
            left join sys_user su1 on su1.id = cw_pr.signature_annotator1 and su1.del_flag = '0'
            left join sys_user su2 on su2.id = cw_pr.signature_annotator2 and su2.del_flag = '0'
            where cw_prnl.del_flag = '0'
        ) b on b.report_id = cw_pa.report_id
        where cw_pa.del_flag = '0' and cw_pa.id = ${id}
    </select>

    <select id="findReportList" resultType="com.jeeplus.test.cw.projectReportArchive.service.dto.ReportDTO">
        select
            cw_pr.signature_annotator1,
            cw_pr.signature_annotator2,
            su1.name as signature_annotator1_name,
            su2.name as signature_annotator2_name,
            cw_prnl.report_date,
            cw_prnl.report_no,
            cw_prnl.id as new_line_id,
            su3.name as create_name,
            cw_pr.create_by,
            cw_pr.id as report_id
        from cw_project_report cw_pr
        inner join cw_project_report_new_line cw_prnl on cw_prnl.report_id = cw_pr.id and cw_prnl.del_flag = '0'
        left join sys_user su1 on su1.id = cw_pr.signature_annotator1 and su1.del_flag = '0'
        left join sys_user su2 on su2.id = cw_pr.signature_annotator2 and su2.del_flag = '0'
        left join sys_user su3 on su3.id = cw_pr.create_by and su3.del_flag = '0'
        ${ew.customSqlSegment}
    </select>


    <select id="queryByReportId" resultType="com.jeeplus.test.cw.projectReportArchive.service.dto.CwProjectReportArchiveDTO">
        select
        cw_pa.id,
        cw_pa.create_by,
        cw_pa.create_date,
        cw_pa.update_by,
        cw_pa.update_date,
        cw_pa.del_flag,
        cw_pa.remarks,
        cw_pa.project_id,
        cw_pa.name,
        cw_pa.audited_units,
        cw_pa.report_num,
        cw_pa.papers_num,
        cw_pa.year,
        cw_pa.number,
        cw_pa.is_number,
        cw_pa.audit_date,
        cw_pa.proc_ins_id,
        cw_pa.process_definition_id,
        cw_pa.status,
        cw_pa.file_number,
        cw_pa.report_id,
        cw_pa.report_remarks
        from cw_project_report_archive cw_pa
        where cw_pa.del_flag = '0' and cw_pa.report_id = #{reportId}
    </select>
</mapper>
