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

    <resultMap id="BaseResultMap" type="com.rongwei.bsentity.domain.ApsProductionOrderDo">
        <id property="id" column="ID" jdbcType="VARCHAR"/>
        <result property="orderno" column="ORDERNO" jdbcType="VARCHAR"/>
        <result property="customname" column="CUSTOMNAME" jdbcType="VARCHAR"/>
        <result property="orderdate" column="ORDERDATE" jdbcType="TIMESTAMP"/>
        <result property="deliverydate" column="DELIVERYDATE" jdbcType="TIMESTAMP"/>
        <result property="allowday" column="ALLOWDAY" jdbcType="INTEGER"/>
        <result property="orderstatus" column="ORDERSTATUS" jdbcType="VARCHAR"/>
        <result property="auditstatus" column="AUDITSTATUS" jdbcType="VARCHAR"/>
        <result property="auditor" column="AUDITOR" jdbcType="VARCHAR"/>
        <result property="auditorid" column="AUDITORID" jdbcType="VARCHAR"/>
        <result property="auditdate" column="AUDITDATE" jdbcType="TIMESTAMP"/>
        <result property="productionstatus" column="PRODUCTIONSTATUS" jdbcType="VARCHAR"/>
        <result property="promisedeliverydate" column="PROMISEDELIVERYDATE" jdbcType="TIMESTAMP"/>
        <result property="customwarehouse" column="CUSTOMWAREHOUSE" jdbcType="VARCHAR"/>
        <result property="deliverytime" column="DELIVERYTIME" jdbcType="INTEGER"/>
        <result property="belongfactory" column="BELONGFACTORY" jdbcType="VARCHAR"/>
        <result property="ordertype" column="ORDERTYPE" jdbcType="VARCHAR"/>
        <result property="salesman" column="SALESMAN" jdbcType="VARCHAR"/>
        <result property="businessunit" column="BUSINESSUNIT" jdbcType="VARCHAR"/>
        <result property="customorderno" column="CUSTOMORDERNO" jdbcType="VARCHAR"/>
        <result property="isfromcustom" column="ISFROMCUSTOM" jdbcType="VARCHAR"/>
        <result property="customordertype" column="CUSTOMORDERTYPE" jdbcType="VARCHAR"/>
        <result property="customorderstatus" column="CUSTOMORDERSTATUS" 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="scheduledeliverydate" column="SCHEDULEDELIVERYDATE" jdbcType="TIMESTAMP"/>
        <result property="actualdeliverydate" column="ACTUALDELIVERYDATE" jdbcType="TIMESTAMP"/>
        <result property="customid" column="CUSTOMID" jdbcType="VARCHAR"/>
        <result property="status" column="STATUS" jdbcType="VARCHAR"/>
        <result property="processinstid" column="PROCESSINSTID" jdbcType="VARCHAR"/>
        <result property="customwarehouseid" column="CUSTOMWAREHOUSEID" jdbcType="VARCHAR"/>
        <result property="blanktype" column="BLANKTYPE" jdbcType="VARCHAR"/>
        <result property="compositionchange" column="COMPOSITIONCHANGE" jdbcType="VARCHAR"/>
        <result property="changeremark" column="CHANGEREMARK" jdbcType="VARCHAR"/>
        <result property="blamkthickness" column="BLAMKTHICKNESS" jdbcType="DECIMAL"/>
        <result property="blankhomogenization" column="BLANKHOMOGENIZATION" jdbcType="VARCHAR"/>
        <result property="finishtemperature" column="FINISHTEMPERATURE" jdbcType="VARCHAR"/>
        <result property="thicknesstolerance" column="THICKNESSTOLERANCE" jdbcType="VARCHAR"/>
        <result property="widthtolerance" column="WIDTHTOLERANCE" jdbcType="VARCHAR"/>
        <result property="lengthtolerance" column="LENGTHTOLERANCE" jdbcType="VARCHAR"/>
        <result property="bendrequirement" column="BENDREQUIREMENT" jdbcType="VARCHAR"/>
        <result property="joint" column="JOINT" jdbcType="VARCHAR"/>
        <result property="swabbing" column="SWABBING" jdbcType="VARCHAR"/>
        <result property="tensilestrength" column="TENSILESTRENGTH" jdbcType="VARCHAR"/>
        <result property="yieldstrength" column="YIELDSTRENGTH" jdbcType="VARCHAR"/>
        <result property="elongationrate" column="ELONGATIONRATE" jdbcType="VARCHAR"/>
        <result property="pinhole" column="PINHOLE" jdbcType="VARCHAR"/>
        <result property="cupping" column="CUPPING" jdbcType="VARCHAR"/>
        <result property="earing" column="EARING" jdbcType="VARCHAR"/>
        <result property="innercorediameter" column="INNERCOREDIAMETER" jdbcType="DECIMAL"/>
        <result property="packagingmethod" column="PACKAGINGMETHOD" jdbcType="VARCHAR"/>
        <result property="tag" column="TAG" jdbcType="VARCHAR"/>
        <result property="surfacerequirement" column="SURFACEREQUIREMENT" jdbcType="VARCHAR"/>
        <result property="istensionlevelling" column="ISTENSIONLEVELLING" jdbcType="VARCHAR"/>
        <result property="packagingrequirement" column="PACKAGINGREQUIREMENT" jdbcType="VARCHAR"/>
        <result property="surfacehardness" column="SURFACEHARDNESS" jdbcType="VARCHAR"/>
        <result property="nomod" column="NOMOD" jdbcType="VARCHAR"/>
        <result property="rollweightrequirement" column="ROLLWEIGHTREQUIREMENT" jdbcType="VARCHAR"/>
        <result property="otherorderstandard" column="OTHERORDERSTANDARD" jdbcType="VARCHAR"/>
        <result property="technicalreviewerid" column="TECHNICALREVIEWERID" jdbcType="VARCHAR"/>
        <result property="technicalreviewername" column="TECHNICALREVIEWERNAME" jdbcType="VARCHAR"/>
        <result property="technicalreviewdate" column="TECHNICALREVIEWDATE" jdbcType="TIMESTAMP"/>
        <result property="qualityreviewerid" column="QUALITYREVIEWERID" jdbcType="VARCHAR"/>
        <result property="qualityreviewername" column="QUALITYREVIEWERNAME" jdbcType="VARCHAR"/>
        <result property="qualityreviewdate" column="QUALITYREVIEWDATE" jdbcType="TIMESTAMP"/>
        <result property="reviewerid" column="REVIEWERID" jdbcType="VARCHAR"/>
        <result property="reviewername" column="REVIEWERNAME" jdbcType="VARCHAR"/>
        <result property="reviewdate" column="REVIEWDATE" jdbcType="TIMESTAMP"/>
        <result property="idmatureprocess" column="IDMATUREPROCESS" jdbcType="VARCHAR"/>
        <result property="otherprecaution" column="OTHERPRECAUTION" jdbcType="VARCHAR"/>
        <result property="enddate" column="ENDDATE" jdbcType="TIMESTAMP"/>
        <result property="plandate" column="PLANDATE" jdbcType="TIMESTAMP"/>
        <result property="reviewtype" column="REVIEWTYPE" jdbcType="VARCHAR"/>
