<?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.PurchContractMapper">
	<resultMap type="com.sunxung.factoring.entity.project.vo.BusinessProcessingTaskVo" id="baseResultMap">
		<result property="id" column="id" />
		<result property="businessNumber" column="business_number" />
		<result property="projectName" column="project_name" />
		<result property="platformName" column="platform_name" />
		<result property="customerName" column="customer_name" />
		<result property="contractName" column="customer_name" />
		<result property="supplierName" column="supplier_name" />
		<result property="gmtModified" column="gmt_modified" />
		<result property="contractNumber" column="contract_number" />
		<association property="status" column="status" resultMap="dictionaryResult" />
		<association property="assignee" column="manager_id" resultMap="userResult" />
	</resultMap>
	<resultMap id="ListResultMap" type="com.sunxung.factoring.entity.purchcontractmanagement.vo.PurchContractCompletedVo">
		<id column="id" property="purchContractId"/>
		<result column="business_number" property="businessNumber"/>
		<result column="project_name" property="projectName"/>
		<result column="contract_number" property="purchContractNumber"/>
		<result column="contract_name" property="purchContractName"/>
		<result column="main_enterprise_id" property="mainEnterpriseId"/>
		<result column="c_project_core_enterprise_id" property="coreEnterpriseId"/>
		<result column="platform_id" property="platformId"/>
		<result column="supplier_id" property="supplierId"/>
		<result column="c_purch_contract_basic_info_id" property="cPurchContractBasicInfoId"/>
	</resultMap>
	<resultMap type="PurchContractManagement" id="listVoResult">
		<result property="id" column="id" />
		<result property="businessNumber" column="business_number" />
		<result property="supplierName" column="supplier_name" />
		<result property="businessName" column="business_name" />
		<result property="customerName" column="customer_name" />
		<result property="tradingPlatformName" column="platform_name" />
		<result property="gmtModified" column="gmt_modified" />
		<result property="contractNumber" column="contract_number" />
		<result property="contractStatus" column="contract_status"/>
		<association property="statusDictionary" column="contract_status" resultMap="dictionaryResult"/>
	</resultMap>

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

	<resultMap id="userResult" type="User">
		<result property="id" column="uid" />
		<result property="name" column="uname" />
	</resultMap>

	<select id="getListInfo" parameterType="java.lang.Long" resultMap="listVoResult">
		SELECT DISTINCT
			t1.id,
			t1.business_number,
			t2.supplier_name ,
			t2.buyer_name AS customer_name,
			t2.contract_number AS contract_number,
			t2.project_name ,
			t4.id did,
			t4.name dname,
			t4.code dcode,
			t1.gmt_modified,
		    t1.contract_status,
			t5.platform_name
		FROM
			c_purch_contract_management t1
				LEFT JOIN c_purch_contract_basic_info t2 ON t2.c_purch_contract_management_id = t1.id
				LEFT JOIN c_project_information t3 ON t3.business_number = t1.business_number
				LEFT JOIN t_dictionary t4 ON t1.contract_status = t4.code
				LEFT JOIN trading_platform t5 ON t5.id = t2.trading_platform_id
		WHERE t1.id = #{id}
		ORDER BY t1.gmt_modified Desc
	</select>

	<select id="findMyCompletedList" parameterType="com.sunxung.factoring.entity.project.vo.SearchBusinessProcessingTask" resultMap="baseResultMap">
		SELECT DISTINCT
		t2.id,
		t3.business_number,
		t3.business_name,
		t4.enterprise_name supplier_name,
		(select t8.contract_number from purch_contract_basic_info t8 where t8.purch_contract_management_id = t2.id order by gmt_create desc limit 1) contract_number,
		t2.contract_status status,
		t2.gmt_modified,
		t5.id did,
		t5.code dcode,
		t5.name dname
		FROM
		business_processing_log t1,
		purch_contract_management t2,
		business_basic_info t3,
		enterprise_customer_info t4,
		t_dictionary t5
		<where>
			t1.busness_key = t2.id AND t2.business_number = t3.business_number
			AND t2.supplier_id = t4.id AND t2.contract_status = t5.id AND t1.end_time IS NOT NULL
			<if test="userId != null">
				AND t1.assignee_id = #{userId}
			</if>
			<if test="simpleClassName != null">
				AND t1.business_type LIKE concat(#{simpleClassName},'%')
			</if>
			<if test="startDate != null">
				<![CDATA[ AND DATE_FORMAT(t2.gmt_create, '%Y-%m-%d')>=  DATE_FORMAT(#{startDate}, '%Y-%m-%d') ]]>
			</if>
			<if test="endDate != null">
				<![CDATA[ AND DATE_FORMAT(t2.gmt_create, '%Y-%m-%d')<=  DATE_FORMAT(#{endDate}, '%Y-%m-%d') ]]>
			</if>
			<if test="fastSearch != null">
				AND (t3.business_number LIKE concat('%',#{fastSearch},'%') OR t3.business_name LIKE concat('%',#{fastSearch},'%') OR t4.enterprise_name LIKE concat('%',#{fastSearch},'%'))
			</if>
			<if test="statusId != null">
				AND t2.contract_status = #{statusId}
			</if>
		</where>
		GROUP BY t1.busness_key,t2.gmt_create
		ORDER BY t2.gmt_modified DESC
	</select>

	<select id="findAllList" parameterType="com.sunxung.factoring.entity.project.vo.SearchBusinessProcessingTask" resultMap="baseResultMap">
		SELECT DISTINCT
		t1.id,
		t1.business_number,
		t2.supplier_name ,
		t2.buyer_name AS customer_name,
		t2.contract_number AS contract_number,
		t2.contract_name,
		t2.project_name ,
		t4.id did,
		t4.name dname,
		t4.code dcode,
		t1.gmt_modified,
		t5.platform_name
		FROM
		c_purch_contract_management t1
		LEFT JOIN c_purch_contract_basic_info t2 ON t2.c_purch_contract_management_id = t1.id
		LEFT JOIN c_project_information t3 ON t3.business_number = t1.business_number
		LEFT JOIN t_dictionary t4 ON t1.contract_status = t4.code
		LEFT JOIN trading_platform t5 ON t5.id = t2.trading_platform_id
		LEFT JOIN c_supplier_info t6 ON t6.id = t3.c_supplier_info_id
		LEFT JOIN c_sys_user t7 ON t7.id = t6.sys_user_id
		<where>
			<if test="status != null and status !='' ">
				AND t1.contract_status = #{status}
			</if>
			<if test="fastSearch != null and fastSearch !='' ">

				and (t2.buyer_name LIKE CONCAT('%',#{fastSearch},'%')
				or t2.supplier_name LIKE CONCAT('%',#{fastSearch},'%')
				OR t3.project_name LIKE CONCAT('%',#{fastSearch},'%'))
			</if>
			<if test="businessNo != null and businessNo !='' ">
				AND (t1.business_number LIKE CONCAT('%',#{businessNo},'%') or t2.contract_number LIKE CONCAT('%',#{businessNo},'%') )
			</if>
			<if test="null != userIds and userIds.size()>0">
				AND t7.project_manager in
				<foreach item="item" collection="userIds" separator="," open="(" close=")"
						 index="">
					#{item}
				</foreach>
			</if>
		</where>
		ORDER BY t1.gmt_modified Desc
	</select>

	<select id="findByManagerId" parameterType="Long" resultType="com.sunxung.factoring.entity.purchcontractmanagement.PurchContractSupplementInfo">
		SELECT
  			*
		FROM
  		`purch_contract_supplement_info`
		WHERE `purch_contract_management_id` = #{id} ;
	</select>
    <select id="getCompletedPurchContractByBusinessNumber" resultMap="ListResultMap">
		SELECT DISTINCT c.business_number,
		c1.id c_purch_contract_basic_info_id,
		c1.contract_name,
		c1.contract_number,
		t2.project_name,
		t5.id                  main_enterprise_id,
		t6.id                  c_project_core_enterprise_id,
		t1.trading_platform_id platform_id,
		t4.id                  supplier_id
		FROM c_purch_contract_management c
		LEFT JOIN c_purch_contract_basic_info c1 ON c1.c_purch_contract_management_id = c.id
		LEFT JOIN c_sales_contract_management t1 ON t1.business_number = c.business_number
		LEFT JOIN c_project_information t2 ON t2.business_number = c.business_number
		LEFT JOIN c_project_core_enterprise t3 ON t3.c_project_information_id = t2.id
		LEFT JOIN c_supplier_info t4 ON t4.id = t2.c_supplier_info_id
		LEFT JOIN c_enterprise t5 ON t5.id = t4.c_enterprise_id
		LEFT JOIN c_enterprise t6 ON t6.id = t3.c_enterprise_id
		where c.contract_status = 'project_init_completed'
		AND c.business_number = #{businessNumber}
	</select>

</mapper>
