ApiServiceImpl.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. package com.ruoyi.biz.service.impl;
  2. import com.ruoyi.biz.domain.*;
  3. import com.ruoyi.biz.service.*;
  4. import com.ruoyi.biz.tools.Tools;
  5. import com.ruoyi.biz.vo.StopDetailVO;
  6. import com.ruoyi.common.constant.Constants;
  7. import com.ruoyi.common.utils.CacheUtils;
  8. import lombok.extern.slf4j.Slf4j;
  9. import org.springframework.core.env.Environment;
  10. import org.springframework.stereotype.Service;
  11. import javax.annotation.PostConstruct;
  12. import javax.annotation.Resource;
  13. import java.math.BigDecimal;
  14. import java.math.RoundingMode;
  15. import java.time.LocalDate;
  16. import java.time.LocalDateTime;
  17. import java.time.ZoneOffset;
  18. import java.util.*;
  19. import java.util.concurrent.ExecutionException;
  20. import java.util.concurrent.Future;
  21. import java.util.stream.Collectors;
  22. /**
  23. * 首页统计数据
  24. *
  25. * @author wukai
  26. * @date 2024/5/4 20:35
  27. */
  28. @Service
  29. @Slf4j
  30. public class ApiServiceImpl implements IApiService {
  31. @Resource
  32. private ITwinCalcDayService twinCalcDayService;
  33. @Resource
  34. private ITwinPanHeadInfoService panHeadInfoService;
  35. @Resource
  36. private ITwinDeviceService deviceService;
  37. @Resource
  38. private IIotService iotService;
  39. @Resource
  40. private AsyncServiceImpl asyncService;
  41. @Resource
  42. private ITaskService taskService;
  43. @Resource
  44. private Environment env;
  45. @Resource
  46. private ITwinFormulaInfoService twinFormulaInfoService;
  47. /**
  48. * 首页统计数据
  49. */
  50. @Override
  51. public void indexCalc() {
  52. List<WeekData> weekDataList = new ArrayList<>();
  53. IndexData indexData = new IndexData();
  54. IndexEfficiency efficiency = new IndexEfficiency();
  55. TwinCalcDay calcDay = twinCalcDayService.calcToday();
  56. /*
  57. 获取当天产量数据
  58. */
  59. efficiency.setAEfficiency(calcDay.getEfficiencyA().floatValue());
  60. efficiency.setBEfficiency(calcDay.getEfficiencyB().floatValue());
  61. efficiency.setTotalLength(calcDay.getLength().floatValue());
  62. efficiency.setALength(calcDay.getLengthA().floatValue());
  63. efficiency.setBLength(calcDay.getLengthB().floatValue());
  64. //界面上重新计算重量 2024-12-25 用总长度除以373
  65. BigDecimal tw = calcDay.getLength().divide(BigDecimal.valueOf(373), 2, BigDecimal.ROUND_HALF_UP);
  66. BigDecimal aw = calcDay.getLengthA().divide(BigDecimal.valueOf(373), 2, BigDecimal.ROUND_HALF_UP);
  67. BigDecimal bw = calcDay.getLengthB().divide(BigDecimal.valueOf(373), 2, BigDecimal.ROUND_HALF_UP);
  68. efficiency.setTotalWeight(tw.floatValue());
  69. efficiency.setAWeight(aw.floatValue());
  70. efficiency.setBWeight(bw.floatValue());
  71. efficiency.setATime(calcDay.getOpenTimeA().divide(BigDecimal.valueOf(3600), 2, BigDecimal.ROUND_HALF_UP).floatValue());
  72. efficiency.setBTime(calcDay.getOpenTimeB().divide(BigDecimal.valueOf(3600), 2, BigDecimal.ROUND_HALF_UP).floatValue());
  73. indexData.setEfficiency(efficiency);
  74. /*
  75. *获取前面7天的数据
  76. */
  77. LocalDateTime ldt = LocalDateTime.now();
  78. if (ldt.getHour() < 7) {
  79. ldt = ldt.minusDays(1);
  80. }
  81. LocalDate localDate = ldt.toLocalDate().minusDays(7);
  82. Date date = Date.from(localDate.atStartOfDay(ZoneOffset.of("+8")).toInstant());
  83. List<TwinCalcDay> list = twinCalcDayService.selectTwinCalcDayListByTime(date);
  84. Map<Date, List<TwinCalcDay>> dayGroup = list.stream().collect(Collectors.groupingBy(TwinCalcDay::getTime, LinkedHashMap::new, Collectors.toList()));
  85. for (Map.Entry<Date, List<TwinCalcDay>> entry : dayGroup.entrySet()) {
  86. TwinCalcDay day = new TwinCalcDay(entry.getKey());
  87. List<TwinCalcDay> days = entry.getValue();
  88. day.calcDays(days);
  89. WeekData weekData = new WeekData();
  90. weekData.convert(day);
  91. weekDataList.add(weekData);
  92. }
  93. indexData.setWeekData(weekDataList);
  94. IndexDevice device = new IndexDevice();
  95. device.setRunningRatio(calcDay.getRunningRatio());
  96. indexData.setDevice(device);
  97. CacheUtils.put(Constants.IOT_TOKEN, Constants.INDEX_CALC, indexData);
  98. }
  99. /**
  100. * 首页告警数据
  101. */
  102. @Override
  103. public void indexAlarms() {
  104. IndexData indexData = new IndexData();
  105. List<IndexAlarm> alarmList = new ArrayList<>();
  106. List<IndexPan> panList = new ArrayList<>();
  107. //配方统计数据
  108. List<FormulaTotal> formulaTotal = new ArrayList<>();
  109. //配方明细数据
  110. List<FormulaDetail> formulaDetail = new ArrayList<>();
  111. //配方记录
  112. List<TwinFormulaInfo> formulaInfos = new ArrayList<>();
  113. //送经量
  114. List<WarpRunIn> warpList = new ArrayList<>();
  115. //平方米克重统计数据
  116. List<GramMass> gramMasses = new ArrayList<>();
  117. //平方米克重明细数据
  118. List<GramMassDetail> gramMassDetails = new ArrayList<>();
  119. iotService.getToken();
  120. TwinDevice searchDevice = new TwinDevice();
  121. searchDevice.setOnline("1");
  122. List<TwinDevice> list = deviceService.selectTwinDeviceList(searchDevice);
  123. Map<String, Long> panMap = new HashMap<>(16);
  124. panHeadInfoService.selectTwinPanHeadInfoList(new TwinPanHeadInfo()).forEach(pan -> {
  125. String key = pan.getDeviceId() + "_" + pan.getPhNum();
  126. panMap.put(key, pan.getPhMax());
  127. });
  128. List<Future<Map<String, Object>>> futureList = new ArrayList<>();
  129. for (int i = 0; i < list.size(); i++) {
  130. TwinDevice twinDevice = list.get(i);
  131. futureList.add(asyncService.currData(twinDevice));
  132. }
  133. int stop1 = 0, stop2 = 0, stop6 = 0, stop8 = 0, onlineNum = 0, otherStop = 0, stopPan = 0;
  134. //停机明细
  135. List<StopDetailVO> stopList = new ArrayList<>();
  136. try {
  137. for (Future<Map<String, Object>> future : futureList) {
  138. Map<String, Object> map = future.get();
  139. TwinDevice device = (TwinDevice) map.get("device");
  140. StopDetailVO sdVO = new StopDetailVO();
  141. sdVO.setDeviceId(device.getDeviceId());
  142. sdVO.setDeviceName(device.getDeviceName());
  143. sdVO.setHasData(true);
  144. int total = (int) map.get("total");
  145. if (total == 0) {
  146. //可能会出现接口返回无数据的情况,需要设置设备状态为离线
  147. sdVO.setHasData(false);
  148. stopList.add(sdVO);
  149. otherStop++;
  150. continue;
  151. }
  152. float speed = (float) map.get("Capacity_data_1");
  153. int stopStatus = (int) map.get("Capacity_data_48");
  154. sdVO.setStop(stopStatus);
  155. sdVO.setSpeed(speed);
  156. stopList.add(sdVO);
  157. //处理配方明细数据
  158. FormulaDetail detail = new FormulaDetail(map);
  159. formulaDetail.add(detail);
  160. TwinFormulaInfo formulaInfo = new TwinFormulaInfo(detail);
  161. formulaInfos.add(formulaInfo);
  162. //处理送经量数据
  163. WarpRunIn warpRunIn = new WarpRunIn(map);
  164. warpList.add(warpRunIn);
  165. //处理平方米克重明细数据
  166. GramMassDetail gramMassDetail = new GramMassDetail(map);
  167. gramMassDetails.add(gramMassDetail);
  168. int data4 = (int) map.get("Capacity_data_4");
  169. if (data4 == 0) {
  170. //Capacity_data_4,如果设定落布米数为0,则证明当前数据无效
  171. //不能跟上面判断合并,不然报空指针
  172. continue;
  173. }
  174. for (int i = 0; i < Tools.ALLOW_ALARM.length; i++) {
  175. //允许的告警编号
  176. boolean flag = (boolean) map.get("Alarm_unit_" + Tools.ALLOW_ALARM[i]);
  177. if (flag) {
  178. IndexAlarm indexAlarm = new IndexAlarm();
  179. indexAlarm.setCode(device.getDeviceCode());
  180. indexAlarm.setName(device.getDeviceName());
  181. indexAlarm.setType(i);
  182. alarmList.add(indexAlarm);
  183. }
  184. }
  185. int alarm27 = (int) map.get("Alarm_unit_27");
  186. IndexAlarm indexAlarm;
  187. if (alarm27 != 0) {
  188. indexAlarm = new IndexAlarm();
  189. indexAlarm.setCode(device.getDeviceCode());
  190. indexAlarm.setName(device.getDeviceName());
  191. indexAlarm.setType(27);
  192. alarmList.add(indexAlarm);
  193. }
  194. indexAlarm = new IndexAlarm();
  195. indexAlarm.setCode(device.getDeviceCode());
  196. indexAlarm.setName(device.getDeviceName());
  197. int alarmType = 0;
  198. if (speed > 0f || stopStatus == 0) {
  199. onlineNum++;
  200. } else {
  201. switch (stopStatus) {
  202. case 1:
  203. stop1++;
  204. alarmType = 10001;
  205. break;
  206. case 2:
  207. stop2++;
  208. alarmType = 10002;
  209. break;
  210. case 6:
  211. stop6++;
  212. alarmType = 10006;
  213. break;
  214. case 7:
  215. //7-盘头剩余圈数达到停机,表示停机叫料
  216. stop8++;
  217. alarmType = 10008;
  218. break;
  219. default:
  220. otherStop++;
  221. }
  222. }
  223. if (alarmType != 0) {
  224. indexAlarm.setType(alarmType);
  225. alarmList.add(indexAlarm);
  226. }
  227. int[] curr = new int[5];
  228. int[] max = new int[5];
  229. float[] panPercent = new float[5];
  230. boolean panFlag = false;
  231. for (int i = 0; i < curr.length; i++) {
  232. int pos = 15 + i;
  233. curr[i] = (int) map.get("Capacity_data_" + pos);
  234. if (curr[i] < 200) {
  235. //盘头小于200圈
  236. panFlag = true;
  237. }
  238. String key = device.getDeviceId() + "_" + (i + 1);
  239. max[i] = 15000;
  240. if (panMap.get(key) != null) {
  241. max[i] = Math.toIntExact(panMap.get(key));
  242. }
  243. panPercent[i] = BigDecimal.valueOf(curr[i] * 100).divide(BigDecimal.valueOf(max[i]), 2, RoundingMode.HALF_UP).floatValue();
  244. if (panPercent[i] > 100) {
  245. panPercent[i] = 100;
  246. }
  247. }
  248. if (panFlag) {
  249. stopPan++;
  250. }
  251. IndexPan pan = new IndexPan();
  252. pan.setCode(device.getDeviceCode());
  253. pan.setName(device.getDeviceName());
  254. pan.setPanPercent(panPercent);
  255. panList.add(pan);
  256. }
  257. } catch (InterruptedException e) {
  258. throw new RuntimeException(e);
  259. } catch (ExecutionException e) {
  260. throw new RuntimeException(e);
  261. }
  262. twinFormulaInfoService.update(formulaInfos);
  263. indexData.setAlarm(alarmList);
  264. indexData.setPan(panList);
  265. IndexDevice device = new IndexDevice();
  266. device.setTotal(list.size());
  267. device.setOnline(onlineNum);
  268. device.setStop1(stop1);
  269. device.setStop2(stop2);
  270. device.setStop6(stop6);
  271. device.setStop8(stop8);
  272. device.setStopPan(stopPan);
  273. // 2024-11-18 告警数改为其他停机数
  274. // device.setAlarm(alarmList.size());
  275. device.setAlarm(otherStop);
  276. indexData.setDevice(device);
  277. CacheUtils.put(Constants.IOT_TOKEN, Constants.INDEX_ALARM, indexData);
  278. Map<Float, Long> temp = formulaDetail.stream().collect(Collectors.groupingBy(FormulaDetail::getFormula_data_15, Collectors.counting()));
  279. for (Float v : temp.keySet()) {
  280. FormulaTotal total = new FormulaTotal();
  281. total.setHeight(v);
  282. int num = Math.toIntExact(temp.get(v));
  283. total.setNum(num);
  284. float percent = BigDecimal.valueOf(num).divide(BigDecimal.valueOf(list.size()), 2, RoundingMode.HALF_UP).floatValue();
  285. total.setPercent(percent);
  286. formulaTotal.add(total);
  287. }
  288. temp = gramMassDetails.stream().collect(Collectors.groupingBy(GramMassDetail::getGramMass, Collectors.counting()));
  289. for (Float v : temp.keySet()) {
  290. GramMass total = new GramMass();
  291. total.setGramMass(v);
  292. int num = Math.toIntExact(temp.get(v));
  293. total.setNum(num);
  294. float percent = BigDecimal.valueOf(num).divide(BigDecimal.valueOf(list.size()), 2, RoundingMode.HALF_UP).floatValue();
  295. total.setPercent(percent);
  296. gramMasses.add(total);
  297. }
  298. CacheUtils.put(Constants.IOT_TOKEN, Constants.INDEX_FORMULA_TOTAL, formulaTotal);
  299. CacheUtils.put(Constants.IOT_TOKEN, Constants.INDEX_FORMULA_DETAIL, formulaDetail);
  300. CacheUtils.put(Constants.IOT_TOKEN, Constants.INDEX_WARP_RUN_IN, warpList);
  301. CacheUtils.put(Constants.IOT_TOKEN, Constants.INDEX_GRAM_MASS, gramMasses);
  302. CacheUtils.put(Constants.IOT_TOKEN, Constants.INDEX_GRAM_MASS_DETAIL, gramMassDetails);
  303. CacheUtils.put(Constants.IOT_TOKEN, Constants.STOP_DETAIL, stopList);
  304. }
  305. @PostConstruct
  306. public void init() {
  307. String flag = "false";
  308. String bl = env.getProperty("data.bl");
  309. if (bl != null && flag.equals(bl)) {
  310. return;
  311. }
  312. taskService.calc2Curr();
  313. indexCalc();
  314. indexAlarms();
  315. }
  316. }