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

    <resultMap id="BaseResultMap" type="com.rongwei.bsentity.domain.ApsBlankOrderDo">
            <id property="id" column="ID" jdbcType="VARCHAR"/>
            <result property="productionorderid" column="PRODUCTIONORDERID" jdbcType="VARCHAR"/>
            <result property="materialname" column="MATERIALNAME" jdbcType="VARCHAR"/>
            <result property="materialcode" column="MATERIALCODE" jdbcType="VARCHAR"/>
            <result property="blanktype" column="BLANKTYPE" jdbcType="VARCHAR"/>
            <result property="producttype" column="PRODUCTTYPE" jdbcType="VARCHAR"/>
            <result property="alloy" column="ALLOY" jdbcType="VARCHAR"/>
            <result property="alloystatus" column="ALLOYSTATUS" jdbcType="VARCHAR"/>
            <result property="thickness" column="THICKNESS" jdbcType="DECIMAL"/>
            <result property="prowidth" column="PROWIDTH" jdbcType="DECIMAL"/>
            <result property="prolength" column="PROLENGTH" jdbcType="DECIMAL"/>
            <result property="singlerollweight" column="SINGLEROLLWEIGHT" jdbcType="DECIMAL"/>
            <result property="numberunit" column="NUMBERUNIT" jdbcType="VARCHAR"/>
            <result property="rollnum" column="ROLLNUM" jdbcType="INTEGER"/>
            <result property="totalweight" column="TOTALWEIGHT" jdbcType="DECIMAL"/>
            <result property="preparematerial" column="PREPAREMATERIAL" jdbcType="VARCHAR"/>
            <result property="defectiveproduct" column="DEFECTIVEPRODUCT" jdbcType="VARCHAR"/>
            <result property="preparematerialbatchno" column="PREPAREMATERIALBATCHNO" jdbcType="VARCHAR"/>
            <result property="outputorderproduct" column="OUTPUTORDERPRODUCT" jdbcType="VARCHAR"/>
            <result property="outputfinishproduct" column="OUTPUTFINISHPRODUCT" jdbcType="VARCHAR"/>
            <result property="outputmaterial" column="OUTPUTMATERIAL" jdbcType="VARCHAR"/>
            <result property="productstatus" column="PRODUCTSTATUS" jdbcType="VARCHAR"/>
            <result property="tenantid" column="TENANTID" jdbcType="VARCHAR"/>
            <result property="roption" column="ROPTION" jdbcType="VARCHAR"/>
            <result property="deleted" column="DELETED" jdbcType="CHAR"/>
            <result property="remark" column="REMARK" jdbcType="VARCHAR"/>
            <result property="createdate" column="CREATEDATE" jdbcType="TIMESTAMP"/>
            <result property="createuserid" column="CREATEUSERID" jdbcType="VARCHAR"/>
            <result property="modifydate" column="MODIFYDATE" jdbcType="TIMESTAMP"/>
            <result property="modifyuserid" column="MODIFYUSERID" jdbcType="VARCHAR"/>
            <result property="createusername" column="CREATEUSERNAME" jdbcType="VARCHAR"/>
            <result property="modifyusername" column="MODIFYUSERNAME" jdbcType="VARCHAR"/>
            <result property="producttypeid" column="PRODUCTTYPEID" jdbcType="VARCHAR"/>
            <result property="inputreportdescribe" column="INPUTREPORTDESCRIBE" jdbcType="VARCHAR"/>
            <result property="craftrouteid" column="CRAFTROUTEID" jdbcType="VARCHAR"/>
            <result property="lockmark" column="LOCKMARK" jdbcType="VARCHAR"/>
            <result property="isstag" column="ISSTAG" jdbcType="VARCHAR"/>
            <result property="schedulingstatus" column="SCHEDULINGSTATUS" jdbcType="VARCHAR"/>
    </resultMap>

    <sql id="Base_Column_List">
        ID,PRODUCTIONORDERID,MATERIALNAME,
        MATERIALCODE,BLANKTYPE,PRODUCTTYPE,
        ALLOY,ALLOYSTATUS,THICKNESS,
        PROWIDTH,PROLENGTH,SINGLEROLLWEIGHT,
        NUMBERUNIT,ROLLNUM,TOTALWEIGHT,
        PREPAREMATERIAL,DEFECTIVEPRODUCT,PREPAREMATERIALBATCHNO,
        OUTPUTORDERPRODUCT,OUTPUTFINISHPRODUCT,OUTPUTMATERIAL,
        PRODUCTSTATUS,TENANTID,ROPTION,
        DELETED,REMARK,CREATEDATE,
        CREATEUSERID,MODIFYDATE,MODIFYUSERID,
        CREATEUSERNAME,MODIFYUSERNAME,PRODUCTTYPEID,
        INPUTREPORTDESCRIBE,CRAFTROUTEID,LOCKMARK,
        ISSTAG,SCHEDULINGSTATUS
    </sql>
    <update id="publish">
        <if test="productionOrderIds != null and productionOrderIds.size() >0">
            UPDATE aps_blank_order b
                <set>
                    PRODUCTSTATUS=(select
                    CASE
                    WHEN GROUP_CONCAT(DISTINCT a.PROCESSSTATUS) = '10' THEN '10'
                    WHEN GROUP_CONCAT(DISTINCT a.PROCESSSTATUS) = '20' THEN '20'
                    WHEN GROUP_CONCAT(DISTINCT a.PROCESSSTATUS) = '30' THEN '30'
                    WHEN GROUP_CONCAT(DISTINCT a.PROCESSSTATUS) = '40' THEN '40'
                    WHEN GROUP_CONCAT(DISTINCT a.PROCESSSTATUS) = '50' THEN '50'
                    WHEN FIND_IN_SET('40',GROUP_CONCAT(DISTINCT a.PROCESSSTATUS)) THEN '40'
                    WHEN FIND_IN_SET('30',GROUP_CONCAT(DISTINCT a.PROCESSSTATUS)) THEN '30'
                    WHEN FIND_IN_SET('20',GROUP_CONCAT(DISTINCT a.PROCESSSTATUS)) THEN '20'
                    ELSE b.PRODUCTSTATUS END
                    END
                    from aps_process_operation a where a.BLANKID = b.ID and deleted='0'
                    ),
                    SCHEDULINGSTATUS="30"
                </set>
            <where>
                AND DELETED='0'
                AND PRODUCTIONORDERID in
                <foreach collection="productionOrderIds" item="id" open="(" close=")" separator=",">
                    #{id}
                </foreach>
            </where>
        </if>
    </update>
    <update id="publishCancel">
        <if test="productionOrderIds != null and productionOrderIds.size() >0">
            UPDATE aps_blank_order b
            <set>
                PRODUCTSTATUS= PRODUCTSTATUS=(select
                CASE
                WHEN GROUP_CONCAT(DISTINCT a.PROCESSSTATUS) = '10' THEN '10'
                WHEN GROUP_CONCAT(DISTINCT a.PROCESSSTATUS) = '20' THEN '20'
                WHEN GROUP_CONCAT(DISTINCT a.PROCESSSTATUS) = '30' THEN '30'
                WHEN GROUP_CONCAT(DISTINCT a.PROCESSSTATUS) = '40' THEN '40'
                WHEN GROUP_CONCAT(DISTINCT a.PROCESSSTATUS) = '50' THEN '50'
                WHEN FIND_IN_SET('50',GROUP_CONCAT(DISTINCT a.PROCESSSTATUS)) THEN '40'
                WHEN FIND_IN_SET('40',GROUP_CONCAT(DISTINCT a.PROCESSSTATUS)) THEN '40'
                WHEN FIND_IN_SET('30',GROUP_CONCAT(DISTINCT a.PROCESSSTATUS)) THEN '30'
                WHEN FIND_IN_SET('20',GROUP_CONCAT(DISTINCT a.PROCESSSTATUS)) THEN '20'
                ELSE b.PRODUCTSTATUS END
                from aps_process_operation a where a.BLANKID = b.ID and deleted='0'
                ),),
                SCHEDULINGSTATUS= "20"
            </set>
            <where>
                AND DELETED='0'
                AND PRODUCTIONORDERID in
                <foreach collection="productionOrderIds" item="id" open="(" close=")" separator=",">
                    #{id}
                </foreach>
            </where>
        </if>
    </update>
    <update id="cancelSchedulingUpdate">
        <if test="idList != null and idList.size() >0">
            update aps_blank_order a
            <set>
                LOCKMARK='n',
                PRODUCTSTATUS='10',
                SCHEDULINGSTATUS='10'
            </set>
            <where>
                DELETED='0'
                    AND ID in
                    <foreach collection="idList" item="id" open="(" close=")" separator=",">
                        #{id}
                    </foreach>
                </where>
        </if>
    </update>
    <update id="reportCheckUpdateJobStatus">
        update  aps_blank_order
        <set>
            SCHEDULINGSTATUS = if((select GROUP_CONCAT(DISTINCT PROCESSSTATUS)='50' from aps_process_operation where
            DELETED ='0' and BLANKID=#{id}),
            '50',SCHEDULINGSTATUS)
        </set>
        <where>
            DELETED ='0'
            AND ID=#{id}
        </where>
    </update>
    <select id="getIdByLockTime" resultType="java.lang.String">
        select DISTINCT abo.ID from  aps_blank_order abo
        left join aps_process_operation apo on abo.ID= apo.BLANKID AND APO.DELETED='0'
        left join aps_process_operation_process_equ apope on apope.PROCESSID=apo.ID and apope.DELETED='0'
        <where>
            abo.DELETED ='0'
            <choose>
                <when test='lockMark=="y"'> and apope.PLANSTARTDATE &lt; #{lockTime} </when>
                <otherwise>and apope.PLANSTARTDATE &gt; #{lockTime}</otherwise>
            </choose>
        </where>
    </select>
    <update id="publishUnlock">
        update aps_blank_order
        <set>
            PROCESSSTATUS=if(PROCESSSTATUS='10',,PROCESSSTATUS),
            LOCKMARK='n'
        </set>
        <where>
            DELETED='0'
            AND PRODUCTSTATUS IN ('30','20')
            AND ID in
            <foreach collection="ids" item="id" open="(" close=")" separator=",">
                #{id}
            </foreach>
        </where>
    </update>
    <select id="checkConflictByBlankId" resultType="java.lang.Integer">
        select count(*) from aps_process_operation_process_equ
            <where>
                DELETED='0'
                AND BLANKID in
                <foreach collection="blankIds" item="id" open="(" close=")" separator=",">
                    #{id}
                </foreach>
                AND (CONFLICTDES is not null or  CONFLICTDES !='' )
            </where>
    </select>
</mapper>
