<?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.task.TaskRejectedDetailMapper">
	<resultMap id="map" type="com.prolog.cs.biz.inbound.dto.task.TaskRejectedDetailDTO">
		<result column="task_rejected_id" property="taskRejectedId"/>
		<result column="warehouse_id" property="warehouseId"/>
		<result column="src_bill_type" property="srcBillType"/>
		<result column="src_header_id" property="srcHeaderId"/>
		<result column="src_line_id" property="srcLineId"/>
		<result column="asn_header_id" property="asnHeaderId"/>
		<result column="asn_line_id" property="asnLineId"/>
		<result column="rcv_header_id" property="rcvHeaderId"/>
		<result column="rcv_line_id" property="rcvLineId"/>
		<result column="item_id" property="itemId"/>
		<result column="qty" property="qty"/>
		<result column="uom" property="uom"/>
		<result column="conversion_rate" property="conversionRate"/>
		<result column="piece_qty" property="pieceQty"/>
		<result column="batch_id" property="batchId"/>
		<result column="batch_num" property="batchNum"/>
		<result column="vendor_batch_num" property="vendorBatchNum"/>
		<result column="active_flag" property="activeFlag"/>
		<result column="remain_qty" property="remainQty"/>
		<result column="result" property="result"/>
		<result column="reason" property="reason"/>
		<result column="manufacturer_lot_num" property="manufacturerLotNum"/>
		<result column="container_code" property="containerCode"/>
		<result column="goods_case_code" property="goodsCaseCode"/>
		<result column="executor" property="executor"/>
		<result column="status" property="status"/>
		<result column="mtl_owner_id" property="mtlOwnerId"/>
		<!-- <result column="segment1" property="segment1"/>
		<result column="segment2" property="segment2"/>
		<result column="attribute1" property="attribute1"/>
		<result column="attribute2" property="attribute2"/>
		<result column="gmt_create" property="gmtCreate"/>
		<result column="creator" property="creator"/>
		<result column="creator_name" property="creatorName"/>
		<result column="gmt_modified" property="gmtModified"/>
		<result column="modifier" property="modifier"/>
		<result column="modifier_name" property="modifierName"/>
		<result column="enterprise_id" property="enterpriseId"/> -->
	</resultMap>
	<sql id="mainClomuns" >
		t1.task_rejected_id,
		t1.warehouse_id,
		t1.src_bill_type,
		t1.src_header_id,
		t1.src_line_id,
		t1.asn_header_id,
		t1.asn_line_id,
		t1.rcv_header_id,
		t1.rcv_line_id,
		t1.item_id,
		t1.qty,
		t1.uom,
		nvl(t1.conversion_rate,1) as conversion_rate,
		nvl(t1.piece_qty,0) as piece_qty,
		floor(nvl(t1.piece_qty,0)/nvl(t1.conversion_rate,1)) as caseQty,
		mod(nvl(t1.piece_qty,0),nvl(t1.conversion_rate,1)) as oddPieceQty,
		t1.batch_id,
		t1.batch_num,
		t1.vendor_batch_num,
		t1.active_flag,
		t1.remain_qty,
		t1.result,
		t1.reason,
		t1.manufacturer_lot_num,
		t1.container_code,
		t1.goods_case_code,
		t1.executor,
		t1.status,
		t1.mtl_owner_id,
		<!-- t1.segment1,
		t1.segment2,
		t1.attribute1,
		t1.attribute2,
		t1.gmt_create,
		t1.creator,
		t1.creator_name,
		t1.modifier,
		t1.modifier_name,
		t1.enterprise_id, -->
		t1.gmt_modified,
	</sql>
	<resultMap id="rcvMap"
		type="com.prolog.cs.biz.inbound.dto.rcv.RcvHDTO">
		<result column="rcv_header_id" property="rcvHeaderId"/>
		<result column="auto_bill_num" property="autoBillNum"/>
		<result column="custom_bill_num" property="customBillNum"/>
		<result column="executor_user" property="executorUser"/>
		
		<result column="rcv_date" property="rcvDate"/>
		<result column="rcv_type" property="rcvType"/>
		<result column="status" property="status"/>
		<result column="case_qty" property="caseQty"/>
		<result column="piece_qty" property="pieceQty"/>
		<result column="access_level" property="accessLevel"/>
		<result column="comments" property="comments"/>
		<result column="mtl_owner_id" property="mtlOwnerId"/>
		<result column="warehouse_id" property="warehouseId"/>
		<result column="asn_header_id" property="asnHeaderId"/>
		
		<result column="print_count" property="printCount"/>
		<result column="print_date" property="printDate"/>
		<result column="audit_date" property="auditDate"/>
		<result column="confirm_date" property="confirmDate"/>
		<result column="audited_name" property="auditedName"/>
		<result column="printed_name" property="printedName"/>
		<result column="confirmed_name" property="confirmedName"/>
		<result column="vendor_id" property="vendorId"/>
		<result column="src_bill_type" property="srcBillType"/>
		<result column="src_header_id" property="srcHeaderId"/>
		<!-- profile字段 -->
		<result column="ship_type" property="shipType"/>
		<result column="arrival_mode" property="arrivalMode"/>
		<result column="vendor_name" property="vendorName"/>
		<result column="warehouse_name" property="warehouseName"/>
		<result column="buyer_mane" property="buyerMane"/>
		<result column="customer_name" property="customerName"/>
		<result column="mtl_owner_name" property="mtlOwnerName"/>

	</resultMap>
	<select id="extractRcvHWithNativeSql" resultMap="rcvMap">
		SELECT
			t1.rcv_header_id,
			t1.auto_bill_num,
			t1.custom_bill_num,
			t1.executor_user,
			t1.rcv_date,
			t1.rcv_type,
			t1.status,
			t1.case_qty,
			t1.piece_qty,
			t1.access_level,
			t1.comments,
			t1.mtl_owner_id,
			t1.warehouse_id,
			t1.asn_header_id,
			t1.vendor_id,
			t1.src_header_id,
			t1.src_bill_type,
			t1.print_count,
			t1.print_date,
			t1.audit_date,
			t1.confirm_date,
			t1.audited_name,
			t1.printed_name,
			t1.confirmed_name,
			t2.mtl_owner_num as mtlOwnerNum,
			t2.ship_type,
			t2.arrival_mode,
			t2.vendor_name,
			t2.warehouse_name,
			t2.buyer_mane,
			t2.customer_name,
			t2.mtl_owner_name,
			nvl(t1.need_user,t2.BUYER_MANE) as needUser,
			nvl(t1.need_dept,t2.ORGN_NAME) as needDept,
			t2.ORGN_NAME as orgnName,
			t1.AGENT  as agent,
			t1.ASN_REMARK as asnRemark,
			nvl(t2.vendor_num, t1.vendor_id)as vendorNum,
			t2.orgn_num as orgnNum,
			t1.shipper_zjm as shipperZjm,
			t1.ASN_AUTO_BILL_NUM as asnBillNum,
			t1.ASN_AUTO_BILL_NUM as asnAutoBillNum,
			t2.transportation_party as transportationParty,
			t2.carriername as carriername,
			t2.ship_from_date as shipFromDate,
			t2.ship_from_add as shipFromAdd,
			t2.on_passage_days as onPassageDays,
			t2.ship_arrive_tem_perature as shipArriveTemPerature,
			t2.ship_arrive_time as shipArriveTime,
			t2.ship_tem_perature as shipTemPerature,
			t2.ship_date as shipDate,
			t2.device_no as deviceNo,
			t2.tempcontrol_mode as tempcontrolMode,
			t2.tempcontrol_status as tempcontrolStatus,
			t1.EXECUTOR_USER as rcvUser,
			t1.auto_bill_num as rcvNo,
			t1.COMMENTS as rcvRemark
		FROM
			wms_in_rcv_headers t1,
			wms_in_rcv_headers_profiles t2 
		WHERE
			t1.rcv_header_id = t2.RCV_HEADER_ID 
			and t1.rcv_header_id IN (
				SELECT DISTINCT
					t1.RCV_HEADER_ID 
				FROM
					wms_in_task_rejected_detail t1 
					LEFT JOIN WMS_IN_RCV_LINES t2 ON t1.RCV_LINE_ID = t2.RCV_LINE_ID
				WHERE
					1 = 1
					<if test="itemZjm != null  and itemZjm !=''">
					and UPPER(t2.ITEM_ZJM) like CONCAT(CONCAT('%',UPPER(#{itemZjm})),'%') 
					</if>
					<if test="bldgId != null  and bldgId !=''">
					AND t2.BLDG_ID  = #{bldgId}
					</if>
					and
					( t1.status = '1' OR t1.status IS NULL ) 
					AND t1.RCV_HEADER_ID IS NOT NULL 
					AND t1.ENTERPRISE_ID = #{enterpriseId}
					GROUP BY
						t1.RCV_HEADER_ID 
			)
		<if test="rcvUser != null  and rcvUser !=''">
			and UPPER(t1.EXECUTOR_USER) like CONCAT(CONCAT('%',UPPER(#{rcvUser})),'%')
		</if>
		<if test="enterpriseId != null  and enterpriseId !=''">
			and t1.enterprise_id = #{enterpriseId}
		</if>
		<if test="rcvNo != null  and rcvNo !=''">
			and UPPER(t1.auto_bill_num) like CONCAT(CONCAT('%',UPPER(#{rcvNo})),'%') 
		</if>
		<if test="billNo != null  and billNo !=''">
			and UPPER(t1.ASN_AUTO_BILL_NUM) like CONCAT(CONCAT('%',UPPER(#{billNo})),'%') 
		</if>
		<if test="shipperZjm != null  and shipperZjm !=''">
			and UPPER(t1.SHIPPER_ZJM) like CONCAT(CONCAT('%',UPPER(#{shipperZjm})),'%') 
		</if>
		<if test="startTime != null">
			and t1.rcv_date &gt;= #{startTime}
		</if>
		<if test="endTime != null">
			and t1.rcv_date &lt;= #{endTime}
		</if>
		<if test="warehouseId != null  and warehouseId !=''">
			and t1.warehouse_id = #{warehouseId}
		</if>
		<if test="mtlOwnerId != null  and mtlOwnerId !=''">
			and t1.mtl_owner_id = #{mtlOwnerId}
		</if>
		<if test="status != null  and status !=''">
			and t1.status = #{status}
		</if>
		<if test="autoBillNum != null  and autoBillNum !=''">
			and UPPER(t1.auto_bill_num) like CONCAT(CONCAT('%',UPPER(#{autoBillNum})),'%')  
		</if>
		<if test="rcvType != null  and rcvType !=''">
			and t1.rcv_type = #{rcvType}
		</if>
		<if test="customBillNum != null  and customBillNum !=''">
			and UPPER(t1.custom_bill_num) like CONCAT(CONCAT('%',UPPER(#{customBillNum})),'%')
		</if>
		<if test="asnAutoBillNum != null  and asnAutoBillNum !=''">
			and UPPER(t1.asn_auto_bill_num) like CONCAT(CONCAT('%',UPPER(#{asnAutoBillNum})),'%')
			<!-- and UPPER(t1.asn_auto_bill_num) like CONCAT(UPPER(#{asnAutoBillNum}),'%') -->
		</if>
		<!-- 需要加入rcvType类型,过滤掉其他的不需要看的数据任务 -->
		<!-- and t1.rcv_type not in ('5','12','13','14','15') -->
		order by t1.rcv_date desc,t1.ASN_AUTO_BILL_NUM desc
	</select>
	<select id="findList" resultMap="map">
		select
		distinct
		<include refid="mainClomuns"></include>
		t2.WAREHOUSE_AREA_CODES as warehouseAreaCodes,
		t2.ITEM_IS_PIECE_OR_CASE as itemIsPieceOrCase,
		t2.WAREHOUSE_AREA_CATEGORYS as warehouseAreaCategorys,
		t2.WAREHOUSE_NAME as warehouseName,
		t2.WAREHOUSE_AREA_NAME as warehouseAreaName,
		t2.batch_production_date as  batchProductionDate,
		t2.batch_expired_date as batchExpiredDate,
		t2.ERP_BATCH_NUM as erpBatchNum,
		t2.PRODUCTION_PLACE as productionPlace,
		
		t3.ITEM_NUM as itemNum,
		t3.ITEM_NAME as itemName,
		t3.CATEGORY_NAME as category,
		t3.ITEM_BARCODE as itemBarcode,
		
		t3.specification as specification,
		t3.ITEM_HEALTH_LISTED_LICENSOR as itemHealthListedLicensor,
		t3.ITEM_HEALTH_PRODUCE_LICENSE as itemHealthProduceLicense,
		t3.ITEM_HEALTH_STORE_CATEGORY as itemHealthStoreCategory,
		t3.ITEM_IS_RECHECK as itemIsRecheck,
		t3.IS_INSTRUMENT as isInstrument,
		t3.ITEM_IS_IMPORTED as itemIsImported,
		
		t3.MANUFACTURER_ID as manufacturerId,
		t3.MANUFACTURER_CODE as manufacturerCode,
		t3.MANUFACTURER_NAME as manufacturerName,
		t3.ITEM_HEALTH_REGISTER_NBR as itemHealthRegisterNbr,
		t3.ITEM_HEALTH_COMMON_NAME as itemHealthCommonName,
		t3.RETURN_INFO as returnInfo,
		t3.RETURN_COLD_QUALITY as returnColdQuality,
		t3.RETURN_EXPLAIN as returnExplain,
		t3.IS_LOCK as isLock,
		t3.IS_HOSTED as isHosted,
		t3.ITEM_IS_SPECIAL as itemIsSpecial,
		t3.IS_BACTERIA as isBacteria,
		t3.SHELF_LIFE_DAYS as shelfLifeDays,
		t3.SHELF_LIFE_UNIT as shelfLifeUnit,
		
		t2.bldg_name as bldgName,
		t2.bldg_code as bldgCode,
		
		t2.DELEGATION_PARTY_ID as delegationPartyId,
		t2.DELEGATION_PARTY_NAME as delegationPartyName,
		t2.SHOW_PRODUCTION_DATE as showProductionDate,
		t2.SHOW_EXPIRED_DATE as showExpiredDate,
		t2.WAREHOUSE_ROOM_CODES as warehouseRoomCodes,
		t2.PIECE_CONVERSION_RATE as pieceConversionRate,
		t2.ASN_LINE_REMARK as asnLineRemark,
		t2.ITEM_ZJM as itemZjm,
		t4.rcv_type as rcvType,
	<!-- 收货数量 -->
	(nvl((SELECT SUM(nvl(t5.PIECE_QTY,0)) FROM WMS_IN_RCV_LINES t5 WHERE t5.ASN_LINE_ID = t1.ASN_LINE_ID <!-- and t5.STOCK_STATUS &lt;&gt; '3' -->),0) - nvl(t1.piece_qty, 0 )) as rcvPieceQty,
	<!--  拒收数量 -->
	nvl(t1.piece_qty, 0 ) AS rejectPieceQty,
	<!-- asn数量 -->
	nvl((SELECT nvl(ITEM_QTY,0)+ nvl(FREE_QTY,0) FROM OMS_ASN_BILL_D t6 WHERE t6.LINEID = t1.ASN_LINE_ID),0) as poPieceQty
		
		FROM
		wms_in_task_rejected_detail t1
		LEFT JOIN
		WMS_IN_RCV_LINES
		t2
		on t1.rcv_line_id = t2.RCV_LINE_ID
		LEFT JOIN
		WMS_IN_RCV_LINES_PROFILES t3
		on t1.rcv_line_id = t3.RCV_LINE_ID
		LEFT JOIN
		wms_in_rcv_headers  t4
		on t4.rcv_header_id = t1.rcv_header_id
		where
		1=1
		<if test="enterpriseId != null  and enterpriseId !=''">
			and t1.enterprise_id = #{enterpriseId}
		</if>
		<if test="warehouseId != null  and warehouseId !=''">
			and t1.warehouse_id = #{warehouseId}
		</if>
		<if test="mtlOwnerId != null  and mtlOwnerId !=''">
			and t1.mtl_owner_id = #{mtlOwnerId}
		</if>
		<if test="rcvHeaderId != null  and rcvHeaderId !=''">
			and t1.rcv_header_id = #{rcvHeaderId}
		</if>
		<if test="itemId != null  and itemId !=''">
			and t1.item_id = #{itemId}
		</if>
		<if test="rcvLineId != null  and rcvLineId !=''">
			and t1.rcv_line_id = #{rcvLineId}
		</if>
		<if test="status != null  and status !=''">
			and t1.status = #{status}
		</if>
		order by t1.gmt_modified
		desc
	</select>
</mapper>
