package com.ruoyi.biz.service.impl; import com.ruoyi.biz.domain.*; import com.ruoyi.biz.service.*; import com.ruoyi.common.constant.Constants; import com.ruoyi.common.utils.CacheUtils; import com.ruoyi.system.service.ISysConfigService; import lombok.extern.slf4j.Slf4j; import org.springframework.core.env.Environment; import org.springframework.stereotype.Service; import javax.annotation.PostConstruct; import javax.annotation.Resource; import java.math.BigDecimal; import java.math.RoundingMode; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.ZoneOffset; import java.util.*; import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; import java.util.stream.Collectors; /** * 首页统计数据 * * @author wukai * @date 2024/5/4 20:35 */ @Service @Slf4j public class ApiServiceImpl implements IApiService { @Resource private ITwinCalcDayService twinCalcDayService; @Resource private ITwinCalc2hrService calc2hrService; @Resource private ITwinPanHeadInfoService panHeadInfoService; @Resource private ISysConfigService configService; @Resource private ITwinDeviceService deviceService; @Resource private IIotService iotService; @Resource private AsyncServiceImpl asyncService; @Resource private ITaskService taskService; @Resource private Environment env; /** * 首页统计数据 */ @Override public void indexCalc() { List weekDataList = new ArrayList<>(); IndexData indexData = new IndexData(); IndexEfficiency efficiency = new IndexEfficiency(); TwinCalcDay calcDay = twinCalcDayService.calcToday(); /* 获取当天产量数据 */ efficiency.setAEfficiency(calcDay.getEfficiencyA().floatValue()); efficiency.setBEfficiency(calcDay.getEfficiencyB().floatValue()); efficiency.setTotalLength(calcDay.getLength().floatValue()); efficiency.setALength(calcDay.getLengthA().floatValue()); efficiency.setBLength(calcDay.getLengthB().floatValue()); efficiency.setTotalWeight(calcDay.getWeight().floatValue()); efficiency.setAWeight(calcDay.getWeightA().floatValue()); efficiency.setBWeight(calcDay.getWeightB().floatValue()); efficiency.setATime(calcDay.getOpenTimeA().divide(BigDecimal.valueOf(3600), 2, BigDecimal.ROUND_HALF_UP).floatValue()); efficiency.setBTime(calcDay.getOpenTimeB().divide(BigDecimal.valueOf(3600), 2, BigDecimal.ROUND_HALF_UP).floatValue()); indexData.setEfficiency(efficiency); /* *获取前面7天的数据 */ LocalDateTime ldt = LocalDateTime.now(); if (ldt.getHour() < 7) { ldt = ldt.minusDays(1); } LocalDate localDate = ldt.toLocalDate().minusDays(7); Date date = Date.from(localDate.atStartOfDay(ZoneOffset.of("+8")).toInstant()); List list = twinCalcDayService.selectTwinCalcDayListByTime(date); Map> dayGroup = list.stream().collect(Collectors.groupingBy(TwinCalcDay::getTime, LinkedHashMap::new, Collectors.toList())); for (Map.Entry> entry : dayGroup.entrySet()) { TwinCalcDay day = new TwinCalcDay(entry.getKey()); List days = entry.getValue(); day.calcDays(days); WeekData weekData = new WeekData(); weekData.convert(day); weekDataList.add(weekData); } indexData.setWeekData(weekDataList); CacheUtils.put(Constants.IOT_TOKEN, Constants.INDEX_CALC, indexData); } /** * 首页告警数据 */ @Override public void indexAlarms() { IndexData indexData = new IndexData(); List alarmList = new ArrayList<>(); List panList = new ArrayList<>(); //配方统计数据 List formulaTotal = new ArrayList<>(); //配方明细数据 List formulaDetail = new ArrayList<>(); //送经量 List warpList = new ArrayList<>(); //平方米克重统计数据 List gramMasses = new ArrayList<>(); //平方米克重明细数据 List gramMassDetails = new ArrayList<>(); int stop1 = 0, stop2 = 0, stop6 = 0, stop8 = 0; iotService.getToken(); TwinDevice searchDevice = new TwinDevice(); searchDevice.setOnline("1"); List list = deviceService.selectTwinDeviceList(searchDevice); Map panMap = new HashMap<>(16); panHeadInfoService.selectTwinPanHeadInfoList(new TwinPanHeadInfo()).forEach(pan -> { String key = pan.getDeviceId() + "_" + pan.getPhNum(); panMap.put(key, pan.getPhMax()); }); List>> futureList = new ArrayList<>(); for (int i = 0; i < list.size(); i++) { TwinDevice twinDevice = list.get(i); futureList.add(asyncService.currData(twinDevice)); } try { for (Future> future : futureList) { Map map = future.get(); TwinDevice device = (TwinDevice) map.get("device"); int total = (int) map.get("total"); if (total == 0) { //可能会出现接口返回无数据的情况,需要设置设备状态为离线 continue; } //处理配方明细数据 FormulaDetail detail = new FormulaDetail(map); formulaDetail.add(detail); //处理送经量数据 WarpRunIn warpRunIn = new WarpRunIn(map); warpList.add(warpRunIn); //处理平方米克重明细数据 GramMassDetail gramMassDetail = new GramMassDetail(map); gramMassDetails.add(gramMassDetail); int data4 = (int) map.get("Capacity_data_4"); if (data4 == 0) { //Capacity_data_4,如果设定落布米数为0,则证明当前数据无效 //不能跟上面判断合并,不然报空指针 continue; } for (int i = 1; i <= 26; i++) { if (i != 9 && i != 25 && i != 24) { //去掉电源故障和卷曲伺服故障和牵引伺服故障 boolean flag = (boolean) map.get("Alarm_unit_" + i); if (flag) { IndexAlarm indexAlarm = new IndexAlarm(); indexAlarm.setCode(device.getDeviceCode()); indexAlarm.setName(device.getDeviceName()); indexAlarm.setType(i); alarmList.add(indexAlarm); } } } int alarm27 = (int) map.get("Alarm_unit_27"); IndexAlarm indexAlarm; if (alarm27 != 0) { indexAlarm = new IndexAlarm(); indexAlarm.setCode(device.getDeviceCode()); indexAlarm.setName(device.getDeviceName()); indexAlarm.setType(27); alarmList.add(indexAlarm); } int stopStatus = (int) map.get("Capacity_data_48"); indexAlarm = new IndexAlarm(); indexAlarm.setCode(device.getDeviceCode()); indexAlarm.setName(device.getDeviceName()); int alarmType = 0; switch (stopStatus) { case 1: stop1++; alarmType = 10001; break; case 2: stop2++; alarmType = 10002; break; case 6: stop6++; alarmType = 10006; break; case 7: //7-盘头剩余圈数达到停机,表示停机叫料 stop8++; alarmType = 10008; default: } if (alarmType != 0) { indexAlarm.setType(alarmType); alarmList.add(indexAlarm); } int[] curr = new int[5]; int[] max = new int[5]; float[] panPercent = new float[5]; for (int i = 0; i < curr.length; i++) { int pos = 15 + i; curr[i] = (int) map.get("Capacity_data_" + pos); String key = device.getDeviceId() + "_" + (i + 1); max[i] = 15000; if (panMap.get(key) != null) { max[i] = Math.toIntExact(panMap.get(key)); } panPercent[i] = BigDecimal.valueOf(curr[i] * 100).divide(BigDecimal.valueOf(max[i]), 2, RoundingMode.HALF_UP).floatValue(); if (panPercent[i] > 100) { panPercent[i] = 100; } } IndexPan pan = new IndexPan(); pan.setCode(device.getDeviceCode()); pan.setName(device.getDeviceName()); pan.setPanPercent(panPercent); panList.add(pan); } } catch (InterruptedException e) { throw new RuntimeException(e); } catch (ExecutionException e) { throw new RuntimeException(e); } indexData.setAlarm(alarmList); indexData.setPan(panList); IndexDevice device = new IndexDevice(); device.setTotal(Integer.parseInt(configService.selectConfigByKey("sys.device.total"))); device.setOnline(list.size()); device.setStop1(stop1); device.setStop2(stop2); device.setStop6(stop6); device.setStop8(stop8); device.setAlarm(alarmList.size()); indexData.setDevice(device); CacheUtils.put(Constants.IOT_TOKEN, Constants.INDEX_ALARM, indexData); Map temp = formulaDetail.stream().collect(Collectors.groupingBy(FormulaDetail::getFormula_data_15, Collectors.counting())); for (Float v : temp.keySet()) { FormulaTotal total = new FormulaTotal(); total.setHeight(v); int num = Math.toIntExact(temp.get(v)); total.setNum(num); float percent = BigDecimal.valueOf(num).divide(BigDecimal.valueOf(list.size()), 2, RoundingMode.HALF_UP).floatValue(); total.setPercent(percent); formulaTotal.add(total); } temp = gramMassDetails.stream().collect(Collectors.groupingBy(GramMassDetail::getGramMass, Collectors.counting())); for (Float v : temp.keySet()) { GramMass total = new GramMass(); total.setGramMass(v); int num = Math.toIntExact(temp.get(v)); total.setNum(num); float percent = BigDecimal.valueOf(num).divide(BigDecimal.valueOf(list.size()), 2, RoundingMode.HALF_UP).floatValue(); total.setPercent(percent); gramMasses.add(total); } CacheUtils.put(Constants.IOT_TOKEN, Constants.INDEX_FORMULA_TOTAL, formulaTotal); CacheUtils.put(Constants.IOT_TOKEN, Constants.INDEX_FORMULA_DETAIL, formulaDetail); CacheUtils.put(Constants.IOT_TOKEN, Constants.INDEX_WARP_RUN_IN, warpList); CacheUtils.put(Constants.IOT_TOKEN, Constants.INDEX_GRAM_MASS, gramMasses); CacheUtils.put(Constants.IOT_TOKEN, Constants.INDEX_GRAM_MASS_DETAIL, gramMassDetails); } @PostConstruct public void init() { String flag = "false"; String bl = env.getProperty("data.bl"); if (bl != null && flag.equals(bl)) { return; } taskService.calc2Curr(); indexCalc(); indexAlarms(); } }