<?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.IncomingRecordHMapper">
	<resultMap id="map" type="com.prolog.cs.biz.inbound.model.IncomingRecordH">
		<result column="billid" property="billid"/>
		<result column="billno" property="billno"/>
		<result column="billtype" property="billtype"/>
		<result column="branchid" property="branchid"/>
		<result column="branchcode" property="branchcode"/>
		<result column="branchname" property="branchname"/>
		<result column="consignorid" property="consignorid"/>
		<result column="consignorcode" property="consignorcode"/>
		<result column="consignorname" property="consignorname"/>
		<result column="consignor_bill_no" property="consignorBillNo"/>
		<result column="refbilltype" property="refbilltype"/>
		<result column="refbillid" property="refbillid"/>
		<result column="refbillcode" property="refbillcode"/>
		<result column="purorg_id" property="purorgId"/>
		<result column="purorg_name" property="purorgName"/>
		<result column="buyer_id" property="buyerId"/>
		<result column="buyer_name" property="buyerName"/>
		<result column="shipper_type" property="shipperType"/>
		<result column="shipperid" property="shipperid"/>
		<result column="shippercode" property="shippercode"/>
		<result column="shippername" property="shippername"/>
		<result column="arrive_address" property="arriveAddress"/>
		<result column="vendorid" property="vendorid"/>
		<result column="vendorcode" property="vendorcode"/>
		<result column="vendorname" property="vendorname"/>
		<result column="acc_goods_bill_no" property="accGoodsBillNo"/>
		<result column="vpo_no" property="vpoNo"/>
		<result column="vendor_receive_date" property="vendorReceiveDate"/>
		<result column="vendor_address" property="vendorAddress"/>
		<result column="refbillno" property="refbillno"/>
		<result column="totpcs" property="totpcs"/>
		<result column="totqty" property="totqty"/>
		<result column="totnetamt" property="totnetamt"/>
		<result column="totamt" property="totamt"/>
		<result column="operatedate" property="operatedate"/>
		<result column="operator" property="operator"/>
		<result column="confirmer" property="confirmer"/>
		<result column="qtoperator" property="qtoperator"/>
		<result column="qtoperator2" property="qtoperator2"/>
		<result column="rcvoperator" property="rcvoperator"/>
		<result column="rcvoperator2" property="rcvoperator2"/>
		<result column="arrivedate" property="arrivedate"/>
		<result column="rcvfromdate" property="rcvfromdate"/>
		<result column="qcdate" property="qcdate"/>
		<result column="paymentdaytype" property="paymentdaytype"/>
		<result column="transferoperatorid" property="transferoperatorid"/>
		<result column="transferoperatorname" property="transferoperatorname"/>
		<result column="remark" property="remark"/>
		<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="datasource" property="datasource"/>
		<result column="building" property="building"/>
	</resultMap>
	<sql id="mainClomuns" >
		t1.billid,
		t1.billno,
		t1.billtype,
		t1.branchid,
		t1.branchcode,
		t1.branchname,
		t1.consignorid,
		t1.consignorcode,
		t1.consignorname,
		t1.consignor_bill_no,
		t1.refbilltype,
		t1.refbillid,
		t1.refbillcode,
		t1.purorg_id,
		t1.purorg_name,
		t1.buyer_id,
		t1.buyer_name,
		t1.shipper_type,
		t1.shipperid,
		t1.shippercode,
		t1.shippername,
		t1.arrive_address,
		t1.vendorid,
		t1.vendorcode,
		t1.vendorname,
		t1.acc_goods_bill_no,
		t1.vpo_no,
		t1.vendor_receive_date,
		t1.vendor_address,
		t1.refbillno,
		t1.totpcs,
		t1.totqty,
		t1.totnetamt,
		t1.totamt,
		t1.operatedate,
		t1.operator,
		t1.confirmer,
		t1.qtoperator,
		t1.qtoperator2,
		t1.rcvoperator,
		t1.rcvoperator2,
		t1.arrivedate,
		t1.rcvfromdate,
		t1.qcdate,
		t1.paymentdaytype,
		t1.transferoperatorid,
		t1.transferoperatorname,
		t1.remark,
		t1.attr_str1,
		t1.attr_str2,
		t1.attr_str3,
		t1.attr_str4,
		t1.attr_str5,
		t1.attr_str6,
		t1.attr_str7,
		t1.attr_str8,
		t1.attr_str9,
		t1.attr_str10,
		t1.attr_num1,
		t1.attr_num2,
		t1.attr_num3,
		t1.attr_num4,
		t1.attr_num5,
		t1.attr_dte1,
		t1.attr_dte2,
		t1.attr_dte3,
		t1.attr_dte4,
		t1.attr_dte5,
		t1.createby,
		t1.createdate,
		t1.lastmodifyby,
		t1.lastmodifydate,
		t1.p_enterpriseid,
		t1.p_enterprisename,
		t1.datasource,
		t1.building,
	</sql>
	<select id="getInsertData" resultMap="map">
		SELECT DISTINCT
			  T1.RCV_HEADER_ID AS BILLID,
			  T1.AUTO_BILL_NUM AS BILLNO,
			  coalesce(T1.RCV_TYPE,T2.BILLTYPE) as BILLTYPE,
			  T2.BRANCHID,
			  T2.BRANCHCODE,
			  T2.BRANCHNAME,
			  T2.CONSIGNORID,
			  coalesce(T2.CONSIGNORCODE ,t3.mtl_owner_num ,T2.CONSIGNORID) CONSIGNORCODE,
			  T2.CONSIGNORNAME,
			  coalesce(t2.CONSIGNOR_BILLNO, '-') AS CONSIGNOR_BILL_NO,  
			  coalesce(T2.REF_BILLTYPE,'-') AS REFBILLTYPE,
        	  t2.BILLID AS REFBILLID,
              t2.BILLNO AS REFBILLCODE,
			  coalesce( t1.ORGN_ID, 'N/A') AS PURORG_ID,
			  coalesce( t3.ORGN_NAME, 'N/A') AS PURORG_NAME,
			  coalesce(t2.BUYER_ID, 'N/A') AS BUYER_ID,
			  coalesce(t2.BUYER_NAME, 'N/A') AS BUYER_NAME,
			  '' AS ARRIVE_ADDRESS,
			  T2.SHIPPER_TYPE,
			  REGEXP_REPLACE(T2.SHIPPERID, '(\w+)(-)+(\w+)', '\3') SHIPPERID,
			  T2.SHIPPERCODE,
			  T2.SHIPPERNAME,
			CASE
			    T2.SHIPPER_TYPE 
			    WHEN '2' THEN
			    T2.SHIPPERNAME ELSE '' 
			  END AS VENDORNAME,
			CASE
			    T2.SHIPPER_TYPE 
			    WHEN '2' THEN
			    T2.SHIPPERCODE ELSE '' 
			  END AS VENDORCODE,
			  '' AS ACC_GOODS_BILL_NO,
			  '' AS VPO_NO,
			  NULL AS VENDOR_RECEIVE_DATE,
			CASE
			    T2.SHIPPER_TYPE 
			    WHEN '2' THEN
			    T2.SHIPMENT_ADDRESS ELSE '' 
			  END AS VENDOR_ADDRESS,
			  coalesce(T2.REF_BILLNO,t2.BILLNO) AS REFBILLNO,
			  (
			  SELECT
			    COALESCE( SUM( COALESCE( L1.ODD_CASE_QTY, 0 ) ), 0 ) 
			  FROM
			    WMS_IN_RCV_INSTOCK_LINES L1 
			  WHERE
			    L1.RCV_HEADER_ID = T1.RCV_HEADER_ID 
			  ) AS TOTPCS, 
			  (
			  SELECT
			    COALESCE( SUM( COALESCE( L1.PIECE_QTY, 0 ) ), 0 ) 
			  FROM
			    WMS_IN_RCV_INSTOCK_LINES L1 
			  WHERE
			    L1.RCV_HEADER_ID = T1.RCV_HEADER_ID 
			  ) AS TOTQTY, 
			  (
			  SELECT
			    COALESCE( SUM( COALESCE( L1.PIECE_QTY, 0 ) * COALESCE( L2.PIECE_PRICE_TI, 0 ) ), 0 ) 
			  FROM
			    WMS_IN_RCV_INSTOCK_LINES L1
			    LEFT JOIN WMS_IN_RCV_LINES_PROFILES L2 ON L1.RCV_LINE_ID = L2.RCV_LINE_ID 
			  WHERE
			    L1.RCV_HEADER_ID = T1.RCV_HEADER_ID 
			  ) AS TOTNETAMT, 
			  (
			  SELECT
			    COALESCE( SUM( COALESCE( L1.PIECE_QTY, 0 ) * COALESCE( L2.PIECE_PRICE_TE, 0 ) ), 0 ) 
			  FROM
			    WMS_IN_RCV_INSTOCK_LINES L1
			    LEFT JOIN WMS_IN_RCV_LINES_PROFILES L2 ON L1.RCV_LINE_ID = L2.RCV_LINE_ID 
			  WHERE
			    L1.RCV_HEADER_ID = T1.RCV_HEADER_ID 
			  ) AS TOTAMT, 
			  NOW()  AS OPERATEDATE,
			  T2.CREATEBY AS OPERATOR, 
			  T1.CONFIRMED_NAME AS CONFIRMER,
			  (SELECT  group_concat( DISTINCT <!-- t4.QC_OPERATOR1 --> t4.MODIFIER separator ',')
			   FROM WMS_IN_RQC_HEADERS t4 WHERE t4.RCV_HEADER_ID = T1.RCV_HEADER_ID GROUP BY t4.RCV_HEADER_ID
			   ) AS QTOPERATOR,
			  '' AS QTOPERATOR2,
			  T1.MODIFIER AS RCVOPERATOR,
