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


    <sql id="BaseSql">
        PSH
        .
        ID
        ,
    PSH.SLAB_NO,
    PSH.SUB_YIELD,
    PSH.REL_YIELD,
    PSH.SUB_RESULT,
    PSH.SUB_INFO,
    PSH.PROCESS_CODE,
    PSH.FORM_PLATE_MODE,
    PSH.ST_NO,
    PSH.SLAB_THICK,
    PSH.SLAB_WID,
    PSH.SLAB_LEN,
    PSH.SLAB_WT,
    PSH.SUB_TIME,
    PSH.TENANT_ID,
    PSH.ATTRIBUTE1,
    PSH.ATTRIBUTE2,
    PSH.ATTRIBUTE3,
    PSH.ATTRIBUTE4,
    PSH.ATTRIBUTE5,
    PSH.VIRTUAL_SLAB_NO,
    PSH.ROLL_THK,
    PSH.ROLL_WID,
    PSH.ROLL_LEN,
    PSH.HEAD_CUT,
    PSH.TAIL_CUT,
    PSH.WID_CUT,
    PSH.SLIT_LEN,
    PSH.NEED_SLAB_LEN,
    PSH.NEED_SLAB_WT,
    PSH.creation_date,    PSH.created_by,    PSH.last_updated_by,    PSH.last_update_date,    PSH.object_version_number
    </sql>

    <select id="selectList" parameterType="com.xisc.pm.api.dto.PmSubHisRequestDTO" resultType="com.xisc.pm.domain.entity.PmSubHis">
        select
        <include refid="BaseSql"/>
        from PM_SUB_HIS PSH
        <where>
            <if test="id !=null">
                and PSH.ID = #{id,jdbcType = INTEGER}
            </if>
            <if test="slabNo !=null">
                and PSH.SLAB_NO like concat(concat('%',#{slabNo,jdbcType = VARCHAR}), '%')
            </if>
            <if test="subResult !=null">
                and PSH.SUB_RESULT = #{subResult,jdbcType = VARCHAR}
            </if>
            <if test="formPlateMode !=null">
                and PSH.FORM_PLATE_MODE = #{formPlateMode,jdbcType = VARCHAR}
            </if>
            <if test="stNo !=null">
                and PSH.ST_NO like concat(concat('%',#{stNo,jdbcType = VARCHAR}), '%')
            </if>
            <if test="subTimeFrom !=null">
                and PSH.SUB_TIME <![CDATA[ >= ]]> #{subTimeFrom}
            </if>
            <if test="subTimeTo !=null">
                and PSH.SUB_TIME <![CDATA[ <= ]]> #{subTimeTo}
            </if>
            <if test="tenantId !=null">
                and PSH.TENANT_ID = #{tenantId,jdbcType = INTEGER}
            </if>
        </where>
    </select>

    <select id="selectStatisticsList" parameterType="com.xisc.pm.api.dto.PmSubHisRequestDTO"
            resultType="com.xisc.pm.api.dto.PmSubHisResponseDTO">
        select
        TO_CHAR(PSH.SUB_TIME, 'YYYY-MM-DD') AS SUB_TIME
        ,COUNT(*) AS SUB_NUM
        ,AVG(PSH.SUB_YIELD) AS SUB_YIELD
        ,SUM(PSH.SLAB_WT) AS SUB_WT
        FROM PM_SUB_HIS PSH
        <where>
            <if test="subTimeFrom !=null">
                and TO_CHAR(PSH.SUB_TIME, 'YYYY-MM-DD') <![CDATA[ >= ]]> #{subTimeFrom}
            </if>
            <if test="subTimeTo !=null">
                and TO_CHAR(PSH.SUB_TIME, 'YYYY-MM-DD') <![CDATA[ <= ]]> #{subTimeTo}
            </if>
            <if test="tenantId !=null">
                and PSH.TENANT_ID = #{tenantId,jdbcType = INTEGER}
            </if>
        </where>
        GROUP BY TO_CHAR(SUB_TIME, 'YYYY-MM-DD')
    </select>
</mapper>

