<?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.TaskQcRecheckDetailMapper">
	<resultMap id="map"
		type="com.prolog.cs.biz.inbound.dto.task.TaskQcRecheckDetailDTO">
		<result column="task_recheck_id" property="taskRecheckId" />
		<result column="enterprise_id" property="enterpriseId" />
		<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="vendor_batch_num" property="vendorBatchNum" />
		<result column="active_flag" property="activeFlag" />
		<result column="remain_qty" property="remainQty" />
		<result column="comments" property="comments" />
		<result column="data_version" property="dataVersion"/>
		<result column="manufacturer_lot_num"
			property="manufacturerLotNum" />
		<result column="executor" property="executor" />
		<result column="container_code" property="containerCode" />
		<result column="goods_case_code" property="goodsCaseCode" />
		<result column="status" property="status" />
		<result column="times" property="times" />
		<result column="odd_piece_qty" property="oddPieceQty" />
		<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="clomuns">
		t1.task_recheck_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,
		floor(coalesce(t1.qty,0)) as caseQty,
		mod(coalesce(t1.piece_qty,0),coalesce(t1.conversion_rate,1)) as odd_piece_qty,
		t1.uom,
		t1.conversion_rate,
		t1.piece_qty,
		t1.batch_id,
		t1.batch_num,
		t1.vendor_batch_num,
		t1.active_flag,
		t1.remain_qty,
		t1.comments,
		t1.result,
		t1.reason,
		t1.manufacturer_lot_num,
		t1.executor,
		t1.container_code,
		t1.goods_case_code,
		t1.status,
		t1.times,
		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,
		t1.data_version,
		t1.REF_BILL_ID as refBillId,
		t1.REF_TYPE as refBillType,
		t1.REF_LINE_ID as refLineId,
		t1.REF_BILL_NO as refBillNo,

	</sql>
	
	<select id="findList" resultMap="map">
		select
		distinct
		<include refid="clomuns"></include>
		t2.WAREHOUSE_AREA_CODES as warehouseAreaCodes,
		t2.ITEM_IS_PIECE_OR_CASE as itemIsPieceOrCase,
		t2.WAREHOUSE_AREA_CATEGORYS as warehouseAreaCategorys,
		t2.ITEM_VOLUME,
		t2.ITEM_WEIGHT,
		t2.ITEM_HEALTH_TWOVOTEFLAG,
		t2.WAREHOUSE_NAME,
		t2.WAREHOUSE_AREA_NAME,
		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,
		t3.ITEM_NAME,
		t3.CATEGORY_NAME,
		t3.ITEM_BARCODE,
		coalesce(t2.ITEM_HEALTH_IS_DOUBLE,0) as itemHealthIsDouble,
		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,
		t3.STORE_TEMPERATURE as storeTemperature,
		
		<!-- 复检人1的信息 -->
		t6.QC_RE_RESULT as qcReResult,
		t6.QC_RE_OPCODE as qcReOpcode,
		t6.QC_RE_SUGGESTION as qcReSuggestion,
		t6.QC_RE_DATE as qcReDate,	
		CASE
			t1.times 
			WHEN 0 THEN
			'质量主管复检' 
			WHEN 1 THEN
			'质量经理复检' ELSE '质量主管复检' 
		END AS rqcReType,
		<!-- 质检行信息处理原因,不足原因,处理措施 -->
		CASE
			t1.REF_TYPE 
			WHEN 0 THEN
			t5.HANDLING_REASON 
			WHEN 1 THEN
			t5.HANDLING_REASON 
			WHEN 2 THEN
			t6.HANDLING_REASON
			WHEN 3 THEN
			t2.RCV_REASON1
		    ELSE t5.HANDLING_REASON
			END AS handlingReason,
		CASE
				t1.REF_TYPE 
				WHEN 0 THEN
				t5.DEF_REASON 
				WHEN 1 THEN
				t5.DEF_REASON 
				WHEN 2 THEN
				t6.DEF_REASON
				WHEN 3 THEN
				t2.RCV_REASON2
				ELSE t5.DEF_REASON
			END AS defReason,
		CASE
				t1.REF_TYPE 
				WHEN 0 THEN
				t5.HANDLING_MEASURE 
				WHEN 1 THEN
				t5.HANDLING_MEASURE 
				WHEN 2 THEN
				t6.HANDLING_MEASURE
				WHEN 3 THEN
				t2.ATTRIBUTE5
		    	ELSE t5.HANDLING_MEASURE
			END AS handlingMeasure,
		CASE
				t1.REF_TYPE 
				WHEN 0 THEN
				t5.DEF_REASON_EXT 
				WHEN 1 THEN
				t5.DEF_REASON_EXT 
				WHEN 2 THEN
				t6.DEF_REASON_EXT
				WHEN 3 THEN
				t2.RCV_REASON3
		    	ELSE t5.DEF_REASON_EXT
			END AS defReasonExt,
		t2.is_delegate as isDelegate,
		t2.approval_number as approvalNumber,
		COALESCE(t5.STOCK_STATUS,t2.STOCK_STATUS) as rqcResult,
		t1.REF_BILL_ID as rqcHeaderId,
		t2.RQC_REPORT_NO as rqcReportNo,
		t3.allow_decimal as allowDecimal,
		CASE t1.REF_TYPE
		WHEN 0 THEN
		'收货'
		WHEN 1 THEN
		'验收'
		WHEN 2 THEN
		'复检'
		ELSE '收货'
		END AS sourceType,
		t4.ASN_AUTO_BILL_NUM as asnAutoBillNum,
		t7.VENDOR_NAME as vendorName
		FROM
		wms_in_task_qc_recheck_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
		LEFT JOIN
		WMS_IN_RCV_HEADERS_PROFILES  t7
		on t7.rcv_header_id = t1.rcv_header_id
		LEFT JOIN
		WMS_IN_RQC_LINES  t5
		on t5.rqc_line_id = t1.ref_line_id
		LEFT JOIN WMS_IN_RQC_RE_LINES t6 ON t6.TASK_RE_RQC_ID = t1.task_recheck_id
		where
		1=1
		<if test="enterpriseId != null  and enterpriseId !=''">
			and t1.enterprise_id = #{enterpriseId}
		</if>
		<if test="rqcHeaderId != null  and rqcHeaderId !=''">
			and t1.REF_BILL_ID = #{rqcHeaderId}
		</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>
		and t1.piece_qty  &lt;&gt; 0
		<!-- and t4.rcv_type not in ('5','12','13','14','15') -->
		order by t1.gmt_modified
		desc
	</select>
	<select id="extractRcvHWithNativeSql" resultMap="rcvMap">
		SELECT
			COALESCE(t0.RQC_HEADER_ID, t1.RCV_HEADER_ID) as rqcHeaderId,
			COALESCE(t0.AUTO_BILL_NUM, t1.AUTO_BILL_NUM) as rqcNo,
			COALESCE(t0.BILL_DATE, t1.rcv_date) as rqcDate,
			COALESCE(t0.REMARK, t1.COMMENTS) as rqcRemark,
			COALESCE(t0.QC_OPERATOR1, t1.EXECUTOR_USER) as rqcUser,
			<!-- t0.QC_OPERATOR1 as rqcUser, -->
			t1.comments as rcvRemark,
			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,
			coalesce( t1.need_user, t2.BUYER_MANE ) AS needUser,
			coalesce( t1.need_dept, t2.ORGN_NAME ) AS needDept,
			t2.ORGN_NAME AS orgnName,
			t1.AGENT AS agent,
			t1.ASN_REMARK AS asnRemark,
			coalesce( 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,
			(select h.shipperid from OMS_ASN_BILL_H h where h.billid = t1.asn_header_id) as shipperid,
			(select h.shipper_type from OMS_ASN_BILL_H h where h.billid = t1.asn_header_id) as shipperType
		FROM
			wms_in_rcv_headers t1
			LEFT JOIN WMS_IN_RQC_HEADERS t0 ON t0.rcv_header_id = t1.rcv_header_id
			LEFT JOIN wms_in_rcv_headers_profiles t2 ON t1.rcv_header_id = t2.RCV_HEADER_ID
		WHERE
			1 = 1 
			AND t1.RCV_HEADER_ID in(
				SELECT DISTINCT
					t1.RCV_HEADER_ID
				FROM
					wms_in_task_qc_recheck_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.REF_BILL_ID IS NOT NULL -->
					AND t1.ENTERPRISE_ID = #{enterpriseId}
					and t1.piece_qty  &lt;&gt; 0
					GROUP BY
						t1.RCV_HEADER_ID

			<!-- SELECT RCV_HEADER_ID FROM wms_in_task_qc_recheck_detail where (status = '1' or status is null)
			<if test="times != null ">
				and TIMES &lt; #{times}
			</if>
			GROUP BY RCV_HEADER_ID
			 -->
			)
		and EXISTS(SELECT 1 FROM wms_in_task_qc_recheck_detail re where t0.RQC_HEADER_ID = re.REF_BILL_ID and re.status = '1')
		<if test="rqcNo != null  and rqcNo !=''">
			and UPPER(t0.AUTO_BILL_NUM) like CONCAT(CONCAT('%', UPPER(#{rqcNo})),'%')
		</if>
		<if test="qcOpcode1 != null  and qcOpcode1 !=''">
			and UPPER(t0.QC_OPERATOR1) like CONCAT(CONCAT('%', UPPER(#{qcOpcode1})),'%')
		</if>
		<if test="rqcUser != null  and rqcUser !=''">
			and UPPER(t0.QC_OPERATOR1) like CONCAT(CONCAT('%', UPPER(#{rqcUser})),'%')
		</if>
		<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})),'%') 
			<!-- and t1.SHIPPER_ZJM like CONCAT(CONCAT('%',#{shipperZjm}),'%')  -->
		</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>
		and t1.rcv_type not in ('5','12','13','14','15')
		order by t1.rcv_date desc,t1.ASN_AUTO_BILL_NUM desc
	</select>
	<update id="updatePreNodeLineId" >  
		UPDATE WMS_IN_TASK_QC_RECHECK_DETAIL set EXECUTOR = #{userName}, REF_TYPE = 2, PRE_NODE_LINE_ID = (select RQC_RE_LINE_ID FROM WMS_IN_RQC_RE_LINES WHERE TASK_RE_RQC_ID = #{taskId} )  WHERE TASK_RECHECK_ID = #{taskId} 
	</update>
</mapper>