<?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.business.reimbursement.mapper.JyReimbursementInfoMapper">

    <select id="findPageList"
            resultType="com.jeeplus.business.reimbursement.service.dto.JyReimbursementInfoDTO">
        SELECT
		DISTINCT a.id,
			a.`no`,
			(CASE
			WHEN a.apply_type = 1 THEN b.type_id
			WHEN a.apply_type = 2 THEN b2.type_id
			WHEN a.apply_type = 3 THEN b3.type_id
									ELSE b4.type_id END) AS type_id,
			(CASE
			WHEN a.apply_type = 1 THEN b.type_name
			WHEN a.apply_type = 2 THEN b2.type_name
			WHEN a.apply_type = 3 THEN b3.type_name
									ELSE b4.type_name END) AS type_name,
			(CASE
			WHEN a.apply_type = 1 THEN b.project_id
			WHEN a.apply_type = 2 THEN b2.project_id
			WHEN a.apply_type = 3 THEN b3.project_id
									ELSE b4.project_id END) AS project_id,
			(CASE
			WHEN a.apply_type = 1 THEN b.project_name
			WHEN a.apply_type = 2 THEN b2.project_name
			WHEN a.apply_type = 3 THEN b3.project_name
									ELSE b4.project_name END) AS project_name,
			(CASE
			WHEN a.apply_type = 1 THEN b.report_number
			WHEN a.apply_type = 2 THEN b2.report_number
			WHEN a.apply_type = 3 THEN b3.report_number
									ELSE b4.report_number END) AS report_number,
			(CASE
			WHEN a.apply_type = 1 THEN so.name
			WHEN a.apply_type = 2 THEN so2.name
			WHEN a.apply_type = 3 THEN so3.name
									ELSE so4.name END) AS dept_name,
			(CASE
			WHEN a.apply_type = 1 THEN us.name
			WHEN a.apply_type = 2 THEN us2.name
			WHEN a.apply_type = 3 THEN us3.name
									ELSE us4.name END) AS name,
			(CASE
			WHEN a.apply_type = 1 THEN trim(b.`number`)
			WHEN a.apply_type = 2 THEN trim(b2.`number`)
			WHEN a.apply_type = 3 THEN trim(b3.`number`)
									ELSE trim(b4.`number`) END) AS `number`,
			c.name as createName,
			o.name as officeName,
			a.create_time,
			a.status,
			(select dv.label from sys_dict_value dv LEFT JOIN sys_dict_type dt on dt.id=dv.dict_type_id where dv.value=a.status and dt.type='program_project_list_info_status') as infoStatus,
			a.proc_ins_id,
			a.process_definition_id,
			d.ID_ AS task_id,
			a.create_by_id as createById,
			a.apply_type
		FROM
			jy_reimbursement_info a

			LEFT JOIN jy_reimbursement_detail_info_borrow b ON a.id = b.info_id AND b.del_flag = 0
			left join jy_reimbursement_type_info t on b.type_id = t.id and t.del_flag = 0
			left join jy_borrow_money 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 jy_reimbursement_detail_info_payment b2 ON a.id = b2.info_id AND b2.del_flag = 0
			left join jy_reimbursement_type_info t2 on b2.type_id = t2.id and t2.del_flag = 0
			left join jy_payment p2 on p2.id = b2.project_id and p2.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 jy_reimbursement_detail_info_other b3 ON a.id = b3.info_id AND b3.del_flag = 0
			left join jy_reimbursement_type_info t3 on b3.type_id = t3.id and t3.del_flag = 0
			left join jy_project_record 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 jy_reimbursement_detail_info_project b4 ON a.id = b4.info_id AND b4.del_flag = 0
			left join jy_reimbursement_type_info t4 on b4.type_id = t4.id and t4.del_flag = 0
			left join jy_project_record 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 sys_user c ON a.create_by_id =  c.id
			left join sys_user_manage_office sumo on sumo.office_id = c.office_id
			left join sys_office o on o.id = c.office_id and o.del_flag = 0
			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="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="findById" resultType="com.jeeplus.business.reimbursement.domain.JyReimbursementInfo">
		select
			a.id,
			a.no,
			a.apply_type,
			a.proc_ins_id,
			a.process_definition_id,
			a.remarks,
			a.create_time,
			a.status,
			c.id as userId,
			c.name as createName,
			o.id as department,
			o.name as departmentName
		from jy_reimbursement_info a
		LEFT JOIN sys_user c ON a.create_by_id = c.id
		left join sys_office o on o.id = c.office_id and o.del_flag = 0
		where a.id = #{id}
	</select>

	<select id="findFiles" resultType="com.jeeplus.sys.domain.WorkAttachmentInfo">
		SELECT
			id,
			url,
			attachment_name AS `name`,
			create_by_id AS `by`,
			create_time
		FROM
			work_attachment
		WHERE
			del_flag = 0
			AND attachment_id = #{id}
	</select>

	<select id="getOtherDetailList" resultType="com.jeeplus.business.reimbursement.domain.JyReimbursementDetailInfoOther">
		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.receipt_number,
		a.days,
		a.content,
		a.report_number,
		a.number,
		su.name as user_name,
		so.name as dept_name,
		jy_type.name as type_name,
		a.project_name as project_name
		from jy_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 jy_reimbursement_type_info jy_type on jy_type.id = a.type_id and jy_type.del_flag = '0'
		where a.del_flag = '0' and a.info_id = #{id}
		order by a.number asc
	</select>

	<select id="getBorrowDetailList" resultType="com.jeeplus.business.reimbursement.domain.JyReimbursementDetailInfoBorrow">
		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.money_lower,
		su.name as user_name,
		so.name as dept_name,
		jy_type.name as type_name,
		a.project_name as project_name
		from jy_reimbursement_detail_info_borrow 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 jy_reimbursement_type_info jy_type on jy_type.id = a.type_id and jy_type.del_flag = '0'
		left join jy_borrow_money bm on a.project_id = bm.id and bm.del_flag = '0'
		where a.del_flag = '0' and a.info_id = #{id}
		order by a.number asc
	</select>

	<select id="getPaymentDetailList" resultType="com.jeeplus.business.reimbursement.domain.JyReimbursementDetailInfoPayment">
		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.payment_lower,
		su.name as user_name,
		so.name as dept_name,
		jy_type.name as type_name,
		a.project_name as project_name
		from jy_reimbursement_detail_info_payment 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 jy_reimbursement_type_info jy_type on jy_type.id = a.type_id and jy_type.del_flag = '0'
		left join jy_payment jp on a.project_id = jp.id and jp.del_flag = '0'
		where a.del_flag = '0' and a.info_id = #{id}
		order by a.number asc
	</select>

	<select id="getProjectDetailList" resultType="com.jeeplus.business.reimbursement.domain.JyReimbursementDetailInfoProject">
		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.receipt_number,
		a.days,
		a.content,
		a.number,
		su.name as user_name,
		so.name as dept_name,
		jy_type.name as type_name,
		pr.name as project_name
		from jy_reimbursement_detail_info_project 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 jy_reimbursement_type_info jy_type on jy_type.id = a.type_id and jy_type.del_flag = '0'
		left join jy_project_record pr on a.project_id = pr.id and pr.del_flag = '0'
		where a.del_flag = '0' and a.info_id = #{id}
		order by a.number asc
	</select>

	<update id="updateStatusById">
		UPDATE jy_reimbursement_info SET status = #{status}
		WHERE id = #{id}
	</update>

	<select id="queryByProjectId"
			resultType="com.jeeplus.business.reimbursement.service.dto.JyReimbursementInfoDTO">
        SELECT
		DISTINCT a.id,
			a.`no`,
			(CASE
			WHEN a.apply_type = 1 THEN b.type_id
			WHEN a.apply_type = 2 THEN b2.type_id
			WHEN a.apply_type = 3 THEN b3.type_id
									ELSE b4.type_id END) AS type_id,
			(CASE
			WHEN a.apply_type = 1 THEN b.type_name
			WHEN a.apply_type = 2 THEN b2.type_name
			WHEN a.apply_type = 3 THEN b3.type_name
									ELSE b4.type_name END) AS type_name,
			(CASE
			WHEN a.apply_type = 1 THEN b.project_id
			WHEN a.apply_type = 2 THEN b2.project_id
			WHEN a.apply_type = 3 THEN b3.project_id
									ELSE b4.project_id END) AS project_id,
			(CASE
			WHEN a.apply_type = 1 THEN b.project_name
			WHEN a.apply_type = 2 THEN b2.project_name
			WHEN a.apply_type = 3 THEN b3.project_name
									ELSE b4.project_name END) AS project_name,
			(CASE
			WHEN a.apply_type = 1 THEN b.report_number
			WHEN a.apply_type = 2 THEN b2.report_number
			WHEN a.apply_type = 3 THEN b3.report_number
									ELSE b4.report_number END) AS report_number,
			(CASE
			WHEN a.apply_type = 1 THEN so.name
			WHEN a.apply_type = 2 THEN so2.name
			WHEN a.apply_type = 3 THEN so3.name
									ELSE so4.name END) AS dept_name,
			(CASE
			WHEN a.apply_type = 1 THEN us.name
			WHEN a.apply_type = 2 THEN us2.name
			WHEN a.apply_type = 3 THEN us3.name
									ELSE us4.name END) AS name,
			(CASE
			WHEN a.apply_type = 1 THEN trim(b.`number`)
			WHEN a.apply_type = 2 THEN trim(b2.`number`)
			WHEN a.apply_type = 3 THEN trim(b3.`number`)
									ELSE trim(b4.`number`) END) AS `number`,
			c.name as createName,
			o.name as officeName,
			a.create_time,
			a.status,
			(select dv.label from sys_dict_value dv LEFT JOIN sys_dict_type dt on dt.id=dv.dict_type_id where dv.value=a.status and dt.type='program_project_list_info_status') as infoStatus,
			a.proc_ins_id,
			a.process_definition_id,
			d.ID_ AS task_id,
			a.create_by_id as createById,
			a.apply_type
		FROM
			jy_reimbursement_info a

			LEFT JOIN jy_reimbursement_detail_info_borrow b ON a.id = b.info_id AND b.del_flag = 0
			left join jy_reimbursement_type_info t on b.type_id = t.id and t.del_flag = 0
			left join jy_borrow_money 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 jy_reimbursement_detail_info_payment b2 ON a.id = b2.info_id AND b2.del_flag = 0
			left join jy_reimbursement_type_info t2 on b2.type_id = t2.id and t2.del_flag = 0
			left join jy_payment p2 on p2.id = b2.project_id and p2.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 jy_reimbursement_detail_info_other b3 ON a.id = b3.info_id AND b3.del_flag = 0
			left join jy_reimbursement_type_info t3 on b3.type_id = t3.id and t3.del_flag = 0
			left join jy_project_record 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 jy_reimbursement_detail_info_project b4 ON a.id = b4.info_id AND b4.del_flag = 0
			left join jy_reimbursement_type_info t4 on b4.type_id = t4.id and t4.del_flag = 0
			left join jy_project_record 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 sys_user c ON a.create_by_id =  c.id
			left join sys_user_manage_office sumo on sumo.office_id = c.office_id
			left join sys_office o on o.id = c.office_id and o.del_flag = 0
			LEFT JOIN act_ru_task d ON a.proc_ins_id = d.PROC_INST_ID_
			where (find_in_set (#{projectId},b4.project_id) or find_in_set (#{projectId},b2.project_id))and a.del_flag = '0'
		ORDER BY a.update_time DESC
    </select>

	<select id="getById" resultType="com.jeeplus.business.reimbursement.service.dto.JyReimbursementInfoDTO">
		select distinct
			a.id,
			a.`no`,
			(CASE
			WHEN a.apply_type = 1 THEN b.type_id
			WHEN a.apply_type = 2 THEN b2.type_id
			WHEN a.apply_type = 3 THEN b3.type_id
									ELSE b4.type_id END) AS type_id,
			(CASE
			WHEN a.apply_type = 1 THEN b.type_name
			WHEN a.apply_type = 2 THEN b2.type_name
			WHEN a.apply_type = 3 THEN b3.type_name
									ELSE b4.type_name END) AS type_name,
			(CASE
			WHEN a.apply_type = 1 THEN b.project_id
			WHEN a.apply_type = 2 THEN b2.project_id
			WHEN a.apply_type = 3 THEN b3.project_id
									ELSE b4.project_id END) AS project_id,
			(CASE
			WHEN a.apply_type = 1 THEN b.project_name
			WHEN a.apply_type = 2 THEN b2.project_name
			WHEN a.apply_type = 3 THEN b3.project_name
									ELSE b4.project_name END) AS project_name,
			(CASE
			WHEN a.apply_type = 1 THEN b.report_number
			WHEN a.apply_type = 2 THEN b2.report_number
			WHEN a.apply_type = 3 THEN b3.report_number
									ELSE b4.report_number END) AS report_number,
			(CASE
			WHEN a.apply_type = 1 THEN so.name
			WHEN a.apply_type = 2 THEN so2.name
			WHEN a.apply_type = 3 THEN so3.name
									ELSE so4.name END) AS dept_name,
			(CASE
			WHEN a.apply_type = 1 THEN us.name
			WHEN a.apply_type = 2 THEN us2.name
			WHEN a.apply_type = 3 THEN us3.name
									ELSE us4.name END) AS name,
			(CASE
			WHEN a.apply_type = 1 THEN trim(b.`number`)
			WHEN a.apply_type = 2 THEN trim(b2.`number`)
			WHEN a.apply_type = 3 THEN trim(b3.`number`)
									ELSE trim(b4.`number`) END) AS `number`,
			c.name as createName,
			o.name as officeName,
			a.create_time,
			a.status,
			(select dv.label from sys_dict_value dv LEFT JOIN sys_dict_type dt on dt.id=dv.dict_type_id where dv.value=a.status and dt.type='program_project_list_info_status') as infoStatus,
			a.proc_ins_id,
			a.process_definition_id,
			d.ID_ AS task_id,
			a.create_by_id as createById,
			a.apply_type
		FROM
			jy_reimbursement_info a

			LEFT JOIN jy_reimbursement_detail_info_borrow b ON a.id = b.info_id AND b.del_flag = 0
			left join jy_reimbursement_type_info t on b.type_id = t.id and t.del_flag = 0
			left join jy_borrow_money 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 jy_reimbursement_detail_info_payment b2 ON a.id = b2.info_id AND b2.del_flag = 0
			left join jy_reimbursement_type_info t2 on b2.type_id = t2.id and t2.del_flag = 0
			left join jy_payment p2 on p2.id = b2.project_id and p2.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 jy_reimbursement_detail_info_other b3 ON a.id = b3.info_id AND b3.del_flag = 0
			left join jy_reimbursement_type_info t3 on b3.type_id = t3.id and t3.del_flag = 0
			left join jy_project_record 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 jy_reimbursement_detail_info_project b4 ON a.id = b4.info_id AND b4.del_flag = 0
			left join jy_reimbursement_type_info t4 on b4.type_id = t4.id and t4.del_flag = 0
			left join jy_project_record 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 sys_user c ON a.create_by_id =  c.id
			left join sys_user_manage_office sumo on sumo.office_id = c.office_id
			left join sys_office o on o.id = c.office_id and o.del_flag = 0
			LEFT JOIN act_ru_task d ON a.proc_ins_id = d.PROC_INST_ID_
			where b2.id = #{id} and a.del_flag = '0'
	</select>


</mapper>