<?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.salescontractmanagement.SalesContractMapper">
	<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="customerName" column="customer_name" />
		<result property="contractNumber" column="contract_number" />
		<result property="contractName" column="contract_name" />
		<result property="customerName" column="customer_name" />
		<result property="supplierName" column="supplier_name" />
		<result property="platformName" column="platform_name" />
		<result property="gmtModified" column="gmt_modified" />
		<association property="status" column="status" resultMap="dictionaryResult" />
		<association property="assignee" column="manager_id" resultMap="userResult" />
	</resultMap>

	<resultMap type="CSalesContract" id="listVoResult">
		<result property="id" column="id" />
		<result property="businessNumber" column="business_number" />
		<result property="customerName" column="customer_name" />
		<result property="businessName" column="business_name" />
		<result property="gmtModified" column="gmt_modified" />
		<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,
			t3.enterprise_name customer_name,
			t2.business_name,
			t1.contract_status status,
			t1.gmt_create,
			t1.gmt_modified,
			t4.id did,
			t4.code dcode,
			t4.name dname
		FROM
			sales_contract_management t1
				LEFT JOIN business_basic_info t2 ON t1.business_number = t2.business_number
				LEFT JOIN enterprise_customer_info t3 ON t2.enterprise_customer_info_id = t3.id
				LEFT JOIN t_dictionary t4 ON t1.contract_status = t4.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 customer_name,
		t2.contract_status status,
		t2.gmt_create,
		t2.gmt_modified,
		t5.id did,
		t5.code dcode,
		t5.name dname
		FROM
		business_processing_log t1,
		sales_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 t3.enterprise_customer_info_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" resultMap="baseResultMap">
		select c.id,
		t1.project_name,
		t1.business_number,
		t3.corporate_name AS supplier_name, -- 主企业
		t5.corporate_name AS customer_name, -- 核心企业
		t6.platform_name,
		t7.contract_number,
		t7.contract_name,
		t8.id did,
		t8.name dname,
		t8.code dcode
		from c_sales_contract_management c
		left join c_project_information t1 on t1.business_number = c.business_number
		left join c_supplier_info t2 on t2.id = t1.c_supplier_info_id
		left join c_enterprise t3 on t3.id = t2.c_enterprise_id
		left join c_project_core_enterprise t4 on t4.c_project_information_id = t1.id
		left join c_enterprise t5 on t5.id = t4.c_enterprise_id
		left join trading_platform t6 on t6.id = c.trading_platform_id
		left join c_sales_contract_basic_info t7 on t7.c_sales_contract_management_id = c.id
		left join t_dictionary t8 on t8.code = c.contract_status
		left join c_sys_user t9 on t9.id = t2.sys_user_id
		<where>
			<if test="status != null and status !='' ">
				AND c.contract_status = #{status}
			</if>
			<if test="fastSearch != null and fastSearch !='' ">
				AND (t1.project_name LIKE CONCAT('%',#{fastSearch},'%') or t3.corporate_name LIKE
				CONCAT('%',#{fastSearch},'%')
				or t5.corporate_name LIKE CONCAT('%',#{fastSearch},'%')
				or t1.business_number LIKE CONCAT('%',#{fastSearch},'%')
				OR t7.contract_number LIKE CONCAT('%',#{fastSearch},'%'))
			</if>
			<if test="businessNo != null and businessNo !='' ">
				AND (t1.business_number LIKE CONCAT('%',#{businessNo},'%') or t7.contract_number LIKE CONCAT('%',#{businessNo},'%') )
			</if>
		<if test="null != userIds and userIds.size()>0">
			AND t9.project_manager in
			<foreach item="item" collection="userIds" separator="," open="(" close=")"
					 index="">
				#{item}
			</foreach>
		</if>
		</where>
		ORDER BY c.gmt_modified DESC
	</select>
	<select id="getBasicInfoById"
			resultType="com.sunxung.factoring.entity.salescontractmanagement.vo.SalesContractBasicVo">
		select c.id,
			   t1.project_name,
			   t1.business_number,
			   t3.corporate_name AS main_enterprise, -- 主企业
			   t5.corporate_name AS enterprise,      -- 核心企业
			   t6.platform_name AS platform
		from c_sales_contract_management c
				 left join c_project_information t1 on t1.business_number = c.business_number
				 left join c_supplier_info t2 on t2.id = t1.c_supplier_info_id
				 left join c_enterprise t3 on t3.id = t2.c_enterprise_id
				 left join c_project_core_enterprise t4 on t4.c_project_information_id = t1.id
				 left join c_enterprise t5 on t5.id = t4.c_enterprise_id
				 left join trading_platform t6 on t6.id = c.trading_platform_id
		where c.id = #{value}
	</select>
	<select id="get" resultType="com.sunxung.factoring.entity.salescontractmanagement.CSalesContract">
		select
		c.*,
		t1.project_name,
		t1.business_number,
		t3.corporate_name AS supplier_name, -- 主企业
		t5.corporate_name AS customer_name, -- 核心企业
		t6.platform_name AS trading_platform_name,
		t7.contract_number
		from c_sales_contract_management c
		left join c_project_information t1 on t1.business_number = c.business_number
		left join c_supplier_info t2 on t2.id = t1.c_supplier_info_id
		left join c_enterprise t3 on t3.id = t2.c_enterprise_id
		left join c_project_core_enterprise t4 on t4.c_project_information_id = t1.id
		left join c_enterprise t5 on t5.id = t4.c_enterprise_id
		left join trading_platform t6 on t6.id = c.trading_platform_id
		left join c_sales_contract_basic_info t7 on t7.c_sales_contract_management_id = c.id
		where c.id = #{value}
		ORDER BY c.gmt_modified DESC
	</select>
	<select id="getBasicByHisId"
			resultType="com.sunxung.factoring.entity.salescontractmanagement.vo.SalesContractBasicVo">
		select c.id,
			   t1.project_name,
			   t1.business_number,
			   t3.corporate_name AS main_enterprise, -- 主企业
			   t5.corporate_name AS enterprise,      -- 核心企业
			   t6.platform_name AS platform,
			   t1.id AS project_information_id,
			   t6.id AS platform_id
				from sales_contract_management_his c
				 left join c_project_information t1 on t1.business_number = c.business_number
				 left join c_supplier_info t2 on t2.id = t1.c_supplier_info_id
				 left join c_enterprise t3 on t3.id = t2.c_enterprise_id
				 left join c_project_core_enterprise t4 on t4.c_project_information_id = t1.id
				 left join c_enterprise t5 on t5.id = t4.c_enterprise_id
				 left join trading_platform t6 on t6.id = c.trading_platform_id
		where c.id = #{value}
	</select>
</mapper>
