<?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.ReceivingRecordBackHMapper">
	<resultMap id="map" type="com.prolog.cs.biz.inbound.model.ReceivingRecordBackH">
		<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="billtype_style" property="billtypeStyle"/>
		<result column="refbillid" property="refbillid"/>
		<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="refbillno" property="refbillno"/>
		<result column="operatedate" property="operatedate"/>
		<result column="rcvoperator" property="rcvoperator"/>
		<result column="rcvoperator2" property="rcvoperator2"/>
		<result column="rcv_date" property="rcvdate"/>
		<result column="arrival_mode" property="arrivalMode"/>
		<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="type" property="type"/>
		<result column="upload_status" property="uploadStatus"/>
		<result column="bill_date" property="billdate"/>
		<result column="GL_BILLNO" property="glBillno"/>
	</resultMap>
	<select id="getRcvInsertData" resultMap="map">
		SELECT DISTINCT
			  T1.RCV_HEADER_ID AS BILLID,
			  T1.AUTO_BILL_NUM AS BILLNO,
			  CASE
			  WHEN T1.RCV_TYPE = '2' THEN '2'
			  ELSE '1'
			  END as BILLTYPE,
			  t2.BILLNO as REFBILLNO,
			  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,
			  CASE
			  WHEN T1.RCV_TYPE = '2' THEN '2'
			  ELSE '1'
			  END as billtype_style,
			  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,
			  coalesce(T2.REF_BILLNO,t2.BILLNO) AS REFBILLNO,
			  NOW()  AS OPERATEDATE,
			  T1.MODIFIER_NAME AS RCVOPERATOR,
			  T1.ATTRIBUTE3 AS RCVOPERATOR2,
			  T1.RCV_DATE,
			  T3.ARRIVAL_MODE,
			  T2.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 ,
			  T2.BILL_DATE,
			  '3' as datasource,
			  '10' as type,
			  '0' as upload_status
		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="rcvHeaderId != null  and rcvHeaderId !=''">
			and T1.RCV_HEADER_ID = #{rcvHeaderId}
		</if>
	</select>

	<select id="getRqcInsertData" resultMap="map">
		SELECT DISTINCT
		T1.RQC_HEADER_ID AS BILLID,
		T1.AUTO_BILL_NUM AS BILLNO,
		T4.AUTO_BILL_NUM AS GL_BILLNO,
		CASE
		WHEN T4.RCV_TYPE = '2' THEN '2'
		ELSE '1'
		END as BILLTYPE,
		T2.BRANCHID,
		T2.BRANCHCODE,
		T2.BRANCHNAME,
		T2.CONSIGNORID,
		t2.BILLNO as REFBILLNO,
		coalesce(T2.CONSIGNORCODE ,t3.mtl_owner_num ,T2.CONSIGNORID) CONSIGNORCODE,
		T2.CONSIGNORNAME,
		coalesce(t2.CONSIGNOR_BILLNO, '-') AS CONSIGNOR_BILL_NO,
		CASE
		WHEN T4.RCV_TYPE = '2' THEN '2'
		ELSE '1'
		END as billtype_style,
		coalesce( T4.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,
		coalesce(T2.REF_BILLNO,t2.BILLNO) AS REFBILLNO,
		NOW()  AS OPERATEDATE,
		T1.QC_OPERATOR1 AS RCVOPERATOR,
		T1.QC_OPERATOR2 AS RCVOPERATOR2,
		T4.RCV_DATE,
		T3.ARRIVAL_MODE,
		T2.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 ,
		T2.BILL_DATE,
		'3' as datasource,
		'20' as type,
		'0' as upload_status
		FROM
		WMS_IN_RQC_HEADERS T1
		LEFT JOIN WMS_IN_RCV_HEADERS T4 ON T1.RCV_HEADER_ID = T4.RCV_HEADER_ID
		LEFT JOIN WMS_IN_RCV_HEADERS_PROFILES T3 ON T1.RCV_HEADER_ID = T3.RCV_HEADER_ID
		LEFT JOIN OMS_ASN_BILL_H T2 ON T4.ASN_HEADER_ID = T2.BILLID
		WHERE 1=1
		<if test="rqcHeaderId != null  and rqcHeaderId !=''">
			and T1.RQC_HEADER_ID = #{rqcHeaderId}
		</if>
	</select>

</mapper>
