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

	<sql id="Base_Column_List">
        cw_pa.create_by,
        cw_pa.create_date,
        cw_pa.update_by,
        cw_pa.update_date,
        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.status,
        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_prnl.report_no,
        cw_prnl.report_date,
        cw_pr.signature_annotator1,
        cw_pr.signature_annotator2,
        cw_prnl.create_by,
        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 set apply_file_type = #{status} where id = #{id}
    </update>
    <update id="updateStatusByArchiveId">
        update cw_project_report_cancell_apply_archived set status = #{status} where archive_id = #{id}
    </update>
    <delete id="deleteFileInfo">
        delete from work_attachment where attachment_id = #{attachmentId}
    </delete>

    <select id="getList" resultType="com.jeeplus.test.cw.reportCancellApplyArchived.service.dto.ArchivedReportInfoDto">
        SELECT
        cw_ar.create_by as createId,
        a.id,
        a.create_by as createById,
        a.create_date,
        a.update_by,
        a.update_date,
        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 = f.id
				LEFT JOIN sys_user g
        on d.project_master_id = g.id
        LEFT JOIN sys_user h
        on c.create_by = h.id
        left join cw_project_report_cancell_apply_archived cw_ar
        on a.id = cw_ar.archive_id
        ${ew.customSqlSegment}
    </select>
    <select id="queryById"
            resultType="com.jeeplus.test.cw.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 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 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_a.del_flag = '0' and cw_a.id = #{id}
	</select>
    <select id="selectByApplyId"
            resultType="com.jeeplus.test.cw.reportCancellApplyArchived.domain.CwProjectReportApplyArchive">
        select * from cw_project_report_cancell_apply_archived where archive_id = #{id}
    </select>
    <select id="findList"
            resultType="com.jeeplus.test.oss.domain.WorkAttachment">
        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.test.cw.reportCancellApplyArchived.service.dto.CwWorkAttachmentArchiveDto">
        select attachment_name as name,url,create_by as userId,create_date,remarks,type as fileType,file_size as size from work_attachment where attachment_id = #{id}
    </select>
    <select id="selectByArchiveId"
            resultType="com.jeeplus.test.cw.reportCancellApplyArchived.domain.CwProjectReportApplyArchive">
        select * from cw_project_report_cancell_apply_archived where archive_id = #{id}
    </select>
    <select id="selectProjectInfo"
            resultType="com.jeeplus.test.cw.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.test.cw.reportCancellApplyArchived.service.dto.ApplyArchiveReportDTO">
        SELECT
        cw_ar.create_by as createId,
        a.id,
        a.create_by as createById,
        a.create_date,
        a.update_by,
        a.update_date,
        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 = f.id
				LEFT JOIN sys_user g
        on d.project_master_id = g.id
        LEFT JOIN sys_user h
        on c.create_by = 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.test.cw.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 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 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>
</mapper>
