<?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.outbound.dao.SaleRedRecordMapper">



    <select id="isWholeBillMakeRed" resultType="java.lang.Long">
		WITH x AS (
		SELECT
			red.src_id,
			red.SRC_LINE_ID,
			sum(red.QTY) redQty
		FROM
			WMS_SALE_RED_RECORD red
		WHERE
			red.SRC_ID = #{omsBIllId}
		GROUP BY
			red.src_id,
			red.SRC_LINE_ID ),
		b AS (
		SELECT
			omsbilld.LINEID ,
			COALESCE(x.redQty, 0) redQty,
			omsbilld.ITEM_QTY
		FROM
			OMS_DO_BILL_D omsbillD
		LEFT JOIN x ON
			x.src_line_id = omsbillD.LINEID
		WHERE
			( omsbillD.billid = #{omsBIllId}))
		SELECT
			count(*)
			FROM b
		WHERE
			b.redqty != b.item_qty
    </select>

	<select id="isWholeTaskHRed" resultType="java.lang.Long">
		WITH W AS (SELECT SUM(PICK_QTY) pick_qty FROM WMS_PICK_TASK_D WHERE PICK_TASK_ID = #{pickTaskHId}),
		B AS (SELECT QTY FROM WMS_SALE_RED_RECORD WHERE PICK_TASK_D_ID IN (SELECT PICK_TASK_D_ID FROM WMS_PICK_TASK_D WHERE PICK_TASK_ID = #{pickTaskHId} ) )
		select COUNT(*) from W,B WHERE W.PICK_QTY = B.QTY
	</select>
</mapper>