package com.rongwei.bscommon.sys.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.rongwei.bsentity.domain.ApsCraftRouteDo;
import com.rongwei.bsentity.domain.ApsProcessOperationDo;
import com.rongwei.bsentity.domain.ApsProcessOperationEquDo;
import com.rongwei.bsentity.domain.ApsProcessOperationProcessEquDo;
import com.rongwei.bsentity.vo.ApsProcessOperationAndMaterialVo;
import com.rongwei.bsentity.vo.ProcessIdRelationVo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 生产订单_坯料计划_工序作业 Mapper 接口
 * </p>
 *
 * @author fpy
 * @since 2024-03-01
 */
public interface ApsProcessOperationDao extends BaseMapper<ApsProcessOperationDo> {
    void updateBachmaterialprocessByIds(@Param("ids") List<String> ids, @Param("batchNum") String batchNum,
                                        @Param("unqualifiedQuantity") int unqualifiedQuantity);

    void updateAboutCheckBatchNum(@Param("id") String id,
                                  @Param("currentCheckNum") Integer currentCheckNum,
                                  @Param("qualifiedQuantity") Integer qualifiedQuantity,
                                  @Param("unqualifiedQuantity") Integer unqualifiedQuantity);

    @Update("UPDATE aps_process_operation set DELETED='1' where BLANKID=#{blanckid}")
    void updateProcessOperation(@Param("blanckid") String blanckid);

    @Update("UPDATE aps_process_operation_out_mater set DELETED='1' where BLANKID=#{blanckid}")
    void updateProcessOperationOutMeter(@Param("blanckid") String blanckid);

    @Update("UPDATE aps_process_operation_equ set DELETED='1' where BLANKID=#{blanckid}")
    void updateProcessOperationOEqu(@Param("blanckid") String blanckid);

    @Update("UPDATE sys_mind_mapping set DELETED='1' where BSID=#{blanckid}")
    void updateSysMindMapping(@Param("blanckid") String blanckid);

    @Select("select MAINID,GROUP_CONCAT(EQUIPMENTID) as EQUIDS from aps_resources_equipment where DELETED='0' group by MAINID")
    List<Map<String, Object>> getEquGroupIds();

    @Select("select apo.*,apoom.ID AS 'outputMaterialId',apoom.OUTPUTMATERIAL AS 'outputMaterial',apoom.PLANROLLNUM AS 'planRollNum',apoom.PLANSTOCKROLLNUM AS 'planStockRollNum',apoom.PLANPRODROLLNUM AS 'planProdRollNum' from aps_process_operation apo LEFT JOIN aps_process_operation_out_mater apoom ON apoom.MAINID = apo.ID AND apoom.DELETED = 0 where apo.DELETED = 0 AND apo.BLANKID = #{blankId}")
    List<ApsProcessOperationAndMaterialVo> selectOperationAndMaterialByBlankId(@Param("blankId") String blankId);

    List<ProcessIdRelationVo> getAllNextProcessOperationById(@Param("ids") List<String> ids);

    @Update("update aps_process_operation_process_equ set BACHMATERIALPROCESS = concat(BACHMATERIALPROCESS,',',#{batchnumber}) where DELETED = 0 AND ID = #{processoperationid}")
    void updateBatchNumberByProcessId(@Param("batchnumber") String batchnumber, @Param("processoperationid") String processoperationid);

    void updateBatchNumberByProcessList(@Param("batchnumber") String batchnumber, @Param("processIdList") List<String> processIdList);

    @Select("select ID,PLANPROCESSRALL from aps_process_operation where DELETED='0' and BLANKID=#{blankid}")
    List<ApsProcessOperationDo> getProcessOperation(@Param("blankid") String blankid);

    void publish(@Param("productionOrderIds") List<String> productionOrderIds);

    void publishCancel(@Param("productionOrderIds") List<String> productionOrderIds);

    @Select("SELECT * FROM aps_process_operation WHERE DELETED = 0 AND BLANKID = #{blankid}")
    List<ApsProcessOperationDo> getProcessOperationByBlankId(@Param("blankid") String blankid);

