<?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}
		<if test="type == '3'.toString">
			, proc_ins_id = NULL, process_definition_id = NULL
		</if>
		WHERE id = #{id}
	</update>


	<select id="findList"
            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_
			${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 != ''">
			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`,
			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_
			${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>
</mapper>
