<?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.workContractInfo.mapper.JyWorkContractInfoMapper">

	<select id="findPageList" resultType="com.jeeplus.business.workContractInfo.domain.JyWorkContractInfo">
		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 as contractType,
			a.contract_type_first,
			a.contract_amount_type,
			a.contract_amount,
			a.contract_opposite,
			a.contract_fee,
			a.fees,
			a.payment_describe,
			a.contract_special,
			a.remarks,
			a.filed_by,
			a.filed_date,
			a.filed_no,
			a.filed_remarks,
			a.proc_ins_id,
			a.process_definition_id,
			a.status,
			a.create_by_id as create_id,
			a.filed_type,
			a.primary_linkman,
			art.ID_ as taskId
		FROM
			jy_work_contract_info a
			LEFT JOIN sys_user c ON a.create_by_id = c.id
			left join jy_work_client_info jy_ci on a.primary_linkman=jy_ci.name
			left join act_ru_task art ON a.proc_ins_id = art.PROC_INST_ID_
		${ew.customSqlSegment}
		ORDER BY a.update_time DESC
	</select>

	<select id="findById" resultType="com.jeeplus.business.workContractInfo.domain.JyWorkContractInfo">
		SELECT
			a.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 as contractType,
			(SELECT dv.label FROM `sys_dict_value` dv
			LEFT JOIN sys_dict_type dt on dt.id=dv.dict_type_id
			where dt.type='jy_contract_type_up' and a.contract_type_first=dv.value) as contractTypeFirst,
			a.contract_amount_type,
			a.contract_amount,
			a.contract_opposite,
			a.contract_fee,
			a.fees,
			a.payment_describe,
			a.contract_special,
			a.remarks,
			a.filed_by,
			a.filed_date,
			a.filed_no,
			a.filed_remarks,
			a.proc_ins_id,
			a.process_definition_id,
			a.status,
			a.create_by_id as create_id,
			a.filed_type,
			a.primary_linkman,
			so.id as department,
			art.ID_ as taskId
		FROM
			jy_work_contract_info a
			left join jy_work_client_info jy_ci on a.primary_linkman=jy_ci.name
			left join sys_office so on a.department=so.id
			left join act_ru_task art ON a.proc_ins_id = art.PROC_INST_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 jy_work_contract_info SET `filed_type` = #{filedType} WHERE del_flag = 0 AND id = #{id}
	</update>



	<select id="getInfoByClientId" resultType="com.jeeplus.business.workContractInfo.service.dto.JyWorkContractInfoDto">
		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>

	<update id="updateContractById">
		UPDATE jy_work_contract_info SET update_By_id=#{updateById},update_time=#{updateTime}, `filed_type` = #{filedType},filed_no=#{filedNo} WHERE del_flag = 0 AND id = #{id}
	</update>

	<select id="findType" resultType="string">
		SELECT dv.label from sys_dict_value dv LEFT JOIN sys_dict_type dt on dt.id=dv.dict_type_id where dv.`value`=#{contractType} and dt.type='jy_contract_type_up'
	</select>

	<select id="findType2" resultType="string">
		SELECT dv.label from sys_dict_value dv LEFT JOIN sys_dict_type dt on dt.id=dv.dict_type_id where dv.`id`=#{contractType} and dt.type='jy_contract_type'
	</select>

</mapper>
