<?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.projectReport.mapper.CwProjectReportMapper">

    <sql id="Base_Column_List">
        cw_wcb.id,
        cw_wcb.create_by_id,
        cw_wcb.create_time,
        cw_wcb.update_by_id,
        cw_wcb.update_time,
        cw_wcb.del_flag,
        cw_wcb.remarks,
        cw_wcb.no,
        cw_wcb.name,
        cw_wcb.abbreviation,
        cw_wcb.english_name,
        cw_wcb.type,
        cw_wcb.usc_code,
        cw_wcb.sc_type,
        cw_wcb.manage_level_type,
        cw_wcb.one_up_company,
        cw_wcb.company_level,
        cw_wcb.business_scope,
        cw_wcb.key_responsibilities,
        cw_wcb.industry,
        cw_wcb.affiliation,
        cw_wcb.organization_type,
        cw_wcb.report_type,
        cw_wcb.one_up_office,
        cw_wcb.registration_cert,
        cw_wcb.approving_bodies,
        cw_wcb.office_id,
        cw_wcb.proc_ins_id,
        cw_wcb.process_definition_id,
        cw_wcb.status,
        cw_wcb.agree_date,
        cw_wcb.agree_user_id
    </sql>

    <sql id="Cw_Re_Column_List">
        cw_re.id AS reviewNewId,
        cw_re.proc_ins_id AS procInsId3,
        cw_re.review_status as reviewStatus,
        cw_re.review_by reviewBy,
        cw_re.review_fee reviewFee,
        cw_re.approval_fee approvalFee,
        cw_re.contract_category contractCategory,
        cw_re.contract_fee contractFee,
        cw_re.contract_rate contractRate,
        cw_re.verify_fee verifyFee,
        cw_re.verify_rate verifyRate,
        cw_re.consult_fee consultFee,
        cw_re.building_fee buildingFee,
        cw_re.install_fee installFee,
        cw_re.building_rate buildingRate,
        cw_re.install_rate installRate,
        cw_re.work_start_date workStartDate,
        cw_re.work_end_date workEndDate,
        cw_re.zi_xun_shou_ru ziXunShouRu
    </sql>

    <select id="queryByIdNew" resultType="com.jeeplus.finance.projectReport.domain.CwProjectReportData">
        SELECT
          a.id,
        a.create_by_id AS createById,
        a.create_time,
        a.update_by_id,
        a.update_time,
        a.del_flag,
        a.remarks,
        a.STATUS,
        a.document_no,
        a.project_report_number,
        a.project_id,
        a.office_id,
        a.signature_type,
        a.signature_annotator1,
        a.signature_annotator2,
        a.signature_contract_id,
        a.proc_ins_id,
        a.process_definition_id,
        a.signature_annotator_status,
        c.NAME AS departmentName,
        d.NAME AS userName,
        a.create_time,
        e.NAME AS projectMasterName,
        a.real_create,
        a.business_type,
        a.audit_business_type,
        a.register_address,
        a.business_location,
        a.security_business,
        suser.NAME AS real_header_name,
        b.project_name AS projectName,
        b.project_number AS projectNumber,
        b.project_level AS projectLevel,
        b.office_id AS officeId,
        b.report_type AS reportType,
        b.project_money,
        b.plan_start_date as planStartDate,
        b.plan_end_date as planEndDate,
        b.create_time as createDate,
        su3.NAME AS projectManager,
        su3.id AS projectManagerId,
        su3.NAME AS memberName,
        cert.type AS professionType,
        cert.profession AS profession,
        cert.enroll_cert_no AS enrollCertNo,
        su2.NAME AS projectCreateByName,
        b.create_time AS projectCreateTime,
        a.industry,
        a.business_objects,
        a.report_name,
        <include refid="Cw_Re_Column_List"></include>,
        suser2.name as reviewByName,
        cw_sai.id as signAndIssueId,
        new_line.report_no as reportNo,
        cw_re.update_time as reportDate,
        cwinfo.contract_no,
        cwinfo.contract_name,
        cwinfo.contract_amount as contractAmount,
        cwinfo.contract_type as contractType,
        cwcp.customer_name as clientName,
        roa.id as onlineArchivingId,
        roa.online_remarks,
        roa.create_time as applyDates,
        roa.proc_ins_id as onlineArchivingProcInsId,
        suser3.name as applyUserName,
        suser3.id as applyUserId,
        roaart.ID_ as onlineArchivingTaskId,
        rpa.id as paperArchivingId,
        rpa.filed_no,
        rpa.confirm_filed_no,
        rpa.status as paperArchivingStatus

    FROM
        cw_project_report a
        LEFT JOIN cw_project_records b ON a.project_id = b.id
        AND b.del_flag = '0'
        LEFT JOIN sys_user su2 ON b.create_by_id = su2.id
        AND su2.del_flag = '0'
        LEFT JOIN sys_user su3 ON b.project_leader_id = su3.id
        AND su3.del_flag = '0'
        LEFT JOIN sys_cert cert ON su3.id = cert.user_id
        AND cert.del_flag = '0'
        LEFT JOIN sys_office c ON a.office_id = c.id
        AND c.del_flag = '0'
        LEFT JOIN sys_user d ON a.create_by_id = d.id
        AND d.del_flag = '0'
        LEFT JOIN sys_user e ON b.project_master_id = e.id
        AND e.del_flag = '0'
        LEFT JOIN sys_user suser ON b.real_header = suser.id
        AND suser.del_flag = '0'
        LEFT JOIN cw_project_report_review_new cw_re ON a.id = cw_re.report_id
        AND cw_re.del_flag = '0'
        LEFT JOIN cw_project_report_sign_and_issue cw_sai ON a.id = cw_sai.report_id
        AND cw_sai.del_flag = '0'
        LEFT JOIN sys_user suser2 ON cw_re.review_by = suser2.id
        AND suser2.del_flag = '0'
        left join cw_project_report_new_line new_line on a.id = new_line.report_id and new_line.del_flag = '0'
        LEFT JOIN cw_work_contract_info cwinfo on b.contract_id = cwinfo.id and cwinfo.del_flag = '0'
        LEFT JOIN cw_work_contract_participant cwcp on cwinfo.id = cwcp.contract_info_id and cwcp.del_flag = '0'
        left join cw_project_report_online_archiving roa on a.id = roa.report_id and roa.del_flag = '0'
        left join act_ru_task roaart ON roa.proc_ins_id = roaart.PROC_INST_ID_
        left join cw_project_report_paper_archiving rpa on a.id = rpa.report_id and rpa.del_flag = '0'

        LEFT JOIN sys_user suser3 ON roa.create_by_id = suser3.id
        AND suser3.del_flag = '0'
    WHERE
        a.id = #{id}
				AND a.del_flag = '0'
        AND cert.type IN ('3', '4')
        AND cert.type = (
            CASE
                WHEN EXISTS (
                    SELECT 1
                    FROM sys_cert sub_cert
                    WHERE sub_cert.user_id = su3.id
                        AND sub_cert.type = '3'
                        AND sub_cert.del_flag = '0'
                ) THEN '3'
                ELSE '4'
            END
        );
    </select>

    <select id="findList" resultType="com.jeeplus.finance.projectReport.domain.CwProjectReportData">
        SELECT
        DISTINCT a.id,
        a.create_by_id as createById,
        a.create_time,
        a.update_by_id,
        a.update_time,
        a.del_flag,
        a.remarks,
        a.status,
        a.document_no,
        a.project_report_number,
        a.project_id,
        a.office_id,
        a.signature_type,
        a.signature_annotator1,
        a.signature_annotator2,
        a.signature_contract_id,
        a.real_create,
        a.proc_ins_id,
        a.process_definition_id,
        a.signature_annotator_status,
        b.project_number as projectNumber,
        b.project_name as projectName,
        b.project_master_id as projectMasterId,
        b.real_header as projectMasterId2,
        c.name as departmentName,
        d.name as userName,
        ifnull(cw_pa.audit_money,"0") as "auditFees",
        e.name as projectMasterName,
        b.real_header as projectMaster2Id,
        f.name as projectMaster2Name,
        cw_prs1.proc_ins_id as proc_ins_id1,
        cw_prs1.process_definition_id as process_definition_id1,
        cw_prs1.status as status1,
        cw_prs1.id as sid1,
        cw_prs2.proc_ins_id as proc_ins_id2,
        cw_prs2.process_definition_id as process_definition_id2,
        cw_prs2.status as status2,
        cw_prs2.id as sid2,
        cw_prs3.proc_ins_id as proc_ins_ids3,
        cw_prs3.process_definition_id as process_definition_id3,
        cw_prs3.status as status3,
        cw_prs3.id as sid3,
        ifnull(cw_rev.review_status,'0') as reviewStatus,
        cw_rev.proc_ins_id as procInsId3,
        cw_rev.id as reviewNewId,
        ifnull(sai.status,'0') as signAndIssueStatus,
        sai.proc_ins_id as procInsId4,
        sai.id as signAndIssueId,
        ifnull(roa.status,'0') as onlineArchivingStatus,
        roa.proc_ins_id as onlineArchivingProcInsId,
        roa.id as onlineArchivingId,
        roaart.ID_ as onlineArchivingTaskId,
        ifnull(rpa.status,'0') as paperArchivingStatus,
        rpa.proc_ins_id as paperArchivingProcInsId,
        rpa.id as paperArchivingId,
        rpaart.ID_ as paperArchivingTaskId,

        new_line.report_no as reportNo,
        cwcb.name as servedUnitName,
        art.ID_ as rev_task_id,
        artart.ID_ as signAndIssueTaskId,
        arta.ID_ as rev_task_id2,
        art1.ID_ as sign_task_id1,
        art2.ID_ as sign_task_id2,
        art3.ID_ as sign_task_id3,
        a.industry,
        a.business_objects,
        a.report_name,
        cw_na.proc_ins_id as applyProcId,
        cw_na.process_definition_id as applyProcDefId,
        ifnull(cw_na.review_status,'0') as applyStatus,
        ifnull(cw_na.take_number_type,'0') as takeNumberType,
        cw_na.improper_take_cause as improperTakeCause,

        artar.ID_ as review_task_number_id,
        cw_nra.proc_ins_id as applyReviewProcId,
        cw_nra.process_definition_id as applyReviewProcDefId,
        ifnull(cw_nra.review_status,'0') as applyReviewStatus,
        cw_ci.contract_name,
        b.project_level
        FROM cw_project_report a
        left join cw_project_records b on a.project_id = b.id
        LEFT JOIN sys_office c on a.office_id = c.id
        LEFT JOIN sys_user d on a.create_by_id = d.id
        LEFT JOIN sys_user e on b.project_master_id = e.id
        LEFT JOIN sys_user f on b.real_header = f.id

        LEFT JOIN cw_project_report_signature cw_prs1 on cw_prs1.report_id = a.id and cw_prs1.type = '1' and cw_prs1.del_flag = '0'
        LEFT JOIN cw_project_report_signature cw_prs2 on cw_prs2.report_id = a.id and cw_prs2.type = '2' and cw_prs2.del_flag = '0'
        LEFT JOIN cw_project_report_signature cw_prs3 on cw_prs3.report_id = a.id and cw_prs3.type = '3' and cw_prs3.del_flag = '0'
        left join act_ru_task art1 ON cw_prs1.proc_ins_id = art1.PROC_INST_ID_
        left join act_ru_task art2 ON cw_prs2.proc_ins_id = art2.PROC_INST_ID_
        left join act_ru_task art3 ON cw_prs3.proc_ins_id = art3.PROC_INST_ID_

        left join cw_project_report_new_line new_line on a.id = new_line.report_id and new_line.del_flag = '0'
        left join cw_project_report_review_new cw_rev on a.id = cw_rev.report_id and cw_rev.del_flag = '0'
        left join cw_project_report_sign_and_issue sai on a.id = sai.report_id and sai.del_flag = '0'
        left join act_ru_task artart ON sai.proc_ins_id = artart.PROC_INST_ID_
        left join cw_project_report_online_archiving roa on a.id = roa.report_id and roa.del_flag = '0'
        left join act_ru_task roaart ON roa.proc_ins_id = roaart.PROC_INST_ID_
        left join cw_project_report_paper_archiving rpa on a.id = rpa.report_id and rpa.del_flag = '0'
        left join act_ru_task rpaart ON rpa.proc_ins_id = rpaart.PROC_INST_ID_

        LEFT JOIN cw_project_report_archive cw_pa on  a.id = cw_pa.report_id and cw_pa.del_flag = '0'
        left join act_ru_task art ON cw_rev.proc_ins_id = art.PROC_INST_ID_
        left join cw_project_members cpm on b.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 cw_work_client_base cwcb on new_line.served_unit_id = cwcb.id
        LEFT JOIN cw_work_contract_info cw_ci on  b.contract_id=cw_ci.id
        LEFT JOIN cw_project_report_number_apply cw_na on  a.id = cw_na.report_id
        LEFT JOIN cw_project_report_number_review_apply cw_nra on  a.id = cw_nra.report_id
         left join act_ru_task arta ON cw_na.proc_ins_id = arta.PROC_INST_ID_
         left join act_ru_task artar ON cw_nra.proc_ins_id = artar.PROC_INST_ID_
        ${ew.customSqlSegment}
        ORDER BY a.create_time DESC
    </select>
    <select id="queryById" resultType="com.jeeplus.finance.projectReport.domain.CwProjectReportData">
        SELECT
        a.id,
        a.create_by_id as createById,
        a.create_time,
        a.update_by_id,
        a.update_time,
        a.del_flag,
        a.remarks,
        a.status,
        a.document_no,
        a.project_report_number,
        a.project_id,
        a.office_id,
        a.signature_type,
        a.signature_annotator1,
        a.signature_annotator2,
        a.signature_contract_id,
        a.proc_ins_id,
        a.process_definition_id,
        a.signature_annotator_status,
        b.project_number as projectNumber,
        b.project_name as projectName,
        c.name as departmentName,
        d.name as userName,
        a.create_time,
        ifnull(cw_pa.audit_money,"") as "auditFees",
        e.name as projectMasterName,
        a.real_create,
        a.business_type,
        a.audit_business_type,
        a.register_address,
        a.business_location,
        a.security_business,
        cw_prs1.proc_ins_id as proc_ins_id1,
        cw_prs1.process_definition_id as process_definition_id1,
        cw_prs1.status as status1,
        cw_prs1.id as sid1,
        cw_prs2.proc_ins_id as proc_ins_id2,
        cw_prs2.process_definition_id as process_definition_id2,
        cw_prs2.status as status2,
        cw_prs2.id as sid2,
        cw_prs3.proc_ins_id as proc_ins_ids3,
        cw_prs3.process_definition_id as process_definition_id3,
        cw_prs3.status as status3,
        cw_prs3.id as sid3,
        new_line.served_unit_id as servedUnitId,
        new_line.report_date as reportDate,
        new_line.report_type as reportType,
        new_line.report_number as reportNumber,
        new_line.report_no as reportNo,
        new_line.opinion_type as opinionType,
        new_line.seal_type as sealType,
        cw_re.proc_ins_id as procInsId3,
        suser.name as real_header_name,
        b.project_master_id as project_master_id1,
        b.real_header as project_master_id2,
        a.industry,
        a.business_objects,
        a.report_name,
        cw_na.proc_ins_id as applyProcId,
        cw_na.process_definition_id as applyProcDefId,
        ifnull(cw_na.review_status,'0') as applyStatus,
        cw_na.take_number_type as takeNumberType,
        cw_na.improper_take_cause as improperTakeCause,

        cw_nra.proc_ins_id as applyReviewProcId,
        cw_nra.process_definition_id as applyReviewProcDefId,
        ifnull(cw_nra.review_status,'0') as applyReviewStatus
        FROM cw_project_report a
        left join cw_project_records b on a.project_id = b.id
        LEFT JOIN sys_office c on a.office_id = c.id
        LEFT JOIN sys_user d on a.create_by_id = d.id
        LEFT JOIN sys_user e on b.project_master_id = e.id
        LEFT JOIN sys_user suser on b.real_header = suser.id
        LEFT JOIN cw_project_report_signature cw_prs1 on cw_prs1.report_id = a.id and cw_prs1.type = '1' and cw_prs1.del_flag = '0'
        LEFT JOIN cw_project_report_signature cw_prs2 on cw_prs2.report_id = a.id and cw_prs2.type = '2' and cw_prs2.del_flag = '0'
        LEFT JOIN cw_project_report_signature cw_prs3 on cw_prs3.report_id = a.id and cw_prs3.type = '3' and cw_prs3.del_flag = '0'
        left join cw_project_report_new_line new_line on a.id = new_line.report_id
        left join cw_project_report_review cw_re on a.id = cw_re.report_id and cw_re.del_flag = '0'
        LEFT JOIN cw_project_report_number_apply cw_na on  a.id = cw_na.report_id and cw_na.del_flag = '0'
        LEFT JOIN cw_project_report_number_review_apply cw_nra on  a.id = cw_nra.report_id and cw_nra.del_flag = '0'
        LEFT JOIN cw_project_report_archive cw_pa on  a.id = cw_pa.report_id and cw_pa.del_flag = '0'
        where a.id = #{id}
    </select>
    <select id="queryContainDelById" resultType="com.jeeplus.finance.projectReport.domain.CwProjectReportData">
        SELECT
        a.id,
        a.create_by_id as createById,
        a.create_time,
        a.update_by_id,
        a.update_time,
        a.del_flag,
        a.remarks,
        a.status,
        a.document_no,
        a.project_report_number,
        a.project_id,
        a.office_id,
        a.signature_type,
        a.signature_annotator1,
        a.signature_annotator2,
        a.signature_contract_id,
        a.proc_ins_id,
        a.process_definition_id,
        b.project_number as projectNumber,
        b.project_name as projectName,
        c.name as departmentName,
        d.name as userName,
        a.create_time,
        a.audit_fees,
        e.name as projectMasterName,
        a.real_create,
        a.business_type,
        a.audit_business_type,
        a.register_address,
        a.business_location,
        a.security_business,
        cw_prs1.proc_ins_id as proc_ins_id1,
        cw_prs1.process_definition_id as process_definition_id1,
        cw_prs1.status as status1,
        cw_prs1.id as sid1,
        cw_prs2.proc_ins_id as proc_ins_id2,
        cw_prs2.process_definition_id as process_definition_id2,
        cw_prs2.status as status2,
        cw_prs2.id as sid2,
        cw_prs3.proc_ins_id as proc_ins_ids3,
        cw_prs3.process_definition_id as process_definition_id3,
        cw_prs3.status as status3,
        cw_prs3.id as sid3,
        new_line.served_unit_id as servedUnitId,
        new_line.report_date as reportDate,
        new_line.report_type as reportType,
        new_line.report_number as reportNumber,
        new_line.report_no as reportNo,
        new_line.opinion_type as opinionType,
        new_line.seal_type as sealType,
        cw_re.proc_ins_id as procInsId3,
        suser.name as real_header_name,
        b.project_master_id as project_master_id1,
        b.real_header as project_master_id2,
        a.industry,
        a.business_objects,
        a.report_name
        FROM cw_project_report a
        left join cw_project_records b on a.project_id = b.id
        LEFT JOIN sys_office c on a.office_id = c.id
        LEFT JOIN sys_user d on a.create_by_id = d.id
        LEFT JOIN sys_user e on b.project_master_id = e.id
        LEFT JOIN sys_user suser on b.real_header = suser.id
        LEFT JOIN cw_project_report_signature cw_prs1 on cw_prs1.report_id = a.id and cw_prs1.type = '1' and cw_prs1.del_flag = '0'
        LEFT JOIN cw_project_report_signature cw_prs2 on cw_prs2.report_id = a.id and cw_prs2.type = '2' and cw_prs2.del_flag = '0'
        LEFT JOIN cw_project_report_signature cw_prs3 on cw_prs3.report_id = a.id and cw_prs3.type = '3' and cw_prs3.del_flag = '0'
        left join cw_project_report_new_line new_line on a.id = new_line.report_id
        left join cw_project_report_review cw_re on a.id = cw_re.report_id and cw_re.del_flag = '0'
        where a.id = #{id}
    </select>
    <select id="findClineList" resultType="com.jeeplus.finance.workClientInfo.service.dto.CwWorkClientBaseDTO">
        select
        <include refid="Base_Column_List"></include>,
        su.name as user_name
        from cw_work_client_base cw_wcb
        left join sys_user su on su.id = cw_wcb.create_by_id
        left join cw_work_client_type cw_wct on cw_wct.client_id = cw_wcb.id and cw_wct.del_flag = '0'
        left join cw_project_client_info cw_info on cw_wcb.id = cw_info.client_id
        LEFT JOIN cw_project_records cw_pro on cw_info.project_id = cw_pro.id
        ${ew.customSqlSegment}
    </select>
    <select id="getUserNameById" resultType="java.lang.String">
        select name from sys_user where id = #{id}
    </select>

    <update id="updateSignatureInfo">
        update cw_project_report set
        signature_contract_id = #{signatureContractId}
        where id = #{id}
    </update>

    <update id="updateSignatureUrl">
        update cw_project_report set
        signature_url = #{signatureUrl}
        where id = #{id}
    </update>

    <update id="updateReportSealType">
        update cw_project_report_new_line set
        seal_type = '已盖章'
        where report_id = #{id}
    </update>

    <update id="updateDelFlag">
        UPDATE cw_project_report SET del_flag = #{flag} where id = #{id}
    </update>




    <select id="queryByContractId" resultType="com.jeeplus.finance.projectReport.domain.CwProjectReportData">
        SELECT
        a.id,
        a.create_by_id as createById,
        a.create_time,
        a.update_by_id,
        a.update_time,
        a.del_flag,
        a.remarks,
        a.status,
        a.report_name,
        a.document_no,
        a.project_report_number,
        a.project_id,
        a.office_id,
        a.signature_type,
        a.signature_annotator1,
        a.signature_annotator2,
        a.signature_contract_id,
        a.proc_ins_id,
        a.process_definition_id,
        b.project_number as projectNumber,
        b.project_name as projectName,
        c.name as departmentName,
        d.name as userName,
        a.create_time,
        a.audit_fees,
        e.name as projectMasterName,
        cw_prs1.proc_ins_id as proc_ins_id1,
        cw_prs1.process_definition_id as process_definition_id1,
        cw_prs1.status as status1,
        cw_prs1.id as sid1,
        cw_prs2.proc_ins_id as proc_ins_id2,
        cw_prs2.process_definition_id as process_definition_id2,
        cw_prs2.status as status2,
        cw_prs2.id as sid2
        FROM cw_project_report a
        left join cw_project_records b on a.project_id = b.id
        LEFT JOIN sys_office c on a.office_id = c.id
        LEFT JOIN sys_user d on a.create_by_id = d.id
        LEFT JOIN sys_user e on b.project_master_id = e.id
        LEFT JOIN cw_project_report_signature cw_prs1 on cw_prs1.report_id = a.id and cw_prs1.type = '1' and cw_prs1.del_flag = '0'
        LEFT JOIN cw_project_report_signature cw_prs2 on cw_prs2.report_id = a.id and cw_prs2.type = '2' and cw_prs2.del_flag = '0'
        where a.signature_contract_id = #{contractId}
    </select>
    <select id="getByProjectId" resultType="com.jeeplus.finance.projectReport.domain.CwProjectReportData">
        SELECT
        a.id,
        a.create_by_id as createById,
        a.create_time,
        a.update_by_id,
        a.update_time,
        a.del_flag,
        a.remarks,
        a.status,
        a.document_no,
        a.project_report_number,
        a.project_id,
        a.office_id,
        a.report_name,
        a.signature_type,
        a.signature_annotator1,
        a.signature_annotator2,
        a.signature_contract_id,
        a.proc_ins_id,
        a.process_definition_id,
        b.project_number as projectNumber,
        b.project_name as projectName,
        c.name as departmentName,
        d.name as userName,
        a.create_time,
        a.audit_fees,
        e.name as projectMasterName,
        cw_prs1.proc_ins_id as proc_ins_id1,
        cw_prs1.process_definition_id as process_definition_id1,
        cw_prs1.status as status1,
        cw_prs1.id as sid1,
        cw_prs2.proc_ins_id as proc_ins_id2,
        cw_prs2.process_definition_id as process_definition_id2,
        cw_prs2.status as status2,
        cw_prs2.id as sid2
        FROM cw_project_report a
        left join cw_project_records b on a.project_id = b.id
        LEFT JOIN sys_office c on a.office_id = c.id
        LEFT JOIN sys_user d on a.create_by_id = d.id
        LEFT JOIN sys_user e on b.project_master_id = e.id
        LEFT JOIN cw_project_report_signature cw_prs1 on cw_prs1.report_id = a.id and cw_prs1.type = '1' and cw_prs1.del_flag = '0'
        LEFT JOIN cw_project_report_signature cw_prs2 on cw_prs2.report_id = a.id and cw_prs2.type = '2' and cw_prs2.del_flag = '0'
        where a.project_id = #{projectId} and a.del_flag = '0'
    </select>
    <select id="getContainDelByProjectId" resultType="com.jeeplus.finance.projectReport.domain.CwProjectReportData">
        SELECT
        a.id,
        a.create_by_id as createById,
        a.create_time,
        a.update_by_id,
        a.update_time,
        a.del_flag,
        a.remarks,
        a.status,
        a.document_no,
        a.project_report_number,
        a.project_id,
        a.office_id,
        a.signature_type,
        a.signature_annotator1,
        a.signature_annotator2,
        a.signature_contract_id,
        a.proc_ins_id,
        a.process_definition_id,
        b.project_number as projectNumber,
        b.project_name as projectName,
        c.name as departmentName,
        d.name as userName,
        a.create_time,
        a.audit_fees,
        a.report_name,
        e.name as projectMasterName,
        cw_prs1.proc_ins_id as proc_ins_id1,
        cw_prs1.process_definition_id as process_definition_id1,
        cw_prs1.status as status1,
        cw_prs1.id as sid1,
        cw_prs2.proc_ins_id as proc_ins_id2,
        cw_prs2.process_definition_id as process_definition_id2,
        cw_prs2.status as status2,
        cw_prs2.id as sid2
        FROM cw_project_report a
        left join cw_project_records b on a.project_id = b.id
        LEFT JOIN sys_office c on a.office_id = c.id
        LEFT JOIN sys_user d on a.create_by_id = d.id
        LEFT JOIN sys_user e on b.project_master_id = e.id
        LEFT JOIN cw_project_report_signature cw_prs1 on cw_prs1.report_id = a.id and cw_prs1.type = '1' and cw_prs1.del_flag = '0'
        LEFT JOIN cw_project_report_signature cw_prs2 on cw_prs2.report_id = a.id and cw_prs2.type = '2' and cw_prs2.del_flag = '0'
        where a.project_id = #{projectId}
    </select>
    <select id="findFileList" resultType="com.jeeplus.sys.domain.WorkAttachmentInfo">
        SELECT * FROM work_attachment a
        left join cw_project_report_file b on a.id = b.report_file_id
        WHERE a.del_flag = 0 AND a.attachment_id = #{id} and b.file_affiliation = 0
    </select>
    <select id="findFileListOnAttachmentFlag" resultType="com.jeeplus.sys.domain.WorkAttachmentInfo">
        SELECT * FROM work_attachment a
        left join cw_project_report_file b on a.id = b.report_file_id
        <where>
            a.del_flag = 0 AND a.attachment_id = #{id} and b.file_affiliation = 0
            <if test="attachmentFlag != null and attachmentFlag != ''">
                and attachment_flag = #{attachmentFlag}
            </if>
        </where>
    </select>

    <select id="findIsExit" resultType="java.lang.Integer">
        SELECT
			COUNT( 0 )
		FROM
			work_attachment
		WHERE
			del_flag = 0
			AND attachment_id = #{id}
			AND attachment_name = #{name}
    </select>

    <delete id="deleteAttachement">
        delete from work_attachment
		WHERE
			del_flag = 0
			AND attachment_id = #{id}
			AND attachment_flag = #{attachmentFlag}
    </delete>

    <select id="getProcessInstanceIdById" resultType="java.lang.String">
        select proc_ins_id as "procInsId" from cw_project_report_signature where id = #{id}
    </select>

    <update id="updateSignatureType">
        update cw_project_report_signature
        set
        <if test="signatureAnnotator1Status != null and signatureAnnotator1Status != ''">
            signature_annotator1_status = #{signatureAnnotator1Status}
        </if>
        <if test="signatureAnnotator2Status != null and signatureAnnotator2Status != ''">
            signature_annotator2_status = #{signatureAnnotator2Status}
        </if>
        <if test="sealAdminStatus != null and sealAdminStatus != ''">
            seal_admin_status = #{sealAdminStatus}
        </if>
        where id = #{id}
    </update>

    <select id="getReportSignatureById" resultType="com.jeeplus.finance.projectReport.domain.CwProjectReportSignature">
        select
          id,
          create_by_id,
          create_time,
          update_by_id,
          update_time,
          del_flag,
          remarks,
          report_id,
          type,
          proc_ins_id,
          status,
          process_definition_id,
          signature_annotator1_status,
          signature_annotator2_status,
          seal_admin_status
        from
          cw_project_report_signature
        where id = #{id}
    </select>

    <insert id="insertProjectReportFile">
        insert into cw_project_report_file (
        id,
        create_by_id,
        create_time,
        update_by_id,
        update_time,
        del_flag,
        report_file_id,
        file_type,
        seal_type,
        remarks,
        file_affiliation
        )
        values
        (
        #{id},
       #{createById},
       #{createTime},
       #{updateById},
       #{updateTime},
       #{delFlag},
       #{reportFileId},
       #{fileType},
       #{sealType},
       #{remarks},
       #{fileAffiliation}
        )
    </insert>

    <select id="getIdByProjectId" resultType="string">
        select
            cw_pr.id
        from cw_project_report cw_pr
        where cw_pr.del_flag = '0' and cw_pr.project_id = #{id}
    </select>

    <select id="getIdByClientId" resultType="string">
        select
            cw_pr.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'
        where cw_pr.del_flag = '0' and cw_prnl.served_unit_id = #{id}
    </select>

    <select id="getIdByContractId" resultType="string">
        select
            cw_pr.id
        from cw_project_report cw_pr
        where cw_pr.del_flag = '0' and cw_pr.project_id in (
            select
                cw_pr.id
            from cw_work_contract_info cw_wci
            inner join cw_project_records cw_pr on cw_wci.id = cw_pr.contract_id and cw_pr.del_flag = '0'
            where cw_wci.del_flag = '0' and cw_wci.id = #{id}
        )
    </select>

    <select id="getByIds" resultType="com.jeeplus.finance.projectReport.domain.CwProjectReport">
        select
        cw_pr.id,
        cw_pr.project_id,
        cw_prnl.report_no,
        cw_pr.office_id,
        cw_pr.create_time,
        cw_pro.project_number,
        cw_pro.project_name,
        so.name as office_name,
        su1.name as project_master_name,
        su.name as createName
        from cw_project_report cw_pr
        inner join cw_project_records cw_pro on cw_pro.id = cw_pr.project_id and cw_pro.del_flag = '0'
        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 cw_pro.project_master_id = su1.id and su1.del_flag = '0'
        left join sys_office so on so.id = cw_pr.office_id and so.del_flag = '0'
        left join sys_user su on cw_pr.create_by_id = su.id and su.del_flag = '0'
        ${ew.customSqlSegment}
    </select>
    <select id="getReviewByS" resultType="com.jeeplus.finance.projectReport.domain.ReviewUser">
        select name,id,sys_user_id
        from cw_project_report_review_user
        where parent_id = (select id from cw_project_report_review_user where name = #{name}) and del_flag = 0 ORDER BY name
    </select>
    <select id="getReviewByPost" resultType="com.jeeplus.finance.projectReport.domain.ReviewUser">
        select a.name,a.id as "sysUserId"
        from sys_user a
        left join sys_user_post b on a.id = b.user_id
        left join sys_post c on b.post_id = c.id
        where c.name = #{postName} and a.del_flag = 0 ORDER BY a.name
    </select>
    <select id="findDtos" resultType="com.jeeplus.sys.domain.WorkAttachmentInfo">
        SELECT
			id,
			url,
			attachment_name AS `name`,
			create_by_id AS `by`,
			create_time
		FROM
			work_attachment
		WHERE
			del_flag = 0
			AND attachment_id = #{id}
    </select>
    <select id="getByNewLineId" resultType="com.jeeplus.finance.projectReport.domain.CwProjectReport">
        select a.*,sys1.name as signatureAnnotator1Name,sys2.name as signatureAnnotator2Name from cw_project_report a
        left join sys_user sys1 on a.signature_annotator1 = sys1.id
        left join sys_user sys2 on a.signature_annotator2 = sys2.id
        where a.id = #{reportId}
    </select>

    <select id="getServedUnitName" resultType="java.lang.String">
        select b.name from cw_project_records cw_pro
        left join cw_work_contract_info a on cw_pro.contract_id = a.id
        left join cw_work_client_base b on a.client_contacts = b.id
        where cw_pro.id = #{id}
    </select>
    <select id="getOfficeId" resultType="java.lang.String">
        select id from sys_office where name = #{name}
    </select>
    <select id="getCorrelationOfficeIdByName" resultType="java.lang.String">
        select group_concat(id) from sys_office where id =(select id from sys_office where name = #{name}) or parent_ids like concat('%',(select id from sys_office where name = #{name}),'%')
    </select>

    <update id="updateSignatureTypeById">
        update cw_project_report set
        signature_type = #{signatureType}
        where id = #{id}
    </update>

    <delete id="deleteIsSign">
        delete from cw_project_report_file where report_file_id = #{id} and file_affiliation = 1
    </delete>

    <select id="checkIsSign" resultType="int">
        select count(id)
        from cw_project_report_file where report_file_id = #{id} and file_affiliation = 1
    </select>

    <update id="updateAnnotatorById" parameterType="com.jeeplus.finance.projectReport.domain.CwProjectReport">
        update cw_project_report
        set signature_annotator1 = #{report.signatureAnnotator1},
            signature_annotator2 = #{report.signatureAnnotator2},
            industry = #{report.industry},
            business_objects = #{report.businessObjects}
        where id = #{report.id}
    </update>

    <update id="saveById">
        update cw_project_report set del_flag = '0' where id = #{id}
    </update>



    <update id="updateCwProjectReportDataById">
        update cw_project_report
        set
        <if test="delFlag != null and delFlag != ''">
            del_flag = #{delFlag},
        </if>
        <if test="documentNo != null and documentNo != ''">
            document_no = #{documentNo},
        </if>
        <if test="projectId != null and projectId != ''">
            project_id = #{projectId},
        </if>
        <if test="officeId != null and officeId != ''">
            office_id = #{officeId},
        </if>
        <if test="signatureType != null and signatureType != ''">
            signature_type = #{signatureType},
        </if>
        <if test="signatureAnnotator1 != null and signatureAnnotator1 != ''">
            signature_annotator1 = #{signatureAnnotator1},
        </if>
        <if test="signatureAnnotator2 != null and signatureAnnotator2 != ''">
            signature_annotator2 = #{signatureAnnotator2},
        </if>
        <if test="signatureContractId != null and signatureContractId != ''">
            signature_contract_id = #{signatureContractId},
        </if>
        <if test="procInsId != null and procInsId != ''">
            proc_ins_id = #{procInsId},
        </if>
        <if test="status != null and status != ''">
            status = #{status},
        </if>
        <if test="processDefinitionId != null and processDefinitionId != ''">
            process_definition_id = #{processDefinitionId},
        </if>
        <if test="auditFees != null and auditFees != ''">
            audit_fees = #{auditFees},
        </if>
        <if test="businessType != null and businessType != ''">
            business_type = #{businessType},
        </if>
        <if test="auditBusinessType != null and auditBusinessType != ''">
            audit_business_type = #{auditBusinessType},
        </if>
        <if test="registerAddress != null and registerAddress != ''">
            register_address = #{registerAddress},
        </if>
        <if test="businessLocation != null and businessLocation != ''">
            business_location = #{businessLocation},
        </if>
        <if test="securityBusiness != null and securityBusiness != ''">
            security_business = #{securityBusiness},
        </if>
        <if test="industry != null and industry != ''">
            industry = #{industry},
        </if>
        <if test="businessObjects != null and businessObjects != ''">
            business_objects = #{businessObjects},
        </if>
        <if test="reportName != null and reportName != ''">
            report_name = #{reportName},
        </if>
        <if test="tenantId != null and tenantId != ''">
            tenant_id = #{tenantId},
        </if>
        <if test="realCreate != null and realCreate != ''">
            real_create = #{realCreate},
        </if>
        update_by_id = #{updateById},
        update_time = #{updateTime}
        where del_flag = 0 and id = #{id}
    </update>

    <select id="getById" resultType="com.jeeplus.finance.projectReport.domain.CwProjectReport">
        SELECT
            a.id,
            a.remarks,
            a.document_no,
            a.project_id,
            a.office_id,
            a.signature_annotator1,
            a.signature_annotator2,
            a.signature_type,
            a.proc_ins_id,
            a.process_definition_id,
            a.STATUS,
            a.signature_contract_id,
            a.real_create,
            a.audit_fees,
            a.business_type,
            a.audit_business_type,
            a.register_address,
            a.business_location,
            a.security_business,
            a.industry,
            a.business_objects,
            a.create_time,
            a.create_by_id,
            a.update_time,
            a.update_by_id,
            a.del_flag,
            a.tenant_id,
            re.proc_ins_id as procInsReview,
            re.review_status as reviewStatus
        FROM
            cw_project_report a
            left join cw_project_report_review re on a.id = re.report_id
        WHERE
            a.id = #{id}
            AND a.del_flag = 0
    </select>

    <select id="getReportByReportNo" resultType="com.jeeplus.finance.projectReport.domain.CwProjectReport">
        select
            cpr.*
        from
            cw_project_report cpr
        left join
            cw_project_report_new_line cw_prnl on cpr.id=cw_prnl.report_id
        where
            cw_prnl.report_no=#{reportNo}
    </select>

    <select id="getReportInfoByNoList" resultType="com.jeeplus.finance.projectReport.domain.CwProjectReport">
        select
            cpr.*,cw_prnl.report_no as "reportNo"
        from
            cw_project_report cpr
        left join
            cw_project_report_new_line cw_prnl on cpr.id=cw_prnl.report_id
        <where>
            <if test="reportNoList != null">
                AND cw_prnl.report_no IN
                <foreach collection="reportNoList" item="item" index="index" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
        </where>

    </select>

    <select id="getReportByReportNo" resultType="com.jeeplus.finance.projectReport.domain.CwProjectReport">
        select
        a.id,
        a.create_by_id as createById,
        a.create_time,
        a.update_by_id,
        a.update_time,
        a.del_flag,
        a.remarks,
        a.status,
        a.document_no,
        a.project_report_number,
        a.project_id,
        a.office_id,
        a.signature_type,
        a.signature_annotator1,
        a.signature_annotator2,
        a.signature_contract_id,
        a.real_create,
        a.proc_ins_id,
        a.process_definition_id,
        a.signature_annotator_status,
        a.audit_fees,
        a.industry,
        a.business_objects
        from
            cw_project_report a
        left join
            cw_project_report_new_line cw_prnl on cpr.id=cw_prnl.report_id
        where
            cw_prnl.report_no=#{reportNo}
    </select>

    <select id="getReportInfoByNoList" resultType="com.jeeplus.finance.projectReport.domain.CwProjectReport">
        select
        a.id,
        a.create_by_id as createById,
        a.create_time,
        a.update_by_id,
        a.update_time,
        a.del_flag,
        a.remarks,
        a.status,
        a.document_no,
        a.project_report_number,
        a.project_id,
        a.office_id,
        a.signature_type,
        a.signature_annotator1,
        a.signature_annotator2,
        a.signature_contract_id,
        a.real_create,
        a.proc_ins_id,
        a.process_definition_id,
        a.signature_annotator_status,
        cw_prnl.report_no as "reportNo",
        a.audit_fees,
        a.industry,
        a.business_objects
        from
            cw_project_report a
        left join
            cw_project_report_new_line cw_prnl on cpr.id=cw_prnl.report_id
        <where>
            <if test="reportNoList != null">
                AND cw_prnl.report_no IN
                <foreach collection="reportNoList" item="item" index="index" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
        </where>

    </select>

    <select id="getReportByReportNo" resultType="com.jeeplus.finance.projectReport.domain.CwProjectReport">
        select
        a.id,
        a.create_by_id as createById,
        a.create_time,
        a.update_by_id,
        a.update_time,
        a.del_flag,
        a.remarks,
        a.status,
        a.document_no,
        a.project_report_number,
        a.project_id,
        a.office_id,
        a.signature_type,
        a.signature_annotator1,
        a.signature_annotator2,
        a.signature_contract_id,
        a.real_create,
        a.proc_ins_id,
        a.process_definition_id,
        a.signature_annotator_status,
        a.audit_fees,
        a.industry,
        a.business_objects
        from
            cw_project_report a
        left join
            cw_project_report_new_line cw_prnl on cpr.id=cw_prnl.report_id
        where
            cw_prnl.report_no=#{reportNo}
    </select>
    <select id="getContactInfos" resultType="com.jeeplus.finance.projectReport.service.dto.ProjectContactInfo">
        SELECT
            cw_wcb.NAME AS clientContactsName,
            cw_wco.NAME AS NAME,
            cw_wco.contact_first,
            cw_wco.contact_second
        FROM
            cw_project_client_info cw_pci
            LEFT JOIN cw_work_client_base cw_wcb ON cw_wcb.id = cw_pci.client_id
            AND cw_wcb.del_flag = '0'
            LEFT JOIN cw_work_client_contact cw_wco ON cw_wcb.id = cw_wco.client_id
            AND cw_wco.del_flag = '0'
        WHERE
            cw_pci.project_id = #{id}
    </select>

    <update id="updatesigStatusById">
        update cw_project_report set signature_annotator1=#{signatureAnnotator1},signature_annotator2=#{signatureAnnotator2},signature_annotator_status=#{signatureAnnotatorStatus} where id=#{id}
    </update>



</mapper>
