<?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.CwProjectReportArchiveApplyMapper">


    <sql id="Archive_File_Column_List">
        a.id as "id",
        a.create_by_id as "createBy.id",
        a.create_time as "createTime",
        a.update_by_id as "updateBy.id",
        a.update_time as "updateTime",
        a.del_flag as "delFlag",
        a.remarks as "remarks",
        a.report_id as "reportId",
        a.report_no as "reportNo",
        a.audit_date as "auditDate",
        a.proc_ins_id as "procInsId",
        a.process_definition_id as "processDefinitionId",
        a.status as "status",
        a.tenant_id as "tenantId"
    </sql>

    <update id="updateApplyInfoById">
        update
        cw_project_report_archive_apply
        set
        <if test="remarks != null and remarks != ''">
            remarks = #{remarks},
        </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="status != null and status != ''">
            status = #{status},
        </if>

        <if test="reportId != null and reportId != ''">
            report_id = #{reportId},
        </if>

        <if test="reportNo != null and reportNo != ''">
            report_no = #{reportNo},
        </if>

        update_by_id = #{updateById},
        update_time = #{updateTime}
        where id = #{id} ;
    </update>

    <select id="get"
            resultType="com.jeeplus.finance.projectReportArchive.service.dto.CwProjectReportArchiveApplyDTO">

        SELECT
            <include refid="Archive_File_Column_List"></include>
        FROM cw_project_report_archive_apply a
         <where>
             WHERE a.del_flag = 0
             <if test="id != null and id != ''">
                 AND a.id = #{id}
             </if>
         </where>

    </select>

    <select id="queryReportById" resultType="com.jeeplus.finance.projectReportArchive.service.dto.CwProjectReportArchiveApplyDTO">
        SELECT
            a.id as "reportId",
            a.document_no as "documentNo",
            a.project_report_number as "projectReportNumber",
            a.signature_type as "signatureType",
            a.signature_annotator1 as "signatureAnnotator1",
            a.signature_annotator2 as "signatureAnnotator2",
            a.signature_contract_id as "signatureContractId",
            a.create_time as "createTime",
            a.real_create as "realCreate",
            a.business_type as "businessType",
            a.audit_business_type as "auditBusinessType",
            a.report_name as "reportName",
            a.industry as "industry",
            b.id as projectId,
            b.project_number as projectNumber,
            b.project_name as projectName,
            e.name as projectMasterName,
            ifnull(cw_pa.audit_money,"") as "auditFees",
            new_line.report_no as reportNo,
            new_line.opinion_type as opinionType,
            new_line.report_type as reportType,
            new_line.approved_no_including_tax as approvedNoIncludingTax,
            new_line.approved_including_tax as approvedIncludingTax,
            new_line.bank_letters_count as bankLettersCount,
            new_line.digital_letter_count as digitalLetterCount,
            su3.name as realCreateName,
            cpraa.id as id,
            cpraa.status as status,
            cpraa.proc_ins_id as procInsId,
            cpraa.process_definition_id as processDefinitionId,
            cpraa.remarks as remarks
        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'
                 LEFT JOIN sys_user su3 on a.real_create = su3.id
                 LEFT JOIN cw_project_report_archive_apply cpraa on a.id = cpraa.report_id
        where cpraa.id = #{id}
    </select>

    <select id="queryReportByReportId" resultType="com.jeeplus.finance.projectReportArchive.service.dto.CwProjectReportArchiveApplyDTO">
        SELECT
            a.id as "reportId",
            a.document_no as "documentNo",
            a.project_report_number as "projectReportNumber",
            a.signature_type as "signatureType",
            a.signature_annotator1 as "signatureAnnotator1",
            a.signature_annotator2 as "signatureAnnotator2",
            a.signature_contract_id as "signatureContractId",
            a.create_time as "createTime",
            a.real_create as "realCreate",
            a.business_type as "businessType",
            a.audit_business_type as "auditBusinessType",
            a.report_name as "reportName",
            a.industry as "industry",
            b.id as projectId,
            b.project_number as projectNumber,
            b.project_name as projectName,
            e.name as projectMasterName,
            ifnull(cw_pa.audit_money,"") as "auditFees",
            new_line.report_no as reportNo,
            new_line.opinion_type as opinionType,
            new_line.report_type as reportType,
            new_line.approved_no_including_tax as approvedNoIncludingTax,
            new_line.approved_including_tax as approvedIncludingTax,
            new_line.bank_letters_count as bankLettersCount,
            new_line.digital_letter_count as digitalLetterCount,
            su3.name as realCreateName
        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'
                 LEFT JOIN sys_user su3 on a.real_create = su3.id
        where a.id = #{id}
    </select>



    <select id="getProjectManagerByProjectId" resultType="java.lang.String">
        SELECT
        CASE
        WHEN project_master_id IS NOT NULL
        AND project_master_id != ''
        AND project_master_id NOT IN ('1602914026461380610', '1602914995043614722', '1614824803453370370', '1602912511302615042')
        THEN project_master_id
        WHEN real_header IS NOT NULL
        AND real_header != ''
        AND real_header NOT IN ('1602914026461380610', '1602914995043614722', '1614824803453370370', '1602912511302615042')
        THEN real_header
        ELSE NULL
        END AS project_manager_id
        FROM cw_project_records
        <where>
            del_flag = 0
            <if test="id != null and id != ''">
                AND id = #{id}
            </if>
        </where>
        LIMIT 1

    </select>
</mapper>
