package com.rongwei.bscommon.sys.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rongwei.bsentity.domain.ApsBlankOrderDo;
import com.rongwei.bsentity.domain.ApsProcessOperationDo;
import com.rongwei.bsentity.domain.ApsProcessOperationProcessEquDo;
import com.rongwei.bsentity.domain.AspCheckItemsDo;
import com.rongwei.bsentity.vo.*;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 生产订单_坯料计划 Mapper 接口
 * </p>
 *
 * @author fpy
 * @since 2024-03-01
 */
public interface ApsBlankOrderDao extends BaseMapper<ApsBlankOrderDo> {

    @Select("select a.*,b.PLANENDDATE from asp_check_items a LEFT JOIN \n" +
            "(select PROCESSDEVICEID,MAX(PLANENDDATE) PLANENDDATE from aps_process_operation_process_equ where DELETED='0' GROUP BY PROCESSDEVICEID) b on a.ID=b.PROCESSDEVICEID\n" +
            "where a.DELETED='0' and a.ID in (${ids}) ORDER BY b.PLANENDDATE ASC")
    List<AspCheckItemsDo> eqAscs(@Param("ids") String ids);

    @Select("select DISTINCT a.*,b.DELIVERYDATE,b.DELIVERYTIME from aps_blank_order a LEFT JOIN aps_production_order b on a.PRODUCTIONORDERID=b.ID " +
            "LEFT JOIN aps_process_operation c on c.BLANKID=a.ID " +
            "where a.PRODUCTIONORDERID=#{orderId} " +
            "and (c.LOCKMARK='n' or c.LOCKMARK is null) " +
            "and a.DELETED='0' and b.DELETED='0' and c.DELETED='0' and a.ISSTAG ='否'")
    List<ApsBlankOrderVo> getByOrderId(@Param("orderId") String orderId);

    @Select("select a.*,b.DELIVERYDATE,b.DELIVERYTIME from aps_blank_order a LEFT JOIN aps_production_order b on a.PRODUCTIONORDERID=b.ID \n" +
            "where a.PRODUCTIONORDERID=#{orderId} and a.ID=#{blanckId} and a.DELETED='0' and b.DELETED='0'")
    List<ApsBlankOrderVo> getByOrderBlanckId(@Param("orderId") String orderId,@Param("blanckId") String blanckId);

    @Select("<script>select a.*,b.DELIVERYDATE,b.DELIVERYTIME from aps_blank_order a LEFT JOIN aps_production_order b on a.PRODUCTIONORDERID=b.ID " +
            "where (a.LOCKMARK is null or a.LOCKMARK='n') and a.DELETED='0' and b.DELETED='0' and b.PRODUCTIONSTATUS='20' and a.ID not in (${hasIds}) " +
            "<if test='tenantId != null and tenantId != &apos;&apos;'> " +
            "  and  b.TENANTID = #{tenantId} " +
            "</if></script>")
    List<ApsBlankOrderVo> getFbNotLock(@Param("hasIds") String hasIds, @Param("tenantId") String tenantId);

    @Select("select a.*,b.DELIVERYDATE,b.DELIVERYTIME from aps_blank_order a LEFT JOIN aps_production_order b on a.PRODUCTIONORDERID=b.ID " +
            "where b.PRODUCTIONSTATUS='20' and (a.LOCKMARK='n' or a.LOCKMARK is null) and a.DELETED='0' and b.DELETED='0' and b.TENANTID = #{tenantId}")
    List<ApsBlankOrderVo> getNotLockOrders(@Param("tenantId") String tenantId);

    @Select("<script>select DISTINCT a.*,c.DELIVERYDATE,c.DELIVERYTIME from aps_blank_order a LEFT JOIN aps_process_operation b on a.ID=b.BLANKID LEFT JOIN aps_production_order c on a.PRODUCTIONORDERID=c.ID\n" +
            "where a.DELETED='0' and b.DELETED='0' and c.DELETED='0' and (b.LOCKMARK='n' OR b.LOCKMARK is null) " +
            "and c.TENANTID = #{tenantId}  and a.SCHEDULINGSTATUS != '10' and a.ISSTAG = '否'" +
            "<if test='hasIds != null and hasIds != &apos;&apos;'> " +
            "    and a.ID not in (${hasIds})" +
            "</if></script>")
    List<ApsBlankOrderVo> getOrdersHasNotLockProcess(@Param("hasIds") String hasIds, @Param("tenantId") String tenantId);