    @Select("SELECT * FROM aps_process_operation WHERE DELETED = 0 and IFNULL(PREVIOUSPROCESSID,'')='' AND BLANKID = #{blankid}")
    ApsProcessOperationDo getFirstProcessOperationByBlankId(@Param("blankid") String blankid);

    @Select("select * from (\n" +
            "\tSELECT o.ID,o.TENANTID,o.ROPTION,o.DELETED,o.REMARK,o.CREATEDATE,o.CREATEUSERID,o.MODIFYDATE,o.MODIFYUSERID,o.CREATEUSERNAME,o.MODIFYUSERNAME,o.OWNEDFACTORY,o.CRAFTSTEPID,o.STEPNUM,o.PROCESSID,o.PROCESSCODE,o.PROCESS,o.PROCESSNAME,o.PROCESSWAY,o.NUMUNIT,o.ONCEPROCESSVAL,o.ONCEPROCESSMIN,o.PERTIMECAPACITY,o.TIMEUNIT,o.PREPRESSWORKMIN,o.LOADINGMIN,o.CUTTINGMIN,o.CUTFINISHMIN,o.MINFLOWBATCH,o.MINFLOWWAITMIN,o.MAXFLOWWAITMIN,o.IFBLANKPROCESS,o.MATERIALCODE,o.MATERIALNAME,o.THICKNESS,o.THICKSTR,o.THICKEND,o.PROWIDTH,o.WIDTHSTR,o.WIDTHEND,o.PROLENGTH,o.LENGTHSTR,o.LENGTHEND,o.INMATERCONDITION,o.OUTMATERREQUEST,o.CANCHOOSEDEVICEID,o.CHOOSEEQU,o.BLANKID,o.PLANINPUTID,o.PLANINPUT,o.PLANOUTPUT,o.PLANINRALL,o.PLANBLANKRALL,o.PLANPROCESSRALL,o.PROCESSWORKSHOPID,o.PROCESSWORKSHOP,o.PROCESSDEVICEID,o.PROCESSDEVICE,e.MAXPLANSTARTDATE as PLANSTARTDATE,o.ACTUALSTARTDATE,o.PLANENDDATE,o.ACTUALFINISHDATE,o.ACTUALIMPUTROLL,o.INPUTUNQUALIFIEDROLL,o.ACTUALCANPROCESSROLL,o.CLOSESTATUS,o.SECONDROLL,o.STARTINGROLL,o.REPORTROLL,o.CHECKOUTROLL,o.UNFINISHROLL,o.CANCELROLL,o.WORKSTATUS,o.PROCESSSTATUS,o.BACHMATERIALPROCESS,o.PROCESSTOTALTIME,o.WAITREPORTID,o.PLANPROCESSNUM,o.PREVIOUSPROCESSID,o.PREVIOUSPROCESS,o.NEXTPROCESSID,o.NEXTPROCESS,o.LEVEL,o.PRODUCTTYPEID,o.PRODUCTTYPE,o.METAL,o.METALSTATE,o.OUTPUTQUALIFIEDNUM,o.OUTPUTUNQUALIFIEDNUM,o.CHANGEINMETER,o.SINGLEROLLWEIGHT,o.LOCKMARK,o.TIMEWAY,o.WORKSPEED,o.LEAVEWAITJOBROLL,o.LEAVEWAITWORKROLL FROM aps_process_operation o\n" +
            "\tleft join (\n" +
            "\t\tSELECT PROCESSID,MAX(PLANSTARTDATE) as MAXPLANSTARTDATE FROM aps_process_operation_process_equ \n" +
            "\t\tWHERE DELETED = 0 AND PROCESSID in ('${nextIds}') \n" +
            "\t\tgroup by PROCESSID\n" +
            "\t) e on o.ID=e.PROCESSID\n" +
            "\tWHERE o.DELETED = 0 and o.LEAVEWAITWORKROLL>0 AND o.ID in ('${nextIds}') \n" +
            ") p1 order by PLANSTARTDATE desc")
    List<ApsProcessOperationDo> getNextProcessOperationsOrderBy(@Param("nextIds") String nextIds);

