123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339 |
- package com.ruoyi.biz.service.impl;
- import com.ruoyi.biz.domain.*;
- import com.ruoyi.biz.service.*;
- import com.ruoyi.biz.tools.Tools;
- import com.ruoyi.biz.vo.StopDetailVO;
- import com.ruoyi.common.constant.Constants;
- import com.ruoyi.common.utils.CacheUtils;
- 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 ITwinPanHeadInfoService panHeadInfoService;
- @Resource
- private ITwinDeviceService deviceService;
- @Resource
- private IIotService iotService;
- @Resource
- private AsyncServiceImpl asyncService;
- @Resource
- private ITaskService taskService;
- @Resource
- private Environment env;
- @Resource
- private ITwinFormulaInfoService twinFormulaInfoService;
- /**
- * 首页统计数据
- */
- @Override
- public void indexCalc() {
- List<WeekData> 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());
- //界面上重新计算重量 2024-12-25 用总长度除以373
- BigDecimal tw = calcDay.getLength().divide(BigDecimal.valueOf(373), 2, BigDecimal.ROUND_HALF_UP);
- BigDecimal aw = calcDay.getLengthA().divide(BigDecimal.valueOf(373), 2, BigDecimal.ROUND_HALF_UP);
- BigDecimal bw = calcDay.getLengthB().divide(BigDecimal.valueOf(373), 2, BigDecimal.ROUND_HALF_UP);
- efficiency.setTotalWeight(tw.floatValue());
- efficiency.setAWeight(aw.floatValue());
- efficiency.setBWeight(bw.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<TwinCalcDay> list = twinCalcDayService.selectTwinCalcDayListByTime(date);
- Map<Date, List<TwinCalcDay>> dayGroup = list.stream().collect(Collectors.groupingBy(TwinCalcDay::getTime, LinkedHashMap::new, Collectors.toList()));
- for (Map.Entry<Date, List<TwinCalcDay>> entry : dayGroup.entrySet()) {
- TwinCalcDay day = new TwinCalcDay(entry.getKey());
- List<TwinCalcDay> days = entry.getValue();
- day.calcDays(days);
- WeekData weekData = new WeekData();
- weekData.convert(day);
- weekDataList.add(weekData);
- }
- indexData.setWeekData(weekDataList);
- IndexDevice device = new IndexDevice();
- device.setRunningRatio(calcDay.getRunningRatio());
- indexData.setDevice(device);
- CacheUtils.put(Constants.IOT_TOKEN, Constants.INDEX_CALC, indexData);
- }
- /**
- * 首页告警数据
- */
- @Override
- public void indexAlarms() {
- IndexData indexData = new IndexData();
- List<IndexAlarm> alarmList = new ArrayList<>();
- List<IndexPan> panList = new ArrayList<>();
- //配方统计数据
- List<FormulaTotal> formulaTotal = new ArrayList<>();
- //配方明细数据
- List<FormulaDetail> formulaDetail = new ArrayList<>();
- //配方记录
- List<TwinFormulaInfo> formulaInfos = new ArrayList<>();
- //送经量
- List<WarpRunIn> warpList = new ArrayList<>();
- //平方米克重统计数据
- List<GramMass> gramMasses = new ArrayList<>();
- //平方米克重明细数据
- List<GramMassDetail> gramMassDetails = new ArrayList<>();
- iotService.getToken();
- TwinDevice searchDevice = new TwinDevice();
- searchDevice.setOnline("1");
- List<TwinDevice> list = deviceService.selectTwinDeviceList(searchDevice);
- Map<String, Long> panMap = new HashMap<>(16);
- panHeadInfoService.selectTwinPanHeadInfoList(new TwinPanHeadInfo()).forEach(pan -> {
- String key = pan.getDeviceId() + "_" + pan.getPhNum();
- panMap.put(key, pan.getPhMax());
- });
- List<Future<Map<String, Object>>> futureList = new ArrayList<>();
- for (int i = 0; i < list.size(); i++) {
- TwinDevice twinDevice = list.get(i);
- futureList.add(asyncService.currData(twinDevice));
- }
- int stop1 = 0, stop2 = 0, stop6 = 0, stop8 = 0, onlineNum = 0, otherStop = 0, stopPan = 0;
- //停机明细
- List<StopDetailVO> stopList = new ArrayList<>();
- try {
- for (Future<Map<String, Object>> future : futureList) {
- Map<String, Object> map = future.get();
- TwinDevice device = (TwinDevice) map.get("device");
- StopDetailVO sdVO = new StopDetailVO();
- sdVO.setDeviceId(device.getDeviceId());
- sdVO.setDeviceName(device.getDeviceName());
- sdVO.setHasData(true);
- int total = (int) map.get("total");
- if (total == 0) {
- //可能会出现接口返回无数据的情况,需要设置设备状态为离线
- sdVO.setHasData(false);
- stopList.add(sdVO);
- otherStop++;
- continue;
- }
- float speed = (float) map.get("Capacity_data_1");
- int stopStatus = (int) map.get("Capacity_data_48");
- sdVO.setStop(stopStatus);
- sdVO.setSpeed(speed);
- stopList.add(sdVO);
- //处理配方明细数据
- FormulaDetail detail = new FormulaDetail(map);
- formulaDetail.add(detail);
- TwinFormulaInfo formulaInfo = new TwinFormulaInfo(detail);
- formulaInfos.add(formulaInfo);
- //处理送经量数据
- 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 = 0; i < Tools.ALLOW_ALARM.length; i++) {
- //允许的告警编号
- boolean flag = (boolean) map.get("Alarm_unit_" + Tools.ALLOW_ALARM[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);
- }
- indexAlarm = new IndexAlarm();
- indexAlarm.setCode(device.getDeviceCode());
- indexAlarm.setName(device.getDeviceName());
- int alarmType = 0;
- if (speed > 0f || stopStatus == 0) {
- onlineNum++;
- } else {
- 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;
- break;
- default:
- otherStop++;
- }
- }
- if (alarmType != 0) {
- indexAlarm.setType(alarmType);
- alarmList.add(indexAlarm);
- }
- int[] curr = new int[5];
- int[] max = new int[5];
- float[] panPercent = new float[5];
- boolean panFlag = false;
- for (int i = 0; i < curr.length; i++) {
- int pos = 15 + i;
- curr[i] = (int) map.get("Capacity_data_" + pos);
- if (curr[i] < 200) {
- //盘头小于200圈
- panFlag = true;
- }
- 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;
- }
- }
- if (panFlag) {
- stopPan++;
- }
- 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);
- }
- twinFormulaInfoService.update(formulaInfos);
- indexData.setAlarm(alarmList);
- indexData.setPan(panList);
- IndexDevice device = new IndexDevice();
- device.setTotal(list.size());
- device.setOnline(onlineNum);
- device.setStop1(stop1);
- device.setStop2(stop2);
- device.setStop6(stop6);
- device.setStop8(stop8);
- device.setStopPan(stopPan);
- // 2024-11-18 告警数改为其他停机数
- // device.setAlarm(alarmList.size());
- device.setAlarm(otherStop);
- indexData.setDevice(device);
- CacheUtils.put(Constants.IOT_TOKEN, Constants.INDEX_ALARM, indexData);
- Map<Float, Long> 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);
- CacheUtils.put(Constants.IOT_TOKEN, Constants.STOP_DETAIL, stopList);
- }
- @PostConstruct
- public void init() {
- String flag = "false";
- String bl = env.getProperty("data.bl");
- if (bl != null && flag.equals(bl)) {
- return;
- }
- taskService.calc2Curr();
- indexCalc();
- indexAlarms();
- }
- }
|