<?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.rqc.RqcHeadersMapper">
	<resultMap id="mainMap"
		type="com.prolog.cs.biz.inbound.dto.rqc.RqcHeadersDTO">
		<result column="rqc_header_id" property="rqcHeaderId" />
		<result column="custom_bill_num" property="customBillNum" />
		<result column="auto_bill_num" property="autoBillNum" />
		<result column="mtl_owner_id" property="mtlOwnerId" />
		<result column="warehouse_id" property="warehouseId" />
		<result column="vendor_id" property="vendorId" />
		<result column="src_bill_type" property="srcBillType" />
		<result column="src_header_id" property="srcHeaderId" />
		<result column="asn_header_id" property="asnHeaderId" />
		<result column="rcv_header_id" property="rcvHeaderId" />
		<result column="bill_date" property="billDate" />
		<result column="status" property="status" />
		<result column="remark" property="remark" />
		<result column="work_bench" property="workBench" />
		<result column="qc_operator1" property="qcOperator1" />
		<result column="qc_operator2" property="qcOperator2" />
		<result column="data_src_type" property="dataSrcType" />
		<result column="IS_INSTOCK_PRINT" property="isInstockPrint" />
		<!-- <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_name" property="modifierName" />
		<result column="modifier" property="modifier" />
		<result column="enterprise_id" property="enterpriseId" />
		 -->
		 
	</resultMap>
	<sql id="mainClomuns">
		t1.rqc_header_id,
		t1.custom_bill_num,
		t1.auto_bill_num,
		t1.mtl_owner_id,
		t1.warehouse_id,
		t1.vendor_id,
		t1.src_bill_type,
		t1.src_header_id,
		t1.asn_header_id,
		t1.rcv_header_id,
		t1.bill_date,
		t1.status,
		t1.remark,
		t1.work_bench,
		coalesce(t1.qc_operator1,t1.MODIFIER_NAME) as qc_operator1,
		t1.qc_operator2,
		t1.data_src_type,
		t1.ENTERPRISE_ID,
		t1.GMT_CREATE,
		t1.CREATOR,
		t1.CREATOR_NAME,
		t1.GMT_MODIFIED,
		t1.MODIFIER,
		t1.MODIFIER_NAME as modifierName,
		t1.IS_INSTOCK_PRINT,
		t1.RQC_TEMPERATURE as rqcTemperature,
	</sql>
	
	<select id="findList" resultMap="mainMap">
		select
		distinct
		<include refid="mainClomuns"></include>
			t2.rcv_date as rcvDate,
			t2.rcv_type as rcvType,
			t3.ship_type as shipType,
			t3.arrival_mode as arrivalMode,
			ifnull(t3.vendor_name,t3.customer_name) as vendorName,
			t3.warehouse_name as warehouseName,
			t3.buyer_mane as buyerMane,
			t3.customer_name as customerName,
			t3.mtl_owner_name as mtlOwnerName,
			t3.ORGN_NAME  as orgnName,
			t2.asn_auto_bill_num as asnBillNo,
			t2.shipper_zjm as shipperZjm,
			t2.auto_bill_num as rcvBillNo,
			coalesce(t3.vendor_num,t1.vendor_id) as vendorNum,
			t3.mtl_owner_num as mtlOwnerNum,
			t1.MODIFIER_NAME as excuteUser,
			t2.EXECUTOR_USER as rcvUser
		FROM
			wms_in_rqc_headers t1 
		    JOIN wms_in_rcv_headers t2 on t1.RCV_HEADER_ID = t2.RCV_HEADER_ID
		    JOIN wms_in_rcv_headers_profiles t3 on t1.RCV_HEADER_ID = t3.RCV_HEADER_ID
		WHERE
			1=1
			and exists (
				SELECT
					t4.RQC_HEADER_ID 
				FROM
					WMS_IN_RQC_LINES t4
				WHERE
					t4.rqc_header_id = t1.rqc_header_id
					AND t4.enterprise_id = #{enterpriseId}
					and t4.stock_status in('0','1')
			) 
		<!-- 6.11新加 -->
		<if test="itemZjm != null  and itemZjm !=''">
			and t1.rqc_header_id in (
			SELECT 
				t1.RQC_HEADER_ID 
			FROM
				WMS_IN_RQC_LINES t1
				LEFT JOIN WMS_IN_RCV_LINES t2 ON t1.RCV_LINE_ID = t2.RCV_LINE_ID 
			WHERE
				1 = 1
				and UPPER(t2.ITEM_ZJM) like CONCAT(CONCAT('%',UPPER(#{itemZjm})),'%') 
			AND t1.RQC_HEADER_ID IS NOT NULL
			GROUP BY
				t1.RQC_HEADER_ID 
			)
		</if>
		<if test="manufacturerLotNum != null  and manufacturerLotNum !=''">
			and t1.rqc_header_id in (
			SELECT
			t1.RQC_HEADER_ID
			FROM
			WMS_IN_RQC_LINES t1
			LEFT JOIN WMS_IN_RCV_LINES t2 ON t1.RCV_LINE_ID = t2.RCV_LINE_ID
			WHERE
			1 = 1
			and UPPER(t2.MANUFACTURER_LOT_NUM) like CONCAT(CONCAT('%',UPPER(#{manufacturerLotNum})),'%')
			AND t1.RQC_HEADER_ID IS NOT NULL
			GROUP BY
			t1.RQC_HEADER_ID
			)
		</if>
		<if test="itemCode != null  and itemCode !=''">
			and t1.rqc_header_id in (
			SELECT
			t1.RQC_HEADER_ID
			FROM
			WMS_IN_RQC_LINES 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
			and UPPER(t3.ITEM_NUM) like CONCAT(CONCAT('%',UPPER(#{itemCode})),'%')
			AND t1.RQC_HEADER_ID IS NOT NULL
			GROUP BY
			t1.RQC_HEADER_ID
			)
		</if>
		<if test="isDoubleHealth != null  and isDoubleHealth !=''">
			and exists (
			SELECT
			1
			FROM
			wms_in_task_instock_detail t5
			WHERE
			t5.rqc_header_id = t1.rqc_header_id
			and status in('0','1')
			)
		</if>
		<if test="rcvNo != null  and rcvNo !=''">
			and t2.auto_bill_num like CONCAT(CONCAT('%',#{rcvNo}),'%')
		</if>
		<if test="billNo != null  and billNo !=''">
			and t2.ASN_AUTO_BILL_NUM like CONCAT(CONCAT('%',#{billNo}),'%') 
		</if>
		<if test="shipperZjm != null  and shipperZjm !=''">
			and UPPER(t2.SHIPPER_ZJM) like CONCAT(CONCAT('%',UPPER(#{shipperZjm})),'%') 
		</if>
		<if test="startTime != null ">
			and t1.gmt_modified &gt;= #{startTime}
		</if>
		<if test="endTime != null ">
			and t1.gmt_modified &lt;= #{endTime}
		</if>
		
		<if test="incloudHids != null and incloudHids.size() >0 ">
			and t1.rqc_header_id in
			<foreach collection="incloudHids" item="item" open="("
				separator="," close=")">
				#{item}
			</foreach>
		</if>
		<if test="excludeHids != null and excludeHids.size() >0 ">
			and t1.rqc_header_id not in
			<foreach collection="excludeHids" item="excludeItem"
				open="(" separator="," close=")">
				#{excludeItem}
			</foreach>
		</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 t1.auto_bill_num = #{autoBillNum}
		</if>
	<!-- 	<if test="rcvType != null  and rcvType !=''">
			and t2.rcv_type = #{rcvType}
		</if> -->
		<if test="customBillNum != null  and customBillNum !=''">
			and t1.custom_bill_num like CONCAT(CONCAT('%',#{customBillNum}),'%')
		</if>
		<if test="printStatus != null  and printStatus !=''">
			<choose>
				<when test="printStatus != null and printStatus == 1">
					AND EXISTS(SELECT 1 FROM wms_in_rqc_lines rl where t1.rqc_header_id = rl.rqc_header_id AND rl.second_status = '1')
				</when>
				<otherwise>
					AND EXISTS(SELECT 1 FROM wms_in_rqc_lines rl where t1.rqc_header_id = rl.rqc_header_id AND (rl.second_status = '0' or rl.second_status is null))
				</otherwise>
			</choose>
		</if>
		order by t1.gmt_modified
		desc
	</select>
	<select id="findByPkId" resultMap="mainMap" parameterType="String">
		select
		distinct
		<include refid="mainClomuns"></include>
			t2.rcv_date as rcvDate,
			t2.rcv_type as rcvType,
			t3.ship_type as shipType,
			t3.arrival_mode as arrivalMode,
			t3.vendor_name as vendorName,
			t3.warehouse_name as warehouseName,
			t3.buyer_mane as buyerMane,
			t3.customer_name as customerName,
			t3.mtl_owner_name as mtlOwnerName,
			t3.ORGN_NAME  as orgnName
		FROM
			wms_in_rqc_headers t1 
		LEFT JOIN wms_in_rcv_headers t2 on t1.RCV_HEADER_ID = t2.RCV_HEADER_ID
		LEFT JOIN wms_in_rcv_headers_profiles t3 on t1.RCV_HEADER_ID = t3.RCV_HEADER_ID	 
		WHERE
		t1.rqc_header_id = #{pkId}
	</select>
	<select id="findByRcvId" resultMap="mainMap" parameterType="String">
		SELECT t0.* (
		select
		<!-- distinct -->
		<include refid="mainClomuns"></include>
			t2.rcv_date as rcvDate,
			t2.rcv_type as rcvType,
			t3.ship_type as shipType,
			t3.arrival_mode as arrivalMode,
			t3.vendor_name as vendorName,
			t3.warehouse_name as warehouseName,
			t3.buyer_mane as buyerMane,
			t3.customer_name as customerName,
			t3.mtl_owner_name as mtlOwnerName,
			t3.ORGN_NAME  as orgnName
		FROM
			wms_in_rqc_headers t1 
		LEFT JOIN wms_in_rcv_headers t2 on t1.RCV_HEADER_ID = t2.RCV_HEADER_ID
		LEFT JOIN wms_in_rcv_headers_profiles t3 on t1.RCV_HEADER_ID = t3.RCV_HEADER_ID	 
		WHERE t1.RCV_HEADER_ID = #{rcvHeaderId} AND EXISTS (SELECT temp.RQC_HEADER_ID,temp.sum  FROM( SELECT RQC_HEADER_ID,SUM(PIECE_QTY) sum FROM WMS_IN_RQC_LINES line  GROUP BY line.RQC_HEADER_ID) temp WHERE temp.sum &lt;&gt; 0 AND temp.RQC_HEADER_ID = t1.RQC_HEADER_ID ) ORDER BY t1.GMT_MODIFIED desc) t0 LIMIT 1
	</select>
</mapper>
