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


    <sql id = "BaseSql">
    PCS.SLAB_ID,
    PCS.SLAB_NO,
    PCS.HEAT_NO,
    PCS.PONO,
    PCS.ACT_THICK,
    PCS.ACT_WIDTH,
    PCS.ACT_LENGTH,
    PCS.ACT_WT,
    PCS.ROLL_TYPE,
    PCS.ROLL_AIM_THICK,
    PCS.ROLL_AIM_WIDTH,
    PCS.STATUS_CODE,
    PCS.SLAB_TYPE,
    PCS.DEST_STOCK_ID,
    PCS.DEST_STOCK_CODE,
    PCS.FACTORY_CODE,
    PCS.FACTORY_NAME,
    PCS.MSC_LINE_NO,
    PCS.CORRECTION_FLAG,
    PCS.TENANT_ID,
    PCS.ATTRIBUTE1,
    PCS.ATTRIBUTE2,
    PCS.ATTRIBUTE3,
    PCS.ATTRIBUTE4,
    PCS.ATTRIBUTE5,
    PCS.SAMPLE_LOT_NO,
    PCS.creation_date,    PCS.created_by,    PCS.last_updated_by,    PCS.last_update_date,    PCS.object_version_number
    </sql>

    <select id = "selectList" resultType = "com.xisc.pm.domain.entity.PmCompositeSlab">
        select
        <include refid = "BaseSql"/>
        from PM_COMPOSITE_SLAB PCS
        <where>
            <if test="slabId !=null">
                and PCS.SLAB_ID = #{slabId,jdbcType = INTEGER}
            </if>
            <if test="slabNo !=null">
                and PCS.SLAB_NO = #{slabNo,jdbcType = VARCHAR}
            </if>
            <if test="heatNo !=null">
                and PCS.HEAT_NO = #{heatNo,jdbcType = VARCHAR}
            </if>
            <if test="pono !=null">
                and PCS.PONO = #{pono,jdbcType = VARCHAR}
            </if>
            <if test="actThick !=null">
                and PCS.ACT_THICK = #{actThick,jdbcType = INTEGER}
            </if>
            <if test="actWidth !=null">
                and PCS.ACT_WIDTH = #{actWidth,jdbcType = INTEGER}
            </if>
            <if test="actLength !=null">
                and PCS.ACT_LENGTH = #{actLength,jdbcType = INTEGER}
            </if>
            <if test="actWt !=null">
                and PCS.ACT_WT = #{actWt,jdbcType = INTEGER}
            </if>
            <if test="rollType !=null">
                and PCS.ROLL_TYPE = #{rollType,jdbcType = VARCHAR}
            </if>
            <if test="rollAimThick !=null">
                and PCS.ROLL_AIM_THICK = #{rollAimThick,jdbcType = INTEGER}
            </if>
            <if test="rollAimWidth !=null">
                and PCS.ROLL_AIM_WIDTH = #{rollAimWidth,jdbcType = INTEGER}
            </if>
            <if test="statusCode !=null">
                and PCS.STATUS_CODE = #{statusCode,jdbcType = VARCHAR}
            </if>
            <if test="slabType !=null">
                and PCS.SLAB_TYPE = #{slabType,jdbcType = VARCHAR}
            </if>
            <if test="destStockId !=null">
                and PCS.DEST_STOCK_ID = #{destStockId,jdbcType = INTEGER}
            </if>
            <if test="destStockCode !=null">
                and PCS.DEST_STOCK_CODE = #{destStockCode,jdbcType = VARCHAR}
            </if>
            <if test="factoryCode !=null">
                and PCS.FACTORY_CODE = #{factoryCode,jdbcType = VARCHAR}
            </if>
            <if test="factoryName !=null">
                and PCS.FACTORY_NAME = #{factoryName,jdbcType = VARCHAR}
            </if>
            <if test="mscLineNo !=null">
                and PCS.MSC_LINE_NO = #{mscLineNo,jdbcType = VARCHAR}
            </if>
            <if test="correctionFlag !=null">
                and PCS.CORRECTION_FLAG = #{correctionFlag,jdbcType = VARCHAR}
            </if>
            <if test="tenantId !=null">
                and PCS.TENANT_ID = #{tenantId,jdbcType = INTEGER}
            </if>
            <if test="attribute1 !=null">
                and PCS.ATTRIBUTE1 = #{attribute1,jdbcType = VARCHAR}
            </if>
            <if test="attribute2 !=null">
                and PCS.ATTRIBUTE2 = #{attribute2,jdbcType = VARCHAR}
            </if>
            <if test="attribute3 !=null">
                and PCS.ATTRIBUTE3 = #{attribute3,jdbcType = VARCHAR}
            </if>
            <if test="attribute4 !=null">
                and PCS.ATTRIBUTE4 = #{attribute4,jdbcType = VARCHAR}
            </if>
            <if test="attribute5 !=null">
                and PCS.ATTRIBUTE5 = #{attribute5,jdbcType = VARCHAR}
            </if>
        </where>
    </select>

    <select id="selectSlabIds" resultType = "com.xisc.pm.domain.entity.PmCompositeSlab">
        SELECT
            DISTINCT	pcs.SLAB_ID
        FROM
            PM_COMPOSITE_SLAB pcs
                LEFT JOIN PM_COMPOSITE_SLAB_LIST pcsl on pcs.SLAB_ID = pcsl.SLAB_ID
                left join PM_PRO_CONT_PROD_ATTR ppcpa on pcsl.BASE_ORDER_NO = ppcpa.ORDER_NO
        <where>
            <if test="tenantId !=null">
                and pcs.TENANT_ID = #{tenantId}
            </if>
            <if test="dto.slabType !=null">
                and pcs.SLAB_TYPE = #{dto.slabType}
            </if>
            <if test="dto.statusCode !=null">
                and pcs.STATUS_CODE = #{dto.statusCode}
            </if>
            <if test="dto.destStockCode !=null">
                and pcs.DEST_STOCK_CODE = #{dto.destStockCode}
            </if>
            <if test="dto.orderNo !=null">
                and pcsl.BASE_ORDER_NO = #{dto.orderNo}
            </if>
            <if test="dto.slabNo !=null">
                <bind name="slabNoLike" value="'%' + dto.slabNo + '%'"/>
                and pcs.SLAB_NO like #{slabNoLike}
            </if>
            <if test="dto.createDateFrom !=null">
                and pcs.creation_date <![CDATA[ >= ]]> #{dto.createDateFrom}
            </if>
            <if test="dto.createDateTo !=null">
                and pcs.creation_date <![CDATA[ <= ]]> #{dto.createDateTo}
            </if>
            <if test="dto.createdById != null">
                and pcs.created_by = #{dto.createdById}
            </if>
        </where>
        INTERSECT
        SELECT
            DISTINCT	pcs.SLAB_ID
        FROM
            PM_COMPOSITE_SLAB pcs
                LEFT JOIN PM_COMPOSITE_SLAB_LIST pcsl on pcs.SLAB_ID = pcsl.SLAB_ID
                left join PM_PRO_CONT_PROD_ATTR ppcpa on pcsl.BASE_ORDER_NO = ppcpa.ORDER_NO
        <where>
            <if test="tenantId !=null">
                and pcs.TENANT_ID = #{tenantId}
            </if>
            <if test="dto.slabType !=null">
                and pcs.SLAB_TYPE = #{dto.slabType}
            </if>
            <if test="dto.statusCode !=null">
                and pcs.STATUS_CODE = #{dto.statusCode}
            </if>
            <if test="dto.destStockCode !=null">
                and pcs.DEST_STOCK_CODE = #{dto.destStockCode}
            </if>
            <if test="dto.consignCustCname !=null">
                <bind name="consignCustCnameLike" value="'%' + dto.consignCustCname  + '%'"/>
                and ppcpa.CONSIGN_CUST_CNAME like #{consignCustCnameLike}
            </if>
            <if test="dto.slabNo !=null">
                <bind name="slabNoLike" value="'%' + dto.slabNo + '%'"/>
                and pcs.SLAB_NO like #{slabNoLike}
            </if>
        </where>
    </select>
    
    <select id="compositePdList" resultType="com.xisc.pm.api.dto.PmCompositeSlabPdDTO">
    	select 
		    pcs.SLAB_ID,
		    pcs.SLAB_NO,
		    pcsl.LIST_ID,
		    pcsl.LAYER_NO,
		    pcsl.BASE_ORDER_ID,
		    pcsl.BASE_ORDER_NO,
		    pcs.STATUS_CODE,
		    pcs.DEST_STOCK_CODE,
		    pcs.HEAT_NO,
		    pcs.PONO,
		    pcs.ACT_THICK,
		    pcs.ACT_WIDTH,
		    pcs.ACT_LENGTH,
		    pcs.ROLL_TYPE,
		    pcs.ROLL_AIM_THICK,
		    pcs.ROLL_AIM_WIDTH,
		    pcsl.BASE_MAT_NO,
		    pcsl.BASE_MAT_THICK,
		    pcsl.BASE_MAT_WIDTH,
		    pcsl.BASE_MAT_LENGTH,
		    pcsl.BASE_MAT_WT,
		    pcsl.COMP_MAT_NO,
		    pcsl.COMP_MAT_PONO
		FROM PM_COMPOSITE_SLAB pcs
		left join PM_COMPOSITE_SLAB_LIST pcsl on pcsl.SLAB_ID = pcs.SLAB_ID
        join MM_SLAB ms on ms.SLAB_NO = pcs.SLAB_NO
		where pcs.STATUS_CODE in ('54','56')
        <if test="dto.tenantId !=null">
            and pcs.TENANT_ID = #{dto.tenantId}
        </if>
        <if test="dto.statusCode != null and dto.statusCode != ''">
            and pcs.STATUS_CODE = #{dto.statusCode}
        </if>
        <if test="dto.stockCode != null and dto.stockCode != ''">
            and pcs.DEST_STOCK_CODE = #{dto.stockCode}
        </if>
        <if test="dto.slabType !=null">
            and pcs.SLAB_TYPE = #{dto.slabType}
        </if>
        <if test="dto.slabNo != null and dto.slabNo != ''">
        	<bind name="slabNoLike" value="'%' + dto.slabNo + '%'"/>
        	and pcs.SLAB_NO like #{slabNoLike}
        </if>
        <if test="dto.createDateFrom != null">
            and pcs.creation_date <![CDATA[ >= ]]> #{dto.createDateFrom}
        </if>
        <if test="dto.createDateTo != null">
            and pcs.creation_date <![CDATA[ <= ]]> #{dto.createDateTo}
        </if>
        <if test="dto.createdById != null">
            and pcs.created_by = #{dto.createdById}
        </if>
    	order by pcs.SLAB_NO ASC,  pcsl.LAYER_NO ASC
    </select>

    <select id="selectMatSigleByMatNo" resultType="com.xisc.pm.domain.vo.WmMatSigleVO">
        SELECT
            wms.MAT_SIGLE_ID,
            wms.MAT_NO,
            wms.HEAT_NO,
            wms.MAT_THICK,
            wms.MAT_TYPE,
            wms.MAT_QTY
        FROM
            WM_MAT_SIGLE wms
        <where>
            <if test="tenantId !=null">
                and wms.TENANT_ID = #{tenantId}
            </if>
            <if test="matSigleIdList != null and matSigleIdList.size() > 0">
                and wms.MAT_SIGLE_ID in
                <foreach collection="matSigleIdList" item="matSigleId" open="(" separator="," close=")">
                    #{matSigleId}
                </foreach>
            </if>
        </where>
    </select>

    <select id="selectBaseMatDataByIds" resultType="com.xisc.pm.domain.vo.MmPlateVO">
        SELECT DISTINCT
            mp.PLATE_ID,
            mp.MAT_NO,
            mp.MAT_ACT_THK,
            mp.MAT_ACT_WID,
            mp.MAT_ACT_LEN,
            mp.MAT_ACT_WGT
        FROM
            MM_PLATE mp
        <where>
            mp.REC_STS = '2'
            <if test="tenantId !=null">
                and mp.TENANT_ID = #{tenantId}
            </if>
            <if test="baseMatIdList != null and baseMatIdList.size() > 0">
                and mp.PLATE_ID in
                <foreach collection="baseMatIdList" item="matId" open="(" separator="," close=")">
                    #{matId}
                </foreach>
            </if>
        </where>
    </select>

    <select id="getCurrentUserUintCode" resultType="string">
        SELECT
            hu.UNIT_CODE
        FROM
            IAM_USER iu
                JOIN HPFM_EMPLOYEE_USER heu ON iu.ID = heu.USER_ID
                JOIN HPFM_EMPLOYEE_ASSIGN hea ON heu.EMPLOYEE_ID = hea.EMPLOYEE_ID
                JOIN HPFM_UNIT hu ON hea.UNIT_ID = hu.UNIT_ID
        WHERE
            hu.UNIT_TYPE_CODE = 'D'
            AND iu.ID = #{userId}
            AND heu.TENANT_ID = #{tenantId}
    </select>

    <select id="getStockLineById" resultType="com.xisc.pm.domain.vo.WmStockInfoVO">
        SELECT
        wsi.STOCK_ID,
        wsi.STOCK_CODE,
        wsi.STOCK_DESC,
        wsi.STOCK_LINE
        FROM
        WM_STOCK_INFO wsi
        WHERE
        wsi.stock_type = 'B'
        AND wsi.enable_flag = 1
        <if test="stockIdList != null and stockIdList.size() > 0">
            and wsi.STOCK_ID in
            <foreach collection="stockIdList" item="id" open="(" separator="," close=")">
                #{id}
            </foreach>
        </if>
        <if test="tenantId !=null">
            AND wsi.TENANT_ID = #{tenantId}
        </if>
    </select>

    <select id="selectContractNum" resultType="com.xisc.pm.domain.vo.QdToPoOrderVO">
        SELECT distinct
            qtpo.PACT_NO,
            qtpo.ORDER_NO,
            qtpo.ORDER_WT,
            qtpo.ORDER_THICK,
            qtpo.ORDER_WIDTH,
            qtpo.ORDER_LEN,
            qtpo.ORDER_UNIT_AIM_WT
        FROM
            QD_TO_PO_ORDER  qtpo
        <where>
            <if test="tenantId !=null">
                AND qtpo.TENANT_ID = #{tenantId}
            </if>
            <if test="orderNoList != null and orderNoList.size() > 0">
                and qtpo.ORDER_NO in
                <foreach collection="orderNoList" item="orderNo" open="(" separator="," close=")">
                    #{orderNo}
                </foreach>
            </if>
        </where>
    </select>

    <select id="getProcCdByMmSlan" resultType="com.xisc.pm.domain.vo.MmSlabVO">
        SELECT
            ms.PROC_CD,
            ms.SLAB_NO,
            ms.WHOLE_BACKLOG_CODE
        FROM
            MM_SLAB ms
        <where>
            <if test="tenantId !=null">
                AND ms.TENANT_ID = #{tenantId}
            </if>
            <if test="slabNo != null">
                and ms.SLAB_NO = #{slabNo}
            </if>
        </where>
    </select>

    <select id="selectPlateByBaseMatNo" resultType="com.xisc.pm.domain.vo.MmPlateVO">
        SELECT
            mp.MAT_NO,
            mp.MAT_ACT_THK,
            mp.MAT_ACT_WID,
            mp.MAT_ACT_LEN,
            mp.MAT_ACT_WGT
        FROM MM_PLATE mp
         <where>
            <if test="tenantId !=null">
                AND mp.TENANT_ID = #{tenantId}
            </if>
        	<if test="baseMatNoList != null and baseMatNoList.size() > 0">
				AND mp.MAT_NO in
				<foreach collection="baseMatNoList" item="baseMatNo" open="(" close=")" separator="," >
					#{baseMatNo}
				</foreach>
			</if>
        </where>
    </select>

    <select id="selectCtDetByOrderList" resultType="com.xisc.pm.domain.vo.CtDetVO">
        SELECT
            cd.ORDER_NO,
            cd.STOCK_NUM,
            cd.PLAN_NUM,
            (COALESCE(cd.STOCK_NUM, 0) - COALESCE(cd.PLAN_NUM, 0)) AS userdBoardNum
        FROM
            CT_DET cd
        <where>
            cd.SUB_BACKLOG_CODE IS NULL
            AND cd.WHOLE_BACKLOG_CODE = '13'
            <if test="tenantId !=null">
                AND cd.TENANT_ID = #{tenantId}
            </if>
            <if test="orderNoList != null and orderNoList.size() > 0">
                AND cd.ORDER_NO in
                <foreach collection="orderNoList" item="orderNo" open="(" close=")" separator="," >
                    #{orderNo}
                </foreach>
            </if>
        </where>
    </select>

    <select id="selectMatPileByMatNo" resultType="com.xisc.pm.domain.vo.WmInStockVO">
        SELECT
        temp.mat_id,
        temp.mat_no,
        temp.mat_kind,
        temp.stock_code,
        temp.pile_no,
        temp.stock_line,
        '' event_type,
        SYSDATE prod_time
        FROM
        (
        SELECT
        t.mat_id,
        t.mat_no,
        t.mat_kind,
        t.stock_code,
        t.pile_no,
        wsi.stock_line,
        ROW_NUMBER ( ) OVER ( PARTITION BY t.mat_no ORDER BY t.creation_date DESC ) AS rn
        FROM
        WM_MAT_PILE_HIS t
        LEFT JOIN wm_stock_info wsi ON t.stock_code = wsi.stock_code
        <where>
            <if test="tenantId !=null">
                AND t.TENANT_ID = #{tenantId}
            </if>
            <if test="baseMatNoList != null and baseMatNoList.size() > 0">
                AND t.mat_no in
                <foreach collection="baseMatNoList" item="matNo" open="(" close=")" separator="," >
                    #{matNo}
                </foreach>
            </if>
        </where>
        ) temp
        WHERE
        temp.rn = 1
    </select>
</mapper>

