<?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.cim.idm.dao.ToSapDao">

    <!-- 通过箱号集合获取要成品入库的信息 -->
    <select id="getFgStockInInfo" resultType="com.cim.idm.model.ToSAPFgStockInDto" parameterType="java.util.List">
        SELECT m.MATERIALSPECNAME ,
        m.PRODUCTORDER ,
        m.PACKINGGRADE ,
        m.ERPFACTORY ,
        m.ERPLOCATION ,
        m.UNIT ,
        m.MATERIALSPECTYPE,
        sum(m.MATERIALQUANTITY ) MATERIALQUANTITY
        FROM MATERIALPACKING m
        LEFT JOIN BS_MES_SHIPPED bms ON m.materialpackingname = bms.lotname
        WHERE bms.RECEIVE_FLAG != 'Y'
        AND m.STOCKSTATE = 'Created'
        AND m.materialpackingname IN
        <foreach item="box" index="index" collection="boxList" open="(" separator="," close=")">
            #{box.materialPackingName}
        </foreach>
        GROUP BY m.MATERIALSPECNAME ,m.PRODUCTORDER  ,m.ERPFACTORY ,m.ERPLOCATION ,m.UNIT ,
        m.PACKINGGRADE,m.MATERIALSPECTYPE

    </select>

    <!-- 更新库存状态 -->
    <update id="updateStockState" parameterType="map">
        UPDATE MATERIALPACKING
        SET STOCKSTATE = #{stockState},UNDOID = #{undoId}
        WHERE MATERIALPACKINGNAME IN
        <foreach item="box" index="index" collection="boxList" open="(" separator="," close=")">
            #{box.materialPackingName}
        </foreach>
    </update>

    <!-- 保存未完成信息 -->
    <insert id="saveUnDoInfo" parameterType="map">
        INSERT INTO BS_MATERIALPACKINGUNDOINFO (
            unDoID, preundoid, UNDODETAILNAME, undoidstate, UNDOIDDATE,
            SITENAME, MATERIALPACKINGNAME, MATERIALPACKINGTYPE, CONTENTMATERIALTYPE,
            MATERIALTYPE, MATERIALSPECNAME, SUPERMATERIALPACKINGNAME, PACKINGGRADE, SUBPACKINGQUANTITY, MATERIALCREATEQUANTITY,
            MATERIALQUANTITY, PACKINGSTATE, OLDPACKINGSTATE, STOCKSTATE, OLDSTOCKSTATE, HOLDSTATE, AREANAME, LOCATIONNAME,
            DURABLENAME, MATERIALPROCESSGROUPNAME, LASTEVENTNAME, LASTEVENTTIMEKEY, LASTEVENTTIME, LASTEVENTUSER,
            LASTEVENTCOMMENT, CREATETIME, CREATEUSER, RECEIVEREQUESTNAME, RECEIVEREQUESTDETAILNAME,
            MAKEDATE, RECEIVEACTNO, STOCKINTYPE, RECEIVETIME, RECEIVEUSER, SHIPREQUESTNAME, SHIPREQUESTDETAILNAME, SHIPTIME,
            SHIPUSER, REASONCODETYPE, OQARESULTSTATE, OQARESULT, ERPLOCATION, ERPFACTORY, REASONCODE, CHARGE, SUPPLIERNAME,ETCSTATE,ERPEVENTTIME,NPFLAG
        )
        SELECT
            #{unDoID}, '' as preundoid, UNDODETAILNAME, 'Completed' as undoidstate, #{commitDate} AS UNDOIDDATE,
            SITENAME, MATERIALPACKINGNAME, MATERIALPACKINGTYPE, CONTENTMATERIALTYPE,
            MATERIALTYPE, MATERIALSPECNAME, SUPERMATERIALPACKINGNAME, PACKINGGRADE, SUBPACKINGQUANTITY, MATERIALCREATEQUANTITY,
            MATERIALQUANTITY, PACKINGSTATE, OLDPACKINGSTATE, STOCKSTATE, OLDSTOCKSTATE, HOLDSTATE, AREANAME, LOCATIONNAME,
            DURABLENAME, MATERIALPROCESSGROUPNAME, LASTEVENTNAME, LASTEVENTTIMEKEY, LASTEVENTTIME, LASTEVENTUSER,
            LASTEVENTCOMMENT, CREATETIME, CREATEUSER, RECEIVEREQUESTNAME, RECEIVEREQUESTDETAILNAME,
            MAKEDATE, RECEIVEACTNO, STOCKINTYPE, RECEIVETIME, RECEIVEUSER, SHIPREQUESTNAME, SHIPREQUESTDETAILNAME, SHIPTIME,
            SHIPUSER, REASONCODETYPE, OQARESULTSTATE, OQARESULT, ERPLOCATION, ERPFACTORY, REASONCODE, CHARGE, SUPPLIERNAME,ETCSTATE,ERPEVENTTIME,NPFLAG
        FROM MATERIALPACKING
        WHERE unDoID = #{unDoID}
    </insert>

</mapper>