<?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.PmRuntimeContractMapper">


    <sql id = "BaseSql">
    PRC.CONTRACT_ID,
    PRC.ORDER_NO,
    PRC.DESIGN_PROD_LINE_CODE,
    PRC.ORDER_THICK,
    PRC.ORDER_WIDTH,
    PRC.ORDER_LEN,
    PRC.DELIVY_DATE,
    PRC.ORDER_CUST_CNAME,
    PRC.CONSIGN_CUST_CNAME,
    PRC.SG_SIGN,
    PRC.ORDER_WT,
    PRC.ORDER_QTY,
    PRC.ORDER_NUM,
    PRC.DELIVY_PLACE_NAME,
    PRC.ST_NO,
    PRC.ORDER_TYPE_CODE,
    PRC.NEED_LECQUERING,
    PRC.HULL_SECTION_NO,
    PRC.ULT_CODE,
    PRC.HEAT_TREAT_MODE,
    PRC.TENANT_ID,
    PRC.ATTRIBUTE1,
    PRC.ATTRIBUTE2,
    PRC.ATTRIBUTE3,
    PRC.ATTRIBUTE4,
    PRC.ATTRIBUTE5,
    PRC.ORDER_NO_ERP,
    PRC.creation_date,    PRC.created_by,    PRC.last_updated_by,    PRC.last_update_date,    PRC.object_version_number
    </sql>

    <select id = "selectList" resultType = "com.xisc.pm.domain.entity.PmRuntimeContract">
        select
        <include refid = "BaseSql"/>
        from PM_RUNTIME_CONTRACT PRC
        <where>
            <if test="contractId !=null">
                and PRC.CONTRACT_ID = #{contractId,jdbcType = INTEGER}
            </if>
            <if test="orderNo !=null">
                and PRC.ORDER_NO = #{orderNo,jdbcType = VARCHAR}
            </if>
            <if test="designProdLineCode !=null">
                and PRC.DESIGN_PROD_LINE_CODE = #{designProdLineCode,jdbcType = VARCHAR}
            </if>
            <if test="orderThick !=null">
                and PRC.ORDER_THICK = #{orderThick,jdbcType = INTEGER}
            </if>
            <if test="orderWidth !=null">
                and PRC.ORDER_WIDTH = #{orderWidth,jdbcType = INTEGER}
            </if>
            <if test="orderLen !=null">
                and PRC.ORDER_LEN = #{orderLen,jdbcType = INTEGER}
            </if>
            <if test="delivyDate !=null">
                and PRC.DELIVY_DATE = #{delivyDate,jdbcType = OTHER}
            </if>
            <if test="orderCustCname !=null">
                and PRC.ORDER_CUST_CNAME = #{orderCustCname,jdbcType = VARCHAR}
            </if>
            <if test="consignCustCname !=null">
                and PRC.CONSIGN_CUST_CNAME = #{consignCustCname,jdbcType = VARCHAR}
            </if>
            <if test="sgSign !=null">
                and PRC.SG_SIGN = #{sgSign,jdbcType = VARCHAR}
            </if>
            <if test="orderWt !=null">
                and PRC.ORDER_WT = #{orderWt,jdbcType = INTEGER}
            </if>
            <if test="orderQty !=null">
                and PRC.ORDER_QTY = #{orderQty,jdbcType = INTEGER}
            </if>
            <if test="orderNum !=null">
                and PRC.ORDER_NUM = #{orderNum,jdbcType = INTEGER}
            </if>
            <if test="delivyPlaceName !=null">
                and PRC.DELIVY_PLACE_NAME = #{delivyPlaceName,jdbcType = VARCHAR}
            </if>
            <if test="stNo !=null">
                and PRC.ST_NO = #{stNo,jdbcType = VARCHAR}
            </if>
            <if test="orderTypeCode !=null">
                and PRC.ORDER_TYPE_CODE = #{orderTypeCode,jdbcType = VARCHAR}
            </if>
            <if test="needLecquering !=null">
                and PRC.NEED_LECQUERING = #{needLecquering,jdbcType = VARCHAR}
            </if>
            <if test="hullSectionNo !=null">
                and PRC.HULL_SECTION_NO = #{hullSectionNo,jdbcType = VARCHAR}
            </if>
            <if test="ultCode !=null">
                and PRC.ULT_CODE = #{ultCode,jdbcType = VARCHAR}
            </if>
            <if test="heatTreatMode !=null">
                and PRC.HEAT_TREAT_MODE = #{heatTreatMode,jdbcType = VARCHAR}
            </if>
            <if test="tenantId !=null">
                and PRC.TENANT_ID = #{tenantId,jdbcType = INTEGER}
            </if>
            <if test="attribute1 !=null">
                and PRC.ATTRIBUTE1 = #{attribute1,jdbcType = VARCHAR}
            </if>
            <if test="attribute2 !=null">
                and PRC.ATTRIBUTE2 = #{attribute2,jdbcType = VARCHAR}
            </if>
            <if test="attribute3 !=null">
                and PRC.ATTRIBUTE3 = #{attribute3,jdbcType = VARCHAR}
            </if>
            <if test="attribute4 !=null">
                and PRC.ATTRIBUTE4 = #{attribute4,jdbcType = VARCHAR}
            </if>
            <if test="attribute5 !=null">
                and PRC.ATTRIBUTE5 = #{attribute5,jdbcType = VARCHAR}
            </if>
            <if test="orderNoErp !=null">
                and PRC.ORDER_NO_ERP = #{orderNoErp,jdbcType = VARCHAR}
            </if>
        </where>
    </select>
    
    <select id="selectAllNumberByRemainingFlag" resultType="java.math.BigDecimal">
    	select 
			nvl(sum(cd.STOCK_NUM), 0) as PD_LACK_QTY
		from PM_RUNTIME_CONTRACT prc 
		left join CT_DET cd on cd.ORDER_NO = prc.ORDER_NO and cd.WHOLE_BACKLOG_CODE = '13'
		where prc.REMAINING_FLAG = 1
		and prc.TENANT_ID = #{tenantId}
		and prc.REQUEST_ID = #{requestId}
    </select>
    
    <delete id="deleteByRequestId">
		delete from PM_RUNTIME_CONTRACT where
		REQUEST_ID in 
		<foreach collection="requestIds" item="requestId" close=")"
			open="(" separator=",">
			#{requestId}
		</foreach>
	</delete>
	
	<select id = "selectWt" resultType = "com.xisc.pm.domain.entity.PmRuntimeContract">
		select 
			prc.ST_NO ,
			sum(cd.STOCK_NUM * ppcpa.ORDER_UNIT_AIM_WT)  as orderWt
		from PM_RUNTIME_CONTRACT prc 
		left join PM_PRO_CONT_PROD_ATTR ppcpa on ppcpa.ORDER_NO = prc.ORDER_NO
		left join CT_DET cd on cd.ORDER_NO = prc.ORDER_NO and cd.WHOLE_BACKLOG_CODE = '13'
		where  prc.TENANT_ID = #{tenantId}
		and prc.REQUEST_ID = #{requestId}
		group by prc.ST_NO
	</select>
    
</mapper>

