|
@@ -0,0 +1,169 @@
|
|
|
+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.DayOfWeek;
|
|
|
+import java.time.LocalDate;
|
|
|
+import java.time.ZoneId;
|
|
|
+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) {
|
|
|
+ Long current = bo.getCurrent();// 当前页码
|
|
|
+ Long size = bo.getSize();// 每页显示条数
|
|
|
+ Long offset = (current - 1) * size;
|
|
|
+ Map<String, Object> params = new HashMap<>();
|
|
|
+ params.put("boy", bo);
|
|
|
+ params.put("offset", offset);
|
|
|
+ params.put("pageSize", size);
|
|
|
+ // 获取总记录数
|
|
|
+ int total = mesLineMapper.countByParams(params);
|
|
|
+ //开始分页查询
|
|
|
+ List<MesLineVO> records = mesLineMapper.queryListPage(params);
|
|
|
+ // 创建并返回 Page 对象
|
|
|
+ return new Page<MesLineVO>(current, size, 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));
|
|
|
+ log.info("本周的星期一:" + mondayOfWeek);
|
|
|
+ // 计算本周的星期天
|
|
|
+ LocalDate sundayOfWeek = now.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY));
|
|
|
+ log.info("本周的星期天:" + sundayOfWeek);
|
|
|
+ //获取星期一致星期天的数据
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+}
|