<?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.IncomingRecordBackDMapper">
	<select id="getInsertData" resultType="com.prolog.cs.biz.inbound.vo.IncomingRecordBackDVO">
		SELECT
			t5.AUTO_BILL_NUM AS billno,
			t7.LINE_NUM AS seqNo,
			t2.SEQNO AS refSeqNo,
			coalesce(t2.CONSIGNORCODE,t2.CONSIGNORID) as consignorcode,
			t2.ITEMCODE,
			t2.ITEMID,
			t2.ITEMNAME,
			t1.MANUFACTURER_LOT_NUM AS lotNo,
			t7.BATCH_PRODUCTION_DATE AS pdateFrom,
			t7.BATCH_EXPIRED_DATE as validUntil,
			t7.ATTRIBUTE2 as locNo,
			t1.ODD_CASE_QTY AS pcs,
			t1.ODD_PIECE_QTY AS odd,
			t1.PIECE_QTY AS qty,
			0 AS rejectQty,
			'' AS reasonCode,
			t1.RCV_HEADER_ID AS rcvHeaderId,
			t1.RCV_LINE_ID AS rcvLineId,
			t1.INVENTORY_STATUS stockStatus,
			t4.sterilizeexpdate sterilizationDate,
			t4.STERILIZELOTNO sterilizationLotNo,
			t8.ITEM_HEALTH_INDUSTRY as industryline,
			t8.billno as refBillNo,
			t8.billid as refBillId,
			t8.BILLTYPE as billType,
			t1.WH_AREA_CATEGORY_CODE as whAreaCategoryCode,
			t1.WH_AREA_CATEGORY_NAME as whAreaCategoryName,
			bmiw.SEGMENT43  as itemIsGift,
			t7.gmt_modified as stockInTime
			FROM wms_in_rcv_instock_lines t1
			LEFT JOIN OMS_ASN_BILL_D t2 ON t1.ASN_LINE_ID = t2.LINEID
			LEFT JOIN OMS_ASN_BILL_h t8 ON t8.BILLID = t2.BILLID and t8.BRANCHID = t2.BRANCHID
			LEFT JOIN WMS_IN_RCV_LINES_PROFILES t4 ON t1.RCV_LINE_ID = t4.RCV_LINE_ID
			LEFT JOIN WMS_IN_RCV_HEADERS t5 ON t1.RCV_HEADER_ID = t5.RCV_HEADER_ID
			LEFT JOIN WMS_IN_RCV_LINES t7 ON t1.RCV_LINE_ID = t7.RCV_LINE_ID
			left join upcloud_base_item_qingniu.base_mtl_item_wms bmiw on bmiw.ITEM_ID  = t1.ITEM_ID
		WHERE 1=1
		<if test="rcvHeaderId != null  and rcvHeaderId !=''">
			and T1.RCV_HEADER_ID = #{rcvHeaderId}
		</if>
	</select>

	<select id="getInsertDataReject" resultType="com.prolog.cs.biz.inbound.vo.IncomingRecordBackDVO">
		SELECT
		t5.AUTO_BILL_NUM AS billno,
		t1.LINE_NUM AS seqNo,
		t2.SEQNO AS refSeqNo,
		coalesce(t2.CONSIGNORCODE,t2.CONSIGNORID) as consignorcode,
		t2.ITEMCODE,
		t2.ITEMID,
		t1.MANUFACTURER_LOT_NUM AS lotNo,
		t1.BATCH_PRODUCTION_DATE AS pdateFrom,
		t1.BATCH_EXPIRED_DATE as validUntil,
		t1.ATTRIBUTE2 as locNo,
		t1.ODD_CASE_QTY AS pcs,
		t1.ODD_PIECE_QTY AS odd,
		CASE
		WHEN t1.STOCK_STATUS = '3' then t1.PIECE_QTY
		ELSE 0
		END AS rejectQty,
		CASE
		WHEN t1.STOCK_STATUS = '3' then 0
		ELSE t1.PIECE_QTY
		END AS qty,
		t1.RCV_HEADER_ID AS rcvHeaderId,
		t1.RCV_LINE_ID AS rcvLineId,
		CASE
		WHEN t1.STOCK_STATUS = '3' then '4'
		ELSE t1.STOCK_STATUS
		END stockStatus,
		CONCAT(t1.RCV_REASON1,'-',t1.RCV_REASON2) reasonCode,
		t4.sterilizeexpdate sterilizationDate,
		t4.STERILIZELOTNO sterilizationLotNo
		FROM WMS_IN_RCV_LINES t1
		LEFT JOIN OMS_ASN_BILL_D t2 ON t1.ASN_LINE_ID = t2.LINEID
		LEFT JOIN WMS_IN_RCV_LINES_PROFILES t4 ON t1.RCV_LINE_ID = t4.RCV_LINE_ID
		LEFT JOIN WMS_IN_RCV_HEADERS t5 ON t1.RCV_HEADER_ID = t5.RCV_HEADER_ID
		WHERE 1=1
		<if test="rcvHeaderId != null  and rcvHeaderId !=''">
			and t1.RCV_HEADER_ID = #{rcvHeaderId}
		</if>
	</select>
</mapper>
