<?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.ledger.LedgerAccountBasicInfoMapper">
	<resultMap type="LedgerAccountBasicInfo" id="baseResultMap">
		<result property="id" column="id" />
		<result property="cashFlowManageBasicInfoId" column="cash_flow_manage_basic_info_id" />
		<result property="enterpriseCustomerInfoId" column="enterprise_customer_info_id" />
		<result property="customerName" column="customer_name" />
		<result property="ledgerNumber" column="ledger_number" />
		<result property="ledgerAmount" column="ledger_amount" />
		<result property="ledgerDate" column="ledger_date" />
		<result property="discountCharge" column="discount_charge" />
		<result property="remark" column="remark" />
		<result property="goodsAmount" column="goods_amount" />
		<result property="gmtModified" column="gmt_modified" />
<!--		<association property="currency" column="currency" resultMap="currencyResult" />-->
		<result property="ledgerStatus" column="ledger_status"/>
		<result property="ledgerType" column="ledger_type" />
		<collection property="assignVoList" ofType="com.sunxung.factoring.entity.ledger.vo.GoodsAssignVo"
					select="com.sunxung.factoring.mapper.ledger.LedgerAccountBasicInfoMapper.findAssignList" column="{id=id}">
			<result property="contractNo" column="contract_no" />
			<result property="quotaDisburseBatch" column="quota_disburse_batch" />
			<result property="businessNumber" column="business_number" />
			<result property="projectName" column="project_name" />
			<result property="enterprise" column="enterprise" />
			<result property="mainEnterprise" column="main_enterprise" />
			<result property="platformName" column="platform_name" />
		</collection>
	</resultMap>

    <select id="findAll" resultMap="baseResultMap">
		SELECT DISTINCT
			tt.id,
		    tt.ledger_number,
		    tt.ledger_amount,