    @Select("SELECT * FROM aps_process_operation WHERE DELETED = 0 AND ID in ('${nextIds}')")
    List<ApsProcessOperationDo> getNextProcessOperations(@Param("nextIds") String nextIds);

    @Select("SELECT * FROM aps_process_operation_process_equ WHERE DELETED = 0 AND PROCESSID = #{processid} ${euqwhr} order by PLANSTARTDATE desc")
    List<ApsProcessOperationProcessEquDo> getProcessOperationProcessEqus(@Param("processid") String processid, @Param("euqwhr") String euqwhr);

    @Select("SELECT * FROM aps_process_operation_process_equ WHERE DELETED = 0 AND PREVIOUSPROCESSESIDS=#{equId}")
    List<ApsProcessOperationProcessEquDo> getNextProcessOperationEqus(@Param("equId") String equId);

    @Update("update aps_process_operation_process_equ set BACHMATERIALPROCESS = IF(BACHMATERIALPROCESS is null || BACHMATERIALPROCESS = '',#{batchnumber},concat(BACHMATERIALPROCESS,',',#{batchnumber})) where DELETED = 0 AND PREVIOUSPROCESSESIDS = #{previousprocessesids}")
    void updateBatchNumberByPrevious(@Param("batchnumber") String batchnumber, @Param("previousprocessesids") String previousprocessesids);

    @Select("SELECT CRAFTROUTEID as ID,ROUTEPRODUCTTYPEID as PRODUCTTYPE,ROUTEMETAL as METAL,ROUTEMETALSTATE as METALSTATE,ROUTETHICKSTR as THICKSTR,ROUTETHICKEND as THICKEND," +
            "ROUTEWIDTHSTR as WIDTHSTR,ROUTEWIDTHEND as WIDTHEND,ROUTELENGTHSTR as LENGTHSTR,ROUTELENGTHEND as LENGTHEND FROM aps_blank_order WHERE ID=#{newblankid}")
    ApsCraftRouteDo getApsCraftRoute(@Param("newblankid") String newblankid);

    @Select("SELECT * from aps_craft_route WHERE ID=#{craftrouteid}")
    ApsCraftRouteDo getCraftRoute(@Param("craftrouteid") String craftrouteid);

    @Select("SELECT * FROM aps_process_operation_equ WHERE DELETED = 0 AND MAINID=#{mainid}")
    List<ApsProcessOperationEquDo> getProcessOperationEqus(@Param("mainid") String mainid);

    @Select("SELECT * FROM aps_process_operation_process_equ WHERE DELETED = 0 AND ID in ('${nextIds}')")
    List<ApsProcessOperationProcessEquDo> getNextProcessOperationEqusByNextIds(@Param("nextIds") String nextIds);

    void updateProgressInventory(@Param("batchnumbers") List<String> batchnumbers);

    @Update("update aps_process_operation_equ e\n" +
            "left join aps_process_operation o on e.MAINID=o.ID\n" +
            "set e.ROLLERID=#{rollerid},e.ROLLERTYPE=#{rollertype}\n" +
            "where e.DELETED='0' and o.BLANKID=#{blanckid} and IFNULL(o.LEVEL,'')>${level} \n" +
            "\tand e.RESOURCEID=#{resourceid} ${levelStr}")
    void updateZJProcessEqu(@Param("blanckid") String blanckid, @Param("resourceid") String resourceid, @Param("levelStr") String levelStr, @Param("rollerid") String rollerid, @Param("rollertype") String rollertype, @Param("level") String level);

    @Select("SELECT * FROM aps_process_operation_process_equ WHERE DELETED = 0 AND BLANKID = #{blankid} and IFNULL(NEXTPROCESSESIDS,'')!=''")
    List<ApsProcessOperationProcessEquDo> getNextOperationProcessEqus(@Param("blankid") String blankid);

}
