<?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.cashflowmanage.CashFlowManageBaseInfoMapper">

	<resultMap type="com.sunxung.factoring.entity.cashflowmanage.vo.CashFlowManageBaseVo" id="BaseResultMap">
		<result property="id" column="id" />
		<result property="customerId" column="enterprise_customer_info_id" />
		<result property="customerName" column="customer_name" />
		<result property="businessName" column="business_name" />
		<result property="businessNumber" column="business_number" />
		<result property="entryOutType" column="entry_out_type" />
		<result property="cashStatus" column="cash_status" />
		<result property="supplierId" column="supplier_id" />
		<result property="supplierName" column="supplier_name" />
		<result property="platformId" column="platform_id" />
		<result property="platformName" column="platform_name" />
		<result property="amount" column="amount" />
		<result property="contractNumber" column="contract_number" />
		<result property="cashDate" column="cash_date" />
		<result property="remark" column="remark" />
		<association property="cashTypeDict" column="cash_type" resultMap="dictionaryResult"/>
		<association property="currencyDict" column="currency" resultMap="currencyDictResult" />
	</resultMap>


	<resultMap type="com.sunxung.factoring.entity.cashflowmanage.vo.CashFlowManageBaseVo" id="listOfResult">
		<result property="id" column="id" />
		<result property="supplierId" column="supplier_id" />
		<result property="customerName" column="customer_name" />
		<result property="businessName" column="business_name" />
		<result property="businessNumber" column="business_number" />
		<result property="entryOutType" column="entry_out_type" />
		<result property="cashStatus" column="cash_status" />
		<result property="amount" column="amount" />
		<result property="cashDate" column="cash_date" />
		<result property="gmtCreate"  column="gmt_create"/>
		<result property="gmtModified" column="gmt_modified" />
		<result property="contractNumber" column="contract_number" />
		<association property="cashTypeDict" column="cash_type" resultMap="dictionaryResult"/>
		<association property="currencyDict" column="currency" resultMap="currencyDictResult" />
<!--		<collection property="cashFlowManageBankVo" javaType="java.util.ArrayList" ofType="com.sunxung.factoring.entity.cashflowmanage.vo.CashFlowManageBankVo">-->
<!--			<result property="id" column="bid" />-->
<!--			<result property="gmtCreate" column="gmt_create" />-->
<!--			<result property="bankName" column="bank_name" />-->
<!--			<result property="bankAccount" column="bank_account" />-->
<!--		</collection>-->
	</resultMap>

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

	<resultMap id="currencyDictResult" type="Dictionary">
		<result property="id" column="cid" />
		<result property="name" column="cname" />
		<result property="code" column="ccode" />
	</resultMap>

	<select id="queryPage" parameterType="com.sunxung.factoring.entity.cashflowmanage.vo.SearchCashFlowManageVo" resultMap="listOfResult">
		select
			e.id,
			e.supplier_id,
			e.customer_name,
			e.business_name,
			e.business_number,
			e.entry_out_type,
			e.cash_status,
			e.cash_type,
		  e.gmt_create,
			e.did,
			e.dcode,
			e.dname,
# 			e.bid,
		    e.cash_date,
 			e.gmt_modified,
