<?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.foreverwin.mesnac.production.mapper.SplitSfcMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.foreverwin.mesnac.production.model.SplitSfc">
        <id column="HANDLE" property="handle" />
        <result column="SITE" property="site" />
        <result column="SFC" property="sfc" />
        <result column="SPLIT_SFC" property="splitSfc" />
        <result column="SPLIT_QTY" property="splitQty" />
        <result column="ROUTER" property="router" />
        <result column="CREATE_USER" property="createUser" />
        <result column="CREATED_DATE_TIME" property="createdDateTime" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        HANDLE, SITE, SFC, SPLIT_SFC, SPLIT_QTY, ROUTER, CREATE_USER, CREATED_DATE_TIME
    </sql>

    <!-- BaseMapper标准查询/修改/删除 -->
    <select id="selectById" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"></include> FROM Z_SPLIT_SFC WHERE HANDLE=#{handle}
    </select>

    <select id="selectByMap" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"></include>
        FROM Z_SPLIT_SFC
        <if test="cm!=null and !cm.isEmpty">
            <where>
                <foreach collection="cm.keys" item="k" separator="AND">
                    <if test="cm[k] != null">
                        ${k} = #{cm[${k}]}
                    </if>
                </foreach>
            </where>
        </if>
    </select>

    <select id="selectBatchIds" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"></include>
        FROM Z_SPLIT_SFC WHERE HANDLE IN (
        <foreach item="item" index="index" collection="coll" separator=",">#{item}
        </foreach>)
    </select>

    <select id="selectOne" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"></include> FROM Z_SPLIT_SFC
        <where>
            <if test="ew.entity.handle!=null">
              HANDLE=#{ew.handle}
            </if>
            <if test="ew.entity.site!=null"> AND SITE=#{ew.entity.site}</if>
            <if test="ew.entity.sfc!=null"> AND SFC=#{ew.entity.sfc}</if>
            <if test="ew.entity.splitSfc!=null"> AND SPLIT_SFC=#{ew.entity.splitSfc}</if>
            <if test="ew.entity.splitQty!=null"> AND SPLIT_QTY=#{ew.entity.splitQty}</if>
            <if test="ew.entity.router!=null"> AND ROUTER=#{ew.entity.router}</if>
            <if test="ew.entity.createUser!=null"> AND CREATE_USER=#{ew.entity.createUser}</if>
            <if test="ew.entity.createdDateTime!=null"> AND CREATED_DATE_TIME=#{ew.entity.createdDateTime}</if>
        </where>
    </select>

    <select id="selectCount" resultType="Integer">
        SELECT COUNT(1) FROM Z_SPLIT_SFC
        <where>
            <if test="ew!=null">
                <if test="ew.entity!=null">
                <if test="ew.entity.handle!=null">
                    HANDLE=#{ew.entity.handle}
                </if>
                <if test="ew.entity.site!=null"> AND SITE=#{ew.entity.site}</if>
                <if test="ew.entity.sfc!=null"> AND SFC=#{ew.entity.sfc}</if>
                <if test="ew.entity.splitSfc!=null"> AND SPLIT_SFC=#{ew.entity.splitSfc}</if>
                <if test="ew.entity.splitQty!=null"> AND SPLIT_QTY=#{ew.entity.splitQty}</if>
                <if test="ew.entity.router!=null"> AND ROUTER=#{ew.entity.router}</if>
                <if test="ew.entity.createUser!=null"> AND CREATE_USER=#{ew.entity.createUser}</if>
                <if test="ew.entity.createdDateTime!=null"> AND CREATED_DATE_TIME=#{ew.entity.createdDateTime}</if>
                </if>
                <if test="ew!=null and ew.sqlSegment!=null and ew.nonEmptyOfWhere">
                    ${ew.sqlSegment}
                </if>
            </if>
        </where>
        <if test="ew!=null and ew.sqlSegment!=null and ew.emptyOfWhere">
            ${ew.sqlSegment}
        </if>
    </select>

    <select id="selectList" resultMap="BaseResultMap">
        SELECT <choose><when test="ew != null and ew.sqlSelect != null">${ew.sqlSelect}</when><otherwise><include refid="Base_Column_List"></include></otherwise></choose> FROM Z_SPLIT_SFC
        <where>
            <if test="ew!=null">
                <if test="ew.entity!=null">
                    <if test="ew.entity.handle!=null">
                        HANDLE=#{ew.entity.handle}
                    </if>
                    <if test="ew.entity.site!=null"> AND SITE=#{ew.entity.site}</if>
                    <if test="ew.entity.sfc!=null"> AND SFC=#{ew.entity.sfc}</if>
                    <if test="ew.entity.splitSfc!=null"> AND SPLIT_SFC=#{ew.entity.splitSfc}</if>
                    <if test="ew.entity.splitQty!=null"> AND SPLIT_QTY=#{ew.entity.splitQty}</if>
                    <if test="ew.entity.router!=null"> AND ROUTER=#{ew.entity.router}</if>
                    <if test="ew.entity.createUser!=null"> AND CREATE_USER=#{ew.entity.createUser}</if>
                    <if test="ew.entity.createdDateTime!=null"> AND CREATED_DATE_TIME=#{ew.entity.createdDateTime}</if>
                </if>
                <if test="ew!=null and ew.sqlSegment!=null and ew.nonEmptyOfWhere">
                    ${ew.sqlSegment}
                </if>
            </if>
        </where>
        <if test="ew!=null and ew.sqlSegment!=null and ew.emptyOfWhere">
            ${ew.sqlSegment}
        </if>
    </select>

    <select id="selectMaps" resultType="HashMap">
        SELECT <choose><when test="ew != null and ew.sqlSelect != null">${ew.sqlSelect}</when><otherwise><include refid="Base_Column_List"></include></otherwise></choose> FROM Z_SPLIT_SFC
        <where>
            <if test="ew!=null">
                <if test="ew.entity!=null">
                    <if test="ew.entity.handle!=null">
                        HANDLE=#{ew.entity.handle}
                    </if>
                    <if test="ew.entity.site!=null"> AND SITE=#{ew.entity.site}</if>
                    <if test="ew.entity.sfc!=null"> AND SFC=#{ew.entity.sfc}</if>
                    <if test="ew.entity.splitSfc!=null"> AND SPLIT_SFC=#{ew.entity.splitSfc}</if>
                    <if test="ew.entity.splitQty!=null"> AND SPLIT_QTY=#{ew.entity.splitQty}</if>
                    <if test="ew.entity.router!=null"> AND ROUTER=#{ew.entity.router}</if>
                    <if test="ew.entity.createUser!=null"> AND CREATE_USER=#{ew.entity.createUser}</if>
                    <if test="ew.entity.createdDateTime!=null"> AND CREATED_DATE_TIME=#{ew.entity.createdDateTime}</if>
                </if>
                <if test="ew!=null and ew.sqlSegment!=null and ew.nonEmptyOfWhere">
                    ${ew.sqlSegment}
                </if>
            </if>
        </where>
        <if test="ew!=null and ew.sqlSegment!=null and ew.emptyOfWhere">
            ${ew.sqlSegment}
        </if>
    </select>

    <select id="selectObjs" resultType="Object">
        SELECT <choose><when test="ew != null and ew.sqlSelect != null">${ew.sqlSelect}</when><otherwise><include refid="Base_Column_List"></include></otherwise></choose> FROM Z_SPLIT_SFC
        <where>
            <if test="ew!=null">
                <if test="ew.entity!=null">
                    <if test="ew.entity.handle!=null">
                        HANDLE=#{ew.entity.handle}
                    </if>
                    <if test="ew.entity.site!=null"> AND SITE=#{ew.entity.site}</if>
                    <if test="ew.entity.sfc!=null"> AND SFC=#{ew.entity.sfc}</if>
                    <if test="ew.entity.splitSfc!=null"> AND SPLIT_SFC=#{ew.entity.splitSfc}</if>
                    <if test="ew.entity.splitQty!=null"> AND SPLIT_QTY=#{ew.entity.splitQty}</if>
                    <if test="ew.entity.router!=null"> AND ROUTER=#{ew.entity.router}</if>
                    <if test="ew.entity.createUser!=null"> AND CREATE_USER=#{ew.entity.createUser}</if>
                    <if test="ew.entity.createdDateTime!=null"> AND CREATED_DATE_TIME=#{ew.entity.createdDateTime}</if>
                </if>
                <if test="ew!=null and ew.sqlSegment!=null and ew.nonEmptyOfWhere">
                    ${ew.sqlSegment}
                </if>
            </if>
        </where>
        <if test="ew!=null and ew.sqlSegment!=null and ew.emptyOfWhere">
            ${ew.sqlSegment}
        </if>
    </select>

    <select id="selectPage" resultMap="BaseResultMap">
        SELECT <choose><when test="ew != null and ew.sqlSelect != null">${ew.sqlSelect}</when><otherwise><include refid="Base_Column_List"></include></otherwise></choose> FROM Z_SPLIT_SFC
        <where>
            <if test="ew!=null">
                <if test="ew.entity!=null">
                    <if test="ew.entity.handle!=null">
                        HANDLE=#{ew.entity.handle}
                    </if>
                    <if test="ew.entity.site!=null"> AND SITE=#{ew.entity.site}</if>
                    <if test="ew.entity.sfc!=null"> AND SFC=#{ew.entity.sfc}</if>
                    <if test="ew.entity.splitSfc!=null"> AND SPLIT_SFC=#{ew.entity.splitSfc}</if>
                    <if test="ew.entity.splitQty!=null"> AND SPLIT_QTY=#{ew.entity.splitQty}</if>
                    <if test="ew.entity.router!=null"> AND ROUTER=#{ew.entity.router}</if>
                    <if test="ew.entity.createUser!=null"> AND CREATE_USER=#{ew.entity.createUser}</if>
                    <if test="ew.entity.createdDateTime!=null"> AND CREATED_DATE_TIME=#{ew.entity.createdDateTime}</if>
                </if>
                <if test="ew!=null and ew.sqlSegment!=null and ew.nonEmptyOfWhere">
                    ${ew.sqlSegment}
                </if>
            </if>
        </where>
        <if test="ew!=null and ew.sqlSegment!=null and ew.emptyOfWhere">
            ${ew.sqlSegment}
        </if>
    </select>

    <select id="selectMapsPage" resultType="HashMap">
        SELECT <choose><when test="ew != null and ew.sqlSelect != null">${ew.sqlSelect}</when><otherwise><include refid="Base_Column_List"></include></otherwise></choose> FROM Z_SPLIT_SFC
        <where>
            <if test="ew!=null">
                <if test="ew.entity!=null">
                    <if test="ew.entity.handle!=null">
                        HANDLE=#{ew.entity.handle}
                    </if>
                    <if test="ew.entity.site!=null"> AND SITE=#{ew.entity.site}</if>
                    <if test="ew.entity.sfc!=null"> AND SFC=#{ew.entity.sfc}</if>
                    <if test="ew.entity.splitSfc!=null"> AND SPLIT_SFC=#{ew.entity.splitSfc}</if>
                    <if test="ew.entity.splitQty!=null"> AND SPLIT_QTY=#{ew.entity.splitQty}</if>
                    <if test="ew.entity.router!=null"> AND ROUTER=#{ew.entity.router}</if>
                    <if test="ew.entity.createUser!=null"> AND CREATE_USER=#{ew.entity.createUser}</if>
                    <if test="ew.entity.createdDateTime!=null"> AND CREATED_DATE_TIME=#{ew.entity.createdDateTime}</if>
                </if>
                <if test="ew!=null and ew.sqlSegment!=null and ew.nonEmptyOfWhere">
                    ${ew.sqlSegment}
                </if>
            </if>
        </where>
        <if test="ew!=null and ew.sqlSegment!=null and ew.emptyOfWhere">
            ${ew.sqlSegment}
        </if>
    </select>

    <insert id="insert" parameterType="com.foreverwin.mesnac.production.model.SplitSfc">
        INSERT INTO Z_SPLIT_SFC
        <trim prefix="(" suffix=")" suffixOverrides=",">
            HANDLE,
            <if test="site!=null">SITE,</if>
            <if test="sfc!=null">SFC,</if>
            <if test="splitSfc!=null">SPLIT_SFC,</if>
            <if test="splitQty!=null">SPLIT_QTY,</if>
            <if test="router!=null">ROUTER,</if>
            <if test="createUser!=null">CREATE_USER,</if>
            <if test="createdDateTime!=null">CREATED_DATE_TIME,</if>
        </trim> VALUES
        <trim prefix="(" suffix=")" suffixOverrides=",">
            #{handle},
            <if test="site!=null">#{site},</if>
            <if test="sfc!=null">#{sfc},</if>
            <if test="splitSfc!=null">#{splitSfc},</if>
            <if test="splitQty!=null">#{splitQty},</if>
            <if test="router!=null">#{router},</if>
            <if test="createUser!=null">#{createUser},</if>
            <if test="createdDateTime!=null">#{createdDateTime},</if>
        </trim>
    </insert>

    <insert id="insertAllColumn" parameterType="com.foreverwin.mesnac.production.model.SplitSfc">
        INSERT INTO Z_SPLIT_SFC
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <include refid="Base_Column_List"></include>
        </trim> VALUES
        <trim prefix="(" suffix=")" suffixOverrides=",">
            #{handle},
            #{site},
            #{sfc},
            #{splitSfc},
            #{splitQty},
            #{router},
            #{createUser},
            #{createdDateTime},
        </trim>
    </insert>


    <update id="updateById">
        UPDATE Z_SPLIT_SFC <trim prefix="SET" suffixOverrides=",">
        <if test="et.site!=null">SITE=#{et.site},</if>
        <if test="et.sfc!=null">SFC=#{et.sfc},</if>
        <if test="et.splitSfc!=null">SPLIT_SFC=#{et.splitSfc},</if>
        <if test="et.splitQty!=null">SPLIT_QTY=#{et.splitQty},</if>
        <if test="et.router!=null">ROUTER=#{et.router},</if>
        <if test="et.createUser!=null">CREATE_USER=#{et.createUser},</if>
        <if test="et.createdDateTime!=null">CREATED_DATE_TIME=#{et.createdDateTime},</if>
        </trim> WHERE HANDLE=#{et.handle} <if test="et instanceof java.util.Map"><if test="et.MP_OPTLOCK_VERSION_ORIGINAL!=null">and ${et.MP_OPTLOCK_VERSION_COLUMN}=#{et.MP_OPTLOCK_VERSION_ORIGINAL}</if></if>
    </update>


    <update id="updateAllColumnById">
        UPDATE Z_SPLIT_SFC <trim prefix="SET" suffixOverrides=",">
        SITE=#{et.site},
        SFC=#{et.sfc},
        SPLIT_SFC=#{et.splitSfc},
        SPLIT_QTY=#{et.splitQty},
        ROUTER=#{et.router},
        CREATE_USER=#{et.createUser},
        CREATED_DATE_TIME=#{et.createdDateTime},
     </trim> WHERE HANDLE=#{et.handle} <if test="et instanceof java.util.Map"><if test="et.MP_OPTLOCK_VERSION_ORIGINAL!=null">and ${et.MP_OPTLOCK_VERSION_COLUMN}=#{et.MP_OPTLOCK_VERSION_ORIGINAL}</if></if>
    </update>


    <update id="update">
        UPDATE Z_SPLIT_SFC <trim prefix="SET" suffixOverrides=",">
        <if test="et.site!=null">SITE=#{et.site},</if>
        <if test="et.sfc!=null">SFC=#{et.sfc},</if>
        <if test="et.splitSfc!=null">SPLIT_SFC=#{et.splitSfc},</if>
        <if test="et.splitQty!=null">SPLIT_QTY=#{et.splitQty},</if>
        <if test="et.router!=null">ROUTER=#{et.router},</if>
        <if test="et.createUser!=null">CREATE_USER=#{et.createUser},</if>
        <if test="et.createdDateTime!=null">CREATED_DATE_TIME=#{et.createdDateTime},</if>
    </trim>
        <where>
            <if test="ew!=null">
                <if test="ew.entity!=null">
                    HANDLE=#{ew.entity.handle}
                    <if test="ew.entity.site!=null"> AND SITE=#{ew.entity.site}</if>
                    <if test="ew.entity.sfc!=null"> AND SFC=#{ew.entity.sfc}</if>
                    <if test="ew.entity.splitSfc!=null"> AND SPLIT_SFC=#{ew.entity.splitSfc}</if>
                    <if test="ew.entity.splitQty!=null"> AND SPLIT_QTY=#{ew.entity.splitQty}</if>
                    <if test="ew.entity.router!=null"> AND ROUTER=#{ew.entity.router}</if>
                    <if test="ew.entity.createUser!=null"> AND CREATE_USER=#{ew.entity.createUser}</if>
                    <if test="ew.entity.createdDateTime!=null"> AND CREATED_DATE_TIME=#{ew.entity.createdDateTime}</if>
                </if>
                <if test="ew!=null and ew.sqlSegment!=null and ew.nonEmptyOfWhere">
                    ${ew.sqlSegment}
                </if>
            </if>
        </where>
        <if test="ew!=null and ew.sqlSegment!=null and ew.emptyOfWhere">
            ${ew.sqlSegment}
        </if>
    </update>

    <delete id="deleteById">
        DELETE FROM Z_SPLIT_SFC WHERE HANDLE=#{handle}
    </delete>

    <delete id="deleteByMap">
        DELETE FROM Z_SPLIT_SFC
        <if test="cm!=null and !cm.isEmpty">
            <where>
                <foreach collection="cm.keys" item="k" separator="AND">
                    <if test="cm[k] != null">
                        ${k} = #{cm[${k}]}
                    </if>
                </foreach>
            </where>
        </if>
    </delete>

    <delete id="delete">
        DELETE FROM Z_SPLIT_SFC
        <where>
            <if test="ew!=null">
                <if test="ew.entity!=null">
                    <if test="ew.entity.handle!=null">
                        HANDLE=#{ew.entity.handle}
                    </if>
                    <if test="ew.entity.site!=null"> AND SITE=#{ew.entity.site}</if>
                    <if test="ew.entity.sfc!=null"> AND SFC=#{ew.entity.sfc}</if>
                    <if test="ew.entity.splitSfc!=null"> AND SPLIT_SFC=#{ew.entity.splitSfc}</if>
                    <if test="ew.entity.splitQty!=null"> AND SPLIT_QTY=#{ew.entity.splitQty}</if>
                    <if test="ew.entity.router!=null"> AND ROUTER=#{ew.entity.router}</if>
                    <if test="ew.entity.createUser!=null"> AND CREATE_USER=#{ew.entity.createUser}</if>
                    <if test="ew.entity.createdDateTime!=null"> AND CREATED_DATE_TIME=#{ew.entity.createdDateTime}</if>
                </if>
                <if test="ew!=null and ew.sqlSegment!=null and ew.nonEmptyOfWhere">
                    ${ew.sqlSegment}
                </if>
            </if>
        </where>
        <if test="ew!=null and ew.sqlSegment!=null and ew.emptyOfWhere">
            ${ew.sqlSegment}
        </if>
    </delete>

    <delete id="deleteBatchIds">
        DELETE FROM Z_SPLIT_SFC WHERE HANDLE IN (
        <foreach item="item" index="index" collection="coll" separator=",">#{item}
        </foreach>)
    </delete>
    <!-- BaseMapper标准查询/修改/删除 -->
    <select id="getSfcData" resultType="com.foreverwin.mesnac.meapi.dto.SfcDto">
        SELECT SO.SHOP_ORDER,cf.VALUE WORK_ORDER,OP.OPERATION,ss.STEP_ID,s.QTY,ST.STATUS FROM SFC S
        JOIN SHOP_ORDER SO ON so.HANDLE=S.SHOP_ORDER_BO
        JOIN STATUS ST ON ST.HANDLE = S.STATUS_BO
        LEFT JOIN CUSTOM_FIELDS CF ON cf.HANDLE=s.SHOP_ORDER_BO AND CF."ATTRIBUTE"='WORK_ORDER'
        JOIN SFC_ROUTING SR ON s.HANDLE=sr.SFC_BO
        JOIN SFC_ROUTER SR2 ON SR.HANDLE =SR2.SFC_ROUTING_BO  AND SR2.IN_USE = 'true'
        JOIN  SFC_STEP SS ON SR2.HANDLE =SS.SFC_ROUTER_BO AND (SS.QTY_IN_QUEUE>0 or SS.QTY_IN_WORK>0)
        JOIN OPERATION OP  ON 'OperationBO:'|| OP.SITE ||','|| OP.OPERATION||',#'=SS.OPERATION_BO
        WHERE S.HANDLE=#{handle}
    </select>
    <select id="getAbnormalQty" resultType="com.foreverwin.mesnac.production.dto.SplitSfcDto">
    SELECT zab.NC_QTY,zsbd.ROUTER_BO,zab.NC_CODE,zab.ABNORMAL_NO FROM Z_ABNORMAL_BILL zab
    JOIN Z_ABNORMAL_BILL_DISPOSE zsbd ON zab.HANDLE=ZSBD.ABNORMAL_BILL_BO
    WHERE  ZAB.SFC=#{sfc} AND zab.ABNORMAL_NO=#{abnormalNo} AND zab.SITE=#{site} AND ZAB.OPERATION=#{operation} AND zab.STATUS='G' AND zab."TYPE"='Z' AND (zsbd.ABNORMAL_METHOD='F' OR zsbd.ABNORMAL_METHOD='C')
    </select>

</mapper>
