cl
2024-01-19 e0fac38b26845f25de479783e0c76cf12a5311e0
guns-vip-main/src/main/java/cn/stylefeng/guns/plcserver/server/OP050/impl/OP050ServerInterfaceImpl.java
@@ -7,10 +7,19 @@
import cn.stylefeng.guns.modular.cm.paramCollection.model.params.ParamCollectionParam;
import cn.stylefeng.guns.modular.cm.paramCollection.service.ParamCollectionService;
import cn.stylefeng.guns.modular.cm.passingStationCollection.model.params.PassingStationCollectionParam;
import cn.stylefeng.guns.modular.cm.passingStationCollection.model.result.PassingStationCollectionResult;
import cn.stylefeng.guns.modular.cm.passingStationCollection.service.PassingStationCollectionService;
import cn.stylefeng.guns.modular.dq.materialTraceability.model.params.MaterialTraceabilityParam;
import cn.stylefeng.guns.modular.dq.materialTraceability.service.MaterialTraceabilityService;
import cn.stylefeng.guns.modular.gm.greaseManage.model.params.GreaseManageParam;
import cn.stylefeng.guns.modular.gm.greaseManage.model.result.GreaseManageResult;
import cn.stylefeng.guns.modular.gm.greaseManage.service.GreaseManageService;
import cn.stylefeng.guns.modular.om.productionOrderBatchInfo.model.params.ProductionOrderBatchInfoParam;
import cn.stylefeng.guns.modular.om.productionOrderBatchInfo.model.result.ProductionOrderBatchInfoResult;
import cn.stylefeng.guns.modular.om.productionOrderBatchInfo.service.ProductionOrderBatchInfoService;
import cn.stylefeng.guns.modular.om.productionOrderRecords.model.params.ProductionOrderRecordsParam;
import cn.stylefeng.guns.modular.om.productionOrderRecords.model.result.ProductionOrderRecordsResult;
import cn.stylefeng.guns.modular.om.productionOrderRecords.service.ProductionOrderRecordsService;
import cn.stylefeng.guns.plcserver.opc.GlobalVariable;
import cn.stylefeng.guns.plcserver.opc.OPCComm;
import cn.stylefeng.guns.plcserver.opc.unit.OPCUnit;
@@ -22,7 +31,9 @@
import org.openscada.opc.lib.da.Group;
import org.openscada.opc.lib.da.Item;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.List;
@@ -49,10 +60,34 @@
    private static String GROUP_NAME  = "OP050_P";//组名
    private static List list = null;
    private static Group group = null;
    private int count = 0;
    /**
     * 设置心跳标志位,保存到全局变量
     */
    public void setHeartbeat(String value) {
        if ("false".equals(value)) {
            OPCUnit.write(opc.getOP050_F_HEART_BEAT(), "true");
            GlobalVariable.OP050_F_HEART_BEAT = true;
            count = 0;
        } else {
            count++;
        }
        // 循环读取频率是:500ms一次,如果超过6次,也就是3s认定超时
        if (count > GlobalVariable.HEART_MAX_COUNT) {
            GlobalVariable.OP050_F_HEART_BEAT = false;
        }
    }
    @Override
    public synchronized void monitor() {
        String ecpStr = "";//异常记录标记
        try{
           /* String isHeartBeat = OPCUnit.read(opc.getOP050_F_HEART_BEAT());
            setHeartbeat(isHeartBeat);*/
            if(GlobalVariable.OP050_F_HEART_BEAT) {
                //PC读工单
                String isPLCStart = OPCUnit.read(opc.getOP050_J_PLC_START());//PLC开始
@@ -69,8 +104,15 @@
                if(isPLCFinish.equals("true")){
                    String isFinish = OPCUnit.read(opc.getOP050_J_PC_FINISH());//PC操作完成
                    if(isFinish.equals("false")){
                        handleInfo();
                        logger.info("handleFinsh开始时间:"+new Date());
                        handleFinsh();
                        logger.info("handleFinsh完成时间:"+new Date());
                        logger.info("handleInfo开始时间:"+new Date());
                        handleInfo();
                        logger.info("handleInfo完成时间:"+new Date());
                        logger.info("handleMaterial开始时间:"+new Date());
                        handleMaterial();
                        logger.info("handleMaterial完成时间:"+new Date());
                    }
                }
@@ -97,20 +139,26 @@
        List<GreaseManageResult> list = greaseManageService.findListBySpec(param);
        if(list.size()>0) {
            String duaDate = list.get(0).getDuaDate();
            LocalDate date1 = LocalDate.parse(duaDate); // 第一个日期
            LocalDate date2 = LocalDate.now(); // 当前日期(或者指定其他日期)
            long daysBetween = ChronoUnit.DAYS.between(date1, date2);
            SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
            String currentDate = format.format(new Date());
            long  daysBetween = getRemainder(currentDate,duaDate);
            if(daysBetween>0){
                OPCUnit.write(opc.getOP050_OIL_STATE(), "false"); //写油脂状态
            }else{
                OPCUnit.write(opc.getOP050_OIL_STATE(), "true"); //写油脂状态
            }
            logger.info("到期日期:"+date1+"当前日期:"+date1+"剩余天数::"+daysBetween);
            //logger.info("到期日期:"+duaDate+"当前日期:"+currentDate+"剩余天数::"+daysBetween);
        }
    }
    public static long getRemainder(String currentDate,String tableDate){
        DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyyMMdd");
        LocalDate localDate1 = LocalDate.parse(currentDate, dateFormatter);
        LocalDate localDate2 = LocalDate.parse(tableDate, dateFormatter);
        long days = ChronoUnit.DAYS.between(localDate1, localDate2);
        return days;
    }
    //PC读工单
    public void handleOrder() {
        IN_TIME = DateTool.getLocalTimeForDate();
@@ -120,6 +168,41 @@
        S_SFC_CODE = OPCUnit.read(opc.getOP050_S_SFC_CODE());//总成编码
        OPCUnit.write(opc.getOP050_J_PC_WRITE_ORDER(), "true"); //PC读工单完成
        logger.info("OP050工位,handleOrder PC处理完成!");
    }
    public void handleMaterial() {
        //查询批次上料信息
        ProductionOrderBatchInfoParam productionOrderBatchInfoParam = new ProductionOrderBatchInfoParam();
        productionOrderBatchInfoParam.setWorkOrderNo(S_ORDER_CODE);
        productionOrderBatchInfoParam.setLocationCode(LOCATION_CODE);
        ProductionOrderBatchInfoService productionOrderBatchInfoService = opc.productionOrderBatchInfoService;
        List<ProductionOrderBatchInfoResult> list1 = productionOrderBatchInfoService.findListBySpec(productionOrderBatchInfoParam);
        for(int i=0;i<list1.size();i++){
            ProductionOrderBatchInfoResult productionOrderBatchInfoResult = list1.get(i);
            int remainingQuantity =  productionOrderBatchInfoResult.getResidueQuantity();//剩余数量
            if(remainingQuantity>0){
                //新增物料追溯
                MaterialTraceabilityParam materialTraceabilityParam = new MaterialTraceabilityParam();
                materialTraceabilityParam.setWorkOrderNo(S_ORDER_CODE);
                materialTraceabilityParam.setProductCode(S_PRODUCT_CODE);
                materialTraceabilityParam.setProductNo(S_SFC_CODE);
                materialTraceabilityParam.setLineCode(PRODUCTION_LINE);
                materialTraceabilityParam.setLocationCode(LOCATION_CODE);
                materialTraceabilityParam.setAssemblyQty(1);
                materialTraceabilityParam.setAssemblyTime(DateTool.getLocalTimeForDate());
                materialTraceabilityParam.setMaterialCode(productionOrderBatchInfoResult.getMaterialCode());//物料编码
                materialTraceabilityParam.setMaterialBatchNo(productionOrderBatchInfoResult.getBatch());//物料批次
                MaterialTraceabilityService materialTraceabilityService = opc.materialTraceabilityService;
                materialTraceabilityService.add(materialTraceabilityParam);
                //更新剩余数量
                productionOrderBatchInfoParam.setId(productionOrderBatchInfoResult.getId());
                productionOrderBatchInfoParam.setResidueQuantity(remainingQuantity-1);//剩余数量
                productionOrderBatchInfoService.update(productionOrderBatchInfoParam);
            }
        }
    }
    //PC读参数信息
@@ -188,6 +271,7 @@
        //处理过站信息
        String S_PRODUCT_STATE_CODE = OPCUnit.read(opc.getOP050_S_PRODUCT_STATE_CODE());
        PassingStationCollectionParam param = new PassingStationCollectionParam();
        param.setWorkOrderNo(S_ORDER_CODE);
        param.setProductCode(S_PRODUCT_CODE);
@@ -200,9 +284,17 @@
        PassingStationCollectionService passingStationCollectionService = opc.passingStationCollectionService;
        passingStationCollectionService.add(param);
        //根据分总成编码,将EOP过站数据绑定关系修改为总成编码
        String  copSfcCode = OPCUnit.read(opc.getOP050_S_SFC_CODE_COP());//COP分总成
        String  bopSfcCode = OPCUnit.read(opc.getOP050_S_SFC_CODE_BOP());//BOP分总成
        //根据分总成编码,将EOP物料数据绑定关系修改为总成编码
        MaterialTraceabilityService materialTraceabilityService = opc.materialTraceabilityService;
        materialTraceabilityService.updateSFC(S_SFC_CODE,copSfcCode);
        materialTraceabilityService.updateSFC(S_SFC_CODE,bopSfcCode);
        //根据分总成编码,将EOP过站数据绑定关系修改为总成编码
        passingStationCollectionService.updateSFC(S_SFC_CODE,copSfcCode);
        passingStationCollectionService.updateSFC(S_SFC_CODE,bopSfcCode);
@@ -212,6 +304,40 @@
        paramCollectionService.updateSFC(S_SFC_CODE,copSfcCode);
        paramCollectionService.updateSFC(S_SFC_CODE,bopSfcCode);
        logger.info("S_SFC_CODE:"+S_SFC_CODE);
        logger.info("copSfcCode:"+copSfcCode);
        logger.info("bopSfcCode:"+bopSfcCode);
        //查询COP和BOP是否合格
        PassingStationCollectionParam passingStationCollectionParam = new PassingStationCollectionParam();
        passingStationCollectionParam.setSfcCode(S_SFC_CODE);
        List<PassingStationCollectionResult> passingStationCollectionResultList =
                opc.passingStationCollectionService.findListBySpec(passingStationCollectionParam);
        String isProductState = "true";
        for(int i=0;i<passingStationCollectionResultList.size();i++){
            PassingStationCollectionResult passingStationCollectionResult = passingStationCollectionResultList.get(i);
            if("false".equals(passingStationCollectionResult.getOutRsSign())){
                isProductState = "false";
                break;
            }
        }
        //修改报工记录
        if("false".equals(S_PRODUCT_STATE_CODE) || "false".equals(isProductState)){
            ProductionOrderRecordsParam productionOrderRecordsParam = new ProductionOrderRecordsParam();
            productionOrderRecordsParam.setWorkOrderNo(S_ORDER_CODE);
            productionOrderRecordsParam.setProductNo(S_SFC_CODE);
            ProductionOrderRecordsService productionOrderRecordsService = opc.productionOrderRecordsService;
            List<ProductionOrderRecordsResult> list = productionOrderRecordsService.findListBySpec(productionOrderRecordsParam);
            if(list.size()>0){
                ProductionOrderRecordsResult productionOrderRecordsResult = list.get(0);
                productionOrderRecordsParam.setId(productionOrderRecordsResult.getId());
                productionOrderRecordsParam.setWhetherPass("false");
                productionOrderRecordsService.update(productionOrderRecordsParam);
            }
        }
        OPCUnit.write(opc.getOP050_J_PC_FINISH(), "true"); //PC完成