<?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.finance.projectReportArchive.mapper.CwProjectReportArchiveMapper">

    <resultMap id="BaseResultMap" type="com.jeeplus.finance.projectReportArchive.service.dto.CwProjectReportArchiveDTO">
            <id property="id" column="id" jdbcType="VARCHAR"/>
            <result property="createBy.id" column="create_by_id" jdbcType="VARCHAR"/>
            <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
            <result property="updateBy.id" column="update_by_id" jdbcType="VARCHAR"/>
            <result property="updateTime" column="update_time" 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="specialPlace" column="special_place" 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_time" jdbcType="VARCHAR"/>
            <result property="createDate" column="create_time" jdbcType="TIMESTAMP"/>
            <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.finance.projectRecords.service.dto.CwProjectRecordsDTO"></association>
        <collection property="workAttachmentDtoList" ofType="com.jeeplus.sys.service.dto.WorkAttachmentInfoDTO" column="id" select="getFileList"></collection>
    </resultMap>

    <resultMap id="ExportResultMap" type="com.jeeplus.finance.projectReportArchive.service.dto.CwProjectReportArchiveDTO">
        <id property="id" column="id" jdbcType="VARCHAR"/>
        <result property="createBy.id" column="create_by_id" jdbcType="VARCHAR"/>
        <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
        <result property="updateBy.id" column="update_by_id" jdbcType="VARCHAR"/>
        <result property="updateTime" column="update_time" 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="specialPlace" column="special_place" 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_time" jdbcType="VARCHAR"/>
        <result property="createDate" column="create_time" jdbcType="TIMESTAMP"/>
        <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"/>
    </resultMap>

    <resultMap id="AttachmentMap" type="com.jeeplus.finance.projectReportArchive.service.dto.WorkAttachmentArchiveDto">
        <id property="id" column="id" jdbcType="VARCHAR"/>
        <result property="createBy.id" column="create_by_id" jdbcType="VARCHAR"/>
        <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
        <result property="updateBy.id" column="update_by_id" jdbcType="VARCHAR"/>
        <result property="createBy.name" column="create_name" jdbcType="VARCHAR"/>
        <result property="updateTime" column="update_time" 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.finance.projectRecords.service.dto.CwProjectRecordsDTO">
        <id property="id" column="id" jdbcType="VARCHAR"/>
        <result property="createBy.id" column="create_by_id" jdbcType="VARCHAR"/>
        <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
        <result property="updateBy.id" column="update_by_id" jdbcType="VARCHAR"/>
        <result property="updateTime" column="update_time" 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_id,
        cw_pa.create_time,
        cw_pa.update_by_id,
        cw_pa.update_time,
        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.special_place,
        cw_pa.audit_date,
        cw_pa.proc_ins_id,
        cw_pa.process_definition_id,
        cw_pa.origin_proc_ins_id,
        cw_pa.origin_process_definition_id,
        cw_pa.status,
        cw_pa.file_number,
        cw_pa.report_id,
        cw_pa.report_remarks,
        cw_pa.audit_money,
        cw_pa.connect_report_id,
        cw_pa.borrow_type,
        date_format(cw_pa.transfer_time, '%Y-%m-%d %H:%i:%s') as transferTime
    </sql>
    <sql id="Archive_File_Column_List">
        cw_paf.id,
        cw_paf.create_by_id,
        cw_paf.create_time,
        cw_paf.update_by_id,
        cw_paf.update_time,
        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_id,
        cw_par.create_time,
        cw_par.update_by_id,
        cw_par.update_time,
        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_id,
        wa.create_time,
        wa.update_by_id,
        wa.update_time,
        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_id,
        cw_pr.create_time,
        cw_pr.update_by_id,
        cw_pr.update_time,
        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_id 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.finance.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_id 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_id 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,
        su2.name as realHeaderName,
        cw_pr.project_name as project_name,
        cw_pr.project_master_id as project_master,
        cw_pr.real_header as real_header,
        cw_pr.project_executor as projectExecutor,
        cw_pr.create_time as cwprCreateTime,
        cw_prnl.report_no,
        date_format(cw_prnl.report_date,'%Y-%m-%d') as report_date,
        art.ID_ as task_id,
        crb.create_by_id as borrowUserId,
        crb.proc_ins_id as procInsId2,
        cw_praa.id as praaId,
        IFNULL(cw_praa.status, 0) AS praaStatus,
        e.ID_ as praaTaskId,
        cw_praa.proc_ins_id as procInsIdApplyForRight,
        cw_praa.process_definition_id as processDefinitionAuditIdApplyForRight,
        (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_project_report_borrow crb on crb.archive_id = cw_pa.id and crb.del_flag = '0'
        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_id
        left join sys_office so on so.id = su.office_id
        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 sys_user su2 on cw_pr.real_header = su2.id and su2.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_report_archive_apply cw_praa on cw_praa.report_id = cw_pa.report_id and cw_praa.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_
        LEFT JOIN act_ru_task e ON cw_praa.proc_ins_id = e.PROC_INST_ID_

        ${ew.customSqlSegment}
        and cw_prnl.report_number is not null and cw_prnl.report_date is not null and cw_pa.del_flag = '0'
        <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_time DESC
    </select>

    <select id="findExportList"  resultType="com.jeeplus.finance.projectReportArchive.service.dto.CwProjectReportArchiveExportDTO">select
        DISTINCT
        <include refid="Base_Column_List"></include>,
        su3.name as projectCreateName,
        date_format(cw_prnl.report_date,'%Y-%m-%d') as report_date,
        cw_pa.audit_date as auditDate,
        cw_pa.remarks as remarks,
        cw_pa.contract,
        cw_pa.approved_no_including_tax as approvedNoIncludingTax,
        cw_pa.approved_including_tax as approvedIncludingTax,
        cw_pa.entrust_type as entrustType,
        cw_pa.special_place as specialPlace,
        date_format(cw_pa.transfer_time, '%Y-%m-%d %H:%i:%s') as transferTime,
        sureview.name as secondAuditName,

        cw_pr.project_name as project_name,
        cw_prnl.report_no,

        su11.NAME AS signature_annotator1_name,
        su22.NAME AS signature_annotator2_name,
        su33.NAME AS realHeaderName,
        su44.NAME AS projectMasterName,
        cw_wcb.name AS auditedUnitsName


        from cw_project_report_archive cw_pa
        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 sys_user su2 on cw_pr.real_header = su2.id and su2.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 sys_user su3 on su3.id = cw_pa.create_by_id
        left join sys_office so on so.id = su3.office_id

        left join cw_project_report pr on cw_pa.report_id=pr.id and pr.del_flag = '0'
        left join cw_project_report_review cprr on cprr.report_id=pr.id and cprr.del_flag = '0'
        left join sys_user sureview on cprr.review_by = sureview.id and sureview.del_flag = '0'

        -- 原子查询b中的表直接关联到主查询
        LEFT JOIN cw_project_report cw_prr ON cw_prr.id = cw_prnl.report_id AND cw_prr.del_flag = '0'
        LEFT JOIN sys_user su11 ON su11.id = cw_prr.signature_annotator1 AND su11.del_flag = '0'
        LEFT JOIN sys_user su22 ON su22.id = cw_prr.signature_annotator2 AND su22.del_flag = '0'
        LEFT JOIN sys_user su33 ON su33.id = cw_pr.real_header AND su33.del_flag = '0'
        LEFT JOIN sys_user su44 ON su44.id = cw_pr.project_master_id AND su44.del_flag = '0'
        LEFT JOIN cw_work_client_base cw_wcb on cw_pa.audited_units = cw_wcb.id and cw_wcb.del_flag = '0'

        ${ew.customSqlSegment}
        and cw_prnl.report_number is not null and cw_prnl.report_date is not null and cw_pa.del_flag = '0'
        <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_time DESC
    </select>


    <select id="findCompletedList" 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,
        su2.name as realHeaderName,
        cw_pr.project_name as project_name,
        cw_pr.project_number as projectNumber,
        cw_prnl.report_no,
        date_format(cw_prnl.report_date,'%Y-%m-%d') as report_date,
        art.ID_ as task_id,
        crb.create_by_id as borrowUserId,
        crb.proc_ins_id as procInsId2,
        (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_project_report_borrow crb on crb.archive_id = cw_pa.id and crb.del_flag = '0'
        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_id 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 sys_user su2 on cw_pr.real_header = su2.id and su2.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 == '-2'">
            and cw_pa.audit_date is null
        </if>
        <if test="sta == '-1'">
            and cw_pa.audit_date is not null
        </if>
        ORDER BY cw_pa.create_time 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_id,
        su.name as report_create_name,
        su1.name as signature_annotator1_name,
        su2.name as signature_annotator2_name,
        cw_prnl.approved_no_including_tax,
        cw_prnl.approved_including_tax
    </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.signature_annotator1,
        b.signature_annotator2,
        b.report_no,
        cw_pa.create_by_id as projectMaster,
        su4.id as realHeader,
        b.approved_no_including_tax as approvedNoIncludingTaxBak,
        b.approved_including_tax as approvedIncludingTaxBak,
        b.proc_ins_id as cprrProcInsId,
        prnl.report_no as "connectReport",
        pr.business_type as businessType,
        pr.report_name as reportName,
        su1.id as projectManager,
        cwci.contract_no as contractNo,
        cwci.contract_name as contractName,
        cwci.client_contacts as clientContacts,
        cwci.contract_amount as contractAmount,
        su2.name as contractCreateName,
        cwcfp.filed_no as filedNo,
        cw_pa.approved_no_including_tax as approvedNoIncludingTax,
        cw_pa.approved_including_tax as approvedIncludingTax,
        cw_pa.contract,
        cw_pa.entrust_type as entrustType,
        cw_pa.approve_remarks as approveRemarks,
        cw_pa.void_reason as voidReason
        from cw_project_report_archive cw_pa
        left join cw_project_report_new_line prnl on cw_pa.connect_report_id=prnl.report_id
        left join cw_project_report pr on cw_pa.report_id=pr.id
        left join cw_project_records prs on pr.project_id=prs.id
        left join cw_work_contract_info cwci on cwci.id=prs.contract_id
        left join cw_work_contract_file_paper cwcfp on cwcfp.contract_info_id=cwci.id
        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_id and su.del_flag = '0'
        left join sys_user su1 on su1.id = prs.create_by_id and su1.del_flag = '0'
        left join sys_user su2 on su2.id = cwci.create_by_id  and su2.del_flag = '0'
        left join sys_user su3 on prs.project_master_id = su3.id and su3.del_flag = '0'
        left join sys_user su4 on prs.real_header = su4.id and su4.del_flag = '0'
        left join(
            select
                <include refid="prnl_column"></include>
            ,cprr.proc_ins_id
            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 cw_project_report_review cprr on cprr.report_id = cw_prnl.report_id
            left join sys_user su on su.id = cw_prnl.create_by_id 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.id = #{id}
    </select>

    <select id="findReportList" resultType="com.jeeplus.finance.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_id,
            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_id and su3.del_flag = '0'
        ${ew.customSqlSegment}
    </select>


    <select id="queryByReportId" resultType="com.jeeplus.finance.projectReportArchive.service.dto.CwProjectReportArchiveDTO">
        select
        cw_pa.id,
        cw_pa.create_by_id,
        cw_pa.create_time,
        cw_pa.update_by_id,
        cw_pa.update_time,
        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>
    <select id="findDtos" resultType="com.jeeplus.finance.projectReportArchive.service.dto.WorkAttachmentArchiveDto">
        SELECT
			wa.id,
			wa.url,
			wa.attachment_name AS `name`,
			wa.create_by_id AS `by`,
			wa.create_time,
			cw_paf.file_type,
            cw_paf.remarks as 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'
		WHERE
			wa.del_flag = 0
			AND wa.attachment_id = #{id}
    </select>

    <update id="updateInfoById">
        update
          cw_project_report_archive
        set
        <if test="remarks != null and remarks != ''">
            remarks = #{remarks},
        </if>
        <if test="projectId != null and projectId != ''">
            project_id = #{projectId},
        </if>
        <if test="name != null and name != ''">
            name = #{name},
        </if>
        <if test="auditedUnits != null and auditedUnits != ''">
            audited_units = #{auditedUnits},
        </if>
        <if test="reportNum != null and reportNum != ''">
            report_num = #{reportNum},
        </if>
        <if test="papersNum != null and papersNum != ''">
            papers_num = #{papersNum},
        </if>
        <if test="year != null and year != ''">
            year = #{year},
        </if>
        <if test="number != null and number != ''">
            number = #{number},
        </if>
        <if test="isNumber != null and isNumber != ''">
            is_number = #{isNumber},
        </if>
        <if test="specialPlace != null and specialPlace != ''">
            special_place = #{specialPlace},
        </if>
        <if test="auditDate != null">
            audit_date = #{auditDate},
        </if>
        <if test="procInsId != null and procInsId != ''">
            proc_ins_id = #{procInsId},
        </if>
        <if test="processDefinitionId != null and processDefinitionId != ''">
            process_definition_id = #{processDefinitionId},
        </if>
        <if test="procInsId != null and procInsId != ''">
            origin_proc_ins_id = #{originProcInsId},
        </if>
        <if test="processDefinitionId != null and processDefinitionId != ''">
            origin_process_definition_id = #{originProcessDefinitionId},
        </if>
        <if test="status != null and status != ''">
            status = #{status},
        </if>
        <if test="fileNumber != null and fileNumber != ''">
            file_number = #{fileNumber},
        </if>
        <if test="reportId != null and reportId != ''">
            report_id = #{reportId},
        </if>
        <if test="reportRemarks != null and reportRemarks != ''">
            report_remarks = #{reportRemarks},
        </if>
        <if test="auditMoney != null and auditMoney != ''">
            audit_money = #{auditMoney},
        </if>
        <if test="connectReportId != null and connectReportId != ''">
            connect_report_id = #{connectReportId},
        </if>
        <if test="borrowType != null and borrowType != ''">
            borrowType = #{borrowType},
        </if>
        <if test="approveRemarks != null and approveRemarks != ''">
            approve_remarks = #{approveRemarks},
        </if>
        <if test="approvedNoIncludingTax != null and approvedNoIncludingTax != ''">
            approved_no_including_tax = #{approvedNoIncludingTax},
        </if>
        <if test="approvedIncludingTax != null and approvedIncludingTax != ''">
            approved_including_tax = #{approvedIncludingTax},
        </if>
        <if test="contract != null and contract != ''">
            contract = #{contract},
        </if>
        <if test="entrustType != null and entrustType != ''">
            entrust_type = #{entrustType},
        </if>
        <if test="voidReason != null and voidReason != ''">
            void_reason = #{voidReason},
        </if>
        update_by_id = #{updateById},
        update_time = #{updateTime}
        where id = #{id} ;
    </update>
    <update id="updateBorrowTypeById">
        UPDATE cw_project_report_archive SET `borrow_type` = #{status} WHERE del_flag = 0 AND id = #{id}
    </update>

    <update id="updateAuditMoneyByReportId">
        UPDATE cw_project_report_archive SET `audit_money` = #{auditMoney} WHERE del_flag = 0 AND report_id = #{reportId}
    </update>

    <select id="getNotFiledYetList" 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,
        crb.borrow_type,
        crb.create_by_id as borrowUserId,
        crb.proc_ins_id as procInsId2,
        cprr.proc_ins_id as cprrProcInsId,
        cprr.process_definition_id as cprrProcDefId,
        (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_project_report_borrow crb on crb.archive_id = cw_pa.id and crb.del_flag = '0'
        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_id 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_report_review cprr on cprr.report_id = cw_prnl.report_id
        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
        and cw_pa.audit_date is null and timestampdiff(day,cw_prnl.report_date,now()) &lt;= 60
        ORDER BY cw_pa.create_time DESC
    </select>



    <select id="findHiById" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"></include>,
        cw_wcb.name as audited_units_name,
        su.name as userName,
        b.report_date,
        b.report_create_name,
        b.signature_annotator1_name,
        b.signature_annotator2_name,
        b.report_no,
        prnl.report_no as "connectReport"
        from cw_project_report_archive cw_pa
        left join cw_project_report_new_line prnl on cw_pa.connect_report_id=prnl.report_id
        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_id 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_id 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.report_id = (select report_id from cw_project_report_archive where id = #{id}) and cw_pa.del_flag = 1 and cw_pa.status = 5
        order by cw_pa.audit_date desc,cw_pa.update_time desc
    </select>


    <select id="getLastAuditDateByReportId" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"></include>,
        cw_wcb.name as audited_units_name,
        su.name as userName,
        b.report_date,
        b.report_create_name,
        b.signature_annotator1_name,
        b.signature_annotator2_name,
        b.report_no,
        prnl.report_no as "connectReport"
        from cw_project_report_archive cw_pa
        left join cw_project_report_new_line prnl on cw_pa.connect_report_id=prnl.report_id
        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_id 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_id 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.report_id = #{reportId} and cw_pa.del_flag = 1 and cw_pa.status = 5
        order by cw_pa.audit_date desc,cw_pa.update_time desc
        limit 1
    </select>

    <select id="getArchiveDateByReportId" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"></include>
        from cw_project_report_archive cw_pa

        where cw_pa.report_id = #{reportId} and cw_pa.del_flag = 0
        order by cw_pa.audit_date desc,cw_pa.update_time desc
    </select>


    <select id="findByReportType" resultType="com.jeeplus.finance.projectReportArchive.service.dto.CwProjectReportArchiveDTO">
        select
            a.number,
            cw_prnl.report_no AS reportNo
        from cw_project_report_archive a
            left join cw_project_report_new_line cw_prnl on a.report_id = cw_prnl.report_id and cw_prnl.del_flag = '0'
            left join sys_user su on su.id = a.create_by_id and su.del_flag = '0'
        where cw_prnl.report_no
            like concat('%',#{reportType},'%')
            and cw_prnl.report_no LIKE concat('%[',#{year}, ']%')
            and a.number is not null
            and a.del_flag = '0'
            and a.status = '5'
            and su.tenant_id != '10006'
        ORDER BY
            a.audit_date DESC
            LIMIT 1;
    </select>

    <update id="updatePlaceByYear">
        UPDATE cw_project_report_archive a
            JOIN cw_project_report_new_line b ON a.report_id = b.report_id
            SET
                a.special_place = #{specialPlace},
                a.transfer_time = NOW()
        WHERE b.report_no LIKE CONCAT('%[', #{year}, ']%')
        AND a.status = '5'
        AND a.del_flag = '0';
    </update>

    <select id="exportList" resultMap="ExportResultMap">
        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,
        su2.name as realHeaderName,
        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,
        crb.create_by_id as borrowUserId,
        crb.proc_ins_id as procInsId2,
        (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_project_report_borrow crb on crb.archive_id = cw_pa.id and crb.del_flag = '0'
        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_id
        left join sys_office so on so.id = su.office_id
        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 sys_user su2 on cw_pr.real_header = su2.id and su2.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 and cw_pa.del_flag = '0'
        <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_time DESC
    </select>
</mapper>
