<?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.reimbursementApproval.approvalInfo.mapper.CwReimbursementInfoMapper">
	<update id="updateStatusById">
		UPDATE cw_reimbursement_info SET type = #{type}
		WHERE id = #{id}
	</update>


	<select id="findList"
            resultType="com.jeeplus.test.cw.reimbursementApproval.approvalInfo.service.dto.RetureListDto">
        SELECT
		DISTINCT a.id,
			a.`no`,
			(CASE
			WHEN a.source_type = 1 THEN b.type_id
			WHEN a.source_type = 2 THEN b2.type_id
			WHEN a.source_type = 3 THEN b3.type_id
			WHEN a.source_type = 4 THEN b4.type_id
									ELSE b5.type_id END) AS type_id,
			(CASE
			WHEN a.source_type = 1 THEN t.name
			WHEN a.source_type = 2 THEN t2.name
			WHEN a.source_type = 3 THEN t3.name
			WHEN a.source_type = 4 THEN t4.name
									ELSE t5.name END) AS type_name,
			(CASE
			WHEN a.source_type = 1 THEN b.project_id
			WHEN a.source_type = 2 THEN b2.contract_id
			WHEN a.source_type = 3 THEN b3.project_id
			WHEN a.source_type = 4 THEN b4.project_id
									ELSE b5.project_id END) AS project_id,
			(CASE
			WHEN a.source_type = 1 THEN p.project_name
			WHEN a.source_type = 2 THEN contr2.contract_name
			WHEN a.source_type = 3 THEN p3.project_name
			WHEN a.source_type = 4 THEN b4.project_name
									ELSE b5.project_name END) AS project_name,
			(CASE
			WHEN a.source_type = 1 THEN b.report_number
			WHEN a.source_type = 2 THEN b2.report_number
			WHEN a.source_type = 3 THEN b3.report_number
			WHEN a.source_type = 4 THEN b4.report_number
									ELSE b5.report_number END) AS report_number,
			(CASE
			WHEN a.source_type = 1 THEN so.name
			WHEN a.source_type = 2 THEN so2.name
			WHEN a.source_type = 3 THEN so3.name
			WHEN a.source_type = 4 THEN so4.name
									ELSE so5.name END) AS dept_name,
			(CASE
			WHEN a.source_type = 1 THEN us.name
			WHEN a.source_type = 2 THEN us2.name
			WHEN a.source_type = 3 THEN us3.name
			WHEN a.source_type = 4 THEN us4.name
									ELSE us5.name END) AS name,
			(CASE
			WHEN a.source_type = 1 THEN b.`number`
			WHEN a.source_type = 2 THEN b2.`number`
			WHEN a.source_type = 3 THEN b3.`number`
			WHEN a.source_type = 4 THEN b4.`number`
									ELSE b5.`number` END) AS `number`,
			a.user_name,
			a.reim_date,
			a.type,
			a.proc_ins_id,
			a.process_definition_id,
			d.ID_ AS task_id,
			a.create_by as create_id,
			a.source_type,
			a.purchase_id,
			pru.purchase_no
		FROM
			cw_reimbursement_info a
			LEFT JOIN cw_reimbursement_detail_info b ON a.id = b.info_id AND b.del_flag = 0
			left join cw_reimbursement_type_info t on b.type_id = t.id and t.del_flag = 0
			left join cw_project_records p on p.id = b.project_id and p.del_flag = 0
			left join sys_office so on so.id = b.dept_id and so.del_flag = 0
			left join sys_user us on us.id = b.user_id and us.del_flag = 0

			LEFT JOIN cw_reimbursement_detail_info_contract b2 ON a.id = b2.info_id AND b2.del_flag = 0
			left join cw_reimbursement_type_info t2 on b2.type_id = t2.id and t2.del_flag = 0
			left join cw_work_contract_info contr2 on contr2.id = b2.contract_id and contr2.del_flag = 0
			left join sys_office so2 on so2.id = b2.dept_id and so2.del_flag = 0
			left join sys_user us2 on us2.id = b2.user_id and us2.del_flag = 0

			LEFT JOIN cw_reimbursement_detail_info_report b3 ON a.id = b3.info_id AND b3.del_flag = 0
			left join cw_reimbursement_type_info t3 on b3.type_id = t3.id and t3.del_flag = 0
			left join cw_project_records p3 on p3.id = b3.project_id and p3.del_flag = 0
			left join sys_office so3 on so3.id = b3.dept_id and so3.del_flag = 0
			left join sys_user us3 on us3.id = b3.user_id and us3.del_flag = 0

			LEFT JOIN cw_reimbursement_detail_info_other b4 ON a.id = b4.info_id AND b4.del_flag = 0
			left join cw_reimbursement_type_info t4 on b4.type_id = t4.id and t4.del_flag = 0
			left join cw_project_records p4 on p4.id = b4.project_id and p4.del_flag = 0
			left join sys_office so4 on so4.id = b4.dept_id and so4.del_flag = 0
			left join sys_user us4 on us4.id = b4.user_id and us4.del_flag = 0

			LEFT JOIN cw_reimbursement_detail_info_procured b5 ON a.id = b5.info_id AND b5.del_flag = 0
			left join cw_reimbursement_type_info t5 on b5.type_id = t5.id and t5.del_flag = 0
			left join cw_project_records p5 on p5.id = b5.project_id and p5.del_flag = 0
			left join sys_office so5 on so5.id = b5.dept_id and so5.del_flag = 0
			left join sys_user us5 on us5.id = b5.user_id and us5.del_flag = 0

			LEFT JOIN sys_user c ON a.create_by =  c.id
			left join sys_user_manage_office sumo on sumo.office_id = c.office_id
			LEFT JOIN act_ru_task d ON a.proc_ins_id = d.PROC_INST_ID_
			left join material_management_pruchase_request_basics pru on pru.id = a.purchase_id and pru.del_flag = '0'
			${ew.customSqlSegment}
		ORDER BY a.update_date DESC
    </select>
	<select id="findFiles" 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="findOfficeList"
			resultType="com.jeeplus.test.cw.reimbursementApproval.approvalInfo.service.dto.TreeUserDto">
		SELECT id,`name`,parent_id FROM sys_office WHERE del_flag = 0
	</select>
	<select id="findUserList"
			resultType="com.jeeplus.test.cw.reimbursementApproval.approvalInfo.service.dto.TreeUserDto">
		SELECT
		a.id,
		a.`name`,
		a.office_id AS parent_id,
		b.`name` AS office_name,
		true AS is_user
		FROM
		sys_user a
		LEFT JOIN sys_office b ON a.office_id = b.id
		WHERE
		a.del_flag = 0
		<if test="name != null and name != ''">
			and a.`name` LIKE CONCAT ('%', #{name}, '%')
		</if>
	</select>
	<select id="findExportList"
			resultType="com.jeeplus.test.cw.reimbursementApproval.approvalInfo.service.dto.RetureListDto">
		SELECT
			a.id,
			a.`no`,
			(CASE
			WHEN a.source_type = 1 THEN b.type_id
			WHEN a.source_type = 2 THEN b2.type_id
			WHEN a.source_type = 3 THEN b3.type_id
			WHEN a.source_type = 4 THEN b4.type_id
									ELSE b5.type_id END) AS type_id,
			(CASE
			WHEN a.source_type = 1 THEN t.name
			WHEN a.source_type = 2 THEN t2.name
			WHEN a.source_type = 3 THEN t3.name
			WHEN a.source_type = 4 THEN t4.name
									ELSE t5.name END) AS type_name,
			(CASE
			WHEN a.source_type = 1 THEN b.project_id
			WHEN a.source_type = 2 THEN b2.contract_id
			WHEN a.source_type = 3 THEN b3.project_id
			WHEN a.source_type = 4 THEN b4.project_id
									ELSE b5.project_id END) AS project_id,
			(CASE
			WHEN a.source_type = 1 THEN p.project_name
			WHEN a.source_type = 2 THEN contr2.contract_name
			WHEN a.source_type = 3 THEN p3.project_name
			WHEN a.source_type = 4 THEN b4.project_name
									ELSE b5.project_name END) AS project_name,
			(CASE
			WHEN a.source_type = 1 THEN b.report_number
			WHEN a.source_type = 2 THEN b2.report_number
			WHEN a.source_type = 3 THEN b3.report_number
			WHEN a.source_type = 4 THEN b4.report_number
									ELSE b5.report_number END) AS report_number,
			(CASE
			WHEN a.source_type = 1 THEN so.name
			WHEN a.source_type = 2 THEN so2.name
			WHEN a.source_type = 3 THEN so3.name
			WHEN a.source_type = 4 THEN so4.name
									ELSE so5.name END) AS dept_name,
			(CASE
			WHEN a.source_type = 1 THEN us.name
			WHEN a.source_type = 2 THEN us2.name
			WHEN a.source_type = 3 THEN us3.name
			WHEN a.source_type = 4 THEN us4.name
									ELSE us5.name END) AS name,
			(CASE
			WHEN a.source_type = 1 THEN b.`number`
			WHEN a.source_type = 2 THEN b2.`number`
			WHEN a.source_type = 3 THEN b3.`number`
			WHEN a.source_type = 4 THEN b4.`number`
									ELSE b5.`number` END) AS `number`,
			a.user_name,
			a.reim_date,
			a.type,
			a.proc_ins_id,
			a.process_definition_id,
			d.ID_ AS task_id,
			a.source_type
		FROM
			cw_reimbursement_info a
			LEFT JOIN cw_reimbursement_detail_info b ON a.id = b.info_id AND b.del_flag = 0
			left join cw_reimbursement_type_info t on b.type_id = t.id and t.del_flag = 0
			left join cw_project_records p on p.id = b.project_id and p.del_flag = 0
			left join sys_office so on so.id = b.dept_id and so.del_flag = 0
			left join sys_user us on us.id = b.user_id and us.del_flag = 0

			LEFT JOIN cw_reimbursement_detail_info_contract b2 ON a.id = b2.info_id AND b2.del_flag = 0
			left join cw_reimbursement_type_info t2 on b2.type_id = t2.id and t2.del_flag = 0
			left join cw_work_contract_info contr2 on contr2.id = b2.contract_id and contr2.del_flag = 0
			left join sys_office so2 on so2.id = b2.dept_id and so2.del_flag = 0
			left join sys_user us2 on us2.id = b2.user_id and us2.del_flag = 0

			LEFT JOIN cw_reimbursement_detail_info_report b3 ON a.id = b3.info_id AND b3.del_flag = 0
			left join cw_reimbursement_type_info t3 on b3.type_id = t3.id and t3.del_flag = 0
			left join cw_project_records p3 on p3.id = b3.project_id and p3.del_flag = 0
			left join sys_office so3 on so3.id = b3.dept_id and so3.del_flag = 0
			left join sys_user us3 on us3.id = b3.user_id and us3.del_flag = 0

			LEFT JOIN cw_reimbursement_detail_info_other b4 ON a.id = b4.info_id AND b4.del_flag = 0
			left join cw_reimbursement_type_info t4 on b4.type_id = t4.id and t4.del_flag = 0
			left join cw_project_records p4 on p4.id = b4.project_id and p4.del_flag = 0
			left join sys_office so4 on so4.id = b4.dept_id and so4.del_flag = 0
			left join sys_user us4 on us4.id = b4.user_id and us4.del_flag = 0

			LEFT JOIN cw_reimbursement_detail_info_procured b5 ON a.id = b5.info_id AND b5.del_flag = 0
			left join cw_reimbursement_type_info t5 on b5.type_id = t5.id and t5.del_flag = 0
			left join cw_project_records p5 on p5.id = b5.project_id and p5.del_flag = 0
			left join sys_office so5 on so5.id = b5.dept_id and so5.del_flag = 0
			left join sys_user us5 on us5.id = b5.user_id and us5.del_flag = 0

			LEFT JOIN sys_user c ON a.create_by =  c.id
			LEFT JOIN act_ru_task d ON a.proc_ins_id = d.PROC_INST_ID_
			${ew.customSqlSegment}
		ORDER BY a.update_date DESC
	</select>
	<select id="relationReimbursementList"
			resultType="com.jeeplus.test.cw.reimbursementApproval.approvalInfo.service.dto.RetureListDto">
		SELECT
			a.id,
			a.`no`,
			b.type_id,
			b.type_name,
			b.project_id,
			b.project_name,
			b.report_number,
			a.user_name,
			b.user_name AS `name`,
			b.dept_name,
			a.reim_date,
			b.`number`,
			a.type,
			a.proc_ins_id,
			a.process_definition_id,
			d.ID_ AS task_id
		FROM
			cw_reimbursement_info a
			LEFT JOIN cw_reimbursement_detail_info b ON a.id = b.info_id AND b.del_flag = 0
			LEFT JOIN sys_user c ON a.create_by =  c.id
			LEFT JOIN act_ru_task d ON a.proc_ins_id = d.PROC_INST_ID_
		WHERE
			a.del_flag = 0
			AND a.project_id IN (SELECT id FROM program_project_list_info WHERE del_flag = 0 AND contract_id = #{id})
		ORDER BY a.update_date DESC
	</select>
    <select id="findProjectList"
            resultType="com.jeeplus.test.program.configuration.projectList.domain.ProgramProjectListInfo">
		SELECT
           a.id,
           b.name AS create_by,
           b.office_id,
           a.create_date,
           a.update_by,
           a.update_date,
           a.del_flag,
           a.contract_id,
           a.contract_name,
           a.client,
           a.client_name,
           a.amount,
           a.contract_type,
           a.name,
           a.no,
           a.project_type,
           a.approval_no,
           a.company,
           a.project_mould,
           a.property_holder,
           a.property_holder_name,
           a.location,
           a.is_first,
           a.year_num,
           a.check_year,
           a.project_manager,
           c.name AS project_manager_name,
           a.report_type,
           a.industry,
           a.enterprise_type,
           a.risk_level,
           a.project_source,
           a.estimate,
           a.plan_end_time,
           a.use_num,
           a.appointment,
           a.work_hours,
           a.remarks,
           a.assessment_enterprise,
           a.assessment_enterprise_name,
           a.link_num,
           a.relationship,
           a.assessment_way,
           a.assessment_objective,
           a.assessment_date,
           a.num,
           a.project_development,
           a.delegate_project_type,
           a.assessment_object,
           a.work_begin_date,
           a.work_end_date,
           a.reporting_date,
           a.is_have,
           a.is_influence,
           a.mode,
           a.status,
           a.proc_ins_id,
           a.process_definition_id,
           prn.report_no,
           pa1.status as status1,
           pa1.id as auditId1,
           pa1.proc_ins_id as procInsId1,
           pa2.status as status2,
           pa2.id as auditId2,
           pa2.proc_ins_id as procInsId2,
           pa3.status as status3,
           pa3.id as auditId3,
           pa3.proc_ins_id as procInsId3,
           pa.status as archive_status,
           pa.id as archive_id,
           pa.proc_ins_id as procInsIdArchive,
           d.id AS issued_id,
           d.proc_ins_id AS procInsId4,
           d.status AS issued_status,
           wci.no AS contract_no
        FROM
            program_project_list_info a
            LEFT JOIN sys_user b ON a.create_by = b.id
            LEFT JOIN sys_user c ON a.project_manager = c.id
            LEFT JOIN work_contract_info wci ON wci.id = a.contract_id and wci.del_flag = '0'
            LEFT JOIN program_report_no prn ON prn.program_id = a.id and prn.del_flag = '0'
            LEFT JOIN program_audit pa1 ON pa1.program_id = a.id and pa1.audit_level = '1' and pa1.del_flag = '0'
            LEFT JOIN program_audit pa2 ON pa2.program_id = a.id and pa2.audit_level = '2' and pa2.del_flag = '0'
            LEFT JOIN program_audit pa3 ON pa3.program_id = a.id and pa3.audit_level = '3' and pa3.del_flag = '0'
            LEFT JOIN program_archive pa ON pa.program_id = a.id and pa.del_flag = '0'
            LEFT JOIN proofread_issued d ON a.id = d.project_id
            ${ew.customSqlSegment}
            ORDER BY a.update_date DESC
	</select>
	<select id="selectInvoiceFlag" resultType="java.lang.String">
		select
         case
             when ff.status = '1' then '开票中'
             when ff.status = '2' then '开票中'
             when ff.status = '3' then '开票中'
             when ff.status = '4' then '开票中'
             when ff.status = '5' then '已开票'
             when ff.status = '6' then '已开票'
             when ff.status = '7' then '已开票'
             when ff.status = '8' then '已开票'
         else '' end
        from
        (select
        fib.program_name as program_name,
        fi.`status` as status,
        fib.program_id as program_id,
        fi.create_date as create_date
        from finance_invoice_base fib
        inner join finance_invoice fi on fi.id = fib.invoice_id and fi.del_flag = '0'
        where fib.del_flag = '0' and fi.status != '9'
        order by fi.create_date desc) ff
        where ff.program_id = #{id}
        limit 1
	</select>
	<select id="getReportNoList"
			resultType="com.jeeplus.test.cw.reimbursementApproval.approvalInfo.service.dto.ReportNoDto">
		select report_no,c.project_name from cw_project_report_new_line a
		left join cw_project_report b
		on a.report_id = b.id
		left join cw_project_records c
		on b.project_id = c.id
		${ew.customSqlSegment}
		ORDER BY a.update_date DESC
	</select>

	<select id="getContractDetailList" resultType="com.jeeplus.test.cw.reimbursementApproval.approvalInfo.domain.CwReimbursementDetailInfoContract">
		select
		a.id,
		a.create_by,
		a.create_date,
		a.del_flag,
		a.info_id,
		a.user_id,
		a.dept_id,
		a.type_id,
		a.contract_id,
		a.report_number,
		a.number,
		a.receipt_number,
		a.days,
		a.content,
		su.name as user_name,
		so.name as dept_name,
		cw_type.name as type_name,
		cw_contract.contract_name as contract_name
		from cw_reimbursement_detail_info_contract a
		left join sys_user su on a.user_id = su.id and su.del_flag = '0'
		left join sys_office so on a.dept_id = so.id and so.del_flag = '0'
		left join cw_reimbursement_type_info cw_type on cw_type.id = a.type_id and cw_type.del_flag = '0'
		left join cw_work_contract_info cw_contract on cw_contract.id = a.contract_id and cw_contract.del_flag = '0'
		where a.del_flag = '0' and a.info_id = #{id}
		order by a.number asc
	</select>

	<select id="getReportDetailList" resultType="com.jeeplus.test.cw.reimbursementApproval.approvalInfo.domain.CwReimbursementDetailInfoReport">
		select
		a.id,
		a.create_by,
		a.create_date,
		a.del_flag,
		a.info_id,
		a.user_id,
		a.dept_id,
		a.type_id,
		a.report_id,
		a.report_number,
		a.number,
		a.receipt_number,
		a.days,
		a.content,
		su.name as user_name,
		so.name as dept_name,
		cw_type.name as type_name,
		cw_report.document_no as document_no,
		cw_project.project_name as project_name,
		a.project_id
		from cw_reimbursement_detail_info_report a
		left join sys_user su on a.user_id = su.id and su.del_flag = '0'
		left join sys_office so on a.dept_id = so.id and so.del_flag = '0'
		left join cw_reimbursement_type_info cw_type on cw_type.id = a.type_id and cw_type.del_flag = '0'
		left join cw_project_report cw_report on cw_report.id = a.report_id and cw_report.del_flag = '0'
		left join cw_project_records cw_project on cw_project.id = a.project_id and cw_project.del_flag = '0'
		where a.del_flag = '0' and a.info_id = #{id}
		order by a.number asc
	</select>

	<select id="getDetailList" resultType="com.jeeplus.test.cw.reimbursementApproval.approvalInfo.domain.CwReimbursementDetailInfo">
		select
		a.id,
		a.create_by,
		a.create_date,
		a.del_flag,
		a.info_id,
		a.user_id,
		a.dept_id,
		a.type_id,
		a.project_id,
		a.report_number,
		a.number,
		a.receipt_number,
		a.days,
		a.content,
		su.name as user_name,
		so.name as dept_name,
		cw_type.name as type_name,
		cw_project.project_name as project_name
		from cw_reimbursement_detail_info a
		left join sys_user su on a.user_id = su.id and su.del_flag = '0'
		left join sys_office so on a.dept_id = so.id and so.del_flag = '0'
		left join cw_reimbursement_type_info cw_type on cw_type.id = a.type_id and cw_type.del_flag = '0'
		left join cw_project_records cw_project on cw_project.id = a.project_id and cw_project.del_flag = '0'
		where a.del_flag = '0' and a.info_id = #{id}
		order by a.number asc
	</select>

	<select id="getOtherDetailList" resultType="com.jeeplus.test.cw.reimbursementApproval.approvalInfo.domain.CwReimbursementDetailInfoOther">
		select
		a.id,
		a.create_by,
		a.create_date,
		a.del_flag,
		a.info_id,
		a.user_id,
		a.dept_id,
		a.type_id,
		a.project_id,
		a.report_number,
		a.number,
		a.receipt_number,
		a.days,
		a.content,
		su.name as user_name,
		so.name as dept_name,
		cw_type.name as type_name,
		a.project_name as project_name
		from cw_reimbursement_detail_info_other a
		left join sys_user su on a.user_id = su.id and su.del_flag = '0'
		left join sys_office so on a.dept_id = so.id and so.del_flag = '0'
		left join cw_reimbursement_type_info cw_type on cw_type.id = a.type_id and cw_type.del_flag = '0'
		left join cw_project_records cw_project on cw_project.id = a.project_id and cw_project.del_flag = '0'
		where a.del_flag = '0' and a.info_id = #{id}
		order by a.number asc
	</select>

	<select id="getProcuredDetailList" resultType="com.jeeplus.test.cw.reimbursementApproval.approvalInfo.domain.CwReimbursementDetailInfoProcured">
		select
		a.id,
		a.create_by,
		a.create_date,
		a.del_flag,
		a.info_id,
		a.user_id,
		a.dept_id,
		a.type_id,
		a.project_id,
		a.report_number,
		a.number,
		a.receipt_number,
		a.days,
		a.content,
		su.name as user_name,
		so.name as dept_name,
		cw_type.name as type_name,
		a.project_name as project_name
		from cw_reimbursement_detail_info_procured a
		left join sys_user su on a.user_id = su.id and su.del_flag = '0'
		left join sys_office so on a.dept_id = so.id and so.del_flag = '0'
		left join cw_reimbursement_type_info cw_type on cw_type.id = a.type_id and cw_type.del_flag = '0'
		left join cw_project_records cw_project on cw_project.id = a.project_id and cw_project.del_flag = '0'
		where a.del_flag = '0' and a.info_id = #{id}
		order by a.number asc
	</select>
</mapper>
