<?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.reimbursement.reimbursementInfo.mapper.ReimbursementInfoMapper">

	<select id="findList" resultType="com.jeeplus.test.reimbursement.reimbursementInfo.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
			reimbursement_info a
			LEFT JOIN 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_
			${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>

	<update id="updateStatusById">
		UPDATE reimbursement_info SET type = #{type}
		<if test="type == '3'.toString">
			, proc_ins_id = NULL, process_definition_id = NULL
		</if>
	    WHERE id = #{id}
	</update>

	<select id="findOfficeList" resultType="com.jeeplus.test.reimbursement.reimbursementInfo.service.dto.TreeUserDto">
		SELECT id,`name`,parent_id FROM sys_office WHERE del_flag = 0
	</select>

	<select id="findUserList" resultType="com.jeeplus.test.reimbursement.reimbursementInfo.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 and a.is_admin is null
			<if test="name != null and name != ''">
				a.`name` LIKE CONCAT ('%', #{name}, '%')
			</if>
	</select>

	<select id="findExportList" resultType="com.jeeplus.test.reimbursement.reimbursementInfo.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
			reimbursement_info a
			LEFT JOIN 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_
			${ew.customSqlSegment}
		ORDER BY a.update_date DESC
	</select>

	<select id="relationReimbursementList" resultType="com.jeeplus.test.reimbursement.reimbursementInfo.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
			reimbursement_info a
			LEFT JOIN 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="findOfficeListByUserName"
			resultType="com.jeeplus.test.reimbursement.reimbursementInfo.service.dto.TreeUserDto">
		SELECT a.id,a.`name`,a.parent_id FROM sys_office a
		LEFT JOIN sys_user b
		on a.id = b.office_id
		WHERE a.del_flag = 0 and b.name = #{name}
	</select>
</mapper>
