MesLineService.java 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. package com.huaxia.imes.service;
  2. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  3. import com.baomidou.mybatisplus.core.metadata.IPage;
  4. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  5. import com.baomidou.mybatisplus.extension.service.IService;
  6. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  7. import com.huaxia.comm.domain.imes.MesLine;
  8. import com.huaxia.comm.domain.imes.SysMeConfig;
  9. import com.huaxia.imes.mapper.MesLineMapper;
  10. import com.huaxia.imes.pojo.MesLineBO;
  11. import com.huaxia.imes.pojo.MesLineVO;
  12. import com.ruoyi.common.annotation.DataSource;
  13. import com.ruoyi.common.core.domain.R;
  14. import com.ruoyi.common.enums.DataSourceType;
  15. import com.ruoyi.common.utils.SecurityUtils;
  16. import com.ruoyi.common.utils.StringUtils;
  17. import lombok.AllArgsConstructor;
  18. import lombok.extern.slf4j.Slf4j;
  19. import org.apache.poi.ss.formula.functions.T;
  20. import org.springframework.stereotype.Service;
  21. import org.springframework.transaction.annotation.Transactional;
  22. import org.springframework.util.Assert;
  23. import java.math.BigDecimal;
  24. import java.time.*;
  25. import java.time.format.TextStyle;
  26. import java.time.temporal.TemporalAdjusters;
  27. import java.util.*;
  28. /**
  29. * @author zx
  30. * @since 2024年10月29日
  31. */
  32. @Service
  33. @Transactional(rollbackFor = Exception.class)
  34. @Slf4j
  35. @AllArgsConstructor
  36. @DataSource(DataSourceType.SLAVE)
  37. public class MesLineService extends ServiceImpl<MesLineMapper, MesLine> implements IService<MesLine> {
  38. private MesLineMapper mesLineMapper;
  39. /**
  40. * 分页查询
  41. *
  42. * @param bo
  43. * @return
  44. */
  45. public IPage<MesLineVO> queryList(MesLineBO bo, int pageNum, int pageSize) {
  46. int offset = (pageNum - 1) * pageSize;
  47. Map<String, Object> params = new HashMap<>();
  48. params.put("boy", bo);
  49. params.put("offset", offset);
  50. params.put("pageSize", pageSize);
  51. // 获取总记录数
  52. int total = mesLineMapper.countByParams(params);
  53. //开始分页查询
  54. List<MesLineVO> records = mesLineMapper.queryListPage(params);
  55. // 创建并返回 Page 对象
  56. return new Page<MesLineVO>(pageNum, pageSize, total).setRecords(records);
  57. }
  58. /**
  59. * 新增数据
  60. *
  61. * @param boy
  62. */
  63. public void add(MesLine boy) {
  64. //校验数据
  65. this.check(boy);
  66. //判断创建时间是否为空
  67. if (boy.getCreateTime() == null) {
  68. boy.setCreateTime(new Date());
  69. }
  70. //通过产线名称和创建如期去查询产线数据是否存在
  71. LambdaQueryWrapper<MesLine> wr = new LambdaQueryWrapper<>();
  72. wr.eq(MesLine::getLineName, boy.getLineName());
  73. wr.eq(MesLine::getCreateTime, boy.getCreateTime());
  74. long aLong = this.count(wr);
  75. Assert.isTrue(aLong == 0, String.format("500-产线%s-%s已存在", boy.getLineName(), boy.getCreateTime()));
  76. //获取登录人信息
  77. boy.setCreateBy(SecurityUtils.getUsername());
  78. this.save(boy);
  79. }
  80. /**
  81. * 修改数据
  82. *
  83. * @param boy
  84. */
  85. public void edit(MesLine boy) {
  86. boy.setUpdateTime(new Date())
  87. .setUpdateBy(SecurityUtils.getUsername());
  88. this.updateById(boy);
  89. }
  90. /**
  91. * 新增数据时校验数据
  92. *
  93. * @param boy
  94. */
  95. private void check(MesLine boy) {
  96. Assert.isTrue(StringUtils.isNotBlank(boy.getLineName()), "500-产线名称不能为空");
  97. Assert.isTrue(boy.getCurrentQty() != null && BigDecimal.valueOf(boy.getCurrentQty()).compareTo(BigDecimal.ZERO) > 0, "500-当日产量必须大于0");
  98. }
  99. /**
  100. * 删除数据
  101. *
  102. * @param ids
  103. */
  104. public void delete(Long[] ids) {
  105. //遍历id匹配数据
  106. for (Long id : ids) {
  107. MesLine mesLine = mesLineMapper.selectById(id);
  108. Assert.isTrue(mesLine != null, "500-数据不存在");
  109. mesLineMapper.deleteById(id);
  110. }
  111. }
  112. /**
  113. * 按天来统计数据
  114. *
  115. * @return
  116. */
  117. public R<Map<String, Long>> countDay() {
  118. //获取当前时间
  119. LocalDate now = LocalDate.now();
  120. // 计算本周的星期一
  121. LocalDate mondayOfWeek = now.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
  122. // 计算本周的星期天
  123. LocalDate sundayOfWeek = now.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY));
  124. //获取星期一致星期天的数据
  125. LambdaQueryWrapper<MesLine> wr = new LambdaQueryWrapper<>();
  126. wr.between(MesLine::getCreateTime, mondayOfWeek, sundayOfWeek);
  127. List<MesLine> boy = mesLineMapper.selectList(wr);
  128. Map<String, Long> map = new LinkedHashMap<>();//格式 key:星期几 value:数量
  129. // 初始化Map中的键,并设置默认值为0
  130. for (DayOfWeek dayOfWeek : DayOfWeek.values()) {
  131. String dayOfWeekName = dayOfWeek.getDisplayName(TextStyle.FULL, Locale.CHINA);
  132. map.put(dayOfWeekName, 0L);
  133. }
  134. //判断集合
  135. if (boy != null && !boy.isEmpty()) {
  136. for (MesLine mesLine : boy) {
  137. try {
  138. //获取星期几
  139. LocalDate date = mesLine.getCreateTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
  140. String week = date.getDayOfWeek().getDisplayName(TextStyle.FULL, Locale.CHINA);
  141. Long count = mesLine.getCurrentQty();
  142. map.put(week, map.getOrDefault(week, 0L) + count);
  143. } catch (Exception e) {
  144. log.warn("数据转换异常", e);
  145. }
  146. }
  147. }
  148. return R.ok(map);
  149. }
  150. /**
  151. * 统计本周 每条产线数据
  152. *
  153. * @return
  154. */
  155. public R<Map<String, Map<String, Object>>> queryDay() {
  156. //获取当前时间
  157. LocalDate now = LocalDate.now();
  158. //计算本周的星期一
  159. LocalDate mondayOfWeek = now.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
  160. // 计算本周的星期天
  161. LocalDate sundayOfWeek = now.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY));
  162. //获取星期一致星期天的数据
  163. LambdaQueryWrapper<MesLine> wr = new LambdaQueryWrapper<>();
  164. wr.between(MesLine::getCreateTime, mondayOfWeek, sundayOfWeek);
  165. List<MesLine> boy = mesLineMapper.selectList(wr);
  166. // 创建一个LinkedHashMap用于存储统计结果
  167. Map<String, Map<String, Object>> lineSummary = new LinkedHashMap<>();
  168. // 遍历集合,统计每条产线的数据
  169. if (boy != null && !boy.isEmpty()) {
  170. for (MesLine mesLine : boy) {
  171. try {
  172. //获取产线名称
  173. String lineName = mesLine.getLineName();
  174. LocalDate createTime = mesLine.getCreateTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
  175. if (createTime.isAfter(mondayOfWeek.minusDays(1)) && createTime.isBefore(sundayOfWeek.plusDays(1))) {
  176. Map<String, Object> lineData = lineSummary.computeIfAbsent(lineName, k -> new HashMap<>());
  177. //累加产量
  178. Long totalOutput = (Long) lineData.getOrDefault("产量", 0L);
  179. Long count = mesLine.getCurrentQty();
  180. lineData.put("产量", totalOutput + count);
  181. //累加不合格数量
  182. Long totalPoorQty = (Long) lineData.getOrDefault("不合格数量", 0L);
  183. Long currentPoorQty = mesLine.getPoorQty();
  184. lineData.put("不合格数量", totalPoorQty + currentPoorQty);
  185. //计算合格数量
  186. Long totalQualifiedQty = (Long) lineData.getOrDefault("合格数量", 0L);
  187. Long updatedTotalOutput = (Long) lineData.get("产量");
  188. Long updatedTotalPoorQty = (Long) lineData.get("不合格数量");
  189. totalQualifiedQty = updatedTotalOutput - updatedTotalPoorQty;
  190. lineData.put("合格数量", totalQualifiedQty);
  191. //更新合格率
  192. if (updatedTotalOutput > 0) {
  193. double qualificationRate = ((double) totalQualifiedQty / (double) updatedTotalOutput) * 100;
  194. lineData.put("合格率", String.format("%.2f%%", qualificationRate));
  195. } else {
  196. lineData.put("合格率", "0.00%");
  197. }
  198. }
  199. } catch (Exception e) {
  200. log.warn("数据转换异常", e);
  201. }
  202. }
  203. }
  204. return R.ok(lineSummary);
  205. }
  206. }