# 		    tt.cid,
# 		    tt.ccode,
# 		    tt.cname,
		    tt.ledger_status,
			tt.ledger_type,
		    tt.gmt_modified
		FROM (
			SELECT
			t1.id,
			t1.ledger_number,
			t1.currency,
# 			t4.id cid,
# 			t4.name cname,
# 			t4.code ccode,
			t1.ledger_amount,
			t1.ledger_status,
			t1.ledger_type,
			t5.business_number,
			t6.project_name,
			t8.corporate_name enterprise,
			t10.corporate_name main_enterprise,
			t1.gmt_modified
			FROM
			ledger_account_basic_info t1
			LEFT JOIN ledger_fund_goods_assign t5 ON t1.id = t5.ledger_account_basic_info_id
			LEFT JOIN c_project_information t6 ON t5.business_number = t6.business_number
			LEFT JOIN c_project_core_enterprise t7 ON t6.id = t7.c_project_information_id
			LEFT JOIN c_enterprise t8 ON t7.c_enterprise_id = t8.id
			LEFT JOIN c_supplier_info t9 ON t6.c_supplier_info_id = t9.id
			LEFT JOIN c_enterprise t10 ON t9.c_enterprise_id = t10.id
		) tt
		<where>
			<if test="statusId != null">
				AND t1.ledger_status = #{statusId}
			</if>
			<if test="searchNumber != null and searchNumber != ''">
				AND (t5.business_number LIKE CONCAT('%',#{searchNumber},'%') OR t5.contract_no LIKE CONCAT('%',#{searchNumber},'%'))
			</if>
			<if test="fastSearch != null and fastSearch != ''">
				AND (t6.project_name LIKE CONCAT('%',#{fastSearch},'%') OR t7. LIKE CONCAT('%',#{fastSearch},'%')
				OR t5.corporate_name LIKE CONCAT('%',#{fastSearch},'%'))
			</if>
		</where>
			ORDER BY tt.gmt_modified Desc
	</select>

	<select id="findAssignList" resultType="com.sunxung.factoring.entity.ledger.vo.GoodsAssignVo">
		SELECT DISTINCT
			t1.business_number,
		    t1.contract_no,
		    t1.quota_disburse_batch,
			t2.project_name,
			t8.corporate_name enterprise,
			t10.corporate_name main_enterprise,
		    t5.platform_name
		FROM
		     ledger_fund_goods_assign t1
			LEFT JOIN c_project_information t2 ON t1.business_number = t2.business_number
			LEFT JOIN c_project_core_enterprise t3 ON t2.id = t3.c_project_information_id
		    LEFT JOIN c_sales_contract_management t4 ON t1.business_number = t4.business_number
		    LEFT JOIN trading_platform t5 ON t4.trading_platform_id = t5.id
			LEFT JOIN c_enterprise t8 ON t3.c_enterprise_id = t8.id
			LEFT JOIN c_supplier_info t9 ON t2.c_supplier_info_id = t9.id
			LEFT JOIN c_enterprise t10 ON t9.c_enterprise_id = t10.id
		WHERE t1.ledger_account_basic_info_id = #{id}
	</select>

	<select id="findBySupplier" resultMap="baseResultMap">
		SELECT DISTINCT
		t1.id,
		t1.cash_flow_manage_basic_info_id,
		t1.ledger_number,
		t2.enterprise_name customer_name,
		t1.business_name,
		t1.business_number,
		t1.currency,
		t4.id cid,
		t4.name cname,
		t4.code ccode,
		t1.ledger_amount,
		t1.ledger_status,
		t3.id did,
		t3.name dname,
		t3.code dcode,
		t1.gmt_modified
		FROM
		ledger_account_basic_info t1
		LEFT JOIN enterprise_customer_info t2 ON t1.enterprise_customer_info_id = t2.id
		LEFT JOIN t_dictionary t3 ON t1.ledger_status = t3.id
		LEFT JOIN t_dictionary t4 ON t1.currency = t4.id
		LEFT JOIN business_basic_info t5 ON t1.business_number = t5.business_number
		LEFT JOIN ledger_fund_goods_assign t6 ON t1.id = t6.ledger_account_basic_info_id
		LEFT JOIN ledger_fund_possession_cost t7 ON t1.id = t7.ledger_account_basic_info_id
		<where>
			t3.`code` = 'ledger_complete'
			<if test="supplierId != null">
			    AND	(t6.supplier_id = #{supplierId} OR t7.supplier_id = #{supplierId})
			</if>
			<if test="customerName !=null">
				AND t2.enterprise_name LIKE concat('%',#{customerName},'%')
			</if>
			<if test="fastSearch != null">
				AND (t1.business_name LIKE concat('%',#{fastSearch},'%') OR t1.business_number LIKE concat('%',#{fastSearch},'%'))
			</if>
		</where>
		ORDER BY t1.gmt_modified Desc
	</select>

	<select id="getDetail" resultMap="baseResultMap">
		SELECT DISTINCT
			t1.id,
		    t1.cash_flow_manage_basic_info_id,
		    t1.enterprise_customer_info_id,
			t1.ledger_number,
			t2.enterprise_name customer_name,
			t1.business_name,
			t1.business_number,
		    t1.discount_charge,
		    t1.remark,
		    t1.is_possession_cost,
		    t1.is_goods_assign,
			t1.currency,
			t4.id cid,
			t4.name cname,
			t4.code ccode,
			t1.ledger_amount,
		    t1.ledger_date,
			t1.ledger_status,
			t3.id tid,
			t3.name tname,
			t3.code tcode,
			t1.gmt_modified,
		    t1.goods_amount
			FROM
			ledger_account_basic_info t1
			LEFT JOIN enterprise_customer_info t2 ON t1.enterprise_customer_info_id = t2.id
			LEFT JOIN t_dictionary t3 ON t1.ledger_type = t3.id
			LEFT JOIN t_dictionary t4 ON t1.currency = t4.id
		WHERE t1.id = #{id}
	</select>

	<select id="getSupplierList" resultType="com.sunxung.factoring.entity.ledger.vo.SupplierListVo">
		SELECT DISTINCT
			t3.supplier_id,
			t4.enterprise_name supplier_name
		FROM
			invoice_basic_info t1,
			invoice_goods_info t2,
			settlement_basic_info t3,
			enterprise_customer_info t4
		WHERE
			t1.id = t2.invoice_basic_info_id
		  AND t2.settlement_basic_info_id = t3.id
		  AND t3.supplier_id = t4.id
		  AND t1.business_number = #{businessNumber}
		  AND t1.invoice_status = ( SELECT t5.id FROM t_dictionary t5 WHERE t5.CODE = 'invoice_pass' )
		UNION
		SELECT DISTINCT
			t2.supplier_id,
			t3.enterprise_name supplier_name
		FROM
			invoice_basic_info t1,
			invoice_supplier_info t2,
			enterprise_customer_info t3
		WHERE
			t1.id = t2.invoice_basic_info_id
		  AND t2.supplier_id = t3.id
		  AND t1.business_number = #{businessNumber}
		  AND t1.invoice_status = ( SELECT t5.id FROM t_dictionary t5 WHERE t5.CODE = 'invoice_pass' )
	</select>
</mapper>
