<?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.PmAutoPdResultMapper">
	<!-- 可根据自己的需求，是否要使用 -->
	<resultMap id="BaseResultMap"
		type="com.xisc.pm.domain.entity.PmAutoPdResult">
		<result column="RESULT_ID" property="resultId"
			jdbcType="DECIMAL" />
		<result column="REQUEST_ID" property="requestId"
			jdbcType="DECIMAL" />
		<result column="VERSION_ID" property="versionId"
			jdbcType="DECIMAL" />
		<result column="POLICY_GROUP_ID" property="policyGroupId"
			jdbcType="DECIMAL" />
		<result column="MSC_LINE_NO" property="mscLineNo"
			jdbcType="VARCHAR" />
		<result column="MOLD_THICK" property="moldThick"
			jdbcType="DECIMAL" />
		<result column="MOLD_WID" property="moldWid" jdbcType="DECIMAL" />
		<result column="VIRTUAL_SLAB_NO" property="virtualSlabNo"
			jdbcType="VARCHAR" />
		<result column="COMBO_SLAB_METHOD" property="comboSlabMethod"
			jdbcType="VARCHAR" />
		<result column="NORM_YIELD" property="normYield"
			jdbcType="DECIMAL" />
		<result column="COMMAND_YIELD" property="commandYield"
			jdbcType="DECIMAL" />
		<result column="ROLL_YIELD" property="rollYield"
			jdbcType="DECIMAL" />
		<result column="ORDER1_THICK" property="order1Thick"
			jdbcType="DECIMAL" />
		<result column="COMBINE_PLATE_WIDTH"
			property="combinePlateWidth" jdbcType="DECIMAL" />
		<result column="COMBINE_PLATE_LEN" property="combinePlateLen"
			jdbcType="DECIMAL" />
		<result column="TRANS_FLAG" property="transFlag"
			jdbcType="DECIMAL" />
		<result column="SINGLE_WEIGHT" property="singleWeight"
			jdbcType="DECIMAL" />
		<result column="INFUR_SLAB_THICK" property="infurSlabThick"
			jdbcType="DECIMAL" />
		<result column="INFUR_SLAB_WID" property="infurSlabWid"
			jdbcType="DECIMAL" />
		<result column="INFUR_SLAB_LEN" property="infurSlabLen"
			jdbcType="DECIMAL" />
		<result column="INFUR_SLAB_MAX_LEN" property="infurSlabMaxLen"
			jdbcType="DECIMAL" />
		<result column="INFUR_SLAB_MIN_LEN" property="infurSlabMinLen"
			jdbcType="DECIMAL" />
		<result column="INFUR_SLAB_WT" property="infurSlabWt"
			jdbcType="DECIMAL" />
		<result column="INFUR_SLAB_MAX_WT" property="infurSlabMaxWt"
			jdbcType="DECIMAL" />
		<result column="INFUR_SLAB_MIN_WT" property="infurSlabMinWt"
			jdbcType="DECIMAL" />
		<result column="NEED_SLAB_LEN" property="needSlabLen"
			jdbcType="DECIMAL" />
		<result column="NEED_SLAB_WT" property="needSlabWt"
			jdbcType="DECIMAL" />
		<result column="PRE_CLEAN_SLAB_THICK"
			property="preCleanSlabThick" jdbcType="DECIMAL" />
		<result column="PRE_CLEAN_SLAB_WIDTH"
			property="preCleanSlabWidth" jdbcType="DECIMAL" />
		<result column="PRE_CLEAN_SLAB_LEN" property="preCleanSlabLen"
			jdbcType="DECIMAL" />
		<result column="PRE_CLEAN_SLAB_MAX_LEN"
			property="preCleanSlabMaxLen" jdbcType="DECIMAL" />
		<result column="PRE_CLEAN_SLAB_MIN_LEN"
			property="preCleanSlabMinLen" jdbcType="DECIMAL" />
		<result column="PRE_CLEAN_SLAB_WT" property="preCleanSlabWt"
			jdbcType="DECIMAL" />
		<result column="PRE_CLEAN_SLAB_MAX_WT"
			property="preCleanSlabMaxWt" jdbcType="DECIMAL" />
		<result column="PRE_CLEAN_SLAB_MIN_WT"
			property="preCleanSlabMinWt" jdbcType="DECIMAL" />
		<result column="PICK_MODE_LEN_DIR" property="pickModeLenDir"
			jdbcType="DECIMAL" />
		<result column="PICK_MODE_WID_DIR" property="pickModeWidDir"
			jdbcType="DECIMAL" />
		<result column="TOT_SMALL_PLATE_NUM"
			property="totSmallPlateNum" jdbcType="DECIMAL" />
		<result column="SAMPLE_LEN_HEAD" property="sampleLenHead"
			jdbcType="DECIMAL" />
		<result column="SAMPLE_LEN_TAIL" property="sampleLenTail"
			jdbcType="DECIMAL" />
		<result column="SAMPLE_LEN_MID" property="sampleLenMid"
			jdbcType="DECIMAL" />
		<result column="ORDER_NUM_IN_PLATE" property="orderNumInPlate"
			jdbcType="DECIMAL" />
		<result column="ROLL_WT_HP" property="rollWtHp"
			jdbcType="DECIMAL" />
		<result column="ROLL_THICK_TOL_MAX" property="rollThickTolMax"
			jdbcType="DECIMAL" />
		<result column="ROLL_THICK_TOL_MIN" property="rollThickTolMin"
			jdbcType="DECIMAL" />
		<result column="HEAD_CUT" property="headCut" jdbcType="DECIMAL" />
		<result column="TAIL_CUT" property="tailCut" jdbcType="DECIMAL" />
		<result column="ROLL_AIM_THICK" property="rollAimThick"
			jdbcType="DECIMAL" />
		<result column="ROLL_AIM_WIDTH" property="rollAimWidth"
			jdbcType="DECIMAL" />
		<result column="ROLL_AIM_LEN" property="rollAimLen"
			jdbcType="DECIMAL" />
		<result column="RS_CODE" property="rsCode" jdbcType="VARCHAR" />
		<result column="BROAD_FLAG" property="broadFlag"
			jdbcType="VARCHAR" />
		<result column="ORDER_NO_AXIS" property="orderNoAxis"
			jdbcType="VARCHAR" />
		<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="selectPmAutoPdContractSummary" resultType="com.xisc.pm.app.service.autoboard.dto.PmAutoPdContractSummaryDTO">
	
		select 
			papc.VIRTUAL_SLAB_NO as virtualSlabNo,
			count(DISTINCT ppcpa.CONSIGN_CUST_CNAME)  as orderCustCnameCount
		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.VIRTUAL_SLAB_NO in
		<foreach collection="virtualSlabNoList" item="virtualSlabNo" open="(" close=")" separator=",">
				#{virtualSlabNo}
			</foreach>
		and papc.REQUEST_ID = #{requestId} 
		and papc.VERSION_ID = #{version}
		GROUP BY papc.VIRTUAL_SLAB_NO
	</select>

	<select id="selectList"
		resultType="com.xisc.pm.domain.entity.PmAutoPdResult">
		select
		pcmt.HEAT_TREAT_MODE,
		pri.REQUEST_CODE,
		pcmt.ST_NO ,
		pcmt.RF_PATH ,
		ppcpa.SG_SIGN,
		papp.NEW_VIRTUAL_SLAB_NO,
		ppcpa.DELIVY_DATE as delivyDate ,
		ppcpa.CONSIGN_CUST_CNAME as orderCustCname,
		ppcpa.DELIVY_PLACE_NAME as delivyPlace,
		papr.*
		from
		PM_AUTO_PD_RESULT papr
		left join PM_CONT_MSC_TECH pcmt on pcmt.MSC_LINE_NO = papr.MSC_LINE_NO and pcmt.ORDER_NO = papr.ORDER_NO_AXIS
		left join PM_PRO_CONT_PROD_ATTR ppcpa on ppcpa.ORDER_NO = papr.ORDER_NO_AXIS
		left join PM_REQUEST_INFO pri on pri.REQUEST_ID = papr.REQUEST_ID
		left join PM_AUTO_PD_PUBLISHED papp on papp.VIRTUAL_SLAB_NO = papr.VIRTUAL_SLAB_NO
		where papr.TENANT_ID = #{organizationId}
		<if test="request.requestId != null">
			and papr.REQUEST_ID = #{request.requestId}
		</if>
		<if test="request.version != null">
			and papr.VERSION_ID = #{request.version}
		</if>
		<if test="request.stNoList != null and request.stNoList.size() > 0">
			and pcmt.ST_NO in 
			<foreach collection="request.stNoList" item="stNoStr" open="(" close=")" separator=",">
				#{stNoStr}
			</foreach>
		</if>
		<if test="request.stNo != null and request.stNo != ''">
			and pcmt.ST_NO = #{request.stNo}
		</if>
		<if test="request.heatTreatMode != null and request.heatTreatMode != ''">
			and pcmt.HEAT_TREAT_MODE = #{request.heatTreatMode}
		</if>
		<if test="request.orderNo != null and request.orderNo != ''">
			and EXISTS (SELECT 1 FROM PM_AUTO_PD_CONTRACT papc where papc.VIRTUAL_SLAB_NO = papr.VIRTUAL_SLAB_NO and papc.ORDER_NO = #{request.orderNo})
		</if>
		<if test="request.mscLineNo != null and request.mscLineNo != ''">
			and papr.MSC_LINE_NO = #{request.mscLineNo}
		</if>
		<if test="request.moldThick != null">
			and papr.MOLD_THICK = #{request.moldThick}
		</if>
		<if test="request.moldWid != null">
			and papr.MOLD_WID = #{request.moldWid}
		</if>
		<if test="request.publishedStatusList != null and request.publishedStatusList.size() > 0">
			and papr.PUBLISHED_STATUS in 
			<foreach collection="request.publishedStatusList" open="(" close=")" separator="," item="publishedStatus">
				#{publishedStatus}
			</foreach>
		</if>
		<if test="request.virtualSlabNoList != null and request.virtualSlabNoList.size() > 0">
			and papr.VIRTUAL_SLAB_NO in 
			<foreach collection="request.virtualSlabNoList" open="(" close=")" separator="," item="virtualSlabNoList">
				#{virtualSlabNoList}
			</foreach>
		</if>
	</select>
	
	<select id="selectExecVersion" resultType="com.xisc.pm.app.service.autoboard.dto.PmAutoPdVersionDTO">
			SELECT
			papr.REQUEST_ID as requestId,
			papr.VERSION_ID as version,
			count( papr.RESULT_ID ) as slabCount,
			sum( papr.COMMAND_YIELD ) as allYield,
			ROUND( sum( papr.COMMAND_YIELD ) / count( * ), 5 ) as averageYield,
			sum( papr.TOT_SMALL_PLATE_NUM ) as smallPlateNum,
			ROUND( sum( papr.SINGLE_WEIGHT ) / count( * ), 3 ) as averageWt,
			sum( papr.SINGLE_WEIGHT ) as allWt,
			max(papr.CREATION_DATE) as createDate
		FROM
			PM_AUTO_PD_RESULT papr
		WHERE
			REQUEST_ID = #{requestId}
			and TENANT_ID = #{tenantId}
		GROUP BY
			REQUEST_ID,
			VERSION_ID
	</select>
	
	<delete id="deleteByRequestId">
		delete from PM_AUTO_PD_RESULT where 
		REQUEST_ID in 
		<foreach collection="requestIds" item="requestId" close=")" open="(" separator=",">
		 #{requestId}
		</foreach>
	</delete>
	
	<select id="selectSummaryDetailList" resultType="com.xisc.pm.app.service.autoboard.dto.PmAutoPdPublishedDetailDTO">
		SELECT
			pcmt.ST_NO,
			pcmt.MSC_LINE_NO,
			papr.MOLD_WID,
			papr.MOLD_THICK,
			sum( papr.SINGLE_WEIGHT ) WT ,
			sum( CASE WHEN papr.PUBLISHED_STATUS = 2 THEN papr.SINGLE_WEIGHT  ELSE 0 END) as publishedWt ,
			max( ppcpa.SG_SIGN ) as SG_SIGN
		FROM
			PM_AUTO_PD_RESULT papr
			INNER JOIN PM_CONT_MSC_TECH pcmt ON pcmt.ORDER_NO = papr.ORDER_NO_AXIS AND pcmt.MSC_LINE_NO = papr.MSC_LINE_NO
			INNER JOIN PM_PRO_CONT_PROD_ATTR ppcpa ON ppcpa.ORDER_NO = papr.ORDER_NO_AXIS 
		WHERE
			papr.REQUEST_ID = #{requestId} 
			AND papr.VERSION_ID = #{version} 
		GROUP BY
			pcmt.ST_NO,
			pcmt.MSC_LINE_NO,
			papr.MOLD_WID,
			papr.MOLD_THICK
	</select>
	
	<update id="updatePublishedStatus">
		update PM_AUTO_PD_RESULT set PUBLISHED_STATUS = #{targetStatus} , OBJECT_VERSION_NUMBER = OBJECT_VERSION_NUMBER + 1 where TENANT_ID = #{tenantId}
		and RESULT_ID in 
		<foreach collection="resultIdList" open="(" close=")" separator="," item="resultId">
			#{resultId}
		</foreach>
	</update>
	
	<update id="initContLock">
		INSERT INTO PM_AUTO_PD_CONT_LOCK ( PAPL_ID, ORDER_NO, LOCK_QTY, TENANT_ID, OBJECT_VERSION_NUMBER ) SELECT
			PM_AUTO_PD_CONT_LOCK_S.NEXTVAL,
			a.ORDER_NO,
			0,
			a.TENANT_ID,
			1 
		FROM
			(
		SELECT DISTINCT
			papc.ORDER_NO,
			papc.TENANT_ID 
		FROM
			PM_AUTO_PD_CONTRACT papc 
			WHERE papc.VIRTUAL_SLAB_NO in 
			<foreach collection="virtualSlabNoList" item="virtualSlabNo" open="(" close=")" separator=",">
				#{virtualSlabNo}
			</foreach>
			and papc.TENANT_ID = #{tenantId}
			and papc.REQUEST_ID = #{requestId} 
			AND papc.VERSION_ID = #{version} 
			and NOT EXISTS ( SELECT 1 FROM PM_AUTO_PD_CONT_LOCK papcl WHERE papcl.ORDER_NO = papc.ORDER_NO )) a
	</update>
	
	<select id="checkStockNum" resultType="com.xisc.pm.app.service.autoboard.dto.PmAutoPdCheckDTO">
		SELECT
			papc.REQUEST_ID,
			papc.VERSION_ID,
			papc.ORDER_NO,
			sum( papc.ORDER_PLATE_NUM ) AS ORDER_PLATE_NUM,
			max( cd.STOCK_NUM ) AS STOCK_NUM 
		FROM
			PM_AUTO_PD_CONTRACT papc
			LEFT JOIN XISC_CT.CT_DET cd ON cd.ORDER_NO = papc.ORDER_NO 
			AND cd.WHOLE_BACKLOG_CODE = '13' 
		WHERE
			papc.TENANT_ID = #{tenantId}
			and papc.REQUEST_ID = #{requestId} 
			AND papc.VERSION_ID = #{version} 
			and papc.VIRTUAL_SLAB_NO in 
			<foreach collection="virtualSlabNoList" item="virtualSlabNo" open="(" close=")" separator=",">
				#{virtualSlabNo}
			</foreach>
		GROUP BY
			papc.REQUEST_ID,
			papc.VERSION_ID,
			papc.ORDER_NO 
		HAVING
			sum( papc.ORDER_PLATE_NUM ) > max(
			cd.STOCK_NUM)
	</select>
	
</mapper>