Browse Source

产线新增统计接口(星期1-7)

zhangxin 8 tháng trước cách đây
mục cha
commit
7712488c6e

+ 21 - 0
prod-line-comm/src/main/java/com/huaxia/comm/domain/imes/BasePageQuery.java

@@ -0,0 +1,21 @@
+package com.huaxia.comm.domain.imes;
+
+import lombok.Data;
+
+/**
+ * @author zx
+ * @since 2024年10月29日
+ */
+@Data
+public class BasePageQuery {
+
+
+    /**
+     * 当前页码, 默认第一页
+     */
+    private Long current = 1L;
+    /**
+     * 每页展示的数据数量,默认每页展示 10 条数据
+     */
+    private Long size = 10L;
+}

+ 51 - 0
prod-line-comm/src/main/java/com/huaxia/comm/domain/imes/MesLine.java

@@ -0,0 +1,51 @@
+package com.huaxia.comm.domain.imes;
+
+import com.baomidou.mybatisplus.annotation.IdType;
+import com.baomidou.mybatisplus.annotation.TableField;
+import com.baomidou.mybatisplus.annotation.TableId;
+import com.baomidou.mybatisplus.annotation.TableName;
+import com.fasterxml.jackson.annotation.JsonFormat;
+import lombok.AllArgsConstructor;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+import lombok.experimental.Accessors;
+
+import java.io.Serializable;
+import java.util.Date;
+
+/**
+ * @author zx
+ * @since 2024年10月29日
+ * 智能产线 实体类
+ */
+@Data
+@AllArgsConstructor
+@NoArgsConstructor
+@TableName("mes_line")
+@Accessors(chain = true)
+public class MesLine implements Serializable {
+
+    private static final long serialVersionUID = 1L;
+
+    @TableId(type = IdType.AUTO)
+    private Long id;//主键id
+
+    private String lineName;//产线名称
+
+    private Long currentQty;//当日产量
+
+    private Long poorQty;//不合格量
+
+    private String createBy;//创建人
+
+    @JsonFormat(locale = "zh_CN", timezone = "GMT+8", pattern = "yyyy-MM-dd")
+    private Date createTime;//创建时间
+
+    private String updateBy;//修改人
+
+    @JsonFormat(locale = "zh_CN", timezone = "GMT+8", pattern = "yyyy-MM-dd")
+    private Date updateTime;//修改时间
+
+    @TableField(exist = false)
+    private String day;//每天(星期几)
+}

+ 0 - 4
prod-line-imes/src/main/java/com/huaxia/imes/controller/CutpieceController.java

@@ -1,16 +1,12 @@
 package com.huaxia.imes.controller;
 
 import com.huaxia.comm.domain.imes.Cutpiece;
-import com.huaxia.imes.dto.CutpieceDTO;
 import com.huaxia.imes.service.CutpieceService;
 import com.ruoyi.common.core.controller.BaseController;
 import com.ruoyi.common.core.domain.R;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.security.access.prepost.PreAuthorize;
-import org.springframework.validation.annotation.Validated;
 import org.springframework.web.bind.annotation.GetMapping;
-import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
 

+ 96 - 0
prod-line-imes/src/main/java/com/huaxia/imes/controller/MesLineController.java

@@ -0,0 +1,96 @@
+package com.huaxia.imes.controller;
+
+import com.baomidou.mybatisplus.core.metadata.IPage;
+import com.huaxia.comm.domain.imes.MesLine;
+import com.huaxia.imes.pojo.MesLineBO;
+import com.huaxia.imes.pojo.MesLineVO;
+import com.huaxia.imes.service.MesLineService;
+import com.ruoyi.common.core.controller.BaseController;
+import com.ruoyi.common.core.domain.AjaxResult;
+import com.ruoyi.common.core.domain.R;
+import com.ruoyi.common.core.page.TableDataInfo;
+import lombok.AllArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.web.bind.annotation.*;
+
+import java.time.DayOfWeek;
+import java.time.LocalDate;
+import java.time.temporal.TemporalAdjusters;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * @author zx
+ * @since 2024年10月29日
+ * 智能产线控制器
+ */
+@RestController
+@RequestMapping("open/mes/line")
+@Slf4j
+@AllArgsConstructor
+public class MesLineController extends BaseController {
+
+    private MesLineService mesLineService;
+
+    /**
+     * 分页查询统计表
+     *
+     * @return
+     */
+    @GetMapping("/list")
+    public AjaxResult queryList(MesLineBO bo) {
+        IPage<MesLineVO> list = mesLineService.queryList(bo);
+        return AjaxResult.success(new TableDataInfo(list.getRecords(), (int) list.getTotal()));
+
+    }
+
+
+    /**
+     * 新增数据
+     *
+     * @param boy
+     * @return
+     */
+    @PostMapping("/add")
+    public R<Void> add(@RequestBody MesLine boy) {
+        mesLineService.add(boy);
+        return R.ok();
+    }
+
+
+    /**
+     * 修改数据
+     *
+     * @param boy
+     * @return
+     */
+    @PutMapping("/edit")
+    public R<Void> edit(@RequestBody MesLine boy) {
+        mesLineService.edit(boy);
+        return R.ok();
+    }
+
+    /**
+     * 删除数据
+     *
+     * @param ids
+     * @return
+     */
+    @DeleteMapping("/remove")
+    public R<Void> remove(Long[] ids) {
+        mesLineService.delete(ids);
+        return R.ok();
+    }
+
+
+    /**
+     * 获取 星期1-7 统计数据
+     * @return
+     */
+    //todo 待实现
+    @GetMapping("/count_day")
+    public R<Map<String,Long>> list(){
+        return mesLineService.countDay();
+    }
+
+}

