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

    <resultMap id="BaseResultMap" type="com.rongwei.bsentity.domain.ApsProcessOperationProcessEquDo">
        <id property="id" column="ID" jdbcType="VARCHAR"/>
        <result property="tenantid" column="TENANTID" jdbcType="VARCHAR"/>
        <result property="roption" column="ROPTION" jdbcType="VARCHAR"/>
        <result property="deleted" column="DELETED" jdbcType="VARCHAR"/>
        <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="ownedfactory" column="OWNEDFACTORY" jdbcType="VARCHAR"/>
        <result property="processid" column="PROCESSID" jdbcType="VARCHAR"/>
        <result property="processworkshop" column="PROCESSWORKSHOP" jdbcType="VARCHAR"/>
        <result property="processdeviceid" column="PROCESSDEVICEID" jdbcType="VARCHAR"/>
        <result property="processdevice" column="PROCESSDEVICE" jdbcType="VARCHAR"/>
        <result property="actualstartdate" column="ACTUALSTARTDATE" jdbcType="TIMESTAMP"/>
        <result property="actualfinishdate" column="ACTUALFINISHDATE" jdbcType="TIMESTAMP"/>
        <result property="closestatus" column="CLOSESTATUS" jdbcType="VARCHAR"/>
        <result property="planprocessrall" column="PLANPROCESSRALL" jdbcType="INTEGER"/>
        <result property="startingroll" column="STARTINGROLL" jdbcType="INTEGER"/>
        <result property="reportroll" column="REPORTROLL" jdbcType="INTEGER"/>
        <result property="checkoutroll" column="CHECKOUTROLL" jdbcType="INTEGER"/>
        <result property="unfinishroll" column="UNFINISHROLL" jdbcType="INTEGER"/>
        <result property="workstatus" column="WORKSTATUS" jdbcType="VARCHAR"/>
        <result property="bachmaterialprocess" column="BACHMATERIALPROCESS" jdbcType="VARCHAR"/>
        <result property="waitreportid" column="WAITREPORTID" jdbcType="VARCHAR"/>
    </resultMap>

    <sql id="Base_Column_List">
        ID,TENANTID,ROPTION,
        DELETED,REMARK,CREATEDATE,
        CREATEUSERID,MODIFYDATE,MODIFYUSERID,
        CREATEUSERNAME,MODIFYUSERNAME,OWNEDFACTORY,
        PROCESSID,PROCESSWORKSHOP,PROCESSDEVICEID,
        PROCESSDEVICE,ACTUALSTARTDATE,ACTUALFINISHDATE,
        CLOSESTATUS,PLANPROCESSRALL,STARTINGROLL,
        REPORTROLL,CHECKOUTROLL,UNFINISHROLL,
        WORKSTATUS,BACHMATERIALPROCESS,WAITREPORTID
    </sql>
    <update id="updateBachmaterialprocessByIds">
        update aps_process_operation_process_equ set BACHMATERIALPROCESS =
        CONCAT_WS(',',BACHMATERIALPROCESS,#{batchNum})
        <where>
            PROCESSID in
            <foreach collection="ids" separator="," open="(" close=")" item="id">
                #{id}
            </foreach>
            <if test="nextIds != null and nextIds.size()>0">
               and ID in
                <foreach collection="nextIds" separator="," open="(" close=")" item="id">
                    #{id}
                </foreach>
            </if>
        </where>
    </update>
    <update id="updateAboutCheckBatchNum">
        update aps_process_operation_process_equ apope
        <set>
            <!--  OUTPUTQUALIFIEDNUM = IFNULL(OUTPUTQUALIFIEDNUM,0)+ #{qualifiedQuantity},
             OUTPUTUNQUALIFIEDNUM = IFNULL(OUTPUTUNQUALIFIEDNUM,0) +#{unqualifiedQuantity},-->
            apope.CHECKOUTROLL = IFNULL(CHECKOUTROLL,0) +#{currentCheckNum},
            apope.UNFINISHROLL = PLANPROCESSRALL -#{currentCheckNum}
            <choose>
                <when test="previousprocessid == null and previousprocessid == ''">
                    <!-- 不存在上一个工序 未完工卷数等于0时 更新 为已完工  -->
                    <!-- 完工状态-->
#                     apope.CLOSESTATUS= CASE
#                     WHEN (apope.PLANPROCESSRALL - #{currentCheckNum}) = 0 THEN '已完工'
#                     ELSE '未完工'
#                     END,
                    <!--  作业状态-->
<!--                    apope.WORKSTATUS= CASE-->
<!--                    WHEN (apope.PLANPROCESSRALL -#{currentCheckNum}) = 0 THEN '已完工'-->
<!--                    ELSE apope.WORKSTATUS-->
<!--                    END,-->
<!--                    &lt;!&ndash;实际完工时间&ndash;&gt;-->
<!--                    apope.ACTUALFINISHDATE = CASE-->
<!--                    WHEN (apope.PLANPROCESSRALL - #{currentCheckNum}) = 0 THEN NOW()-->
<!--                    ELSE apope.ACTUALFINISHDATE-->
<!--                    END-->
                </when>
                <otherwise>
                    <!-- 存在上一个工序 上道工序作业已完工 当前工序作业明细的已开工卷数=已检验卷数-->
<!--                    apope.CLOSESTATUS =-->
<!--                    CASE-->
<!--                    &lt;!&ndash;并且当前工序明细的已开工卷数=已检验卷数&ndash;&gt;-->
<!--                        WHEN-->
<!--                        (-->
<!--                        &lt;!&ndash;并且上道工序作业已完工状态=已完工&ndash;&gt;-->
<!--                         select (select apo.CLOSESTATUS='已完工' from aps_process_operation apo where apo.ID =#{previousprocessid} )-->
<!--                        &lt;!&ndash;并且作业明细待加工料卷批次号数量-工序作业已取消卷数<=0&ndash;&gt;-->
<!--                        and (select IFNULL(LENGTH(apope.BACHMATERIALPROCESS) - LENGTH(REPLACE(apope.BACHMATERIALPROCESS, ',',-->
<!--                        '')) + 1 ,0)-IFNULL(apo.CANCELROLL,0)&lt;=0 from aps_process_operation apo where apo.ID =#{processOperationId}-->
<!--                        ) and apope.STARTINGROLL = apope.CHECKOUTROLL+#{currentCheckNum}  from dual )THEN '已完工'-->
<!--                        ELSE '未完工'-->
<!--                    END,-->
<!--                    apope.WORKSTATUS =-->
<!--                    CASE-->
<!--                        WHEN  (select (select apo.CLOSESTATUS='已完工' from aps_process_operation apo where apo.ID =#{previousprocessid} )-->
<!--                    &lt;!&ndash;并且作业明细待加工料卷批次号数量-工序作业已取消卷数<=0&ndash;&gt;-->
<!--                    and (select IFNULL(LENGTH(apope.BACHMATERIALPROCESS) - LENGTH(REPLACE(apope.BACHMATERIALPROCESS, ',',-->
<!--                    '')) + 1 ,0)-IFNULL(apo.CANCELROLL,0)&lt;=0 from aps_process_operation apo where apo.ID =#{processOperationId}-->
<!--                    ) and apope.STARTINGROLL = apope.CHECKOUTROLL+#{currentCheckNum}  from dual) THEN '已完工'-->
<!--                        ELSE apope.WORKSTATUS-->
<!--                    END,-->
<!--                    apope.ACTUALFINISHDATE =  CASE-->
<!--                    WHEN  (select (select apo.CLOSESTATUS='已完工' from aps_process_operation apo where apo.ID =#{previousprocessid} )-->
<!--                    &lt;!&ndash;并且作业明细待加工料卷批次号数量-工序作业已取消卷数<=0&ndash;&gt;-->
<!--                    and (select IFNULL(LENGTH(apope.BACHMATERIALPROCESS) - LENGTH(REPLACE(apope.BACHMATERIALPROCESS, ',',-->
<!--                    '')) + 1 ,0)-IFNULL(apo.CANCELROLL,0)&lt;=0 from aps_process_operation apo where apo.ID =#{processOperationId}-->
<!--                    ) and apope.STARTINGROLL = apope.CHECKOUTROLL+#{currentCheckNum}  from dual) THEN NOW()-->
<!--                    ELSE apope.ACTUALFINISHDATE-->
<!--                    END-->
                </otherwise>
            </choose>

        </set>
        <where>
            ID =#{id}
        </where>
    </update>

    <!--    <resultMap id="userOrdersResultMap" type="com.rongwei.bsentity.vo.ScheduleGanttVo">-->
    <!--        <id property="processworkshop" column="PROCESSWORKSHOP"/>-->
    <!--        <collection property="processList" ofType="com.rongwei.bsentity.vo.ScheduleGanttVo.ProcessVo" javaType="java.util.List">-->
    <!--            <result property="processdevice" column="PROCESSDEVICE"/>-->
    <!--            <result property="processdeviceid" column="PROCESSDEVICEID"/>-->
    <!--            <result property="planstartdate" column="PLANSTARTDATE"/>-->
    <!--            <result property="planenddate" column="PLANENDDATE"/>-->
    <!--            <result property="processids" column="PROCESSIDS"/>-->
    <!--            <result property="processdetailids" column="PROCESSDETAILIDS"/>-->
    <!--            <result property="itemname" column="ITEMNAME"/>-->
    <!--            <result property="itemdesc" column="ITEMDESC"/>-->
    <!--        </collection>-->
    <!--    </resultMap>-->


    <select id="getGanttDataList" resultType="com.rongwei.bsentity.vo.GanttVos">
        SELECT
        UUID_SHORT() as uuid,
        apope.ID,
        apope.PLANPROCESSRALL,
        apope.PLANSTARTDATE,
        apope.PLANENDDATE,
        apope.PROCESSWORKSHOPID,
        apope.PROCESSWORKSHOP,
        apope.PROCESSDEVICEID,
        apope.PROCESSDEVICE,
        apope.STARTINGROLL,
        apope.REPORTROLL,
        apope.CHECKOUTROLL,
        apope.UNFINISHROLL,
        apope.WORKSTATUS,
        apope.CONFLICTDES,
        apope.HASCONFLICT,
        apope.SOFTCONFLICTDES,
        apope.nextprocessesids,
        apope.previousprocessesids,
        apope.CANCELROLL,
        apo.ID as processid,
        apo.PLANINPUT AS INMATERCONDITION,
        apo.PROCESSSTATUS,
        apope.ACTUALSTARTDATE,
        apope.ACTUALFINISHDATE,
        apo.PROCESSNAME,
        apo.LOCKMARK as ProcessLock, -- 原工序作业的是否锁定改名
        apope.LOCKMARKDETAIL as LOCKMARK, -- 增加作业明细的是否锁定字段
        apo.PROCESSWAY,
        abo.ID AS blankOrderId,
        abo.OUTPUTORDERPRODUCT,
        DATE(abo.PROMISEDATESTART) AS PROMISEDATESTART,
        DATE(abo.PROMISEDATEEND) AS PROMISEDATEEND,
        DATE(abo.DELIVERYDATE) AS DELIVERYDATE,
        apop.PRODUCTNAME ,
        apro.ID AS productionorderid,
        apro.ORDERNO,
        apro.CUSTOMNAME,
        apro.ORDERDATE,
        art.ROLLERTYPE,
        ifnull(apop.CUSTOMORDERNO,apro.CUSTOMORDERNO) CUSTOMORDERNO,
        Date(apro.PROMISEDELIVERYDATE) as PROMISEDELIVERYDATE,
        Date(apro.SCHEDULEDELIVERYDATE) as SCHEDULEDELIVERYDATE,
        acm.CUSTOMERABBREVIATION as CUSTOMERABBREVIATION,
        acm.CUSTOMERUNIT,
        abo.BLANKNUMBER as blankNumber,
        abo.PLANHAVEMATERIALDATE as planhavematerialdate,
        apo.SINGLEROLLWEIGHT,
        apo.SINGLEROLLWEIGHT * apope.PLANPROCESSRALL as totalRollWeight,
        IF(IFNULL(apope.BACHMATERIALPROCESS,'')=''and IFNULL(apope.DOINGBATCHNUMBER,'')=''and IFNULL(apope.REPORTBATCHNUMBER,'')='',
        IF(IFNULL(apope.BLANKBATCHNUMBER,'')='' and abo.PREPAREMATERIAL='是',SUBSTRING_INDEX(abo.PREPAREMATERIALBATCHNO, ',', 1),apope.BLANKBATCHNUMBER),
        IF(apo.PROCESS = "小卷成退"OR apo.PROCESS = "小卷包装",
        CONCAT_WS(',',
        SUBSTRING_INDEX( if(apope.BACHMATERIALPROCESS='',null,trim( BOTH ',' FROM apope.BACHMATERIALPROCESS)), ',', 1 ),
        SUBSTRING_INDEX( if(apope.DOINGBATCHNUMBER='',null,trim( BOTH ',' FROM apope.DOINGBATCHNUMBER)), ',', 1 ),
        SUBSTRING_INDEX( if(apope.REPORTBATCHNUMBER='',null,trim( BOTH ',' FROM apope.REPORTBATCHNUMBER)), ',', 1 )
        ),
        CONCAT_WS(',',
        if(apope.BACHMATERIALPROCESS='',null,trim( BOTH ',' FROM apope.BACHMATERIALPROCESS)),
        if(apope.DOINGBATCHNUMBER='',null,trim( BOTH ',' FROM apope.DOINGBATCHNUMBER)),
        if(apope.REPORTBATCHNUMBER='',null,trim( BOTH ',' FROM apope.REPORTBATCHNUMBER))
        )
        )
        ) AS SHOWBATCHNUMBER
        FROM
        aps_process_operation_process_equ apope
        JOIN aps_process_operation apo ON apope.PROCESSID = apo.ID
        join aps_blank_order abo ON apo.BLANKID = abo.ID
        JOIN aps_production_order apro ON abo.PRODUCTIONORDERID = apro.ID
        LEFT JOIN aps_roller_type art on apope.ROLLERID = art.ID
        LEFT JOIN aps_customer_management acm ON apro.CUSTOMID = acm.ID
        LEFT JOIN (
        SELECT
        apop.BLANKID, GROUP_CONCAT( apop.PRODUCTNAME ) as PRODUCTNAME,
        GROUP_CONCAT( distinct apd.CUSTOMORDERNO) as CUSTOMORDERNO
        FROM
        aps_process_output_product apop
        left join aps_product_detail apd on apop.PRODUCTID = apd.id
        WHERE
        apop.DELETED = '0'
        GROUP BY
        apop.BLANKID
        ) apop on abo.id=apop.BLANKID
        <where>
            <if test="factoryId != null and factoryId != '' and factoryId != 'ALL'">
                apope.TENANTID = #{factoryId}
                and abo.TENANTID= #{factoryId}
            </if>
            and abo.SCHEDULINGSTATUS != '10'
            and apope.DELETED = '0'
            and apo.DELETED = '0'
            and abo.DELETED = '0'
            and apro.DELETED = '0'
            and (apope.PROCESSDEVICE is not null and apope.PROCESSDEVICE !='' )
            and (apope.PROCESSWORKSHOP is not null and apope.PROCESSWORKSHOP !='' )
            <if test="ids != null and ids.size()>0">
                and apope.ID in
                <foreach collection="ids" item="id" open="(" close=")" separator=",">
                    #{id}
                </foreach>
            </if>
            <if test="workshopIds != null and workshopIds.size()>0">
                and apope.PROCESSWORKSHOPID in
                <foreach collection="workshopIds" item="workshopId" open="(" close=")" separator=",">
                    #{workshopId}
                </foreach>
            </if>
            <if test="orderNos !=null and orderNos.size()>0">
                and apro.ORDERNO in
                <foreach collection="orderNos" item="orderNo" open="(" close=")" separator=",">
                    #{orderNo}
                </foreach>
            </if>
            <if test="equIds != null and equIds.size()>0">
                and apope.PROCESSDEVICEID in
                <foreach collection="equIds" item="equId" open="(" close=")" separator=",">
                    #{equId}
                </foreach>
            </if>
            <if test="equName !=null and equName!=''">
                <bind name="pattern" value="'%' + equName + '%'"/>
                and apope.PROCESSDEVICE like #{pattern}
            </if>

            <if test="startTime != null and endTime != null ">
                and apope.PLANSTARTDATE &lt;= #{endTime} and apope.PLANENDDATE >= #{startTime}
            </if>
            <if test="processids != null ">and apope.PROCESSID in (${processids})</if>
            <if test="blankNumber != null and blankNumber != ''">
                <bind name="blankNumberPattern" value="'%' + blankNumber + '%'"/>
                and abo.BLANKNUMBER like #{blankNumberPattern}
            </if>
        </where>
        # order by apope.PROCESSWORKSHOP,apope.PROCESSDEVICE,apope.PLANSTARTDATE asc,apope.PLANENDDATE desc
    </select>
    <select id="selectNeedUpdate" resultType="com.rongwei.bsentity.vo.CommonUpdateProductionStatusReq">
        SELECT
            apd.ID AS 'productDetailId',
            MIN( abo.PRODUCTSTATUS ) AS 'minProductionStatus',
            MAX( abo.PRODUCTSTATUS ) AS 'maxProductionStatus'
        FROM
            aps_product_detail apd
            LEFT JOIN aps_process_output_product apop ON apd.ID = apop.PRODUCTID
            AND apop.DELETED = 0
            LEFT JOIN aps_blank_order abo ON apop.BLANKID = abo.ID
            AND abo.DELETED = 0
        WHERE
            apd.DELETED = 0
            AND apd.ID IN
            <foreach collection="productIdList" open="(" close=")" separator="," item="item">
                #{item}
            </foreach>
        GROUP BY
            apd.ID,
            abo.ID
    </select>


    <sql id="appendSql">
        (SELECT apo.CLOSESTATUS = '已完工'
                   AND  IFNULL(LENGTH(apope.BACHMATERIALPROCESS) - LENGTH(REPLACE(apope.BACHMATERIALPROCESS, ',', '')) + 1 ,0)
                            -IFNULL(apo1.CANCELROLL,0)&lt;=0
                   AND apope.STARTINGROLL = apope.CHECKOUTROLL
        FROM
            aps_process_operation apo INNER JOIN
            aps_process_operation apo1
        WHERE
            apo.ID = #{data.previousprocessid}
          and apo1.ID=#{data.id})
    </sql>
    <update id="updateAboutStateByProcessId">
        <foreach collection="updateIds" item="data" separator=";">
            <if  test="data.processdetailids != null and data.processdetailids.size()>0">
                <foreach collection="data.processdetailids" item="id" >
                    update aps_process_operation_process_equ apope
                    <set>
                        <!--并且当前工序明细的已开工卷数=已检验卷数-->
                        apope.CLOSESTATUS = CASE  WHEN <include refid="appendSql"/> THEN '已完工' ELSE '未完工'  END,
                        apope.WORKSTATUS = CASE WHEN <include refid="appendSql"/> THEN '已完工' ELSE apope.WORKSTATUS  END,
                        apope.ACTUALFINISHDATE=  CASE WHEN <include refid="appendSql"/> THEN NOW() ELSE apope.ACTUALFINISHDATE END
                    </set>
                    <where>
                        apope.ID=#{id}
                        and apope.CLOSESTATUS='未完工';
                    </where>
                </foreach>
            </if>

            update aps_process_operation
            <set>
                <!--实际完工时间-->
                ACTUALFINISHDATE = (select IF(COUNT(ID) !=0 ,NULL,NOW()) from aps_process_operation_process_equ where
                PROCESSID =#{data.id} and CLOSESTATUS !='已完工'),
                <!-- 完工状态-->
                CLOSESTATUS = (select IF(COUNT(ID) !=0 ,'未完工','已完工') from aps_process_operation_process_equ where
                PROCESSID =#{data.id} and CLOSESTATUS !='已完工'),
                <!-- 作业状态-->
                WORKSTATUS = (select IF(COUNT(ID) !=0 ,'加工中','已完工') from aps_process_operation_process_equ where
                PROCESSID =#{data.id} and CLOSESTATUS !='已完工'),
                <!-- 生产状态-->
                PROCESSSTATUS = (select IF(COUNT(ID) !=0 ,'40','50') from aps_process_operation_process_equ where PROCESSID
                =#{data.id} and CLOSESTATUS !='已完工')
            </set>
            <where>
                ID =#{data.id}
                AND CLOSESTATUS !='已完工'
            </where>
        </foreach>

    </update>
    <update id="publish">
        UPDATE aps_process_operation_process_equ
        <set>
            LOCKMARK ='y',
            WORKSTATUS='待开工'
        </set>
            <where>
                BLANKID in (
                select ID from aps_blank_order where PRODUCTIONORDERID IN
                <foreach collection="productIdList" item="id" open="(" close=")" separator=",">
                    #{id}
                </foreach>
                AND DELETED='0'
                )
            </where>
    </update>
    <update id="publishCancel">
        UPDATE aps_process_operation_process_equ
        <set>
            LOCKMARK ='n',
            WORKSTATUS='待开工'
        </set>
        <where>
            BLANKID in (
            select ID from aps_blank_order where PRODUCTIONORDERID IN
            <foreach collection="productIdList" item="id" open="(" close=")" separator=",">
                #{id}
            </foreach>
            AND DELETED='0'
            )
        </where>
    </update>
    <select id="getOptionalEquipmentById" resultType="com.rongwei.bsentity.vo.OptionalEquipmentVo">
        SELECT
        CONCAT(aci.USEDEPTID, '/', aci.ID ) AS value ,
        CONCAT(aci.USEDEPTNAME, '/', aci.CHECKITEMNAME) AS label
        FROM asp_check_items aci
        where
        aci.DELETED='0'
        and aci.TENANTID=#{factoryId}
        and IFNULL(DEVICESTATUS,'正常使用')!='停用' and IFNULL(DEVICESTATUS,'正常使用')!='报废'  and  PRODUCETYPE='1'
        and aci.USEDEPTID != '' and aci.USEDEPTID is not null
        <if test="typeList != null and typeList.size > 0">
            and aci.CHECKITEMTYPE IN
            <foreach collection="typeList" separator="," item="item" open="(" close=")">
                #{item}
            </foreach>
        </if>

        GROUP BY aci.ID
        ORDER BY aci.USEDEPTNAME,aci.CHECKITEMNAME,aci.CHECKITEMNAME ASC
        <!--        SELECT-->
        <!--        CONCAT(aci.USEDEPTID, '/', aci.ID ) AS value ,-->
        <!--        CONCAT(aci.USEDEPTNAME, '/', aci.CHECKITEMNAME) AS label-->
        <!--        FROM-->
        <!--        aps_process_operation_process_equ apope-->
        <!--        LEFT JOIN aps_process_operation apo ON apope.PROCESSID = apo.ID-->
        <!--        LEFT JOIN asp_check_items aci ON FIND_IN_SET(aci.ID,apo.CANCHOOSEDEVICEID)-->
        <!--        left join sys_dict c on c.DICTTYPE='asp_check_item_type' AND aci.CHECKITEMTYPE =c.VALUE-->
        <!--        AND aci.DELETED = '0'-->
        <!--        <where>-->
        <!--            apope.DELETED = '0'-->
        <!--            <if test="idList !=null and idList.size() >0">-->
        <!--                AND apope.ID IN-->
        <!--                <foreach collection="idList" item="id" open="(" close=")" separator=",">-->
        <!--                    #{id}-->
        <!--                </foreach>-->
        <!--            </if>-->
        <!--            and apo.CANCHOOSEDEVICEID is not null-->
        <!--            GROUP BY  aci.ID-->
        <!--            <if test="idList !=null and idList.size() >1">-->
        <!--                &lt;!&ndash; 存在多选工序作业时 筛选共有的设备 &ndash;&gt;-->
        <!--                <bind name="idLength" value="idList.size()"/>-->
        <!--                HAVING count(*)>=#{idLength}-->
        <!--            </if>-->
        <!--            ORDER BY aci.USEDEPTNAME,aci.CHECKITEMNAME,aci.CHECKITEMNAME ASC-->
        <!--        </where>-->
    </select>
    <select id="checkConflict" resultType="java.lang.String">
        select CONCAT("订单",apo1.ORDERNO,"-",
            acm.CUSTOMERABBREVIATION,"的作业-",a.PROCESSNAME,"已发布，换线后存在冲突，不能换线") from
        <foreach collection="checkConflictVos" separator="union all" item="data" open="(" close=")">(
            select
            apope. ID,apo.PROCESSNAME,
            apo.BLANKID from aps_process_operation_process_equ apope
            left join aps_process_operation apo on apope.PROCESSID =apo.ID and apo.DELETED='0'
            <where>
                apope.DELETED='0'
                AND apope.PROCESSDEVICEID =#{data.equId}
                AND (
                (#{data.planStartDate}
                &lt; apope.PLANENDDATE and #{data.planEndDate} > apope.PLANSTARTDATE) or
                (
                apope.PLANSTARTDATE &lt; #{data.planEndDate} and apope.PLANENDDATE > #{data.planStartDate})
                )
                AND apope.ID != #{data.id}
                AND apope.
                WORKSTATUS = '待开工'
                AND apo.PROCESSSTATUS IN ('30','40')
                limit 0,1
            </where>
                    )
        </foreach>
                a left join aps_blank_order abo on a.BLANKID =abo.ID AND abo.DELETED='0'
                  LEFT JOIN aps_production_order apo1 on apo1.ID=abo.PRODUCTIONORDERID and apo1.DELETED='0'
                  LEFT JOIN aps_customer_management acm on acm.ID = apo1.CUSTOMID AND acm.DELETED='0'
        <where>
            a.ID IS NOT NULL
            AND a.BLANKID is not null
            limit 0,1
        </where>
    </select>
    <select id="getConflictId" resultType="com.rongwei.bsentity.domain.ApsProcessOperationProcessEquDo">
        select apope.*
        from aps_process_operation_process_equ apope
        left join aps_process_operation app on apope.PROCESSID = app.ID and app.DELETED='0'
        <where>
            apope.DELETED='0'
            AND apope.ID != #{id}
            AND apope.PROCESSDEVICEID =#{equId}
            AND (
            (#{startTime}&lt; apope.PLANENDDATE and #{endTime} > apope.PLANSTARTDATE)
            or
            (apope.PLANSTARTDATE &lt; #{endTime} and apope.PLANENDDATE > #{startTime})
            )
            <if test="processway == '合并加工'">
                and ((app.PROCESSWAY != '合并加工') or(app.PROCESSWAY = '合并加工' and apope.PLANSTARTDATE!=
                #{startTime}) )
            </if>
        </where>
    </select>
    <select id="getWorkshopAndDeviceByFactoryId" resultType="com.rongwei.bsentity.vo.GanttVos">
        SELECT
            apope.PROCESSDEVICEID AS processdeviceid,
            ifnull(aci.CHECKITEMNAME,apope.PROCESSDEVICE) AS processdevice,
            ifnull(aci.USEDEPTID,apope.PROCESSWORKSHOPID) AS processworkshopid,
            ifnull(aci.USEDEPTNAME,apope.PROCESSWORKSHOP) AS processworkshop,
            aci.CHECKITEMTYPE,
            ape.PRODUCTLINENAME AS productlinename,
            ape.productlineid AS productlineid
        FROM
            aps_process_operation_process_equ apope
            LEFT JOIN asp_check_items aci ON apope.PROCESSDEVICEID = aci.ID
            LEFT JOIN (
                SELECT
                    ape.EQUIPMENTID,
                    apl.PRODUCTLINENAME,
                    apl.ID AS productlineid
                FROM
                    aps_resources_equipment ape
                    JOIN aps_production_line apl ON ape.MAINID = apl.ID
                WHERE
                    apl.DELETED = '0'
                    AND ape.DELETED = '0'
            ) ape ON ape.EQUIPMENTID = apope.PROCESSDEVICEID
        WHERE
            apope.DELETED = '0'
            AND apope.TENANTID = #{factoryId}
            AND ( apope.PROCESSDEVICEID IS NOT NULL AND apope.PROCESSDEVICEID != '' )
        group BY
            processdeviceid
        ORDER BY
            processworkshop,processdevice,CHECKITEMTYPE
    </select>
    <select id="getTimeOverlapByEquId" resultType="com.rongwei.bsentity.domain.ApsProcessOperationProcessEquDo">
        select apope.*
        from aps_process_operation_process_equ apope
        left join aps_process_operation app on apope.PROCESSID = app.ID and app.DELETED='0'
        <where>
            apope.ID != #{id}
            AND apope.DELETED='0'
            AND  apope.PROCESSDEVICEID =#{equId}
            and ( #{oldStartDate} BETWEEN apope.PLANSTARTDATE AND  apope.PLANENDDATE
            OR  #{oldEndDate} BETWEEN apope.PLANSTARTDATE AND  apope.PLANENDDATE )
            <if test="currentProcessWay == '合并加工'">
                and #{oldStartDate} != apope.PLANSTARTDATE
            </if>
        </where>
    </select>
    <select id="getDataByInputIdAndId" resultType="com.rongwei.bsentity.domain.ApsProcessOperationProcessEquDo">
        select apope.*
        from aps_process_operation_process_equ apope
        left join aps_process_operation app on apope.PROCESSID = app.ID
        <where>
            apope.DELETED='0'
            and app.DELETED='0'
            and apope.PLANPROCESSRALL - apope.STARTINGROLL - apope.CANCELROLL >0
            and app.PLANINPUTID =#{inputId}
            AND apope.ID IN
            <foreach collection="ids" item="id" open="(" close=")" separator=",">
                #{id}
            </foreach>
            ORDER BY PLANSTARTDATE desc
        </where>
    </select>
    <select id="getNeedSelectEquTypes" resultType="java.lang.String">
        select GROUP_CONCAT(app.EQUIPMENTTYPE) AS 'needSelectEquTypes'
        from aps_process_operation_process_equ apope
                 LEFT JOIN aps_process_operation apo ON apo.DELETED = 0 AND apope.PROCESSID = apo.ID
                 LEFT JOIN aps_production_processes app ON app.DELETED = 0 AND app.ID = apo.PROCESSID
        where apope.DELETED = 0 AND apope.ID in
        <foreach collection="operationJobDetailsId" open="(" close=")" item="item" separator=",">
            #{item}
        </foreach>
    </select>
    <select id="selectMyProcessVo" resultType="com.rongwei.bsentity.vo.ProcessVo">
        SELECT
            apope.ID AS 'processEquId',
            apope.PLANSTARTDATE AS 'planStartTime',
            apope.PLANENDDATE AS 'planEndTime',
            apo.PROCESSWAY AS 'processWay',
            apope.CONFLICTDES AS 'conflictdes',
            apope.SOFTCONFLICTDES AS 'softconflictdes'
        FROM
            aps_process_operation_process_equ apope
                LEFT JOIN aps_process_operation apo ON apope.PROCESSID = apo.ID AND apo.DELETED = 0
        WHERE
            apope.DELETED = 0
            AND apope.PROCESSDEVICEID = #{equId}
            AND apope.PLANSTARTDATE > #{date1}
            AND apope.PLANENDDATE &lt; #{date2}
    </select>
</mapper>