<!-- 			  T1.EXECUTOR_USER AS RCVOPERATOR, -->
			  '' AS RCVOPERATOR2,
			  T1.COMMENTS AS REMARK,
			  'N' as ATTR_STR1,
			  T2.ATTR_STR2,
			  T2.ATTR_STR3,
			  T2.ATTR_STR4,
			  T2.ATTR_STR5,
			  T2.ATTR_STR6,
			  T2.ATTR_STR7,
			  T2.ATTR_STR8,
			  T2.ATTR_STR9,
			  T2.ATTR_STR10,
			  T2.ATTR_NUM1,
			  T2.ATTR_NUM2,
			  T2.ATTR_NUM3,
			  T2.ATTR_NUM4,
			  T2.ATTR_NUM5,
			  T2.ATTR_DTE1,
			  T2.ATTR_DTE2,
			  T2.ATTR_DTE3,
			  T2.ATTR_DTE4,
			  T2.ATTR_DTE5,
			  T2.CREATEBY,
			  T2.CREATEDATE,
			  T2.LASTMODIFYBY,
			  T2.LASTMODIFYDATE,
			  T2.P_ENTERPRISEID,
			  T2.P_ENTERPRISENAME ,
			  '3' as datasource,
			  '' as building
		FROM
		  WMS_IN_RCV_HEADERS T1
		  LEFT JOIN OMS_ASN_BILL_H T2 ON T1.ASN_HEADER_ID = T2.BILLID
		  LEFT JOIN WMS_IN_RCV_HEADERS_PROFILES T3 ON T1.RCV_HEADER_ID = T3.RCV_HEADER_ID
		  WHERE 1=1 
		<if test="rcvHeaderIds != null  and rcvHeaderIds.size >0 ">
			 AND T1.RCV_HEADER_ID IN 
            <foreach collection="rcvHeaderIds" item="item" open="("
                     separator="," close=")">
                #{item}
            </foreach>
		</if>
		<if test="asnHeaderId != null  and asnHeaderId !=''">
			and T1.ASN_HEADER_ID = #{asnHeaderId}
		</if>
		        
	</select>
	
</mapper>