    @Select("UPDATE aps_process_operation set LOCKMARK=null where LOCKMARK='y' and DELETED='0' and " +
            "BLANKID in (SELECT ID FROM aps_blank_order where DELETED='0' and SCHEDULINGSTATUS='10' and ID in (${hasIds}))")
    void setLockMarkNullByToAps(@Param("hasIds") String hasIds);

    @Update("delete from aps_process_operation_process_equ where BLANKID in (${blankids}) and (CANCELROLL is null or CANCELROLL<PLANPROCESSRALL)")
    void deleteBlankDetail(@Param("blankids") String blankids);

    @Select("UPDATE aps_process_operation_process_equ set LOCKMARK='n' where DELETED='0' and BLANKID in (${hasIds}) " +
            "and PROCESSID NOT IN (select ID from aps_process_operation where DELETED='0' and BLANKID in (${hasIds}) AND LOCKMARK='y')")
    void setProDetailLockMark(@Param("hasIds") String hasIds);

    @Select("select * FROM (\n" +
            "select PROCESSDEVICEID,PLANSTARTDATE,PLANENDDATE,GROUP_CONCAT(PROCESSID) PROCESSID from (select * from aps_process_operation_process_equ " +
            "where DELETED='0' ORDER BY PROCESSID ASC) a GROUP BY PROCESSDEVICEID,PLANSTARTDATE,PLANENDDATE" +
            ") b where b.PROCESSID like '%${processid}%'")
    List<ApsProcessOperationProcessEquDo> getGroupProcessOperationProcessEqus(@Param("processid") String processid);

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

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

    @Update("update aps_process_operation set PROCESSSTATUS = '20' where PROCESSSTATUS = '10' AND BLANKID IN (select ID from aps_blank_order where DELETED = 0 AND PRODUCTIONORDERID in (${orderIds}))")
    void publishUnlock(@Param("ids") List<String> ids);

    @Update("update aps_process_operation set PROCESSSTATUS = '20' where BLANKID IN (${blankIds}) AND PROCESSSTATUS = '10'")
    void updateProcessOperationByOrderIds(@Param("blankIds") String blankIds);

    void cancelSchedulingUpdate(@Param("idList") List<String> idList);

    void reportCheckUpdateJobStatus(@Param("id") String id);

    @Select("select PRODUCTID from aps_process_output_product where DELETED = 0 AND BLANKID in (${idStrs}) group by PRODUCTID")
    List<String> selectOutputProductIdList(@Param("idStrs") String idStrs);

    @Select("select BLANKID AS 'blankId',MAX(PROCESSSTATUS) AS 'maxProcessStatus',MIN(PROCESSSTATUS) AS 'minProcessStatus' from aps_process_operation where BLANKID = (select BLANKID from aps_process_operation where ID = #{productProcessId})")
    BlankIdAndProcessStatusVo selectProcessStatusByProcessId(@Param("productProcessId") String productProcessId);

    List<ApsProcessOperationDo> getProcessInfoByLockTime(@Param("lockTime") Date LockTime, @Param("lockMark") String lockMark);

    List<ApsProcessOperationProcessEquDo> getProcessDetailInfoByLockTime(@Param("lockTime") Date LockTime, @Param("lockMark") String lockMark);

    int checkConflictByBlankId(@Param("blankIds") List<String> blankIds);

    @Select("select * from sys_dict where DELETED = 0 AND ID =(select PID from sys_dict where DELETED = 0 AND ID = (select PID from sys_dict where DELETED = 0 AND ID = #{productionorderid})) limit 1")
    String selectDictAboutProductType(@Param("productionorderid") String productionorderid);

    @Select("SELECT apoom.OUTPUTFINISHPRODUCT FROM aps_process_operation apo LEFT JOIN aps_process_operation_out_mater apoom ON apo.ID = apoom.MAINID AND apoom.DELETED = 0 where apo.DELETED = 0 AND apoom.OUTPUTFINISHPRODUCT is not null AND apoom.OUTPUTFINISHPRODUCT != '' AND apo.BLANKID = #{id}")
    List<String> selectProcessByBlankId(@Param("id") String id);

    @Update("update aps_process_operation set SECONDROLL = SECONDROLL+1 where ID = #{processoperationid}")
    void processAddSecondRoll(@Param("processoperationid") String processoperationid);

