<?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.rongwei.bscommon.sys.dao.ApsProcessOperationDao">
    <update id="updateBachmaterialprocessByIds">

        update aps_process_operation
        <set>
<!--            <if test="batchNum != null and batchNum!=''">-->
<!--                BACHMATERIALPROCESS = CONCAT_WS(',',BACHMATERIALPROCESS,#{batchNum}),-->
<!--            </if>-->
            INPUTUNQUALIFIEDROLL = INPUTUNQUALIFIEDROLL +#{unqualifiedQuantity}
        </set>
        <where>
            ID in
            <foreach collection="ids" separator="," open="(" close=")" item="id">
                #{id}
            </foreach>
        </where>
    </update>
    <update id="updateAboutCheckBatchNum">
        update aps_process_operation
        <set>
            OUTPUTQUALIFIEDNUM = IFNULL(OUTPUTQUALIFIEDNUM,0)+ #{qualifiedQuantity},
            OUTPUTUNQUALIFIEDNUM = IFNULL(OUTPUTUNQUALIFIEDNUM,0) +#{unqualifiedQuantity},
            CHECKOUTROLL = IFNULL(CHECKOUTROLL,0) +#{currentCheckNum},
            UNFINISHROLL = UNFINISHROLL -#{currentCheckNum},
            <!-- 完工状态-->
            CLOSESTATUS = case when UNFINISHROLL -#{currentCheckNum} = SECONDROLL+CANCELROLL then '已完工' else '加工中' end,
<!--            ,-->
<!--            &lt;!&ndash;实际完工时间&ndash;&gt;-->
<!--            ACTUALFINISHDATE = (select IF(COUNT(ID) !=0 ,NULL,NOW()) from aps_process_operation_process_equ where-->
<!--            PROCESSID =#{id} and CLOSESTATUS !='已完工'),-->

<!--            &lt;!&ndash; 作业状态&ndash;&gt;-->
<!--            WORKSTATUS = (select IF(COUNT(ID) !=0 ,'加工中','已完工') from aps_process_operation_process_equ where-->
<!--            PROCESSID =#{id} and CLOSESTATUS !='已完工'),-->
<!--            &lt;!&ndash; 生产状态&ndash;&gt;-->
<!--            PROCESSSTATUS = (select IF(COUNT(ID) !=0 ,'40','50') from aps_process_operation_process_equ where PROCESSID-->
<!--            =#{id} and CLOSESTATUS !='已完工')-->
        </set>
        <where>
            ID =#{id}
        </where>
    </update>
    <update id="updateBatchNumberByProcessList">
        update aps_process_operation_process_equ
        set BACHMATERIALPROCESS = IF(BACHMATERIALPROCESS is null || BACHMATERIALPROCESS = '',#{batchnumber},concat(BACHMATERIALPROCESS,',',#{batchnumber}))
        where DELETED = 0
        AND PROCESSID in
        <foreach collection="processIdList" separator="," open="(" item="item" close=")">
            #{item}
        </foreach>
    </update>
    <resultMap id="ProcessIdRelationMap" type="com.rongwei.bsentity.vo.ProcessIdRelationVo">
        <result property="id" column="ID" jdbcType="VARCHAR"/>
        <result property="nextprocessid" column="NEXTPROCESSID" jdbcType="VARCHAR"/>
        <result property="previousprocessid" column="PREVIOUSPROCESSID" jdbcType="VARCHAR"/>
        <result property="processdetailid" column="PROCESSDETAILID" jdbcType="VARCHAR"/>
    </resultMap>
    <select id="getAllNextProcessOperationById" resultMap="ProcessIdRelationMap">
        WITH RECURSIVE cte AS (
        SELECT
        a.ID,a.NEXTPROCESSID,a.PREVIOUSPROCESSID
        FROM
        aps_process_operation a
        WHERE
        a.DELETED = '0'
        AND a.ID IN
        <foreach collection="ids" open="(" close=")" separator="," item="id">
            #{id}
        </foreach>
        UNION ALL
        SELECT
        k.ID,k.NEXTPROCESSID,k.PREVIOUSPROCESSID
        FROM
        aps_process_operation k
        INNER JOIN cte c ON FIND_IN_SET( k.ID, c.NEXTPROCESSID )
        ) SELECT
        a.* ,apope.ID as PROCESSDETAILID
        FROM
        cte a
        left join (select PROCESSID,GROUP_CONCAT(DISTINCT ID) ID from aps_process_operation_process_equ
        where DELETED='0' group by PROCESSID) apope on a.ID=apope.PROCESSID
    </select>
    <update id="publish">
        <if test="productionOrderIds != null and productionOrderIds.size() >0">
            update aps_process_operation a
            <set>
                PROCESSSTATUS=if(PROCESSSTATUS='20','30',PROCESSSTATUS),
                LOCKMARK='y'
            </set>
            <where>
                DELETED='0' AND BLANKID in (select ID from aps_blank_order WHERE PRODUCTIONORDERID in
                <foreach collection="productionOrderIds" open="(" close=")" item="id" separator=",">
                    #{id}
                </foreach>
               and DELETED='0')
            </where>
        </if>
    </update>
    <update id="publishCancel">
        <if test="productionOrderIds != null and productionOrderIds.size() >0">
            update aps_process_operation a
            <set>
                PROCESSSTATUS='20'
            </set>
            <where>
                DELETED='0' AND BLANKID in (select ID from aps_blank_order WHERE PRODUCTIONORDERID in
                <foreach collection="productionOrderIds" open="(" close=")" item="id" separator=",">
                    #{id}
                </foreach>
                and DELETED='0')
                AND PROCESSSTATUS ='30'
            </where>
        </if>
    </update>
</mapper>
