123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222 |
- package com.huaxia.imes.service;
- import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
- import com.baomidou.mybatisplus.core.metadata.IPage;
- import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
- import com.baomidou.mybatisplus.extension.service.IService;
- import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
- import com.huaxia.comm.domain.imes.MesLine;
- import com.huaxia.comm.domain.imes.SysMeConfig;
- import com.huaxia.imes.mapper.MesLineMapper;
- import com.huaxia.imes.pojo.MesLineBO;
- import com.huaxia.imes.pojo.MesLineVO;
- import com.ruoyi.common.annotation.DataSource;
- import com.ruoyi.common.core.domain.R;
- import com.ruoyi.common.enums.DataSourceType;
- import com.ruoyi.common.utils.SecurityUtils;
- import com.ruoyi.common.utils.StringUtils;
- import lombok.AllArgsConstructor;
- import lombok.extern.slf4j.Slf4j;
- import org.apache.poi.ss.formula.functions.T;
- import org.springframework.stereotype.Service;
- import org.springframework.transaction.annotation.Transactional;
- import org.springframework.util.Assert;
- import java.math.BigDecimal;
- import java.time.*;
- import java.time.format.TextStyle;
- import java.time.temporal.TemporalAdjusters;
- import java.util.*;
- /**
- * @author zx
- * @since 2024年10月29日
- */
- @Service
- @Transactional(rollbackFor = Exception.class)
- @Slf4j
- @AllArgsConstructor
- @DataSource(DataSourceType.SLAVE)
- public class MesLineService extends ServiceImpl<MesLineMapper, MesLine> implements IService<MesLine> {
- private MesLineMapper mesLineMapper;
- /**
- * 分页查询
- *
- * @param bo
- * @return
- */
- public IPage<MesLineVO> queryList(MesLineBO bo, int pageNum, int pageSize) {
- int offset = (pageNum - 1) * pageSize;
- Map<String, Object> params = new HashMap<>();
- params.put("boy", bo);
- params.put("offset", offset);
- params.put("pageSize", pageSize);
- // 获取总记录数
- int total = mesLineMapper.countByParams(params);
- //开始分页查询
- List<MesLineVO> records = mesLineMapper.queryListPage(params);
- // 创建并返回 Page 对象
- return new Page<MesLineVO>(pageNum, pageSize, total).setRecords(records);
- }
- /**
- * 新增数据
- *
- * @param boy
- */
- public void add(MesLine boy) {
- //校验数据
- this.check(boy);
- //判断创建时间是否为空
- if (boy.getCreateTime() == null) {
- boy.setCreateTime(new Date());
- }
- //通过产线名称和创建如期去查询产线数据是否存在
- LambdaQueryWrapper<MesLine> wr = new LambdaQueryWrapper<>();
- wr.eq(MesLine::getLineName, boy.getLineName());
- wr.eq(MesLine::getCreateTime, boy.getCreateTime());
- long aLong = this.count(wr);
- Assert.isTrue(aLong == 0, String.format("500-产线%s-%s已存在", boy.getLineName(), boy.getCreateTime()));
- //获取登录人信息
- boy.setCreateBy(SecurityUtils.getUsername());
- this.save(boy);
- }
- /**
- * 修改数据
- *
- * @param boy
- */
- public void edit(MesLine boy) {
- boy.setUpdateTime(new Date())
- .setUpdateBy(SecurityUtils.getUsername());
- this.updateById(boy);
- }
- /**
- * 新增数据时校验数据
- *
- * @param boy
- */
- private void check(MesLine boy) {
- Assert.isTrue(StringUtils.isNotBlank(boy.getLineName()), "500-产线名称不能为空");
- Assert.isTrue(boy.getCurrentQty() != null && BigDecimal.valueOf(boy.getCurrentQty()).compareTo(BigDecimal.ZERO) > 0, "500-当日产量必须大于0");
- }
- /**
- * 删除数据
- *
- * @param ids
- */
- public void delete(Long[] ids) {
- //遍历id匹配数据
- for (Long id : ids) {
- MesLine mesLine = mesLineMapper.selectById(id);
- Assert.isTrue(mesLine != null, "500-数据不存在");
- mesLineMapper.deleteById(id);
- }
- }
- /**
- * 按天来统计数据
- *
- * @return
- */
- public R<Map<String, Long>> countDay() {
- //获取当前时间
- LocalDate now = LocalDate.now();
- // 计算本周的星期一
- LocalDate mondayOfWeek = now.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
- // 计算本周的星期天
- LocalDate sundayOfWeek = now.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY));
- //获取星期一致星期天的数据
- LambdaQueryWrapper<MesLine> wr = new LambdaQueryWrapper<>();
- wr.between(MesLine::getCreateTime, mondayOfWeek, sundayOfWeek);
- List<MesLine> boy = mesLineMapper.selectList(wr);
- Map<String, Long> map = new LinkedHashMap<>();//格式 key:星期几 value:数量
- // 初始化Map中的键,并设置默认值为0
- for (DayOfWeek dayOfWeek : DayOfWeek.values()) {
- String dayOfWeekName = dayOfWeek.getDisplayName(TextStyle.FULL, Locale.CHINA);
- map.put(dayOfWeekName, 0L);
- }
- //判断集合
- if (boy != null && !boy.isEmpty()) {
- for (MesLine mesLine : boy) {
- try {
- //获取星期几
- LocalDate date = mesLine.getCreateTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
- String week = date.getDayOfWeek().getDisplayName(TextStyle.FULL, Locale.CHINA);
- Long count = mesLine.getCurrentQty();
- map.put(week, map.getOrDefault(week, 0L) + count);
- } catch (Exception e) {
- log.warn("数据转换异常", e);
- }
- }
- }
- return R.ok(map);
- }
- /**
- * 统计本周 每条产线数据
- *
- * @return
- */
- public R<Map<String, Map<String, Object>>> queryDay() {
- //获取当前时间
- LocalDate now = LocalDate.now();
- //计算本周的星期一
- LocalDate mondayOfWeek = now.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
- // 计算本周的星期天
- LocalDate sundayOfWeek = now.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY));
- //获取星期一致星期天的数据
- LambdaQueryWrapper<MesLine> wr = new LambdaQueryWrapper<>();
- wr.between(MesLine::getCreateTime, mondayOfWeek, sundayOfWeek);
- List<MesLine> boy = mesLineMapper.selectList(wr);
- // 创建一个LinkedHashMap用于存储统计结果
- Map<String, Map<String, Object>> lineSummary = new LinkedHashMap<>();
- // 遍历集合,统计每条产线的数据
- if (boy != null && !boy.isEmpty()) {
- for (MesLine mesLine : boy) {
- try {
- //获取产线名称
- String lineName = mesLine.getLineName();
- LocalDate createTime = mesLine.getCreateTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
- if (createTime.isAfter(mondayOfWeek.minusDays(1)) && createTime.isBefore(sundayOfWeek.plusDays(1))) {
- Map<String, Object> lineData = lineSummary.computeIfAbsent(lineName, k -> new HashMap<>());
- //累加产量
- Long totalOutput = (Long) lineData.getOrDefault("产量", 0L);
- Long count = mesLine.getCurrentQty();
- lineData.put("产量", totalOutput + count);
- //累加不合格数量
- Long totalPoorQty = (Long) lineData.getOrDefault("不合格数量", 0L);
- Long currentPoorQty = mesLine.getPoorQty();
- lineData.put("不合格数量", totalPoorQty + currentPoorQty);
- //计算合格数量
- Long totalQualifiedQty = (Long) lineData.getOrDefault("合格数量", 0L);
- Long updatedTotalOutput = (Long) lineData.get("产量");
- Long updatedTotalPoorQty = (Long) lineData.get("不合格数量");
- totalQualifiedQty = updatedTotalOutput - updatedTotalPoorQty;
- lineData.put("合格数量", totalQualifiedQty);
- //更新合格率
- if (updatedTotalOutput > 0) {
- double qualificationRate = ((double) totalQualifiedQty / (double) updatedTotalOutput) * 100;
- lineData.put("合格率", String.format("%.2f%%", qualificationRate));
- } else {
- lineData.put("合格率", "0.00%");
- }
- }
- } catch (Exception e) {
- log.warn("数据转换异常", e);
- }
- }
- }
- return R.ok(lineSummary);
- }
- }
|