# 			e.bank_name,
# 			e.bank_account,
			e.amount,
		    e.currency,
		    e.cid,
		    e.ccode,
		    e.cname
		from
		(select
			a.id,
			a.supplier_id,
			c.enterprise_name customer_name,
			a.business_name,
			a.business_number,
			a.entry_out_type,
			a.cash_status,
			a.cash_type,
		  a.gmt_create,
			b.id did,
			b.code dcode,
			b.name dname,
# 			d.id as bid,
		    a.cash_date,
			a.gmt_modified,
# 			d.bank_name,
# 			d.bank_account,
			a.amount as amount,
		    a.currency,
		    e.id cid,
		    e.code ccode,
		    e.name cname
		from cash_flow_manage_basic_info a
		LEFT JOIN t_dictionary b ON a.cash_type = b.code
		LEFT JOIN enterprise_customer_info c on a.enterprise_customer_info_id = c.id
# 		LEFT JOIN cash_flow_manage_entry_info d on a.id = d.cash_flow_manage_basic_info_id
		LEFT JOIN t_dictionary e ON a.currency = e.id
		where a.entry_out_type = '1'
<!--		<if test="userIds != null and userIds.size()>0">-->
<!--			AND f.manager_id in-->
<!--			<foreach collection="userIds" index="index" item="item"  open="(" separator="," close=")">-->
<!--				#{item}-->
<!--			</foreach>-->
<!--		</if>-->
		<if test="entryOutType != null and entryOutType !=''">
			and a.entry_out_type = #{entryOutType}
		</if>
		<if test="cashStatus != null and cashStatus !=''">
			and a.cash_status = #{cashStatus}
		</if>
<!--		<if test="startDate != null">-->
<!--			<![CDATA[ AND DATE_FORMAT(d.gmt_create, '%Y-%m-%d')>=  DATE_FORMAT(#{startDate}, '%Y-%m-%d') ]]>-->
<!--		</if>-->
<!--		<if test="endDate != null">-->
<!--			<![CDATA[ AND DATE_FORMAT(d.gmt_create, '%Y-%m-%d')<=  DATE_FORMAT(#{endDate}, '%Y-%m-%d') ]]>-->
<!--		</if>-->
		<if test="fastSearch != null and fastSearch !=''">
			AND (a.business_name LIKE concat('%',#{fastSearch},'%') OR c.enterprise_name LIKE concat('%',#{fastSearch},'%'))
		</if>
		UNION ALL
		select
			a.id,
			a.supplier_id,
			c.enterprise_name customer_name,
			a.business_name,
			a.business_number,
			a.entry_out_type,
			a.cash_status,
			a.cash_type,
		  a.gmt_create,
			b.id did,
			b.code dcode,
			b.name dname,
# 			d.id as bid,
			a.cash_date,
 			a.gmt_modified,
# 			d.from_bank_name bank_name,
# 			d.from_bank_account bank_account,
			a.amount as amount,
		    a.currency,
			e.id cid,
			e.code ccode,
			e.name cname
		from cash_flow_manage_basic_info a
		LEFT JOIN t_dictionary b ON a.cash_type = b.code
		LEFT JOIN enterprise_customer_info c on a.enterprise_customer_info_id = c.id
# 		LEFT JOIN cash_flow_manage_out_info d on a.id = d.cash_flow_manage_basic_info_id
		LEFT JOIN t_dictionary e ON a.currency = e.id
		where a.entry_out_type = '2'
<!--		<if test="userIds != null and userIds.size()>0">-->
<!--			AND f.manager_id in-->
<!--			<foreach collection="userIds" index="index" item="item"  open="(" separator="," close=")">-->
<!--				#{item}-->
<!--			</foreach>-->
<!--		</if>-->
		<if test="entryOutType != null and entryOutType !=''">
			and a.entry_out_type = #{entryOutType}
		</if>
		<if test="cashStatus != null and cashStatus !=''">
			and a.cash_status = #{cashStatus}
		</if>
<!--		<if test="startDate != null">-->
<!--			<![CDATA[ AND DATE_FORMAT(d.gmt_create, '%Y-%m-%d')>=  DATE_FORMAT(#{startDate}, '%Y-%m-%d') ]]>-->
<!--		</if>-->
<!--		<if test="endDate != null">-->
<!--			<![CDATA[ AND DATE_FORMAT(d.gmt_create, '%Y-%m-%d')<=  DATE_FORMAT(#{endDate}, '%Y-%m-%d') ]]>-->
<!--		</if>-->
		<if test="fastSearch != null and fastSearch !=''">
			AND (a.business_name LIKE concat('%',#{fastSearch},'%') OR c.enterprise_name LIKE concat('%',#{fastSearch},'%'))
		</if>
		) e
 		order by e.gmt_modified desc
	</select>

	<select id="getDetail" resultMap="BaseResultMap">
		SELECT DISTINCT
			t1.id,
			t1.enterprise_customer_info_id,
			t2.enterprise_name  customer_name,
			t1.business_name,
			t1.business_number,
			t1.entry_out_type,
			t1.cash_status,
			t1.supplier_id,
			t3.enterprise_name supplier_name,
			t1.trading_platform_id,
		    t6.platform_name,
		    t1.contract_number,
		    t1.cash_date,
		    t1.remark,
			t1.amount,
			t4.id did,
			t4.name dname,
			t4.code dcode,
			t5.id cid,
			t5.name cname,
			t5.code ccode
		FROM
			cash_flow_manage_basic_info t1
				LEFT JOIN enterprise_customer_info t2 ON t1.enterprise_customer_info_id = t2.id
				LEFT JOIN enterprise_customer_info t3 ON t1.supplier_id = t3.id
				LEFT JOIN t_dictionary t4 ON t1.cash_type = t4.id
				LEFT JOIN t_dictionary t5 ON t1.currency = t5.id
				LEFT JOIN trading_platform t6 on t1.trading_platform_id = t6.id
		WHERE  t1.id = #{id}
	</select>
</mapper>