    @Update("update aps_process_operation_process_equ set BACHMATERIALPROCESS = REPLACE(REPLACE(REPLACE(BACHMATERIALPROCESS,'${batchnumber},',''),',${batchnumber}',''),'${batchnumber}','') where DELETED = 0 AND PROCESSID IN (select ID from aps_process_operation where DELETED = 0 AND PREVIOUSPROCESSID = #{processoperationid})")
    void processDeleteBatchNumber(@Param("batchnumber") String batchnumber, @Param("processoperationid") String processoperationid);

    @Update("update sys_mind_mapping set DELETED = 1 where BSID = #{id}")
    void removeMindMappingByBlankId(@Param("id") String id);

    @Delete("delete from aps_process_operation_backup where BLANKID = #{id}")
    void deleteProcessBackupByBlankId(@Param("id") String id);

    @Delete("delete from aps_process_operation_process_equ_backup where BLANKID = #{id}")
    void deleteProcessEquBackupByBlankId(@Param("id") String id);

    @Select("select ALLOYNAMES AS processmetals,NEXTALLOYNAMES AS nextprocessmetals from aps_alloy_close where DELETED='0'")
    List<WashingMetal> getWashingMetalConfig();

    @Select("SELECT * FROM aps_blank_order WHERE DELETED = 0 AND PRODUCTIONORDERID = #{productionorderId}")
    List<ApsBlankOrderDo> selectBlankOrderByProductionorderId(@Param("productionorderId") String productionorderId);

    @Update("update aps_process_operation_process_equ set BACHMATERIALPROCESS = REPLACE(REPLACE(REPLACE(BACHMATERIALPROCESS,'${batchnumber},',''),',${batchnumber}',''),'${batchnumber}','') where DELETED = 0 AND PREVIOUSPROCESSESIDS = #{processoperationequid} AND PROCESSID IN (select ID from aps_process_operation where DELETED = 0 AND PLANINPUTID = #{joboutputmaterialid})")
    void updateBatchNumberByBorrow(@Param("batchnumber") String batchnumber, @Param("processoperationequid") String processoperationequid, @Param("joboutputmaterialid") String joboutputmaterialid);

    List<UpdateBlankDeliveryDateVo> getBlankDeliveryDate(@Param("blankId") String blankId);

    IPage<ProductPlanReportVo> selectPageReport(Page<ProductPlanReportVo> queryPage, @Param("param") ProductPlanReportParamVo param);

    @Select("select count(1) from aps_work_in_progress_inventory where DELETED = 0 AND (BLANKBATCHNUMBER =  #{batchNumber} OR BLANKBATCHNUMBER like CONCAT(#{batchNumber},'-%') OR #{batchNumber} like CONCAT(BLANKBATCHNUMBER,'-%'))")
    Integer selectCountWorkIn(@Param("batchNumber") String batchNumber);

    @Select("select GROUP_CONCAT(distinct IF(b.BACHMATERIALPROCESS = '', null,b.BACHMATERIALPROCESS) ) AS 'BACHMATERIALPROCESS' from aps_process_operation a LEFT JOIN aps_process_operation_process_equ b ON b.DELETED = 0 AND a.ID = b.PROCESSID where a.DELETED = 0 AND a.ID = #{processId} AND (a.PREVIOUSPROCESSID is null OR a.PREVIOUSPROCESSID = '') group by a.ID")
    String selectBatchNumbersByProcessId(@Param("processId") String processId);

    @Select("select * from aps_process_operation_process_equ where DELETED = 0 AND FIND_IN_SET(#{batchnumber},BACHMATERIALPROCESS) limit 1")
    ApsProcessOperationProcessEquDo selectUseThisBatchNumberProcessEqu(@Param("batchnumber") String batchnumber);

    @Select("select * from aps_blank_order where DELETED = 0 AND FIND_IN_SET(#{batchNumber},PREPAREMATERIALBATCHNO) AND ID != #{blankId} limit 1")
    ApsBlankOrderDo countThisBatchNumber(@Param("batchNumber") String batchNumber, @Param("blankId") String blankId);

    String selectIsWaitGetMaterialById(@Param("id") String id);

    String selectPlanEndProductionWorkshopsById(@Param("productionorderid") String productionorderid);

    void updateFinishDateAboutProcessEqu(@Param("needScheduleDate") String needScheduleDate);

    void removeLockNextDayProcessEqu(@Param("needScheduleDate") String needScheduleDate);

    void removeLockNextTwoDayProcessEqu(@Param("needScheduleDate") String needScheduleDate);

    List<ApsProcessOperationProcessEquDo> selectNeedRescheduleEqu(@Param("needScheduleDate") String needScheduleDate);
}
