<?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.reportCancellApplyArchived.mapper.ReportCancellApplyArchivedMapper">

	<sql id="Base_Column_List">
        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.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.apply_file_type,
        cw_pa.file_number,
        cw_pa.report_id,
        cw_pa.report_remarks,
        cw_pa.carchived_type,
        cw_a.apply_file_type
    </sql>
	<sql id="prnl_column">
        cw_pr.id as report_id,
        cw_pr.project_id as project_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
    </sql>
    <update id="updateArchiveStyle">
        update cw_project_report_cancell_apply set apply_file_type = #{status} where id = #{id}
    </update>
    <update id="updateCancellArchiveType">
        update cw_project_report_cancell_apply_archived set apply_file_type = #{status} where id = #{id}
    </update>
    <update id="updateStatusByArchiveId">
        update cw_project_report_cancell_apply set apply_file_type = #{status} where id = #{id}
    </update>
    <delete id="deleteFileInfo">
        delete from work_attachment where attachment_id = #{attachmentId}
    </delete>

    <select id="getList" resultType="com.jeeplus.finance.reportCancellApplyArchived.service.dto.ArchivedReportInfoDto">
        SELECT
        DISTINCT
        cw_ar.create_by_id as createId,
        a.id,
        a.create_by_id as createById,
        a.create_time,
        a.create_time as "createDateT",
        a.update_by_id,
        a.update_time,
        a.del_flag,
        a.remarks,
        a.status,
        ifnull(a.apply_file_type,'0') as apply_file_type,
				a.proc_ins_id,
        a.process_definition_id,
        b.report_no,
        b.id as "reportId",
				c.document_no,
				c.office_id,
				d.project_number as projectNumber,
				d.project_name,
				d.project_master_id,
				e.name as departmentName,
				f.name as userName,
				g.name as projectMasterName,
				h.name as reportSponsor,
				h.id as reportSponsorId,
				d.project_master_id as projectMasterId,
				d.real_header as projectMaster2Id,
                k.name as projectMaster2Name,
				cw_ar.proc_ins_id as cwProcInsId,
				cw_ar.process_definition_id as cwProDefId,
                art.ID_ as archived_task_id,
        cw_ar.number

				FROM cw_project_report_cancell_apply a
				LEFT JOIN cw_project_report_new_line b
				on a.report_new_line_id = b.id
				LEFT JOIN cw_project_report c
				on b.report_id = c.id
				LEFT JOIN cw_project_records d
				on c.project_id = d.id
				LEFT JOIN sys_office e
        on c.office_id = e.id
				LEFT JOIN sys_user f
        on a.create_by_id = f.id
        left join sys_user_manage_office sumo on sumo.office_id = f.office_id
				LEFT JOIN sys_user g on d.project_master_id = g.id
                LEFT JOIN sys_user k on d.real_header = k.id
        LEFT JOIN sys_user h
        on c.create_by_id = h.id
        left join cw_project_report_cancell_apply_archived cw_ar
        on a.id = cw_ar.archive_id
        left join act_ru_task art ON cw_ar.proc_ins_id = art.PROC_INST_ID_

        ${ew.customSqlSegment}
        <if test="officeIds !=null and officeIds != ''">
            and c.office_id in (${officeIds})
        </if>
        order by a.create_time desc
    </select>
    <select id="queryById"
            resultType="com.jeeplus.finance.reportCancellApplyArchived.service.dto.ApplyArchiveReportDTO">
		select
		<include refid="Base_Column_List"></include>,
        cw_a.id as id,
		su.name as user_name,
		b.report_date,
		b.report_create_name,
		b.signature_annotator1_name,
		b.signature_annotator2_name,
		b.report_no
		from cw_project_report_cancell_apply cw_a
		left join cw_project_report_cancell_apply_archived cw_pa on cw_a.id = cw_pa.archive_id
		left join sys_user su on su.id = cw_pa.create_by_id
		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
		left join sys_user su on su.id = cw_prnl.create_by_id
		left join sys_user su1 on su1.id = cw_pr.signature_annotator1
		left join sys_user su2 on su2.id = cw_pr.signature_annotator2
		) b on b.report_id = cw_pa.report_id
		where cw_a.id = #{id}
	</select>
    <select id="selectByApplyId"
            resultType="com.jeeplus.finance.reportCancellApplyArchived.domain.CwProjectReportApplyArchive">
        select * from cw_project_report_cancell_apply_archived where archive_id = #{id}
    </select>
    <select id="findList"
            resultType="com.jeeplus.sys.domain.WorkAttachmentInfo">
        SELECT * FROM work_attachment WHERE del_flag = 0 AND attachment_id = #{id}
    </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="selectFileInfoByAttid"
            resultType="com.jeeplus.finance.reportCancellApplyArchived.service.dto.CwWorkAttachmentArchiveDto">
        select attachment_name as name,url,create_by_id as userId,create_time,remarks,type as fileType,file_size as size from work_attachment where attachment_id = #{id}
    </select>
    <select id="selectByArchiveId"
            resultType="com.jeeplus.finance.reportCancellApplyArchived.domain.CwProjectReportApplyArchive">
        select * from cw_project_report_cancell_apply_archived where archive_id = #{id}
    </select>
    <select id="selectProjectInfo"
            resultType="com.jeeplus.finance.projectRecords.service.dto.CwProjectRecordsDTO">
        select * from cw_project_records where id = #{id}
    </select>
    <select id="getUserInfo" resultType="com.jeeplus.sys.service.dto.UserDTO">
        select * from sys_user where id = #{userId}
    </select>
    <select id="selectFilePaperByContractInfoId"
            resultType="com.jeeplus.finance.reportCancellApplyArchived.service.dto.ApplyArchiveReportDTO">
        SELECT
        cw_ar.create_by_id as createId,
        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.apply_file_type,
				a.proc_ins_id,
        a.process_definition_id,
				c.document_no,
				c.office_id,
				d.project_number as projectNumber,
				d.project_name,
				d.project_master_id,
				e.name as departmentName,
				f.name as userName,
				g.name as projectMasterName,
				h.name as reportSponsor,
				d.project_master_id as projectMasterId,
				cw_ar.proc_ins_id as cwProcInsId,
				cw_ar.process_definition_id as cwProDefId

				FROM cw_project_report_cancell_apply a
				LEFT JOIN cw_project_report_new_line b
				on a.report_new_line_id = b.id
				LEFT JOIN cw_project_report c
				on b.report_id = c.id
				LEFT JOIN cw_project_records d
				on c.project_id = d.id
				LEFT JOIN sys_office e
        on c.office_id = e.id
				LEFT JOIN sys_user f
        on a.create_by_id = f.id
				LEFT JOIN sys_user g
        on d.project_master_id = g.id
        LEFT JOIN sys_user h
        on c.create_by_id = h.id
        left join cw_project_report_cancell_apply_archived cw_ar
        on a.id = cw_ar.archive_id and cw_ar.del_flag = 0
        WHERE a.del_flag = 0 AND a.id = #{id}
    </select>
    <select id="queryByArchivedId"
            resultType="com.jeeplus.finance.reportCancellApplyArchived.service.dto.ApplyArchiveReportDTO">
        select
        <include refid="Base_Column_List"></include>,
        cw_a.id as id,
        su.name as user_name,
        b.report_date,
        b.report_create_name,
        b.signature_annotator1_name,
        b.signature_annotator2_name,
        b.report_no
        from cw_project_report_cancell_apply cw_a
        left join cw_project_report_cancell_apply_archived cw_pa on cw_a.id = cw_pa.archive_id
        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.del_flag = '0' and cw_pa.id = #{id}
    </select>

    <select id="getNewLineId" resultType="java.lang.String">
        select id from cw_project_report_new_line where report_id = #{id}
    </select>
    <select id="getBfId" resultType="java.lang.String">
        select id from cw_project_report_cancell_apply where report_new_line_id = #{newLineId}
    </select>
    <select id="getInfoByBFID"
            resultType="com.jeeplus.finance.reportCancellApplyArchived.domain.CwProjectReportApplyArchive">
        select * from cw_project_report_cancell_apply_archived where archive_id = #{bfId}
    </select>
    <select id="getProjectByNewLineId" resultType="com.jeeplus.finance.projectRecords.service.dto.CwProjectRecordsDTO">
        select
            <include refid="Project_Column_List"></include>,
            su2.name as project_master_name,
            su3.name as real_header_name
        from cw_project_records cw
        left join cw_project_report cw_p on cw.id = cw_p.project_id
        left join cw_project_report_new_line cw_n on cw_p.id = cw_n.report_id
        left join sys_user su2 on su2.id = cw.project_master_id and su2.del_flag = '0'
        left join sys_user su3 on su3.id = cw.real_header and su3.del_flag = '0'
        where cw_n.id = #{reportNewLineId}
    </select>
    <sql id="Project_Column_List">
        cw.id,
        cw.create_by_id,
        cw.create_time,
        cw.update_by_id,
        cw.update_time,
        cw.del_flag,
        cw.remarks,
        cw.project_number,
        cw.project_name,
        cw.office_id,
        cw.project_money,
        cw.audit_year,
        cw.plan_start_date,
        cw.plan_end_date,
        cw.project_master_id,
        cw.project_leader_id,
        cw.proc_ins_id,
        cw.status,
        cw.process_definition_id,
        cw.contract_id,
        cw.business_type,
        cw.risk_level,
        cw.real_header,
        cw.report_type,
        cw.report_review
    </sql>

    <update id="updateInfoById">
        UPDATE cw_project_report_cancell_apply_archived
        SET
        <if test="remarks != null and remarks != ''">
            remarks = #{remarks},
        </if>
        <if test="projectId != null and projectId != ''">
            project_id = #{projectId},
        </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="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="applyFileType != null and applyFileType != ''">
            apply_file_type = #{applyFileType},
        </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="carchivedType != null and carchivedType != ''">
            carchived_type = #{carchivedType},
        </if>
        <if test="archiveId != null and archiveId != ''">
            archive_id = #{archiveId},
        </if>
		update_by_id = #{updateById},
		update_time = #{updateTime}
        WHERE
            id = #{id}
            AND del_flag = 0
    </update>

    <select id="getById" resultType="com.jeeplus.finance.reportCancellApplyArchived.domain.CwProjectReportApplyArchive">
        SELECT
            id,
            remarks,
            project_id,
            report_num,
            papers_num,
            YEAR,
            number,
            is_number,
            audit_date,
            proc_ins_id,
            process_definition_id,
            apply_file_type,
            file_number,
            report_id,
            report_remarks,
            carchived_type,
            archive_id,
            create_time,
            create_by_id,
            update_time,
            update_by_id,
            del_flag,
            tenant_id
        FROM
            cw_project_report_cancell_apply_archived
        WHERE
            id = #{id}
            AND del_flag = 0
    </select>
</mapper>