<!--        <result property="schedulstatus" column="SCHEDULSTATUS" jdbcType="VARCHAR"/>-->
    </resultMap>

    <sql id="Base_Column_List">
        ID,ORDERNO,CUSTOMNAME,
        ORDERDATE,DELIVERYDATE,ALLOWDAY,
        ORDERSTATUS,AUDITSTATUS,AUDITOR,
        AUDITORID,AUDITDATE,PRODUCTIONSTATUS,
        PROMISEDELIVERYDATE,CUSTOMWAREHOUSE,DELIVERYTIME,
        BELONGFACTORY,ORDERTYPE,SALESMAN,
        BUSINESSUNIT,CUSTOMORDERNO,ISFROMCUSTOM,
        CUSTOMORDERTYPE,CUSTOMORDERSTATUS,
        TENANTID,ROPTION,DELETED,
        REMARK,CREATEDATE,CREATEUSERID,
        MODIFYDATE,MODIFYUSERID,CREATEUSERNAME,
        MODIFYUSERNAME,SCHEDULEDELIVERYDATE,ACTUALDELIVERYDATE,
        CUSTOMID,STATUS,PROCESSINSTID,
        CUSTOMWAREHOUSEID,BLANKTYPE,COMPOSITIONCHANGE,
        CHANGEREMARK,BLAMKTHICKNESS,BLANKHOMOGENIZATION,
        FINISHTEMPERATURE,THICKNESSTOLERANCE,WIDTHTOLERANCE,
        LENGTHTOLERANCE,BENDREQUIREMENT,JOINT,
        SWABBING,TENSILESTRENGTH,YIELDSTRENGTH,
        ELONGATIONRATE,PINHOLE,CUPPING,
        EARING,INNERCOREDIAMETER,PACKAGINGMETHOD,
        TAG,SURFACEREQUIREMENT,ISTENSIONLEVELLING,
        PACKAGINGREQUIREMENT,SURFACEHARDNESS,NOMOD,
        ROLLWEIGHTREQUIREMENT,OTHERORDERSTANDARD,TECHNICALREVIEWERID,
        TECHNICALREVIEWERNAME,TECHNICALREVIEWDATE,QUALITYREVIEWERID,
        QUALITYREVIEWERNAME,QUALITYREVIEWDATE,REVIEWERID,
        REVIEWERNAME,REVIEWDATE,IDMATUREPROCESS,
        OTHERPRECAUTION,ENDDATE,PLANDATE,
        REVIEWTYPE,SCHEDULSTATUS
    </sql>
    <update id="publish">
        <if test="ids != null and ids.size() >0">
            <foreach collection="ids" item="id" separator=";">
                update aps_production_order b
                <set>
                    PRODUCTIONSTATUS=(
                    SELECT
                    CASE
                    WHEN GROUP_CONCAT(DISTINCT apo.PROCESSSTATUS) = '10' THEN '10'
                    WHEN GROUP_CONCAT(DISTINCT apo.PROCESSSTATUS) = '20' THEN '20'
                    WHEN GROUP_CONCAT(DISTINCT apo.PROCESSSTATUS) = '30' THEN '30'
                    WHEN GROUP_CONCAT(DISTINCT apo.PROCESSSTATUS) = '40' THEN '40'
                    WHEN GROUP_CONCAT(DISTINCT apo.PROCESSSTATUS) = '50' THEN '50'
                    WHEN FIND_IN_SET('40',GROUP_CONCAT(DISTINCT apo.PROCESSSTATUS)) THEN '40'
                    WHEN FIND_IN_SET('30',GROUP_CONCAT(DISTINCT apo.PROCESSSTATUS)) THEN '30'
                    WHEN FIND_IN_SET('20',GROUP_CONCAT(DISTINCT apo.PROCESSSTATUS)) THEN '20'
                    ELSE b.PRODUCTIONSTATUS END
                    FROM
                    aps_process_operation apo
                    LEFT JOIN aps_blank_order abo ON apo.BLANKID = abo.ID AND abo.DELETED='0'
                    where apo.DELETED='0'
                    and abo.PRODUCTIONORDERID =#{id}
                    GROUP BY abo.PRODUCTIONORDERID
                    )
                </set>
                <where>
                    ID = #{id} and DELETED='0'
                </where>
            </foreach>
        </if>
    </update>
    <update id="publishCancel">
        <if test="ids != null and ids.size() >0">
            <foreach collection="ids" item="id" separator=";">
                update aps_production_order b
                <set>
                    PRODUCTIONSTATUS=(
                    SELECT
                    CASE
                        WHEN GROUP_CONCAT(DISTINCT apo.PROCESSSTATUS) = '10' THEN '10'
                        WHEN GROUP_CONCAT(DISTINCT apo.PROCESSSTATUS) = '20' THEN '20'
                        WHEN GROUP_CONCAT(DISTINCT apo.PROCESSSTATUS) = '30' THEN '30'
                        WHEN GROUP_CONCAT(DISTINCT apo.PROCESSSTATUS) = '40' THEN '40'
                        WHEN GROUP_CONCAT(DISTINCT apo.PROCESSSTATUS) = '50' THEN '50'
                        WHEN FIND_IN_SET('50',GROUP_CONCAT(DISTINCT apo.PROCESSSTATUS)) THEN '40'
                        WHEN FIND_IN_SET('40',GROUP_CONCAT(DISTINCT apo.PROCESSSTATUS)) THEN '40'
                        WHEN FIND_IN_SET('30',GROUP_CONCAT(DISTINCT apo.PROCESSSTATUS)) THEN '30'
                        WHEN FIND_IN_SET('20',GROUP_CONCAT(DISTINCT apo.PROCESSSTATUS)) THEN '20'
                        ELSE b.PRODUCTIONSTATUS
                    END
                    FROM
                    aps_process_operation apo
                    LEFT JOIN aps_blank_order abo ON apo.BLANKID = abo.ID AND abo.DELETED='0'
                    where apo.DELETED='0'
                    and abo.PRODUCTIONORDERID =#{id}
                    GROUP BY abo.PRODUCTIONORDERID)
                </set>
                <where>
                    ID = #{id} and DELETED='0'
                </where>
            </foreach>
        </if>
    </update>
    <select id="getStartWorkById" resultType="java.lang.String">
        select DISTINCT CONCAT( apo.CUSTOMNAME,'-',GROUP_CONCAT(DISTINCT apo.ORDERNO )  )
        from aps_production_order apo
        left join aps_blank_order abo on apo.ID=abo.PRODUCTIONORDERID AND abo.DELETED='0'
        left join aps_process_operation apo1 on apo1.BLANKID =abo.ID and apo1.DELETED='0'
        <where>
             apo.DELETED='0'
            <if test="ids != null and ids.size() >0">
                and apo.ID in
                <foreach collection="ids" open="(" close=")" separator="," item="id">
                    #{id}
                </foreach>
            </if>
            and apo1.PROCESSSTATUS in ('40','50')
        </where>
        group by apo.CUSTOMNAME
    </select>
    <select id="getStartWorkByBlankId" resultType="java.lang.String">
        select DISTINCT CONCAT( apo.CUSTOMNAME,'-',GROUP_CONCAT(DISTINCT apo.ORDERNO )  )
        from aps_production_order apo
        left join aps_blank_order abo on apo.ID=abo.PRODUCTIONORDERID AND abo.DELETED='0'
        <where>
            apo.DELETED='0'
            <if test="ids != null and ids.size() >0">
                and abo.ID in
                <foreach collection="ids" open="(" close=")" separator="," item="id">
                    #{id}
                </foreach>
            </if>
            and abo.PRODUCTSTATUS in ('40','50')
        </where>
        group by apo.CUSTOMNAME
    </select>
    <select id="getDataByBlankId" resultType="com.rongwei.bsentity.domain.ApsProductionOrderDo">
        select * from aps_production_order where ID in(
        select PRODUCTIONORDERID from aps_blank_order where DELETED ='0' AND ID IN
        <foreach collection="blankIds" open="(" close=")" separator="," item="id">
                                                              #{id}
        </foreach>
        )
        and SCHEDULEDELIVERYDATE>PROMISEDELIVERYDATE
    </select>
</mapper>
