Prechádzať zdrojové kódy

轧钢管控统计将数据缓存;构建定尺的微服务模块

lingpeng.li 4 mesiacov pred
rodič
commit
f90f193297

+ 26 - 3
zgztBus/jeecg-module-lesm/src/main/java/org/jeecg/modules/homePageData/controller/HomePageDataController.java

@@ -10,6 +10,7 @@ import org.jeecg.common.util.oConvertUtils;
 import org.jeecg.modules.homePageData.entity.IllegalResult;
 import org.jeecg.modules.homePageData.service.IHomePageDataService;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.redis.core.RedisTemplate;
 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestParam;
@@ -18,6 +19,7 @@ import org.springframework.web.bind.annotation.RestController;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.concurrent.TimeUnit;
 
 /**
  * @Description: 首页数据
@@ -34,6 +36,9 @@ public class HomePageDataController {
     @Autowired
     private IHomePageDataService homePageDataService;
 
+    @Autowired
+    private RedisTemplate<String, Object> redisTemplate;
+
 
     /**
      * 评价
@@ -85,13 +90,31 @@ public class HomePageDataController {
      * @return
      */
     @AutoLog(value = "轧钢管控")
-    @ApiOperation(value="轧钢管控", notes="轧钢管控")
+    @ApiOperation(value = "轧钢管控", notes = "轧钢管控")
     @GetMapping(value = "/rollingControl")
