ApiYrServiceImpl.java 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. package com.jjt.biz.service.impl;
  2. import com.alibaba.fastjson2.JSON;
  3. import com.jjt.biz.service.IApiYrService;
  4. import com.jjt.biz.vo.*;
  5. import com.jjt.calc.domain.TwinCalcDayYhj;
  6. import com.jjt.calc.domain.TwinCalcHourYhj;
  7. import com.jjt.calc.service.ITwinCalcDayYhjService;
  8. import com.jjt.calc.service.ITwinCalcHourYhjService;
  9. import com.jjt.common.constant.CacheConstants;
  10. import com.jjt.common.core.redis.RedisCache;
  11. import com.jjt.common.utils.DateUtils;
  12. import com.jjt.rz.domain.TwinCalcHourRz;
  13. import com.jjt.rz.service.ITwinCalcHourRzService;
  14. import com.jjt.rz.service.ITwinDeviceRzService;
  15. import com.jjt.rz.service.ITwinDeviceTypeService;
  16. import com.jjt.rz.vo.CompareVO;
  17. import com.jjt.ws.domain.TwinRzCalcMonth;
  18. import com.jjt.ws.service.ITwinRzCalcMonthService;
  19. import lombok.extern.slf4j.Slf4j;
  20. import org.springframework.stereotype.Service;
  21. import javax.annotation.Resource;
  22. import java.math.BigDecimal;
  23. import java.math.RoundingMode;
  24. import java.time.LocalDate;
  25. import java.time.LocalDateTime;
  26. import java.time.ZoneOffset;
  27. import java.util.*;
  28. import java.util.concurrent.atomic.AtomicInteger;
  29. import java.util.concurrent.atomic.AtomicReference;
  30. import java.util.stream.Collectors;
  31. /**
  32. * 首页统计数据
  33. *
  34. * @author wukai
  35. * @date 2024/5/4 20:35
  36. */
  37. @Service
  38. @Slf4j
  39. public class ApiYrServiceImpl implements IApiYrService {
  40. @Resource
  41. private RedisCache redisCache;
  42. @Resource
  43. private ITwinRzCalcMonthService rzCalcMonthService;
  44. @Resource
  45. private ITwinCalcDayYhjService calcDayYhjService;
  46. @Resource
  47. private ITwinDeviceRzService rzService;
  48. @Resource
  49. private ITwinCalcHourRzService hourRzService;
  50. @Resource
  51. private ITwinDeviceTypeService typeService;
  52. /**
  53. * 获取产线状态
  54. *
  55. * @return 结果
  56. */
  57. @Override
  58. public YrProdLineStatusVO status() {
  59. YrProdLineStatusVO vo = new YrProdLineStatusVO();
  60. vo.mock();
  61. return vo;
  62. }
  63. /**
  64. * 获取生产效率
  65. *
  66. * @return 结果
  67. */
  68. @Override
  69. public List<YrProdEfficiencyVO> eff() {
  70. List<YrProdEfficiencyVO> list = new ArrayList<>();
  71. YrProdEfficiencyVO[] arr = new YrProdEfficiencyVO[9];
  72. for (int i = 0; i < 9; i++) {
  73. YrProdEfficiencyVO vo = new YrProdEfficiencyVO(i);
  74. vo.mock();
  75. arr[i] = vo;
  76. }
  77. YrProdEfficiencyVO line0 = new YrProdEfficiencyVO(0);
  78. // for (int i = 1; i < 9; i++) {
  79. // YrProdEfficiencyVO vo = arr[i];
  80. // line0.setEffA(line0.getEffA().add(vo.getEffA()));
  81. // line0.setEffB(line0.getEffB().add(vo.getEffB()));
  82. // line0.setTimeA(line0.getTimeA().add(vo.getTimeA()));
  83. // line0.setTimeB(line0.getTimeB().add(vo.getTimeB()));
  84. // line0.setLengthA(line0.getLengthA().add(vo.getLengthA()));
  85. // line0.setLengthB(line0.getLengthB().add(vo.getLengthB()));
  86. // line0.setWeightA(line0.getWeightA().add(vo.getWeightA()));
  87. // line0.setWeightB(line0.getWeightB().add(vo.getWeightB()));
  88. // line0.setWeight(line0.getWeight().add(vo.getWeight()));
  89. // line0.setLength(line0.getLength().add(vo.getLength()));
  90. //
  91. // }
  92. List<TwinCalcHourYhj> calcList = redisCache.getCacheObject(CacheConstants.YHJ_TODAY);
  93. Map<Integer, Map<String, Integer>> sumByDeviceAndTeam = calcList.stream()
  94. .collect(Collectors.groupingBy(
  95. TwinCalcHourYhj::getDeviceId,
  96. Collectors.groupingBy(
  97. TwinCalcHourYhj::getTeam,
  98. Collectors.summingInt(TwinCalcHourYhj::getLength)
  99. )
  100. ));
  101. AtomicInteger openTimes = new AtomicInteger(0);
  102. sumByDeviceAndTeam.forEach((deviceId, teamMap) -> {
  103. Integer lengthA = teamMap.get("A");
  104. Integer lengthB = teamMap.get("B");
  105. if (lengthB == null) {
  106. lengthB = 0;
  107. }
  108. Integer length = lengthA + lengthB;
  109. YrProdEfficiencyVO vo = arr[deviceId];
  110. if (length > 0) {
  111. openTimes.incrementAndGet();
  112. } else {
  113. vo.setOpen(0);
  114. vo.setRatio(BigDecimal.ZERO);
  115. vo.setTimeA(BigDecimal.ZERO);
  116. vo.setTimeB(BigDecimal.ZERO);
  117. vo.setEffA(BigDecimal.ZERO);
  118. vo.setEffB(BigDecimal.ZERO);
  119. }
  120. BigDecimal weightA = BigDecimal.valueOf(lengthA).divide(BigDecimal.valueOf(1000), 2, RoundingMode.HALF_UP);
  121. BigDecimal weightB = BigDecimal.valueOf(lengthB).divide(BigDecimal.valueOf(1000), 2, RoundingMode.HALF_UP);
  122. BigDecimal weight = BigDecimal.valueOf(length).divide(BigDecimal.valueOf(1000), 2, RoundingMode.HALF_UP);
  123. vo.setLengthA(BigDecimal.valueOf(lengthA));
  124. vo.setLengthB(BigDecimal.valueOf(lengthB));
  125. vo.setWeightA(weightA);
  126. vo.setWeightB(weightB);
  127. vo.setWeight(weight);
  128. vo.setLength(BigDecimal.valueOf(length));
  129. line0.setEffA(line0.getEffA().add(vo.getEffA()));
  130. line0.setEffB(line0.getEffB().add(vo.getEffB()));
  131. line0.setTimeA(line0.getTimeA().add(vo.getTimeA()));
  132. line0.setTimeB(line0.getTimeB().add(vo.getTimeB()));
  133. line0.setLengthA(line0.getLengthA().add(vo.getLengthA()));
  134. line0.setLengthB(line0.getLengthB().add(vo.getLengthB()));
  135. line0.setWeightA(line0.getWeightA().add(vo.getWeightA()));
  136. line0.setWeightB(line0.getWeightB().add(vo.getWeightB()));
  137. line0.setWeight(line0.getWeight().add(vo.getWeight()));
  138. line0.setLength(line0.getLength().add(vo.getLength()));
  139. });
  140. line0.setEffA(line0.getEffA().divide(BigDecimal.valueOf(8), 2, RoundingMode.HALF_UP));
  141. line0.setEffB(line0.getEffB().divide(BigDecimal.valueOf(8), 2, RoundingMode.HALF_UP));
  142. list.add(line0);
  143. for (int i = 1; i < 9; i++) {
  144. YrProdEfficiencyVO vo = arr[i];
  145. list.add(vo);
  146. }
  147. return list;
  148. }
  149. /**
  150. * 获取生产趋势
  151. *
  152. * @return 结果
  153. */
  154. @Override
  155. public List<YrProdTradeVO> prodTrade() {
  156. return mockProd();
  157. }
  158. /**
  159. * 获取能耗趋势
  160. *
  161. * @return 结果
  162. */
  163. @Override
  164. public List<YrEnergyTradeVO> energyTrade() {
  165. return mockEnergy();
  166. }
  167. /**
  168. * 获取能耗趋势
  169. *
  170. * @return 结果
  171. */
  172. public List<YrEnergyTradeVO> energyTrade(List<TwinRzCalcMonth> list) {
  173. List<YrEnergyTradeVO> result = new ArrayList<>();
  174. for (TwinRzCalcMonth cm : list) {
  175. YrEnergyTradeVO vo = new YrEnergyTradeVO();
  176. LocalDate ldt = DateUtils.toLocalDate(cm.getDataDate());
  177. vo.setDate(ldt.getDayOfMonth() + "");
  178. vo.setTips(ldt.toString());
  179. vo.split(cm.getLengthPrice(), cm.getDUse(), cm.getQLowUse(), cm.getSUse());
  180. result.add(vo);
  181. }
  182. return result;
  183. }
  184. /**
  185. * 获取生产趋势
  186. *
  187. * @return 结果
  188. */
  189. public List<YrProdTradeVO> prodTrade(List<TwinRzCalcMonth> list, List<TwinCalcDayYhj> yhjList) {
  190. Map<Date, List<TwinCalcDayYhj>> yhjMap = yhjList.stream().collect(Collectors.groupingBy(TwinCalcDayYhj::getTime));
  191. List<YrProdTradeVO> result = new ArrayList<>();
  192. for (TwinRzCalcMonth cm : list) {
  193. YrProdTradeVO vo = new YrProdTradeVO();
  194. LocalDate ldt = DateUtils.toLocalDate(cm.getDataDate());
  195. List<TwinCalcDayYhj> yhjs = yhjMap.get(cm.getDataDate());
  196. vo.setDate(ldt.getDayOfMonth() + "");
  197. vo.setTips(ldt.toString());
  198. vo.split(yhjs, cm.getLength(), cm.getWeight());
  199. result.add(vo);
  200. }
  201. return result;
  202. }
  203. /**
  204. * 模拟数据定时任务
  205. */
  206. @Override
  207. public void mock() {
  208. YrTwinVO vo = new YrTwinVO();
  209. YrProdLineStatusVO status = status();
  210. vo.setStatus(status);
  211. List<YrProdEfficiencyVO> effList = eff();
  212. int open = 0;
  213. for (YrProdEfficiencyVO eff : effList) {
  214. if (eff.getLine() == 0) {
  215. status.setWeight(eff.getWeight().setScale(0, RoundingMode.HALF_UP));
  216. status.setLength(eff.getLength().setScale(0, RoundingMode.HALF_UP));
  217. Random random = new Random();
  218. BigDecimal baiPi = eff.getLength().multiply(BigDecimal.valueOf(0.5 + random.nextDouble())).setScale(0, RoundingMode.HALF_UP);
  219. status.setBaiPei(baiPi);
  220. } else {
  221. if (eff.getLength().compareTo(BigDecimal.ZERO) > 0) {
  222. open++;
  223. }
  224. }
  225. }
  226. status.setOpenProd(open);
  227. //白坯投放量处理--start
  228. Map<String, Object> result = redisCache.getCacheObject(CacheConstants.VMS_STOCK);
  229. Map<String, Long> lineMap = (Map<String, Long>) result.get("out-rz-line");
  230. lineMap.remove("@type"); // 手动移除多余字段
  231. AtomicReference<Long> baiPei = new AtomicReference<>(0L);
  232. lineMap.forEach((key, value) -> {
  233. baiPei.updateAndGet(v -> v + value);
  234. });
  235. status.setBaiPei(BigDecimal.valueOf(baiPei.get()));
  236. //白坯投放量处理--end
  237. vo.setEff(effList);
  238. TwinRzCalcMonth calcMonth = new TwinRzCalcMonth();
  239. Map<String, Object> params = new HashMap<>(16);
  240. params.put("start", LocalDate.now().minusDays(30).toString());
  241. calcMonth.setParams(params);
  242. List<TwinRzCalcMonth> list = rzCalcMonthService.selectTwinRzCalcMonthList(calcMonth);
  243. TwinCalcDayYhj yhj = new TwinCalcDayYhj();
  244. yhj.setParams(params);
  245. List<TwinCalcDayYhj> yhjList = calcDayYhjService.selectTwinCalcDayYhjList(yhj);
  246. calcMonth.setParams(params);
  247. vo.setProdTrade(prodTrade(list, yhjList));
  248. vo.setEnergyTrade(energyTrade(list));
  249. redisCache.setCacheObject(CacheConstants.RZ_MOCK, vo);
  250. }
  251. private List<YrProdTradeVO> mockProd() {
  252. List<YrProdTradeVO> list = new ArrayList<>();
  253. LocalDate end = LocalDate.now().minusDays(1);
  254. LocalDate start = end.minusDays(30);
  255. do {
  256. YrProdTradeVO vo = new YrProdTradeVO();
  257. vo.setDate(start.getDayOfMonth() + "");
  258. vo.setTips(start.toString());
  259. vo.mock();
  260. list.add(vo);
  261. start = start.plusDays(1);
  262. } while (!start.isAfter(end));
  263. return list;
  264. }
  265. private List<YrEnergyTradeVO> mockEnergy() {
  266. List<YrEnergyTradeVO> list = new ArrayList<>();
  267. LocalDate end = LocalDate.now().minusDays(1);
  268. LocalDate start = end.minusDays(30);
  269. do {
  270. YrEnergyTradeVO vo = new YrEnergyTradeVO();
  271. vo.setDate(start.getDayOfMonth() + "");
  272. vo.setTips(start.toString());
  273. vo.mock();
  274. list.add(vo);
  275. start = start.plusDays(1);
  276. } while (!start.isAfter(end));
  277. return list;
  278. }
  279. @Resource
  280. private ITwinCalcHourYhjService yhjService;
  281. /**
  282. * 工艺对比
  283. *
  284. * @param reqs 输入参数
  285. * @return 结果集
  286. */
  287. @Override
  288. public List<CompareVO> compare(List<YrCompareBackReq> reqs, boolean flag) {
  289. List<CompareVO> result = new ArrayList<>();
  290. for (YrCompareBackReq req : reqs) {
  291. CompareVO vo = itemProcessor(req, flag);
  292. int totalLength = getTotalLength(req);
  293. vo.setLength(totalLength);
  294. result.add(vo);
  295. }
  296. return result;
  297. }
  298. /**
  299. * 获取印花产量数据
  300. *
  301. * @param req 请求
  302. * @return 结果
  303. */
  304. private Integer getTotalLength(YrCompareBackReq req) {
  305. //获取印花产量数据
  306. TwinCalcHourYhj yhjSearch = new TwinCalcHourYhj();
  307. yhjSearch.setDeviceId(req.getLine());
  308. Map<String, Object> params = new HashMap<>(16);
  309. params.put("sTime", DateUtils.toDate(req.getStart()));
  310. params.put("eTime", DateUtils.toDate(req.getEnd()));
  311. yhjSearch.setParams(params);
  312. List<TwinCalcHourYhj> yhjList = yhjService.selectTwinCalcHourYhjList(yhjSearch);
  313. int totalLength = yhjList.stream()
  314. .mapToInt(TwinCalcHourYhj::getLength)
  315. .sum();
  316. return totalLength;
  317. }
  318. private CompareVO itemProcessor(YrCompareBackReq req, boolean flag) {
  319. CompareVO vo = new CompareVO();
  320. vo.setLine(req.getLine());
  321. vo.setStart(req.getStart());
  322. vo.setEnd(req.getEnd());
  323. vo.setYh(10);
  324. CompareVO.Qz qz = vo.getQz();
  325. CompareVO.Hz hz = vo.getHz();
  326. LocalDateTime ldt = req.getEnd().minusHours(1);
  327. Date date = Date.from(ldt.toLocalDate().atStartOfDay(ZoneOffset.of("+8")).toInstant());
  328. TwinCalcHourRz search = new TwinCalcHourRz();
  329. search.setDataDate(date);
  330. search.setHour(ldt.getHour());
  331. search.setLine(req.getLine());
  332. List<TwinCalcHourRz> list = hourRzService.selectTwinCalcHourRzList(search);
  333. list.forEach(item -> {
  334. Map<String, Object> map = JSON.parseObject(item.getData(), Map.class);
  335. map.put("device", item.getDeviceName());
  336. map.put("online", item.getOnline());
  337. if ("Forward".equals(item.getWsName())) {
  338. qzProcessor(qz, item, flag, map);
  339. } else if ("Back".equals(item.getWsName())) {
  340. hzProcessor(hz, item, flag, map);
  341. } else {
  342. //留给烘固机
  343. }
  344. });
  345. return vo;
  346. }
  347. private void qzProcessor(CompareVO.Qz qz, TwinCalcHourRz item, boolean flag, Map<String, Object> map) {
  348. //前整
  349. switch (item.getTypeId()) {
  350. case 2:
  351. //四节定型机
  352. if ("Y".equals(item.getOnline())) {
  353. qz.setDx(qz.getDx() + 1);
  354. }
  355. if (flag) {
  356. qz.addDxPara(map);
  357. }
  358. break;
  359. case 3:
  360. //双棍烫光机
  361. if ("Y".equals(item.getOnline())) {
  362. qz.setTg(qz.getTg() + 1);
  363. }
  364. if (flag) {
  365. qz.addTgPara(map);
  366. }
  367. break;
  368. case 4:
  369. //高梳机
  370. if ("Y".equals(item.getOnline())) {
  371. qz.setGs(qz.getGs() + 1);
  372. }
  373. if (flag) {
  374. qz.addGsPara(map);
  375. }
  376. break;
  377. case 5:
  378. //双棍刷毛
  379. if ("Y".equals(item.getOnline())) {
  380. qz.setSm(qz.getSm() + 1);
  381. }
  382. if (flag) {
  383. qz.addSmPara(map);
  384. }
  385. break;
  386. default:
  387. break;
  388. }
  389. }
  390. private void hzProcessor(CompareVO.Hz hz, TwinCalcHourRz item, boolean flag, Map<String, Object> map) {
  391. //后整
  392. switch (item.getTypeId()) {
  393. case 6:
  394. //六节定型机
  395. if ("Y".equals(item.getOnline())) {
  396. hz.setDx(hz.getDx() + 1);
  397. }
  398. if (flag) {
  399. hz.addDxPara(map);
  400. }
  401. break;
  402. case 3:
  403. //双棍烫光机
  404. if ("Y".equals(item.getOnline())) {
  405. hz.setTg(hz.getTg() + 1);
  406. }
  407. if (flag) {
  408. hz.addTgPara(map);
  409. }
  410. break;
  411. case 4:
  412. //高梳机
  413. if ("Y".equals(item.getOnline())) {
  414. hz.setGs(hz.getGs() + 1);
  415. }
  416. if (flag) {
  417. hz.addGsPara(map);
  418. }
  419. break;
  420. case 5:
  421. //双棍刷毛
  422. if ("Y".equals(item.getOnline())) {
  423. hz.setSm(hz.getSm() + 1);
  424. }
  425. if (flag) {
  426. hz.addSmPara(map);
  427. }
  428. break;
  429. case 7:
  430. //起毛机
  431. if ("Y".equals(item.getOnline())) {
  432. hz.setQm(hz.getQm() + 1);
  433. }
  434. if (flag) {
  435. hz.addQmPara(map);
  436. }
  437. break;
  438. case 9:
  439. //烫剪机
  440. if ("Y".equals(item.getOnline())) {
  441. hz.setTj(hz.getTj() + 1);
  442. }
  443. if (flag) {
  444. hz.addTjPara(map);
  445. }
  446. break;
  447. default:
  448. break;
  449. }
  450. }
  451. }