<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.jeeplus.test.cw.projectReport.mapper.CwProjectReportMapper">

    <sql id="Base_Column_List">
        cw_wcb.id,
        cw_wcb.create_by,
        cw_wcb.create_date,
        cw_wcb.update_by,
        cw_wcb.update_date,
        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>

    <select id="findList" resultType="com.jeeplus.test.cw.projectReport.domain.CwProjectReportData">
        SELECT
        a.id,
        a.create_by as createById,
        a.create_date,
        a.update_by,
        a.update_date,
        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,
        b.project_number as projectNumber,
        b.project_name as projectName,
        b.project_master_id as projectMasterId,
        c.name as departmentName,
        d.name as userName,
        a.create_date,
        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,
        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,
        new_line.report_no as reportNo
        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 = 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'
        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 and new_line.del_flag = '0'
        left join cw_project_report_review cw_rev on a.id = cw_rev.report_id and cw_rev.del_flag = '0'
        ${ew.customSqlSegment}
        <if test="currentUser.id != '1'.toString">
            <if test="isBmzr == '0'.toString">
                and (a.create_by = #{currentUser.id} or b.id in (
                select cw_pm.project_id
                from cw_project_members cw_pm
                where cw_pm.user_id = #{currentUser.id} and cw_pm.del_flag = '0' and cw_pm.status = '0'
                ))
            </if>
            <if test="isBmzr == '1'.toString">
                and (a.create_by in (
                select su.id from sys_user su where su.del_flag = '0' and su.office_id in
                (select so.id from sys_office so where so.id = #{currentUser.officeDTO.id} or so.parent_ids like concat('%',#{currentUser.officeDTO.id},'%') and so.del_flag = '0')
                ) or b.id in (
                select cw_pm.project_id
                from cw_project_members cw_pm
                where cw_pm.user_id = #{currentUser.id} and cw_pm.del_flag = '0' and cw_pm.status = '0'
                ))
            </if>
            <if test="isBmzr == '3'.toString">
                and cw_rev.review_by = #{currentUser.id} and cw_rev.review_status != '0' and cw_rev.review_status != '1'
            </if>
        </if>
        ORDER BY a.create_date DESC
    </select>
    <select id="queryById" resultType="com.jeeplus.test.cw.projectReport.domain.CwProjectReportData">
        SELECT
        a.id,
        a.create_by as createById,
        a.create_date,
        a.update_by,
        a.update_date,
        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_date,
        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
        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 = 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.test.cw.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
        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.test.cw.projectReport.domain.CwProjectReportData">
        SELECT
        a.id,
        a.create_by as createById,
        a.create_date,
        a.update_by,
        a.update_date,
        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_date,
        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 = 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.test.cw.projectReport.domain.CwProjectReportData">
        SELECT
        a.id,
        a.create_by as createById,
        a.create_date,
        a.update_by,
        a.update_date,
        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_date,
        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 = 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="findFileList" resultType="com.jeeplus.test.oss.domain.WorkAttachment">
        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="findIsExit" resultType="java.lang.Integer">
        SELECT
			COUNT( 0 )
		FROM
			work_attachment
		WHERE
			del_flag = 0
			AND attachment_id = #{id}
			AND attachment_name = #{name}
    </select>

    <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.test.cw.projectReport.domain.CwProjectReportSignature">
        select
          id,
          create_by,
          create_date,
          update_by,
          update_date,
          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,
        create_date,
        update_by,
        update_date,
        del_flag,
        report_file_id,
        file_type,
        seal_type,
        remarks,
        file_affiliation
        )
        values
        (
        #{id},
       #{createBy},
       #{createDate},
       #{updateBy},
       #{updateDate},
       #{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.test.cw.projectReport.domain.CwProjectReport">
        select
        cw_pr.id,
        cw_pr.project_id,
        cw_prnl.report_no,
        cw_pr.office_id,
        cw_pr.create_date,
        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 = su.id and su.del_flag = '0'
        ${ew.customSqlSegment}
    </select>
    <select id="getReviewByS" resultType="com.jeeplus.test.cw.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.test.cw.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.test.workContract.service.dto.WorkAttachmentDto">
        SELECT
			id,
			url,
			attachment_name AS `name`,
			create_by AS `by`,
			create_date
		FROM
			work_attachment
		WHERE
			del_flag = 0
			AND attachment_id = #{id}
    </select>
    <select id="getByNewLineId" resultType="com.jeeplus.test.cw.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>
</mapper>
