<?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.inkasso.factoring.invoicemanage.dao.InvoiceBasicInfoMapper">
	<resultMap type="com.inkasso.factoring.settlement.model.dto.BusinessProcessingTaskDTO" id="baseResultMap">
		<result property="id" column="id" />
		<result property="businessNumber" column="business_number" />
		<result property="customerName" column="enterprise_name" />
		<result property="taskId" column="task_id" />
		<result property="taskName" column="task_name" />
		<result property="taskDefKey" column="task_def_key" />
		<result property="businessName" column="business_name" />
		<result property="openAmount" column="open_amount" />
		<result property="gmtModified" column="gmt_modified" />
		<result property="supplierName" column="supplierName" />
		<result property="platformName" column="platform_name" />
		<association property="status" column="invoice_status" resultMap="dictionaryResult"/>
		<association property="amountKind" column="amount_kind" resultMap="kindDictionaryResult" />
	</resultMap>
	<resultMap id="dictionaryResult" type="com.inkasso.factoring.dict.entity.Dictionary">
		<result property="id" column="did" />
		<result property="name" column="dname" />
		<result property="code" column="dcode" />
	</resultMap>

	<resultMap id="typeDictionaryResult" type="com.inkasso.factoring.dict.entity.Dictionary">
		<result property="id" column="tid" />
		<result property="name" column="tname" />
		<result property="code" column="tcode" />
	</resultMap>

	<resultMap id="kindDictionaryResult" type="com.inkasso.factoring.dict.entity.Dictionary">
		<result property="id" column="kid" />
		<result property="name" column="kname" />
		<result property="code" column="kcode" />
	</resultMap>
	<select id="findAllList" resultMap="baseResultMap">
		select * from
		(SELECT DISTINCT
		t1.historic_task_id task_id,
		t1.task_name,
		t1.task_def_key,
		t2.id,
		t2.business_number ,
		t4.corporate_name enterprise_name ,
		t3.project_name business_name,
		t2.amount_kind,
		t2.open_amount,
		t2.invoice_status,
		t2.gmt_modified,
		t5.id did,
		t5.code dcode,
		t5.name dname,
		t6.id kid,
		t6.code kcode,
		t6.name kname,
		t8.corporate_name supplierName,
		t9.platform_name
		FROM
		c_invoice_basic_info t2
		LEFT JOIN business_processing_log t1 ON t1.busness_key = t2.id and t1.business_type = 'invoiceManager' and t1.end_time is null
		LEFT JOIN c_project_information t3 ON t2.business_number = t3.business_number
		LEFT JOIN c_enterprise t4 ON t2.enterprise_customer_info_id = t4.id
		LEFT JOIN t_dictionary t5 ON t5.id = t2.invoice_status
		LEFT JOIN t_dictionary t6 ON t6.id = t2.amount_kind
		LEFT JOIN c_supplier_info t7 ON t3.c_supplier_info_id = t7.id
		LEFT JOIN c_enterprise t8 ON t7.c_enterprise_id = t8.id
		left join trading_platform t9 on t2.trading_platform_id = t9.id
		left join c_sys_user t10 on t10.login_name = t2.user_create
		<where>
			<if test="userIds != null and userIds.size()>0">
				AND  t10.id in
				<foreach collection="userIds" index="index" item="item"  open="(" separator="," close=")">
					#{item}
				</foreach>
			</if>
			<if test="businessNo != null and businessNo != ''">
				and t3.business_number = #{businessNo}
			</if>
			<if test="status != null and status != '' ">
				AND t5.code = #{status}
			</if>
			<if test="fastSearch != null and fastSearch != '' ">
				AND (t3.project_name LIKE concat('%',#{fastSearch},'%') OR t4.corporate_name LIKE concat('%',#{fastSearch},'%') or t8.corporate_name LIKE concat('%',#{fastSearch},'%'))
			</if>
		</where>
		ORDER BY
		t2.gmt_create ,t1.id DESC ) t
		GROUP BY t.id,t.gmt_modified
		ORDER BY
		t.gmt_modified desc
	</select>
	<select id="saleSelectSettlementOfPayment"
			resultType="com.inkasso.factoring.settlement.entity.SettlementBasicInfo">
		select
		DISTINCT t1.*,m.acceptance_number acceptanceNumber, t2.acceptance_goods_number,
		sum(t2.purchase_total_price)  settlementAmount,
		t6.name as statusName
		from c_settlement_basic_info t1
		LEFT JOIN c_supplier_info t5 ON t1.supplier_id = t5.id
		LEFT JOIN c_settlement_goods_info t2 ON t1.id = t2.c_settlement_basic_info_id
		LEFT JOIN c_acceptance_goods_info t3 ON t3.id = t2.c_acceptance_goods_info_id
		LEFT JOIN c_acceptance_basic_info m ON m.id = t3.c_acceptance_basic_info_id
		LEFT JOIN
		(
		SELECT
		IFNULL( sum( c.open_quantity ), 0 ) num,
		c.c_settlement_goods_info_id
		FROM
		c_invoice_goods_info c
		left join c_invoice_basic_info d on d.id=c.c_invoice_basic_info_id
		WHERE
		d.invoice_status in ( SELECT id FROM t_dictionary WHERE CODE in ('invoice_create','invoice_pass' ) )
		GROUP BY
		c.c_settlement_goods_info_id
		) t4 on  t2.id = t4.c_settlement_goods_info_id
		LEFT JOIN t_dictionary t6 ON t1.settlement_status = t6.code
		LEFT JOIN (select b.* from  c_settlement_basic_info a LEFT JOIN business_processing_log b  ON a.id = b.busness_key	WHERE
		a.business_number = #{businessNumber} ORDER BY b.gmt_create desc LIMIT 1  ) t7 ON t1.id = t7.busness_key
		WHERE
		t1.business_number = #{businessNumber}
		AND (t1.settlement_status = 'project_init_completed' or (t7.business_type = 'BillUnitPriceAdjust' and t7.task_def_key = 'finance_review' and t7.status = '驳回'))
		AND t3.goods_quantity >  IFNULL(t4.num,0)
		GROUP BY t2.acceptance_goods_number
	</select>
	<select id="saleSelectSettlementOfPaymentGoods"
			resultType="com.inkasso.factoring.invoicemanage.entity.InvoiceGoodsInfo">
		select
		t1.id settlement_basic_info_id ,
		t1.settlement_number,
		t2.acceptance_goods_number,
		m.acceptance_number acceptanceNumber,
		t3.goods_name,
		t3.goods_factory,
		t3.goods_type,
		t3.goods_unit,
		t2.goods_quantity,
		t2.id settlement_goods_info_id,
		t2.sales_price,
		t2.sales_total_price,
		IFNULL(t4.num,0) usedNum
		from c_settlement_basic_info t1
		LEFT JOIN c_supplier_info t5 ON t1.supplier_id = t5.id
		LEFT JOIN c_settlement_goods_info t2 ON t1.id = t2.c_settlement_basic_info_id
		LEFT JOIN c_acceptance_goods_info t3 ON t3.id = t2.c_acceptance_goods_info_id
		LEFT JOIN c_acceptance_basic_info m ON m.id = t3.c_acceptance_basic_info_id
		LEFT JOIN
		(
		SELECT
		IFNULL( sum( c.open_quantity ), 0 ) num,
		c.c_settlement_goods_info_id
		FROM
		c_invoice_goods_info c
		left join c_invoice_basic_info d on d.id=c.c_invoice_basic_info_id
		WHERE
		d.invoice_status in ( SELECT id FROM t_dictionary WHERE CODE in ('invoice_create','invoice_pass' ) )
		GROUP BY
		c.c_settlement_goods_info_id
		) t4 on  t2.id = t4.c_settlement_goods_info_id
		WHERE
		t1.business_number = #{businessNumber}
		AND t1.settlement_status = 'project_init_completed'
		AND t3.goods_quantity >  IFNULL(t4.num,0)
	</select>
	<select id="saleSelectGoodsOfDiscount"
			resultType="com.inkasso.factoring.settlement.entity.SettlementGoodsInfo">
		SELECT
		t1.*,
		t3.goods_name,
		t3.goods_type,
		t3.goods_unit
		FROM
		c_settlement_goods_info t1
		LEFT JOIN c_settlement_basic_info t2 ON t2.id = t1.c_settlement_basic_info_id
		LEFT JOIN c_acceptance_goods_info t3 ON t3.id = t1.c_acceptance_goods_info_id
		WHERE t2.settlement_status  in (select id from t_dictionary where code in ('bill_need_sign','bill_sign')) and t2.business_number = #{businessNumber}
	</select>


</mapper>