+ 35 - 0
prod-line-imes/src/main/java/com/huaxia/imes/mapper/MesLineMapper.java

@@ -0,0 +1,35 @@
+package com.huaxia.imes.mapper;
+
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import com.huaxia.comm.domain.imes.MesLine;
+import com.huaxia.imes.pojo.MesLineVO;
+import com.ruoyi.common.annotation.DataSource;
+import com.ruoyi.common.enums.DataSourceType;
+import org.apache.ibatis.annotations.Mapper;
+import org.apache.ibatis.annotations.Param;
+
+import java.util.List;
+import java.util.Map;
+
+/**
+ * @author zx
+ * @since 2024年10月29日
+ */
+@Mapper
+@DataSource(DataSourceType.SLAVE)
+public interface MesLineMapper extends BaseMapper<MesLine> {
+
+    /**
+     * 获取总条数
+     * @param params
+     * @return
+     */
+    int countByParams(@Param("params") Map<String, Object> params);
+
+    /**
+     * 开始分页查询
+     * @param params
+     * @return
+     */
+    List<MesLineVO> queryListPage(@Param("params") Map<String, Object> params);
+}

+ 1 - 3
prod-line-imes/src/main/java/com/huaxia/imes/dto/CutpieceDTO.java → prod-line-imes/src/main/java/com/huaxia/imes/pojo/CutpieceDTO.java

@@ -1,14 +1,12 @@
-package com.huaxia.imes.dto;
+package com.huaxia.imes.pojo;
 
 import com.fasterxml.jackson.annotation.JsonFormat;
 import lombok.Getter;
 import lombok.Setter;
 
 import javax.validation.constraints.NotNull;
-import java.time.LocalDate;
 import java.time.LocalDateTime;
 import java.time.LocalTime;
-import java.util.Date;
 
 /**
  * @author zx

+ 22 - 0
prod-line-imes/src/main/java/com/huaxia/imes/pojo/MesLineBO.java

@@ -0,0 +1,22 @@
+package com.huaxia.imes.pojo;
+
+import com.huaxia.comm.domain.imes.BasePageQuery;
+import lombok.Getter;
+import lombok.Setter;
+
+import java.util.Date;
+
+/**
+ * @author zx
+ * @since 2024年10月29日
+ */
+@Getter
+@Setter
+public class MesLineBO extends BasePageQuery {
+
+
+    private String lineName;// 产线名称
+
+
+    private Date startTime,endTime;// 开始时间,结束时间
+}

+ 10 - 0
prod-line-imes/src/main/java/com/huaxia/imes/pojo/MesLineVO.java

@@ -0,0 +1,10 @@
+package com.huaxia.imes.pojo;
+
+import com.huaxia.comm.domain.imes.MesLine;
+
+/**
+ * @author zx
+ * @since 2024年10月29日
+ */
+public class MesLineVO extends MesLine {
+}

+ 169 - 0
prod-line-imes/src/main/java/com/huaxia/imes/service/MesLineService.java

@@ -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);
+    }
+}

+ 52 - 0
prod-line-imes/src/main/resources/mapper/MesLineMapper.xml

@@ -0,0 +1,52 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<!DOCTYPE mapper
+        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
+        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+<mapper namespace="com.huaxia.imes.mapper.MesLineMapper">
+
+
+    <!--获取总条数-->
+    <select id="countByParams" resultType="int">
+        SELECT COUNT(*)
+        FROM mes_line
+        <where>
+            <if test="params.boy != null">
+                <if test="params.boy.lineName != null and params.boy.lineName != ''">
+                    line_name like concat('%', #{params.boy.lineName}, '%')
+                </if>
+                <if test="params.boy.startTime != null">
+                    and create_time gl;
+                    #{params.boy.startTime}
+                </if>
+                <if test="params.boy.endTime != null">
+                    and create_time le;
+                    #{params.boy.endTime}
+                </if>
+            </if>
+        </where>
+    </select>
+
+    <!--开始分页查询-->
+    <select id="queryListPage" resultType="com.huaxia.imes.pojo.MesLineVO">
+        SELECT *
+        FROM mes_line
+        <where>
+            <if test="params.boy != null">
+                <if test="params.boy.lineName != null and params.boy.lineName != ''">
+                    line_name like concat('%', #{params.boy.lineName}, '%')
+                </if>
+                <if test="params.boy.startTime != null">
+                    and create_time gl;
+                    #{params.boy.startTime}
+                </if>
+                <if test="params.boy.endTime != null">
+                    and create_time le;
+                    #{params.boy.endTime}
+                </if>
+            </if>
+        </where>
+        ORDER BY id
+        OFFSET #{params.offset} ROWS
+        FETCH NEXT #{params.size} ROWS ONLY
+    </select>
+</mapper>