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

    <resultMap id="BaseResultMap" type="com.rongwei.bsentity.domain.ApsProductDetailDo">
            <id property="id" column="ID" jdbcType="VARCHAR"/>
            <result property="materialname" column="MATERIALNAME" jdbcType="VARCHAR"/>
            <result property="materialcode" column="MATERIALCODE" 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="specialfication" column="SPECIALFICATION" jdbcType="VARCHAR"/>
            <result property="mergeordernumber" column="MERGEORDERNUMBER" jdbcType="VARCHAR"/>
            <result property="totalordercount" column="TOTALORDERCOUNT" jdbcType="DECIMAL"/>
            <result property="planout" column="PLANOUT" jdbcType="DECIMAL"/>
            <result property="realout" column="REALOUT" jdbcType="DECIMAL"/>
            <result property="singlerollweightstart" column="SINGLEROLLWEIGHTSTART" jdbcType="DECIMAL"/>
            <result property="singlerollweightend" column="SINGLEROLLWEIGHTEND" jdbcType="DECIMAL"/>
            <result property="numberunit" column="NUMBERUNIT" jdbcType="VARCHAR"/>
            <result property="rollnum" column="ROLLNUM" jdbcType="INTEGER"/>
            <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="mainid" column="MAINID" jdbcType="VARCHAR"/>
            <result property="producttypeid" column="PRODUCTTYPEID" jdbcType="VARCHAR"/>
            <result property="inputmaterialdescription" column="INPUTMATERIALDESCRIPTION" jdbcType="VARCHAR"/>
            <result property="materialid" column="MATERIALID" jdbcType="VARCHAR"/>
            <result property="customorderdetailid" column="CUSTOMORDERDETAILID" jdbcType="VARCHAR"/>
    </resultMap>

    <sql id="Base_Column_List">
        ID,MATERIALNAME,MATERIALCODE,
        PRODUCTTYPE,ALLOY,ALLOYSTATUS,
        THICKNESS,PROWIDTH,PROLENGTH,
        SPECIALFICATION,MERGEORDERNUMBER,TOTALORDERCOUNT,
        PLANOUT,REALOUT,SINGLEROLLWEIGHTSTART,
        SINGLEROLLWEIGHTEND,NUMBERUNIT,ROLLNUM,
        PRODUCTSTATUS,TENANTID,ROPTION,
        DELETED,REMARK,CREATEDATE,
        CREATEUSERID,MODIFYDATE,MODIFYUSERID,
        CREATEUSERNAME,MODIFYUSERNAME,MAINID,
        PRODUCTTYPEID,INPUTMATERIALDESCRIPTION,MATERIALID,
        CUSTOMORDERDETAILID
    </sql>
    <update id="publish">
        <if test="productionOrderIds != null and productionOrderIds.size() >0">
            <foreach collection="productionOrderIds"  item="id" separator=";">
                update aps_product_detail b set PRODUCTSTATUS=
                (
                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.PRODUCTSTATUS
                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}
                )
                WHERE MAINID=#{id}
            </foreach>
        </if>
    </update>
    <update id="updateProductstatus">
        <if test="productionOrderIds != null and productionOrderIds.size() >0">
            update aps_product_detail set PRODUCTSTATUS ='10'
            <where>
                DELETED='0'
                AND MAINID IN
                <foreach collection="productionOrderIds" open="(" close=")" item="id" separator=",">
                    #{id}
                </foreach>
            </where>

        </if>
    </update>
    <update id="updateProductStatusByBlankId">
        update aps_product_detail set PRODUCTSTATUS =#{productStatus} where ID IN(
        SELECT distinct PRODUCTID from aps_process_output_product where DELETED='0' AND
        BLANKID IN
        <foreach collection="blankIds" open="(" close=")" item="id" separator=",">
            #{id}
        </foreach>
        )
    </update>
    <select id="publishCancel">
        <if test="productionOrderIds != null and productionOrderIds.size() >0">
            <foreach collection="productionOrderIds" item="orderId" separator=";">
                update aps_product_detail b
                <set>
                    PRODUCTSTATUS=(
                    SELECT
                    CASE
                    WHEN GROUP_CONCAT(DISTINCT abo.PRODUCTSTATUS) = '10' THEN '10'
                    WHEN GROUP_CONCAT(DISTINCT abo.PRODUCTSTATUS) = '20' THEN '20'
                    WHEN GROUP_CONCAT(DISTINCT abo.PRODUCTSTATUS) = '30' THEN '30'
                    WHEN GROUP_CONCAT(DISTINCT abo.PRODUCTSTATUS) = '40' THEN '40'
                    WHEN GROUP_CONCAT(DISTINCT abo.PRODUCTSTATUS) = '50' THEN '50'
                    WHEN FIND_IN_SET('50',GROUP_CONCAT(DISTINCT abo.PRODUCTSTATUS)) THEN '40'
                    WHEN FIND_IN_SET('40',GROUP_CONCAT(DISTINCT abo.PRODUCTSTATUS)) THEN '40'
                    WHEN FIND_IN_SET('30',GROUP_CONCAT(DISTINCT abo.PRODUCTSTATUS)) THEN '30'
                    WHEN FIND_IN_SET('20',GROUP_CONCAT(DISTINCT abo.PRODUCTSTATUS)) THEN '20'
                    ELSE b.PRODUCTSTATUS
                    END
                    FROM  aps_blank_order abo
                    where abo.DELETED='0'
                    and abo.PRODUCTIONORDERID =#{orderId}
                    GROUP BY abo.PRODUCTIONORDERID
                    )
                </set>
                <where>
                    MAINID = #{orderId} and DELETED='0'
                </where>
            </foreach>
        </if>
    </select>
</mapper>
