ApiServiceImpl.java 12 KB

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