<?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.TaskExceptionDetailMapper">
	<resultMap id="mainMap" type="com.prolog.cs.biz.inbound.dto.task.TaskExceptionDetailDTO">
		<result column="task_exception_id" property="taskExceptionId"/>
		<result column="mtl_owner_id" property="mtlOwnerId"/>
		<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="manufacturer_lot_num" property="manufacturerLotNum"/>
		<result column="vendor_batch_num" property="vendorBatchNum"/>
		
		<result column="active_flag" property="activeFlag"/>
		<result column="remain_qty" property="remainQty"/>
		<result column="status" property="status"/>
		<result column="qc_status" property="qcStatus"/>
		<result column="result" property="result"/>
		<result column="reason" property="reason"/>
		<result column="comments" property="comments"/>
		<result column="executor" property="executor"/>
		<result column="container_code" property="containerCode"/>
		<result column="goods_case_code" property="goodsCaseCode"/>
		<result column="node_type" property="nodeType"/>
		<result column="node_task_id" property="nodeTaskId"/>
		<result column="data_version" property="dataVersion"/>
		
		<!-- <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"/> -->
		
		<!-- 额外字段  -->
		<result column="ITEM_VOLUME" property="itemVolume" />
		<result column="ITEM_WEIGHT" property="itemWeight" />
		<result column="WAREHOUSE_NAME" property="warehouseName" />
		<result column="WAREHOUSE_AREA_NAME"
			property="warehouseAreaName" />
		<result column="ITEM_NUM" property="itemNum" />
		<result column="ITEM_NAME" property="itemName" />
		<result column="CATEGORY_NAME" property="categoryName" />
		<result column="ITEM_BARCODE" property="itemBarcode" />
	</resultMap>
	<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"/>

		<!-- <result column="asn_auto_bill_num" property="asnAutoBillNum"/>
		<result column="buyer_id" property="buyerId"/>
		<result column="src_orgn_id" property="srcOrgnId"/>
		<result column="src_warehouse_id" property="srcWarehouseId"/>
		<result column="src_mtl_owner_id" property="srcMtlOwnerId"/>
		<result column="enterprise_id" property="enterpriseId"/>
		<result column="src_auto_bill_num" property="srcAutoBillNum"/>
		<result column="audited_by" property="auditedBy"/>
		<result column="confirmed_by" property="confirmedBy"/>
		<result column="printed_by" property="printedBy"/>
		<result column="status_as" property="statusAs"/>
		<result column="orgn_id" property="orgnId"/>
		<result column="customer_id" property="customerId"/>
		<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"/> -->
	</resultMap>
	<sql id="mainClomuns">
		t1.task_exception_id,
		t1.mtl_owner_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,
		t1.conversion_rate,
		t1.piece_qty,
		t1.batch_id,
		t1.batch_num,
		t1.vendor_batch_num,
		t1.manufacturer_lot_num,
		
		t1.active_flag,
		t1.remain_qty,
		t1.comments,
		t1.container_code,
		t1.goods_case_code,
		t1.status,
		t1.qc_status,
		t1.result,
		t1.reason,
		t1.executor,
		t1.node_type,
		t1.data_version,
		t1.node_task_id,
			
		t1.ENTERPRISE_ID,
		t1.GMT_CREATE as gmtCreate,
		t1.CREATOR,
		t1.CREATOR_NAME as creatorName,
		t1.GMT_MODIFIED as gmtModified,
		t1.MODIFIER,
		t1.MODIFIER_NAME as modifierName,
		
		floor(nvl(t1.qty,0)) as caseQty,
		mod(nvl(t1.piece_qty,0),nvl(t1.conversion_rate,1)) as oddPieceQty,
	</sql>
	<select id="findByPkId" resultMap="mainMap" parameterType="String">
		select
		<include refid="mainClomuns"></include>
		t2.ITEM_VOLUME,
		t2.ITEM_WEIGHT,
		t2.WAREHOUSE_NAME,
		t2.WAREHOUSE_AREA_NAME,
		t3.ITEM_NUM,
		t3.ITEM_NAME,
		t3.CATEGORY_NAME,
		t3.ITEM_BARCODE,
		t2.batch_production_date as  productionDateOld,
		t2.batch_expired_date as expiredDateOld
		FROM
		wms_in_task_exception_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
		where
		t1.task_exception_id = #{pkId}
	</select>
	<select id="findList" resultMap="mainMap">
		select
		distinct
		<include refid="mainClomuns"></include>
		t2.ITEM_VOLUME,
		t2.ITEM_WEIGHT,
		t2.WAREHOUSE_NAME,
		t2.WAREHOUSE_AREA_NAME,
		t3.ITEM_NUM,
		t3.ITEM_NAME,
		t3.CATEGORY_NAME,
		t3.ITEM_BARCODE,
		t2.batch_production_date as  productionDateOld,
		t2.batch_expired_date as expiredDateOld,
		<!-- 6.11新增 -->
		t1.batch_id as batchIdOld,
		t1.batch_num as batchNumOld,
		t1.batch_id as batchIdNew,
		t1.batch_num as batchNumNew,
		t2.VENDOR_BATCH_NUM as vendorBatchNumOld,
		t2.VENDOR_BATCH_NUM as vendorBatchNumNew,
		t1.manufacturer_lot_num as manufacturerLotNumOld,
		t1.manufacturer_lot_num as manufacturerLotNumNew,
		t1.qty as qtyOld,
		t1.qty as qtyNew,
		t2.UOM_TYPE as uomType,
		t2.batch_production_date as  productionDateNew,
		t2.batch_expired_date as expiredDateNew,
		t1.piece_qty as pieceQtyOld,
		t1.piece_qty as pieceQtyNew,
		t3.MANUFACTURER_NAME as  manufacturer,
		t3.SPECIFICATION as specification,
		t2.APPROVAL_NUMBER as approvalNumber,
		
		t2.SHOW_PRODUCTION_DATE as showProductionDateOld,
		t2.SHOW_PRODUCTION_DATE as showProductionDateNew,
		t2.SHOW_EXPIRED_DATE as showExpiredDateOld,
		t2.SHOW_EXPIRED_DATE as showExpiredDateNew,
		
		nvl(t3.SHELF_LIFE_DAYS,0) as shelfLifeDays,
		nvl(t3.SHELF_LIFE_UNIT,'日') as shelfLifeUnit,
		t3.ITEM_HEALTH_LISTED_LICENSOR as itemHealthListedLicensor,
		t2.PRODUCTION_PLACE as productionPlace,
		CASE
		t2.ITEM_IS_PIECE_OR_CASE 
		WHEN '0' THEN
		'整散合一' 
		WHEN '1' THEN
		'整散分开' ELSE '整散合一' 
	    END AS itemHealthStoreCategory,
	    t3.ITEM_HEALTH_REGISTER_NBR as itemHealthRegisterNbr
		<!-- 6.11新增 -->
		FROM
		wms_in_task_exception_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
		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>
	<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,
			coalesce(t2.VENDOR_NUM,t2.CUSTOMER_NUM) as shipperCode,
			coalesce(t2.VENDOR_NAME,t2.CUSTOMER_NAME) as shipperName
		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 RCV_HEADER_ID FROM wms_in_task_exception_detail where status = #{taskStatus} or status is null  GROUP BY RCV_HEADER_ID -->
				<!-- 6.11新加 -->
				SELECT DISTINCT
					t1.RCV_HEADER_ID 
				FROM
					wms_in_task_exception_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>
					<if test="containerCode != null  and containerCode !=''">
					AND UPPER(t1.CONTAINER_CODE) like CONCAT(CONCAT('%',UPPER(#{containerCode})),'%') 
					</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 
			<!-- 6.11新加 -->
				)
		<if test="rcvUser != null  and rcvUser !=''">
			and UPPER(t1.EXECUTOR_USER) like CONCAT(CONCAT('%',UPPER(#{rcvUser})),'%')
		</if>
			<!-- 6.11新加 -->
		<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="shipperCode != null  and shipperCode !=''">
			and (UPPER(t2.VENDOR_NUM) like CONCAT(CONCAT('%',UPPER(#{shipperCode})),'%')  or UPPER(t2.CUSTOMER_NUM) like CONCAT(CONCAT('%',UPPER(#{shipperCode})),'%'))
		</if>
		<if test="shipperName != null  and shipperName !=''">
			and (UPPER(t2.VENDOR_NAME) like CONCAT(CONCAT('%',UPPER(#{shipperName})),'%')  or UPPER(t2.CUSTOMER_NAME) like CONCAT(CONCAT('%',UPPER(#{shipperName})),'%'))
		</if>
		<if test="startTime != null">
			and t1.rcv_date &gt;= #{startTime}
		</if>
		<if test="endTime != null">
			and t1.rcv_date &lt;= #{endTime}
		</if>
		<!-- 6.11新加 -->
		
		<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="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>
		order by t1.rcv_date desc,t1.ASN_AUTO_BILL_NUM desc
	</select>
</mapper>
