package com.rongwei.bscommon.sys.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.rongwei.bsentity.domain.ApsBlankOrderDo;
import com.rongwei.bsentity.domain.ApsProcessOperationProcessEquDo;
import com.rongwei.bsentity.domain.AspCheckItemsDo;
import com.rongwei.bsentity.vo.ApsBlankOrderVo;
import com.rongwei.bsentity.vo.BlankIdAndProcessStatusVo;
import com.rongwei.bsentity.vo.WashingMetal;
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 a.*,b.DELIVERYDATE,b.DELIVERYTIME from aps_blank_order a LEFT JOIN aps_production_order b on a.PRODUCTIONORDERID=b.ID where a.PRODUCTIONORDERID=#{orderId} and a.DELETED='0' and b.DELETED='0'")
    List<ApsBlankOrderVo> getByOrderId(@Param("orderId") String orderId);

    @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' " +
            "<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("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<String> getIdByLockTime(@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);
}
