<?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.assess.workContract.mapper.WorkContractInfoMapper">

	<select id="findPageList" resultType="com.jeeplus.assess.workContract.domain.WorkContractInfo">
		SELECT
			a.id,
			c.`name` AS create_by_id,
			a.create_time,
			a.update_by_id,
			a.update_time,
			a.del_flag,
			a.client_id,
			a.client_name,
			a.`no`,
			a.`name`,
			DATE_FORMAT(a.contract_date,'%Y-%m-%d') as contract_date,
			a.effective_date,
			a.closing_date,
			a.contract_type,
			a.contract_amount_type,
			a.contract_amount,
			a.contract_opposite,
			a.contract_fee,
			a.fees,
			a.describes,
			a.contract_special,
			a.remarks,
			a.filed_by,
			a.filed_date,
			d.filed_no,
			d.confirm_filed_no,
			a.filed_remarks,
			a.proc_ins_id,
			a.process_definition_id,
			a.status,
			f.borrow_type,
			b.ID_ as task_id,
			a.create_by_id as create_id,
			d.filed_type,
			e.ID_ as task_filed_id,
			g.ID_ as task_borrow_id
		FROM
			work_contract_info a
			LEFT JOIN sys_user c ON a.create_by_id = c.id
			LEFT JOIN act_ru_task b ON a.proc_ins_id = b.PROC_INST_ID_
			LEFT JOIN work_contract_file d ON a.id = d.contract_info_id
			LEFT JOIN act_ru_task e ON d.proc_ins_id = e.PROC_INST_ID_
			LEFT JOIN work_contract_borrow f ON a.id = f.contract_info_id
			LEFT JOIN act_ru_task g ON f.proc_ins_id = g.PROC_INST_ID_
		${ew.customSqlSegment}
		ORDER BY a.update_time DESC
	</select>

	<select id="findById" resultType="com.jeeplus.assess.workContract.domain.WorkContractInfo">
		SELECT
			a.id,
			a.client_id,
			a.client_name,
			a.`no`,
			a.`name`,
			a.contract_date,
			a.effective_date,
			a.closing_date,
			a.contract_type,
			a.contract_amount_type,
			a.contract_amount,
			a.contract_opposite,
			a.contract_fee,
			a.fees,
			a.describes,
			a.contract_special,
			a.remarks,
			a.filed_by,
			a.filed_date,
			a.filed_no,
			d.confirm_filed_no,
			a.filed_remarks,
			a.proc_ins_id,
			a.borrow_type,
			a.create_time,
			a.create_by_id,
			a.update_time,
			a.status,
			a.update_by_id,
			a.del_flag,
			d.id as contract_file_id,
			d.filed_type
		FROM
			work_contract_info a
			LEFT JOIN work_contract_file d ON a.id = d.contract_info_id
		WHERE
			a.id = #{id}
			AND a.del_flag = 0
	</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="findList" resultType="com.jeeplus.sys.domain.WorkAttachmentInfo">
		SELECT * FROM work_attachment WHERE del_flag = 0 AND attachment_id = #{id}
	</select>

	<select id="findDtos" 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="findByAttachmentIdAndFlag" resultType="com.jeeplus.sys.service.dto.WorkAttachmentInfoDTO">
		SELECT
			id,
			url,
			attachment_name AS `name`,
			create_by_id AS `by`,
			create_time
		FROM
			work_attachment
		WHERE
			del_flag = 0
			AND attachment_id = #{attachmentId}
			And attachment_flag = #{attachmentFlag}
	</select>

	<update id="updateStatusById">
		UPDATE work_contract_info SET `status` = #{status} WHERE del_flag = 0 AND id = #{id}
	</update>

	<select id="relationProjectList" resultType="com.jeeplus.assess.program.configuration.projectList.domain.ProgramProjectListInfo">
		SELECT
           a.id,
           b.name AS create_by_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
        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
		WHERE
			a.contract_id = #{id}
            ORDER BY a.update_time DESC
	</select>



	<select id="getInfoByClientId" resultType="com.jeeplus.assess.workContract.service.dto.WorkContractInfoDto">
		SELECT
			a.id,
			a.client_id,
			a.client_name,
			a.`no`,
			a.`name`,
			a.contract_date,
			a.effective_date,
			a.closing_date,
			a.contract_type,
			a.contract_amount_type,
			a.contract_amount,
			a.contract_opposite,
			a.contract_fee,
			a.fees,
			a.describes,
			a.contract_special,
			a.remarks,
			a.filed_by,
			a.filed_date,
			a.filed_no,
			d.confirm_filed_no,
			a.filed_remarks,
			a.proc_ins_id,
			a.borrow_type,
			a.create_time,
			a.create_by_id,
			a.update_time,
			a.update_by_id,
			a.del_flag,
			d.id as contract_file_id,
			d.filed_type
		FROM
			work_contract_info a
			LEFT JOIN work_contract_file d ON a.id = d.contract_info_id
		WHERE
			a.client_id = #{clientId}
			AND a.del_flag = 0
	</select>
</mapper>
