<?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.prolog.cs.biz.inbound.dao.OmsAsnBillDMapper">
<!-- <resultMap id="map" type="com.prolog.cs.basic.warehouse..model.OmsAsnBillD">
	<result column="lineid" property="lineid"/>
	<result column="seqno" property="seqno"/>
	<result column="billid" property="billid"/>
	<result column="billno" property="billno"/>
	<result column="branchid" property="branchid"/>
	<result column="branchcode" property="branchcode"/>
	<result column="branchname" property="branchname"/>
	<result column="branchareaid" property="branchareaid"/>
	<result column="brancharea" property="brancharea"/>
	<result column="consignorid" property="consignorid"/>
	<result column="consignorname" property="consignorname"/>
	<result column="itemid" property="itemid"/>
	<result column="itemcode" property="itemcode"/>
	<result column="itemname" property="itemname"/>
	<result column="manufacturer" property="manufacturer"/>
	<result column="parernt_itemid" property="parerntItemid"/>
	<result column="category_id" property="categoryId"/>
	<result column="size_code" property="sizeCode"/>
	<result column="color_code" property="colorCode"/>
	<result column="revision" property="revision"/>
	<result column="pack_size" property="packSize"/>
	<result column="pack_units" property="packUnits"/>
	<result column="item_pcs" property="itemPcs"/>
	<result column="item_odd" property="itemOdd"/>
	<result column="item_qty" property="itemQty"/>
	<result column="free_qty" property="freeQty"/>
	<result column="item_weight" property="itemWeight"/>
	<result column="item_volume" property="itemVolume"/>
	<result column="price" property="price"/>
	<result column="tni_price" property="tniPrice"/>
	<result column="amt" property="amt"/>
	<result column="tni_amt" property="tniAmt"/>
	<result column="mach_type" property="machType"/>
	<result column="lot_no" property="lotNo"/>
	<result column="batch_no" property="batchNo"/>
	<result column="pdate_from" property="pdateFrom"/>
	<result column="valid_until" property="validUntil"/>
	<result column="approval_number" property="approvalNumber"/>
	<result column="production_place" property="productionPlace"/>
	<result column="line_remark" property="lineRemark"/>
	<result column="closed_reason" property="closedReason"/>
	<result column="reject_qty" property="rejectQty"/>
	<result column="rt_vendorid" property="rtVendorid"/>
	<result column="rtreason" property="rtreason"/>
	<result column="paperno" property="paperno"/>
	<result column="edition" property="edition"/>
	<result column="status" property="status"/>
	<result column="attr_str1" property="attrStr1"/>
	<result column="attr_str2" property="attrStr2"/>
	<result column="attr_str3" property="attrStr3"/>
	<result column="attr_str4" property="attrStr4"/>
	<result column="attr_str5" property="attrStr5"/>
	<result column="attr_str6" property="attrStr6"/>
	<result column="attr_str7" property="attrStr7"/>
	<result column="attr_str8" property="attrStr8"/>
	<result column="attr_str9" property="attrStr9"/>
	<result column="attr_str10" property="attrStr10"/>
	<result column="attr_num1" property="attrNum1"/>
	<result column="attr_num2" property="attrNum2"/>
	<result column="attr_num3" property="attrNum3"/>
	<result column="attr_num4" property="attrNum4"/>
	<result column="attr_num5" property="attrNum5"/>
	<result column="attr_dte1" property="attrDte1"/>
	<result column="attr_dte2" property="attrDte2"/>
	<result column="attr_dte3" property="attrDte3"/>
	<result column="attr_dte4" property="attrDte4"/>
	<result column="attr_dte5" property="attrDte5"/>
	<result column="createby" property="createby"/>
	<result column="createdate" property="createdate"/>
	<result column="lastmodifyby" property="lastmodifyby"/>
	<result column="lastmodifydate" property="lastmodifydate"/>
	<result column="p_enterpriseid" property="pEnterpriseid"/>
	<result column="p_enterprisename" property="pEnterprisename"/>
	<result column="consignorcode" property="consignorcode"/>
</resultMap> -->

	<select id="queryOmsAsnBillDRemainQty" resultType="java.util.Map">
		WITH 
		<!-- 收货行 -->
		table1 AS (
		SELECT
			tX1.asn_line_id,
			coalesce( sum( tX1.PIECE_QTY ), 0 ) TOTAL_RCV_PIECE_QTY 
		FROM
			WMS_IN_RCV_LINES tX1 
		WHERE
			
			1=1
			<if test="lineids != null and lineids.size() >0 ">
			and tX1.asn_line_id IN 
			<foreach collection="lineids" item="item" open="("
				separator="," close=")">
				#{item}
			</foreach>
			</if>
		and tX1.src_line_id is null
		GROUP BY
			tX1.asn_line_id 
		),
		<!-- 拒收 -->
		table2 AS (
		SELECT
			asn_line_id,
			coalesce( sum( PIECE_QTY ), 0 ) TOTAL_RCV_PIECE_QTY 
		FROM
			WMS_IN_TASK_REJECTED_DETAIL 
		WHERE
			1=1
		<if test="lineids != null and lineids.size() >0 ">
			and asn_line_id IN 
			<foreach collection="lineids" item="item" open="("
				separator="," close=")">
				#{item}
			</foreach>
		</if>
		GROUP BY
			asn_line_id 
		),
		<!-- 异常收货行 -->
		table3 AS (
		SELECT
		attr_str1 as asn_line_id,
		coalesce( sum( affirmed_qty ), 0 ) TOTAL_RCV_PIECE_QTY
		FROM
		IN_RECEIVING_ABNORMAL_D
		WHERE
		1=1
		<if test="lineids != null and lineids.size() >0 ">
			and attr_str1 IN
			<foreach collection="lineids" item="item" open="("
					 separator="," close=")">
				#{item}
			</foreach>
		</if>
		GROUP BY
		attr_str1
		)
		SELECT
		t1.lineid as "lineid",
		(
		    coalesce(t1.ITEM_QTY,0) + coalesce(t1.FREE_QTY,0)
		    - coalesce(table1.TOTAL_RCV_PIECE_QTY,0)
		    - coalesce(table2.TOTAL_RCV_PIECE_QTY,0)
			- coalesce(table3.TOTAL_RCV_PIECE_QTY,0)
		) as "remainQty"
		FROM
			OMS_ASN_BILL_D t1
			LEFT JOIN table1 ON table1.asn_line_id = t1.LINEID
			LEFT JOIN table2 ON table2.asn_line_id = t1.LINEID
		    LEFT JOIN table3 ON table3.asn_line_id = t1.LINEID
		WHERE
			1=1
		<if test="lineids != null and lineids.size() >0 ">
			and t1.LINEID IN 
			<foreach collection="lineids" item="item" open="("
				separator="," close=")">
				#{item}
			</foreach>
		</if>
		
	</select>

	<select id="findByBillId" resultType="com.prolog.cs.biz.inbound.model.OmsAsnBillH">
select
	ITEMCODE,
	PDATE_FROM,
	VALID_UNTIL,
	CONSIGNORID,
	CONSIGNORNAME,
	SHIPPERID,
	CONSIGNORCODE
from oms_asn_bill_h
where BILLID = #{billId}

	</select>
</mapper>
