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


    <sql id = "BaseSql">
    PPCS.PPCS_ID,
    PPCS.PRO_CONT_ID,
    PPCS.PD_MAT_NO,
    PPCS.ORDER_NO,
    PPCS.ORDER_SEQ_NO,
    PPCS.ORDER_PLATE_NUM,
    PPCS.PD_MAT_TYPE,
    PPCS.SLAB_NO,
    PPCS.PLATE_NO,
    PPCS.PD_STATUS,
    PPCS.AXIS_FLAG,
    PPCS.ORDER_THICK,
    PPCS.ORDER_WIDTH,
    PPCS.ORDER_LEN,
    PPCS.ORDER_LEN_MIN,
    PPCS.ORDER_LEN_MAX,
    PPCS.SMALL_PLATE_THICK,
    PPCS.SMALL_PLATE_WIDTH,
    PPCS.SMALL_PLATE_LEN,
    PPCS.TENANT_ID,
    PPCS.ATTRIBUTE1,
    PPCS.ATTRIBUTE2,
    PPCS.ATTRIBUTE3,
    PPCS.ATTRIBUTE4,
    PPCS.ATTRIBUTE5,
    PPCS.creation_date,    PPCS.created_by,    PPCS.last_updated_by,    PPCS.last_update_date,    PPCS.object_version_number
    </sql>

    <select id = "selectList" resultType = "com.xisc.pm.domain.entity.PmPdContSum">
        select
        <include refid = "BaseSql"/>
        from PM_PD_CONT_SUM PPCS
        <where>
            <if test="ppcsId !=null">
                and PPCS.PPCS_ID = #{ppcsId,jdbcType = INTEGER}
            </if>
            <if test="proContId !=null">
                and PPCS.PRO_CONT_ID = #{proContId,jdbcType = INTEGER}
            </if>
            <if test="pdMatNo !=null">
                and PPCS.PD_MAT_NO = #{pdMatNo,jdbcType = VARCHAR}
            </if>
            <if test="orderNo !=null">
                and PPCS.ORDER_NO = #{orderNo,jdbcType = VARCHAR}
            </if>
            <if test="orderSeqNo !=null">
                and PPCS.ORDER_SEQ_NO = #{orderSeqNo,jdbcType = INTEGER}
            </if>
            <if test="orderPlateNum !=null">
                and PPCS.ORDER_PLATE_NUM = #{orderPlateNum,jdbcType = INTEGER}
            </if>
            <if test="pdMatType !=null">
                and PPCS.PD_MAT_TYPE = #{pdMatType,jdbcType = VARCHAR}
            </if>
            <if test="slabNo !=null">
                and PPCS.SLAB_NO = #{slabNo,jdbcType = VARCHAR}
            </if>
            <if test="plateNo !=null">
                and PPCS.PLATE_NO = #{plateNo,jdbcType = VARCHAR}
            </if>
            <if test="pdStatus !=null">
                and PPCS.PD_STATUS = #{pdStatus,jdbcType = VARCHAR}
            </if>
            <if test="axisFlag !=null">
                and PPCS.AXIS_FLAG = #{axisFlag,jdbcType = INTEGER}
            </if>
            <if test="orderThick !=null">
                and PPCS.ORDER_THICK = #{orderThick,jdbcType = INTEGER}
            </if>
            <if test="orderWidth !=null">
                and PPCS.ORDER_WIDTH = #{orderWidth,jdbcType = INTEGER}
            </if>
            <if test="orderLen !=null">
                and PPCS.ORDER_LEN = #{orderLen,jdbcType = INTEGER}
            </if>
            <if test="orderLenMin !=null">
                and PPCS.ORDER_LEN_MIN = #{orderLenMin,jdbcType = INTEGER}
            </if>
            <if test="orderLenMax !=null">
                and PPCS.ORDER_LEN_MAX = #{orderLenMax,jdbcType = INTEGER}
            </if>
            <if test="smallPlateThick !=null">
                and PPCS.SMALL_PLATE_THICK = #{smallPlateThick,jdbcType = INTEGER}
            </if>
            <if test="smallPlateWidth !=null">
                and PPCS.SMALL_PLATE_WIDTH = #{smallPlateWidth,jdbcType = INTEGER}
            </if>
            <if test="smallPlateLen !=null">
                and PPCS.SMALL_PLATE_LEN = #{smallPlateLen,jdbcType = INTEGER}
            </if>
            <if test="tenantId !=null">
                and PPCS.TENANT_ID = #{tenantId,jdbcType = INTEGER}
            </if>
            <if test="attribute1 !=null">
                and PPCS.ATTRIBUTE1 = #{attribute1,jdbcType = VARCHAR}
            </if>
            <if test="attribute2 !=null">
                and PPCS.ATTRIBUTE2 = #{attribute2,jdbcType = VARCHAR}
            </if>
            <if test="attribute3 !=null">
                and PPCS.ATTRIBUTE3 = #{attribute3,jdbcType = VARCHAR}
            </if>
            <if test="attribute4 !=null">
                and PPCS.ATTRIBUTE4 = #{attribute4,jdbcType = VARCHAR}
            </if>
            <if test="attribute5 !=null">
                and PPCS.ATTRIBUTE5 = #{attribute5,jdbcType = VARCHAR}
            </if>
        </where>
    </select>

    <select id="selectConPanelMaterialsList" resultType="com.xisc.pm.api.dto.PmPdContPanelMaterialDTO">
        SELECT
            ppc.ORDER_NO,
            ppcs.PD_MAT_NO,
            ppcs.PD_STATUS,
            ppcs.ORDER_PLATE_NUM,
            ppcpa.ORDER_WT,
            ppcpa.ORDER_QTY,
            ppc.PD_TOT_WT,
            ppc.PD_TOT_QTY,
            ppc.PD_LACK_WT,
            ppc.PD_LACK_QTY,
            ppcpa.MSC,
            ppcpa.PSC,
            ppcpa.APN,
            ppcpa.ORDER_TYPE_CODE
        FROM
            PM_PRO_CONT ppc
                LEFT JOIN PM_PD_CONT_SUM ppcs ON ppc.PRO_CONT_ID = ppcs.PRO_CONT_ID
                LEFT JOIN PM_PRO_CONT_PROD_ATTR ppcpa ON ppc.PRO_CONT_ID = ppcpa.PRO_CONT_ID
        <where>
            <if test="tenantId !=null">
                and ppc.TENANT_ID = #{tenantId}
            </if>
            <if test="orderNoFrom !=null">
                and ppc.ORDER_NO  <![CDATA[ >= ]]> #{orderNoFrom}
            </if>
            <if test="orderNoTo !=null">
                and ppc.ORDER_NO  <![CDATA[ <= ]]> #{orderNoTo}
            </if>
        </where>
        ORDER BY ppc.creation_date DESC
    </select>
    
    <delete id="deleteByMatNo">
		delete from PM_PD_CONT_SUM 
		where TENANT_ID = #{tenantId}
		and PD_MAT_NO in
		<foreach collection="removePdMatNo" item="matNo" open="(" close=")" separator=",">
			#{matNo}
		</foreach>
	</delete>
</mapper>

