<?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.xisc.pm.infra.mapper.PmAutoPdContractMapper">
	<!-- 可根据自己的需求，是否要使用 -->
	<resultMap id="BaseResultMap"
		type="com.xisc.pm.domain.entity.PmAutoPdContract">
		<result column="PAPC_ID" property="papcId" jdbcType="DECIMAL" />
		<result column="VIRTUAL_SLAB_NO" property="virtualSlabNo"
			jdbcType="VARCHAR" />
		<result column="ORDER_NO" property="orderNo" jdbcType="VARCHAR" />
		<result column="ORDER_SEQ_NO" property="orderSeqNo"
			jdbcType="DECIMAL" />
		<result column="ORDER_PLATE_NUM" property="orderPlateNum"
			jdbcType="DECIMAL" />
		<result column="AXIS_FLAG" property="axisFlag"
			jdbcType="DECIMAL" />
		<result column="TENANT_ID" property="tenantId"
			jdbcType="DECIMAL" />
		<result column="OBJECT_VERSION_NUMBER"
			property="objectVersionNumber" jdbcType="DECIMAL" />
		<result column="CREATION_DATE" property="creationDate"
			jdbcType="VARCHAR" />
		<result column="CREATED_BY" property="createdBy"
			jdbcType="DECIMAL" />
		<result column="LAST_UPDATED_BY" property="lastUpdatedBy"
			jdbcType="DECIMAL" />
		<result column="LAST_UPDATE_DATE" property="lastUpdateDate"
			jdbcType="VARCHAR" />
		<result column="ATTRIBUTE1" property="attribute1"
			jdbcType="VARCHAR" />
		<result column="ATTRIBUTE2" property="attribute2"
			jdbcType="VARCHAR" />
		<result column="ATTRIBUTE3" property="attribute3"
			jdbcType="VARCHAR" />
		<result column="ATTRIBUTE4" property="attribute4"
			jdbcType="VARCHAR" />
		<result column="ATTRIBUTE5" property="attribute5"
			jdbcType="VARCHAR" />
	</resultMap>

	<select id="selectWithOrderWtByVirtualSlabNo"
		resultType="com.xisc.pm.domain.entity.PmAutoPdContract">
		SELECT
			papc.ORDER_NO ,
			papc.ORDER_PLATE_NUM ,
			ROUND( papc.ORDER_PLATE_NUM * ppcpa.ORDER_UNIT_AIM_WT , 3 ) as orderWt
		FROM PM_AUTO_PD_CONTRACT papc
		left join PM_PRO_CONT_PROD_ATTR ppcpa on ppcpa.ORDER_NO = papc.ORDER_NO
		where 
		papc.TENANT_ID = #{tenantId}
		and papc.REQUEST_ID = #{requestId}
		and papc.VERSION_ID = #{version}
		and papc.VIRTUAL_SLAB_NO in 
		<foreach collection="virtualSlabNoList" open="(" close=")" item="virtualSlabNo" separator=",">
			#{virtualSlabNo}
		</foreach>
		
	</select>
	<select id="selectList"
		resultType="com.xisc.pm.domain.entity.PmAutoPdContract">

		SELECT
		papc.ORDER_NO as ORDER_NO ,
		LISTAGG(DISTINCT pcmt.MSC_LINE_NO , ',') WITHIN GROUP (ORDER BY pcmt.MSC_LINE_NO) as
		MSC_LINE_NO,
		max(pcmt.ST_NO) as ST_NO,
		nvl(max(gp.ORDER_PLATE_NUM),0) as ORDER_PLATE_NUM,
		max(pstg.STEEL_GROUP_CODE) as STEEL_GROUP_CODE,
		max(ppcpa.SG_SIGN) as SG_SIGN ,
		max(ppcpa.DELIVY_DATE) as DELIVY_DATE ,
		max(ppcpa.DELIVY_PLACE_NAME) as delivyPlace ,
		max(ppcpa.CONSIGN_CUST_CNAME) as orderCustCname ,
		<!-- max(cd.STOCK_NUM) - nvl(max(gp.ORDER_PLATE_NUM),0) as contractQty, -->
		max(papc.QTY) - nvl(max(gp.ORDER_PLATE_NUM),0) as contractQty, 
		max(ppcpa.ORDER_WIDTH) as ORDER_WIDTH,
		max(ppcpa.ORDER_THICK) as ORDER_THICK,
		max(ppcpa.ORDER_LEN) as ORDER_LEN,
		max(case WHEN papcl.LOCK_QTY > 0 then papcl.LOCK_QTY else  0 end) as LOCK_QTY
		FROM PM_RUNTIME_CONTRACT papc
		left join PM_CONT_MSC_TECH pcmt on pcmt.ORDER_NO = papc.ORDER_NO
		left join PM_STEEL_TYPE_GROUP pstg on pstg.ST_NO = pcmt.ST_NO
		left join PM_PRO_CONT_PROD_ATTR ppcpa on ppcpa.ORDER_NO = papc.ORDER_NO
		left join PM_PRO_CONT ppc on ppc.ORDER_NO = papc.ORDER_NO
		left join CT_MAIN cm on cm.ORDER_NO = ppc.ORDER_NO
		left join CT_DET cd on cd.ORDER_NO = cm.ORDER_NO and cd.WHOLE_BACKLOG_CODE = '13'
		left join PM_AUTO_PD_CONT_LOCK papcl on papcl.ORDER_NO = papc.ORDER_NO
		LEFT JOIN (SELECT ORDER_NO , nvl(SUM(ORDER_PLATE_NUM), 0) as
		ORDER_PLATE_NUM FROM PM_AUTO_PD_CONTRACT
		where REQUEST_ID = #{requestId} and VERSION_ID = #{version} and TENANT_ID =
		#{tenantId} GROUP BY ORDER_NO ) gp on gp.ORDER_NO = papc.ORDER_NO
		where papc.TENANT_ID = #{tenantId}
		and papc.REQUEST_ID = #{requestId}
		GROUP BY papc.ORDER_NO
		<if test="remainCount != null and remainCount == 'Y'.toString()">
			having max(papc.QTY) > nvl(max(gp.ORDER_PLATE_NUM),0)
		</if>
	</select>

	<select id="selectNumberByRemainingFlag"
		resultType="com.xisc.pm.app.service.autoboard.dto.PmAutoPdVersionDTO">
		select
		VERSION_ID as version,
		nvl(sum(papc.ORDER_PLATE_NUM), 0) as remainingContractQty
		from PM_RUNTIME_CONTRACT prc
		left join PM_AUTO_PD_CONTRACT papc on prc.ORDER_NO = papc.ORDER_NO and
		papc.REQUEST_ID = prc.REQUEST_ID
		where prc.REMAINING_FLAG = 1
		and prc.TENANT_ID = #{tenantId}
		and prc.REQUEST_ID = #{requestId}
		AND VERSION_ID IS NOT NULL
		GROUP BY VERSION_ID
	</select>

	<delete id="deleteByRequestId">
		delete from PM_AUTO_PD_CONTRACT where
		REQUEST_ID in 
		<foreach collection="requestIds" item="requestId" close=")"
			open="(" separator=",">
			#{requestId}
		</foreach>
	</delete>
	
</mapper>