-    public Result<List<Map<String, String>>> rollingControl(@RequestParam(name="modelId",required=false) String modelId) {
-        if (oConvertUtils.isEmpty(modelId)){
+    public Result<List<Map<String, String>>> rollingControl(@RequestParam(name = "modelId", required = false) String modelId) {
+        if (oConvertUtils.isEmpty(modelId)) {
             return Result.error("查询失败,请设置模型!");
         }
+
+        // 定义 Redis 缓存的 key
+        String redisKey = "rollingControl:" + modelId;
+
+        // 从 Redis 缓存中获取数据
+        @SuppressWarnings("unchecked")
+        List<Map<String, String>> cachedRes = (List<Map<String, String>>) redisTemplate.opsForValue().get(redisKey);
+
+        if (cachedRes != null) {
+            // 如果缓存中有数据,直接返回
+            return Result.OK(cachedRes);
+        }
+
+        // 如果缓存中没有数据,从数据库查询
         List<Map<String, String>> res = homePageDataService.rollingControl(modelId);
+
+        // 将查询结果存储到 Redis,并设置过期时间为 30 分钟
+        redisTemplate.opsForValue().set(redisKey, res, 30, TimeUnit.MINUTES);
+
         return Result.OK(res);
     }
 

+ 19 - 12
zgztBus/jeecg-module-lesm/src/main/java/org/jeecg/modules/homePageData/service/impl/HomePageDataServiceImpl.java

@@ -30,7 +30,9 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.data.domain.Sort;
 import org.springframework.data.mongodb.core.MongoTemplate;
 import org.springframework.data.mongodb.core.aggregation.Aggregation;
+import org.springframework.data.mongodb.core.aggregation.AggregationOptions;
 import org.springframework.data.mongodb.core.aggregation.AggregationResults;
+import org.springframework.data.mongodb.core.aggregation.TypedAggregation;
 import org.springframework.data.mongodb.core.query.Criteria;
 import org.springframework.data.mongodb.core.query.Query;
 import org.springframework.stereotype.Service;
@@ -39,7 +41,6 @@ import java.math.BigDecimal;
 import java.math.RoundingMode;
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
-import java.time.Instant;
 import java.time.LocalDate;
 import java.time.LocalDateTime;
 import java.time.ZoneId;
@@ -219,20 +220,26 @@ public class HomePageDataServiceImpl implements IHomePageDataService {
                 .map(DeviceInformation::getId)
                 .collect(Collectors.toList());
 
-        // 构建 Aggregation 管道
-        Aggregation aggregation = Aggregation.newAggregation(
+        // 构建 AggregationOptions
+        AggregationOptions aggregationOptions = AggregationOptions.builder()
+                .allowDiskUse(true) // 启用外部排序
+                .build();
+
+        // 创建 TypedAggregation 并附加选项
+        TypedAggregation<DeviceStatiscsModelMongodb> typedAggregation = Aggregation.newAggregation(
+                DeviceStatiscsModelMongodb.class, // 指定聚合操作针对的类型
                 Aggregation.match(Criteria.where("deviceInformationId").in(deviceIds)), // 过滤条件
                 Aggregation.sort(Sort.by(Sort.Direction.DESC, "datestr")), // 按 datestr 降序排列
-                Aggregation.group("deviceInformationId") // 按 deviceInformationId 分组
-                        .first("deviceInformationId").as("deviceInformationId") // 获取分组后第一条记录的字段
-                        .first("power").as("power") // 获取分组后第一条记录的 power
-                        .first("datestr").as("datestr"), // 获取分组后第一条记录的 datestr
-                Aggregation.project("deviceInformationId", "power", "datestr") // 只返回需要的字段
-        );
-
-        // 执行 Aggregation 查询
+                Aggregation.group("deviceInformationId")
+                        .first("deviceInformationId").as("deviceInformationId")
+                        .first("power").as("power")
+                        .first("datestr").as("datestr"),
+                Aggregation.project("deviceInformationId", "power", "datestr")
+        ).withOptions(aggregationOptions); // 将选项绑定到聚合管道
+
+        // 执行聚合查询
         AggregationResults<DeviceStatiscsModelMongodb> aggregationResults = mongoTemplate.aggregate(
-                aggregation,
+                typedAggregation,
                 "leanmodel_run_realtime", // 集合名称
                 DeviceStatiscsModelMongodb.class // 返回的对象类型
         );

+ 57 - 0
zgztBus/jeecg-module-lesm/src/main/java/org/jeecg/modules/productionConsume/entity/ProductionDevice.java

@@ -0,0 +1,57 @@
+package org.jeecg.modules.productionConsume.entity;
+
+import com.baomidou.mybatisplus.annotation.IdType;
+import com.baomidou.mybatisplus.annotation.TableId;
+import com.baomidou.mybatisplus.annotation.TableName;
+import io.swagger.annotations.ApiModel;
+import io.swagger.annotations.ApiModelProperty;
+import lombok.Data;
+import lombok.EqualsAndHashCode;
+import lombok.experimental.Accessors;
+import org.jeecgframework.poi.excel.annotation.Excel;
+
+import java.io.Serializable;
+
+@Data
+@TableName("production_device")
+@Accessors(chain = true)
+@EqualsAndHashCode(callSuper = false)
+@ApiModel(value = "production_device对象", description = "生产消耗设备关系表")
+public class ProductionDevice implements Serializable {
+
+    private static final long serialVersionUID = 1L;
+
+    /**
+     * 主键
+     */
+    @TableId(type = IdType.ASSIGN_ID)
+    @ApiModelProperty(value = "主键")
+    private String id;
+    /**
+     * 区域名称
+     */
+    @Excel(name = "区域名称", width = 15)
+    @ApiModelProperty(value = "区域名称")
+    private String deviceRegion;
+    /**
+     * 设备名称
+     */
+    @Excel(name = "设备名称", width = 15)
+    @ApiModelProperty(value = "设备名称")
+    private String deviceTitle;
+    /**
+     * 变量名称
+     */
+    @Excel(name = "变量名称", width = 15)
+    @ApiModelProperty(value = "变量名称")
+    private String deviceVariable;
+
+    /**
+     * 备注
+     */
+    @Excel(name = "备注", width = 15)
+    @ApiModelProperty(value = "备注")
+    private String remark;
+
+
+}

+ 4 - 2
zgztBus/zgztREADME.md

@@ -26,7 +26,7 @@
      ├─jeecg-cloud-lesm          --精益事件服务模块(7004)
      ├─jeecg-cloud-sbm           --钢坯管控服务模块(7005)
      ├─jeecg-cloud-ccm           --连铸管控服务模块(7006)
-     ├─jeecg-cloud-scale           --连铸管控服务模块(7007)
+     ├─jeecg-cloud-scale         --定尺服务模块(7007)
      ├─jeecg-visual
         ├─jeecg-cloud-monitor        --微服务监控模块 (9111)
         ├─jeecg-cloud-xxljob         --微服务xxljob定时任务服务端 (9080)
@@ -149,4 +149,6 @@ http://localhost:7004/doc.html#/home			精益事件服务
 
 http://localhost:7005/doc.html#/home			钢坯管控服务
 
-http://localhost:7006/doc.html#/home			连铸管控服务
+http://localhost:7006/doc.html#/home			连铸管控服务
+
+http://localhost:7007/doc.html#/home			定尺服务