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


	<select id="findList"
            resultType="com.jeeplus.ccpm.approvalInfo.service.dto.CcpmRetureListDto">
        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 = 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_id AS create_id,
			a.source_type,
			a.purchase_id,
			a.reimbursement_type,
			a.payment_status,
			a.payment_time,
			pru.purchase_no,
                         a.update_time,
                         a.approval_time
		FROM
			ccpm_reimbursement_info a
			LEFT JOIN ccpm_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 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 ccpm_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 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 ccpm_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 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 ccpm_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 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 ccpm_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 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_id = c.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_time DESC
    </select>
	<select id="findFiles" resultType="com.jeeplus.sys.domain.WorkAttachmentInfo">
		SELECT
			id,
			url,
			attachment_name AS `name`,
			create_by_id AS `by`,
			attachment_flag AS `attachmentFlag`,
			create_time
		FROM
			work_attachment
		WHERE
			del_flag = 0
			AND attachment_id = #{id}
	</select>
	<select id="findReimFiles" resultType="com.jeeplus.sys.domain.WorkAttachmentInfo">
		SELECT
			id,
			url,
			type,
			file_size as `fileSize`,
			attachment_name AS `name`,
			create_by_id AS `by`,
			attachment_flag AS `attachmentFlag`,
			attachment_id AS `attachmentId`,
			create_time
		FROM
			work_attachment
		WHERE
			del_flag = 0
			AND attachment_id = #{infoId} and attachment_flag = #{attachmentFlag}
	</select>

	<select id="findOfficeList"
			resultType="com.jeeplus.ccpm.approvalInfo.service.dto.CcpmTreeUserDto">
		SELECT id,`name`,parent_id FROM sys_office WHERE del_flag = 0
	</select>
	<select id="findUserList"
			resultType="com.jeeplus.ccpm.approvalInfo.service.dto.CcpmTreeUserDto">
		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.ccpm.approvalInfo.service.dto.CcpmRetureListDto">
		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
			ccpm_reimbursement_info a
			LEFT JOIN ccpm_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 zs_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 ccpm_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 ccpm_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 ccpm_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 ccpm_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 ccpm_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 ccpm_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 ccpm_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 ccpm_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_id =  c.id
			LEFT JOIN act_ru_task d ON a.proc_ins_id = d.PROC_INST_ID_
			${ew.customSqlSegment}
		ORDER BY a.update_time DESC
	</select>
	<select id="relationReimbursementList"
			resultType="com.jeeplus.ccpm.approvalInfo.service.dto.CcpmRetureListDto">
		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
			ccpm_reimbursement_info a
			LEFT JOIN ccpm_reimbursement_detail_info b ON a.id = b.info_id AND b.del_flag = 0
			LEFT JOIN sys_user c ON a.create_by_id =  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_time DESC
	</select>
    <select id="findProjectList"
            resultType="com.jeeplus.ccpm.approvalInfo.domain.CcpmProgramProjectListInfo">
		SELECT
           a.id,
           b.name AS create_by_id,
           b.office_id,
           a.create_time,
           a.update_by_id,
           a.update_time,
           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_id = 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_time 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_time as create_time
        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_time desc) ff
        where ff.program_id = #{id}
        limit 1
	</select>
	<select id="getReportNoList"
			resultType="com.jeeplus.ccpm.approvalInfo.service.dto.CcpmReportNoDto">
		select report_no,c.project_name from ccpm_project_report_new_line a
		left join ccpm_project_report b
		on a.report_id = b.id
		left join ccpm_project_records c
		on b.project_id = c.id
		${ew.customSqlSegment}
		ORDER BY a.update_time DESC
	</select>

	<select id="getContractDetailList" resultType="com.jeeplus.ccpm.approvalInfo.domain.CcpmReimbursementDetailInfoContract">
		select
		a.id,
		a.create_by_id,
		a.create_time,
		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,
		zs_type.name as type_name
		from ccpm_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 zs_type on zs_type.id = a.type_id and zs_type.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.ccpm.approvalInfo.domain.CcpmReimbursementDetailInfoReport">
		select
		a.id,
		a.create_by_id,
		a.create_time,
		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,
		zs_type.name as type_name,
		a.project_id
		from ccpm_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 zs_type on zs_type.id = a.type_id and zs_type.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.ccpm.approvalInfo.domain.CcpmReimbursementDetailInfo">
		select
		a.id,
		a.create_by_id,
		a.create_time,
		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,
		zs_type.name as type_name
		from ccpm_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 zs_type on zs_type.id = a.type_id and zs_type.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.ccpm.approvalInfo.domain.CcpmReimbursementDetailInfoOther">
		select
		a.id,
		a.create_by_id,
		a.create_time,
		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,
		zs_type.name as type_name,
		a.project_name as project_name
		from ccpm_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 zs_type on zs_type.id = a.type_id and zs_type.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.ccpm.approvalInfo.domain.CcpmReimbursementDetailInfoProcured">
		select
		a.id,
		a.create_by_id,
		a.create_time,
		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,
		zs_type.name as type_name,
		a.project_name as project_name
		from ccpm_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 zs_type on zs_type.id = a.type_id and zs_type.del_flag = '0'
		where a.del_flag = '0' and a.info_id = #{id}
		order by a.number asc
	</select>

	<select id="getEffectiveDataByInvoiceNumber" resultType="java.lang.Integer">
		select count(a.id) as count from ccpm_reimbursement_amount_info a
		left join ccpm_reimbursement_info ri on ri.id = a.info_id
		<where>
			a.number = #{invoiceNumber} and a.del_flag = 0
			<if test="id != null and id != ''">
				and a.info_id != #{id}
			</if>
			and ri.del_flag = 0
			and (ri.type = '2' or ri.type = '5')
		</where>
	</select>

	<select id="getEffectiveDataByNumbers" resultType="string">
		select a.number as count from ccpm_reimbursement_amount_info a
		left join ccpm_reimbursement_info ri on ri.id = a.info_id
		<where>
			a.number in
			<foreach collection="invoiceNumber" item="number" index="index" open="(" close=")" separator=",">
				#{number}
			</foreach>
			and a.del_flag = 0
			<if test="id != null and id != ''">
				and a.info_id != #{id}
			</if>
			and ri.del_flag = 0
			and (ri.type = '2' or ri.type = '5')
		</where>
	</select>

	<select id="getEffectiveDataByNumbers2" resultType="string">
		SELECT IFNULL(GROUP_CONCAT(CAST(count AS CHAR) SEPARATOR ','), '') AS counts
		FROM (
		select a.number as count from ccpm_reimbursement_amount_info a
		left join ccpm_reimbursement_info ri on ri.id = a.info_id
		<where>
			a.number in
			<foreach collection="invoiceNumber" item="number" index="index" open="(" close=")" separator=",">
				#{number}
			</foreach>
			and a.del_flag = 0
			and ri.del_flag = 0
			and (ri.type = '2' or ri.type = '5') and a.info_id != #{id}
		</where>
		union all
		select a.number as count from reimbursement_amount_info a
		left join reimbursement_info ri on ri.id = a.info_id
		<where>
			a.number in
			<foreach collection="invoiceNumber" item="number" index="index" open="(" close=")" separator=",">
				#{number}
			</foreach>
			and a.del_flag = 0
			and ri.del_flag = 0
			and (ri.type = '2' or ri.type = '5') and a.info_id != #{id}
		</where>
		union all
		select a.number as count from cw_reimbursement_amount_info a
		left join cw_reimbursement_info ri on ri.id = a.info_id
		<where>
			a.number in
			<foreach collection="invoiceNumber" item="number" index="index" open="(" close=")" separator=",">
				#{number}
			</foreach>
			and a.del_flag = 0
			and ri.del_flag = 0
			and (ri.type = '2' or ri.type = '5') and a.info_id != #{id}
		</where>
		union all
		select a.number as count from zs_reimbursement_amount_info a
		left join zs_reimbursement_info ri on ri.id = a.info_id
		<where>
			a.number in
			<foreach collection="invoiceNumber" item="number" index="index" open="(" close=")" separator=",">
				#{number}
			</foreach>
			and a.del_flag = 0
			and ri.del_flag = 0
			and (ri.type = '2' or ri.type = '5') and a.info_id != #{id}
		</where>
		union all
		select a.number as count from consultancy_reimbursement_amount_info a
		left join consultancy_reimbursement_info ri on ri.id = a.info_id
		<where>
			a.number in
			<foreach collection="invoiceNumber" item="number" index="index" open="(" close=")" separator=",">
				#{number}
			</foreach>
			and a.del_flag = 0
			and ri.del_flag = 0
			and (ri.type = '2' or ri.type = '5') and a.info_id != #{id}
		</where>
		)AS result;

	</select>



	<select id="getInvoiceReimbursementAmountList" resultType="com.jeeplus.ccpm.approvalInfo.service.dto.CcpmReimbursementAmountInfoDTO">
		select
		a.id,
		a.create_by_id,
		a.create_time,
		a.update_by_id,
		a.update_time,
		a.del_flag,
		a.info_id,
		a.code,
		a.number,
		a.amount,
		a.tax_amount,
		a.count,
		a.tenant_id,
		a.reimbursement_type,
		a.invoice_type,
		a.invoice_project_name,
		date_format(a.invoice_date,'%Y-%m-%d %h:%i:%s') as 'invoiceDate',
		a.invoice_date,
		a.invoice_unit,
		a.buyer_name,
		a.remarks,
		ri.type as "status",
		date_format(ri.reim_date,'%Y-%m-%d') as 'reimDate',
		su.name as "reimbursementApplicantName",
		so.name as "officeName",
		sdv.label as "invoiceTypeStr"
		from
		ccpm_reimbursement_amount_info a
		left join ccpm_reimbursement_info ri on ri.id = a.info_id
		left join sys_user su on su.id = ri.create_by_id
		left join sys_office so on so.id = su.office_id
		left join sys_dict_value sdv on sdv.value = a.invoice_type
		left join sys_dict_type sdt on sdt.id = sdv.dict_type_id and sdt.type = 'invoice_reimbursement_type'
		<where>
			a.del_flag = 0
			and a.info_id in
			<foreach collection="idList" item="id" index="index" open="(" close=")" separator=",">
				#{id}
			</foreach>
		</where>
	</select>

	<select id="findById" resultType="com.jeeplus.ccpm.approvalInfo.domain.CcpmReimbursementInfo">
		SELECT
	id,
	user_id,
	user_name,
	NO,
	department,
	reim_date,
	remarks,
	type,
	proc_ins_id,
	process_definition_id,
	source_type,
	purchase_id,
	reimbursement_type,
	create_time,
	create_by_id,
	update_time,
	update_by_id,
	del_flag,
	tenant_id
FROM
	ccpm_reimbursement_info
WHERE
	id = #{id}
	AND del_flag = 0
	</select>

	<update id="updatePaymentById">
		update
			ccpm_reimbursement_info
		set payment_status = #{paymentStatus},
			payment_time = case
			when #{paymentStatus} = '1' then NOW()
			else null end
		where id = #{id}
	</update>


	<delete id="deleteByIdAndNumber">
		delete from ccpm_reimbursement_amount_info
		<where>
			number = #{invoiceNumber}
			and info_id = #{id}
		</where>
	</delete>
</mapper>
