<?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.sunxung.factoring.mapper.purchcontractmanagement.PurchContractBasicInfoMapper">
	<resultMap type="com.sunxung.factoring.entity.purchcontractmanagement.PurchContractManagement" id="baseResultMap">
		<result column="id" property="id" />
		<result column="user_create" property="userCreate" />
		<result column="user_modified" property="userModified" />
		<result column="gmt_create" property="gmtCreate" />
		<result column="gmt_modified" property="gmtModified" />
		<result column="contract_number" property="contractNumber" />
		<result column="contract_name" property="contractName" />
		<result column="supplier_name" property="supplierName" />
	</resultMap>

	<resultMap id="reqDictionaryResult" type="Dictionary">
		<result property="id" column="dtid" />
		<result property="name" column="dtname" />
		<result property="code" column="dtcode" />
	</resultMap>

	<resultMap id="dictionaryResult" type="Dictionary">
		<result property="id" column="did" />
		<result property="name" column="dname" />
		<result property="code" column="dcode" />
	</resultMap>

	<select id="getLatestBasicInfo" parameterType="java.lang.Long" resultMap="baseResultMap">
		select
			t1.id,
			t1.contract_number,
			t1.contract_name,
-- 			t1.contract_period,
			t1.repurch_method,
			t1.contract_plan_amount_type,
			t1.contract_plan_amount,
			t1.purch_contract_management_id,
			t1.user_create,
			t1.user_modified,
			t1.gmt_create,
			t1.gmt_modified,
			t2.id did,
			t2.code dcode,
			t2.name dname,
			t3.id dtid,
			t3.code dtcode,
			t3.name dtname
		FROM
		c_purch_contract_basic_info t1
				LEFT JOIN t_dictionary t2 ON t1.contract_plan_amount_type = t2.id
				LEFT JOIN t_dictionary t3 ON t1.repurch_method = t3.id
		<if test="conclusion == null">
			WHERE t1.c_purch_contract_management_id = #{purchContractManageId}
		</if>
		<if test="conclusion != null">
			LEFT JOIN purch_contract_sign_approval t4 ON t1.id = t4.purch_contract_basic_info_id
			WHERE t1.purch_contract_management_id = #{purchContractManageId} AND t4.approval_conclusion = #{conclusion}
		</if>
		GROUP BY t1.gmt_create DESC LIMIT 1
	</select>

	<select id="getDetail" parameterType="java.lang.Long" resultMap="baseResultMap">
		select
			t1.id,
			t1.contract_number,
			t1.contract_name,
			t1.contract_period,
			t1.repurch_method,
			t1.contract_plan_amount_type,
			t1.contract_plan_amount,
			t1.purch_contract_management_id,
			t1.user_create,
			t1.user_modified,
			t1.gmt_create,
			t1.gmt_modified,
			t2.id did,
			t2.code dcode,
			t2.name dname,
			t3.id dtid,
			t3.code dtcode,
			t3.name dtname

		FROM
			purch_contract_basic_info t1
				LEFT JOIN t_dictionary t2 ON t1.contract_plan_amount_type = t2.id
				LEFT JOIN t_dictionary t3 ON t1.repurch_method = t3.id
		WHERE t1.id = #{id}
	</select>

	<select id="findFileIdByPurchContractId" resultType="java.lang.Long">
		SELECT file_id FROM purch_contract_info_file_ref WHERE purch_contract_id=#{purchContractId}
	</select>

	<delete id="deleteFileRef">
		DELETE FROM purch_contract_info_file_ref
		<where>
			<if test="purchContractId != null ">
				AND purch_contract_id=#{purchContractId}
			</if>
			<if test="fileId != null ">
				AND file_id=#{fileId}
			</if>
		</where>
	</delete>
	<insert id="addFileRef" useGeneratedKeys="true">
		INSERT INTO purch_contract_info_file_ref(
			purch_contract_id,file_id
		)VALUES(
			#{purchContractId},#{fileId}
		)
	</insert>

</mapper>
