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


    <sql id="BaseSql">
        PCSL.LIST_ID,
    PCSL.SLAB_ID,
    PCSL.LAYER_NO,
    PCSL.BASE_ORDER_ID,
    PCSL.BASE_ORDER_NO,
    PCSL.BASE_MAT_ID,
    PCSL.BASE_MAT_NO,
    PCSL.BASE_MAT_THICK,
    PCSL.BASE_MAT_WIDTH,
    PCSL.BASE_MAT_LENGTH,
    PCSL.BASE_MAT_WT,
    PCSL.COMP_MAT_TYPE,
    PCSL.COMP_MAT_ID,
    PCSL.COMP_MAT_NO,
    PCSL.COMP_MAT_PONO,
    PCSL.COMP_MAT_NUM,
    PCSL.TENANT_ID,
    PCSL.ATTRIBUTE1,
    PCSL.ATTRIBUTE2,
    PCSL.ATTRIBUTE3,
    PCSL.ATTRIBUTE4,
    PCSL.ATTRIBUTE5,

    PCSL.creation_date,    PCSL.created_by,    PCSL.last_updated_by,    PCSL.last_update_date,    PCSL.object_version_number
    </sql>

    <select id="selectList" resultType="com.xisc.pm.domain.entity.PmCompositeSlabList">
        select
        <include refid="BaseSql"/>
        from PM_COMPOSITE_SLAB_LIST PCSL
        <where>
            <if test="listId !=null">
                and PCSL.LIST_ID = #{listId,jdbcType = INTEGER}
            </if>
            <if test="slabId !=null">
                and PCSL.SLAB_ID = #{slabId,jdbcType = INTEGER}
            </if>
            <if test="layerNo !=null">
                and PCSL.LAYER_NO = #{layerNo,jdbcType = INTEGER}
            </if>
            <if test="baseOrderId !=null">
                and PCSL.BASE_ORDER_ID = #{baseOrderId,jdbcType = INTEGER}
            </if>
            <if test="baseOrderNo !=null">
                and PCSL.BASE_ORDER_NO = #{baseOrderNo,jdbcType = VARCHAR}
            </if>
            <if test="baseMatId !=null">
                and PCSL.BASE_MAT_ID = #{baseMatId,jdbcType = INTEGER}
            </if>
            <if test="baseMatNo !=null">
                and PCSL.BASE_MAT_NO = #{baseMatNo,jdbcType = VARCHAR}
            </if>
            <if test="baseMatThick !=null">
                and PCSL.BASE_MAT_THICK = #{baseMatThick,jdbcType = INTEGER}
            </if>
            <if test="baseMatWidth !=null">
                and PCSL.BASE_MAT_WIDTH = #{baseMatWidth,jdbcType = INTEGER}
            </if>
            <if test="baseMatLength !=null">
                and PCSL.BASE_MAT_LENGTH = #{baseMatLength,jdbcType = INTEGER}
            </if>
            <if test="baseMatWt !=null">
                and PCSL.BASE_MAT_WT = #{baseMatWt,jdbcType = INTEGER}
            </if>
            <if test="compMatType !=null">
                and PCSL.COMP_MAT_TYPE = #{compMatType,jdbcType = VARCHAR}
            </if>
            <if test="compMatNo !=null">
                and PCSL.COMP_MAT_NO = #{compMatNo,jdbcType = VARCHAR}
            </if>
            <if test="compMatPono !=null">
                and PCSL.COMP_MAT_PONO = #{compMatPono,jdbcType = VARCHAR}
            </if>
            <if test="compMatNum !=null">
                and PCSL.COMP_MAT_NUM = #{compMatNum,jdbcType = INTEGER}
            </if>
            <if test="tenantId !=null">
                and PCSL.TENANT_ID = #{tenantId,jdbcType = INTEGER}
            </if>
            <if test="attribute1 !=null">
                and PCSL.ATTRIBUTE1 = #{attribute1,jdbcType = VARCHAR}
            </if>
            <if test="attribute2 !=null">
                and PCSL.ATTRIBUTE2 = #{attribute2,jdbcType = VARCHAR}
            </if>
            <if test="attribute3 !=null">
                and PCSL.ATTRIBUTE3 = #{attribute3,jdbcType = VARCHAR}
            </if>
            <if test="attribute4 !=null">
                and PCSL.ATTRIBUTE4 = #{attribute4,jdbcType = VARCHAR}
            </if>
            <if test="attribute5 !=null">
                and PCSL.ATTRIBUTE5 = #{attribute5,jdbcType = VARCHAR}
            </if>
        </where>
    </select>

    <select id="queryOrderAttrByOrderNo" resultType="com.xisc.pm.domain.entity.PmProContProdTtr">
        SELECT
        t.ORDER_NO AS orderNo,
        t.ORDER_QTY AS orderQty,
        t.ORDER_QTY - t.orderNoUsedQty as notUserOderQty
        FROM
        (
        SELECT
        ppcpa.ORDER_NO,
        ppcpa.ORDER_QTY,
        NVL(count(pcsl.LIST_ID), 0) AS orderNoUsedQty
        FROM
        PM_PRO_CONT_PROD_ATTR ppcpa
        LEFT JOIN PM_COMPOSITE_SLAB_LIST pcsl ON ppcpa.ORDER_NO = pcsl.BASE_ORDER_NO
        LEFT JOIN PM_COMPOSITE_SLAB pcs ON pcs.SLAB_ID = pcsl.SLAB_ID and pcs.STATUS_CODE IN ( 50, 52, 54, 56, 58 )
        WHERE
        1= 1
        <if test="orderNoList != null and orderNoList.size() != 0">
            and ppcpa.ORDER_NO in
            <foreach collection="orderNoList" index="index" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="tenantId != null">
            and ppcpa.TENANT_ID = #{tenantId}
        </if>
        GROUP BY
        ppcpa.PRO_CONT_ID,
        ppcpa.ORDER_NO,
        ppcpa.ORDER_QTY
        ) t
        WHERE
        t.ORDER_QTY - t.orderNoUsedQty > 0
    </select>

    <select id="selectStockCodeBySlabNo" resultType="com.xisc.pm.domain.vo.WmStockInfoVO1">
        SELECT DISTINCT
            pclp.TMP_SLAB_NO,
            wsi.STOCK_CODE,
            pcs.FACTORY_CODE
        FROM
            PM_CMD_LARGE_PLT pclp
                left join PM_COMPOSITE_SLAB_LIST pcsl on pcsl.BASE_MAT_NO = pclp.SLAB_NO 
				left join PM_COMPOSITE_SLAB pcs on pcs.SLAB_ID = pcsl.SLAB_ID
                JOIN WM_MAT_PILE wap ON pcs.SLAB_NO = wap.MAT_NO
                JOIN WM_STOCK_INFO wsi ON wsi.STOCK_ID = wap.STOCK_ID
        <where>
            <if test="tenantId != null">
                and pclp.TENANT_ID = #{tenantId}
            </if>
            <if test="tmpSlabNoList != null and tmpSlabNoList.size() != 0">
                and pclp.TMP_SLAB_NO in
                <foreach collection="tmpSlabNoList" index="index" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
        </where>
    </select>


</mapper>

