package com.mes.job; import cn.hutool.core.collection.CollectionUtil; import cn.hutool.json.JSONObject; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.github.xingshuangs.iot.protocol.s7.serializer.S7Serializer; import com.mes.common.config.Const; import com.mes.damage.service.DamageService; import com.mes.largenscreen.entity.PieChartVO; import com.mes.s7.entity.S7DataGHTwo; import com.mes.temperingglass.entity.TemperingGlassInfo; import com.mes.temperingglass.service.TemperingGlassInfoService; import com.mes.tools.WebSocketUtils; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import javax.annotation.Resource; import java.time.LocalDate; import java.time.ZoneId; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.stream.Collectors; /** * @author SNG-010 */ @Component @Slf4j public class PlcTemperingGlassTask { @Autowired private TemperingGlassInfoService temperingGlassInfoService; @Autowired private DamageService damageService; @Autowired @Qualifier("s7SerializerGHTwo") private S7Serializer s7SerializerGHTwo; @Resource private WebSocketUtils webSocketUtils; @Scheduled(fixedDelay = 1000) public void temperingGlassHome() { JSONObject jsonObject = new JSONObject(); //正在等待进片的玻璃 List waitingGlass = new ArrayList<>(); try { waitingGlass = selectWaitingGlassByOpc(); if (CollectionUtil.isNotEmpty(waitingGlass)) { jsonObject.append("waitingGlass", waitingGlass); } } catch (Exception e) { log.info("钢化前获取异常:{}", e.getMessage()); } try { List intoGlass = selectIntoGlass(waitingGlass); if (CollectionUtil.isNotEmpty(intoGlass)) { for (int i = 0; i < intoGlass.size(); i++) { String jsonName = "intoGlass"; if (i != 0) { jsonName = jsonName + i; } jsonObject.append(jsonName, temperingGlassInfoService.list(new LambdaQueryWrapper() .eq(TemperingGlassInfo::getEngineerId, intoGlass.get(i).getEngineerId()) .eq(TemperingGlassInfo::getTemperingLayoutId, intoGlass.get(i).getTemperingLayoutId()))); } } } catch (Exception e) { log.info("钢化前获取异常:{}", e.getMessage()); } try { //出炉后的玻璃 List outGlass = temperingGlassInfoService.selectOutGlass(); if (CollectionUtil.isNotEmpty(outGlass)) { jsonObject.append("outGlass", outGlass); } } catch (Exception e) { log.info("钢化前获取异常:{}", e.getMessage()); } webSocketUtils.sendToWeb("temperingGlass", jsonObject); } @Scheduled(fixedDelay = 1000) public void temperingIsRun() { JSONObject jsonObject = new JSONObject(); //正在进行的任务 List temperingTaskType = temperingGlassInfoService.selectTaskType(); jsonObject.append("temperingTaskType", temperingTaskType); webSocketUtils.sendToWeb("temperingIsRun", jsonObject); } @Scheduled(fixedDelay = 1000) public void largenScreen() { JSONObject jsonObject = new JSONObject(); //大屏钢化信息 Date startOfToday = new Date(LocalDate.now().atStartOfDay(ZoneId.systemDefault()).toInstant().toEpochMilli()); Integer putGlass = temperingGlassInfoService.count( new QueryWrapper() .eq("state", Const.TEMPERING_NEW) .gt("create_time", startOfToday) ); jsonObject.append("temperingTaskType", putGlass); //大屏钢化信息 List temperingGlassInfoList = temperingGlassInfoService.list( new QueryWrapper() .select("engineer_id", "tempering_layout_id") .eq("state", Const.TEMPERING_START) .gt("create_time", startOfToday) .groupBy("engineer_id", "tempering_layout_id") ); jsonObject.append("temperingGlassInfoList", temperingGlassInfoList.size()); //大屏钢化信息 List temperingGlassInfoInList = temperingGlassInfoService.list( new LambdaQueryWrapper() .select(TemperingGlassInfo::getEngineerId, TemperingGlassInfo::getTemperingLayoutId) // 选择要去重的字段 .eq(TemperingGlassInfo::getState, Const.TEMPERING_DROP) .groupBy(TemperingGlassInfo::getEngineerId, TemperingGlassInfo::getTemperingLayoutId) // 按 engineerId 和 temperingLayoutId 分组 ); jsonObject.append("temperingGlassInfoInList", temperingGlassInfoInList.size()); //钢化饼图数据 List pieChartVOS = temperingGlassInfoService.queryPieChart(); jsonObject.append("pieChartVOS", pieChartVOS); webSocketUtils.sendToWeb("largenScreen", jsonObject); } /** * fixedRate : 上一个调用开始后再次调用的延时(不用等待上一次调用完成) * fixedDelay : 上一个调用结束后再次调用的延时 */ private List selectWaitingGlassByOpc() { //获取等待进炉中的玻璃信息 try { S7DataGHTwo s7DataGHTwo = s7SerializerGHTwo.read(S7DataGHTwo.class); String engineerEntity = s7DataGHTwo.getF09EngineerId(); Integer temperingLayoutIdEntity = s7DataGHTwo.getF09TemperingLayoutId(); if (null == engineerEntity || null == temperingLayoutIdEntity) { engineerEntity = s7DataGHTwo.getF08EngineerId(); temperingLayoutIdEntity = s7DataGHTwo.getF08TemperingLayoutId(); if (null == engineerEntity || null == temperingLayoutIdEntity) { log.info("获取参数异常,结束本次任务"); return new ArrayList<>(); } } return temperingGlassInfoService.list(new LambdaQueryWrapper() .eq(TemperingGlassInfo::getEngineerId, engineerEntity) .eq(TemperingGlassInfo::getTemperingLayoutId, temperingLayoutIdEntity)); } catch (Exception e) { log.info("获取钢化参数异常:{}", e); return new ArrayList<>(); } } /** * 获取正在钢化的版图信息:获取3炉已经摆好的玻璃小片,排除正在摆的 * * @param list * @return */ private List selectIntoGlass(List list) { List temperingGlassInfoList = temperingGlassInfoService.queryEngineerAndLayoutId(); log.info("钢化中获取到的数据为{}", temperingGlassInfoList); if (CollectionUtil.isEmpty(temperingGlassInfoList)) { return new ArrayList<>(); } if (CollectionUtil.isEmpty(list)) { return temperingGlassInfoList; } else { String engineerId = list.get(0).getEngineerId(); int temperingLayoutId = list.get(0).getTemperingLayoutId(); List tempList = temperingGlassInfoList.stream().filter(e -> !(e.getEngineerId().equals(engineerId) && temperingLayoutId == e.getTemperingLayoutId())).collect(Collectors.toList()); return tempList; } } }