Prechádzať zdrojové kódy

修改mybatisplus插件配置,新增限电列表分页

王波 1 mesiac pred
rodič
commit
39aeb9be76

+ 7 - 1
runeconomy-xk/pom.xml

@@ -170,7 +170,13 @@
         <dependency>
             <groupId>com.baomidou</groupId>
             <artifactId>mybatis-plus-boot-starter</artifactId>
-            <version>3.4.2</version>
+            <version>3.5.3.1</version>
+        </dependency>
+        <!-- pom.xml 中添加 -->
+        <dependency>
+            <groupId>com.github.jsqlparser</groupId>
+            <artifactId>jsqlparser</artifactId>
+            <version>4.6</version>  <!-- 关键版本 -->
         </dependency>
 
         <dependency>

+ 47 - 64
runeconomy-xk/src/main/java/com/gyee/runeconomy/config/MybatisPlusConfig.java

@@ -1,64 +1,47 @@
-//package com.gyee.runeconomy.config;
-//
-//
-//import cn.hutool.core.date.DatePattern;
-//import cn.hutool.core.date.DateUtil;
-//import cn.hutool.core.util.IdUtil;
-//import com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator;
-//import com.baomidou.mybatisplus.extension.plugins.OptimisticLockerInterceptor;
-//import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
-//import org.springframework.context.annotation.Bean;
-//import org.springframework.context.annotation.Configuration;
-//
-//import java.util.Date;
-//
-///**
-// * @ClassName MybatisPlusConfig
-// * @Description 配置分页插件
-// * @Author 谢生杰
-// * @Date 2020/9/25 18:24
-// * @Version 1.0
-// **/
-//@Configuration
-//public class MybatisPlusConfig {
-//    /**
-//     * 分页插件
-//     */
-//    @Bean
-//    public PaginationInterceptor paginationInterceptor() {
-//        return new PaginationInterceptor();
-//    }
-//
-//    /**
-//     * @description:
-//     * @author: xiruipeng
-//     * @date: 2022/9/16 15:08
-//     * @param: []
-//     * @return: com.baomidou.mybatisplus.extension.plugins.OptimisticLockerInterceptor
-//     **/
-//    @Bean
-//    public OptimisticLockerInterceptor optimisticLockerInterceptor() {
-//        return new OptimisticLockerInterceptor();
-//    }
-//
-//
-//    /**
-//     * 自定义主键生成:生成的 当前时间 + 随机数的格式
-//     *
-//     * @return
-//     */
-//    @Bean
-//    public IdentifierGenerator idGenerator() {
-//        return new IdentifierGenerator() {
-//            @Override
-//            public Number nextId(Object entity) {
-//                return null;
-//            }
-//
-//            @Override
-//            public String nextUUID(Object entity) {
-//                return DateUtil.format(new Date(), DatePattern.PURE_DATETIME_PATTERN) + IdUtil.simpleUUID();
-//            }
-//        };
-//    }
-//}
+package com.gyee.runeconomy.config;
+
+import cn.hutool.core.date.DatePattern;
+import cn.hutool.core.util.IdUtil;
+import com.baomidou.mybatisplus.annotation.DbType;
+import com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator;
+import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
+import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
+import com.gyee.runeconomy.util.DateUtil;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+import java.util.Date;
+
+@Configuration
+public class MybatisPlusConfig {
+
+    /**
+     * 新版插件配置(包含分页和乐观锁)
+     */
+    @Bean
+    public MybatisPlusInterceptor mybatisPlusInterceptor() {
+        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
+        PaginationInnerInterceptor pagination = new PaginationInnerInterceptor(DbType.POSTGRE_SQL);
+        pagination.setOptimizeJoin(false);  // 修复PG分页语法冲突
+        interceptor.addInnerInterceptor(pagination);
+        return interceptor;
+    }
+
+    /**
+     * 自定义主键生成器(保持原逻辑)
+     */
+    @Bean
+    public IdentifierGenerator idGenerator() {
+        return new IdentifierGenerator() {
+            @Override
+            public Number nextId(Object entity) {
+                return null;
+            }
+
+            @Override
+            public String nextUUID(Object entity) {
+                return DateUtil.format(new Date(), DatePattern.PURE_DATETIME_PATTERN) + IdUtil.simpleUUID();
+            }
+        };
+    }
+}

+ 71 - 43
runeconomy-xk/src/main/java/com/gyee/runeconomy/controller/agc/AgcDeviateController.java

@@ -6,16 +6,18 @@ import cn.hutool.core.date.DateUtil;
 import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.TypeReference;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.baomidou.mybatisplus.core.metadata.IPage;
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.gyee.common.model.PointData;
 import com.gyee.runeconomy.config.GyeeConfig;
 import com.gyee.runeconomy.init.CacheContext;
-import com.gyee.runeconomy.mapper.PowerLossesMapper;
 import com.gyee.runeconomy.model.PowerLosses;
 import com.gyee.runeconomy.model.PowerLossesDTO;
 import com.gyee.runeconomy.model.StatusTime;
 import com.gyee.runeconomy.model.auto.PointInfo;
 import com.gyee.runeconomy.model.auto.ProBasicEquipment;
 import com.gyee.runeconomy.model.auto.ProBasicEquipmentPoint;
+import com.gyee.runeconomy.service.PowerLossesService;
 import com.gyee.runeconomy.service.agc.AgcDeviateService;
 import com.gyee.runeconomy.service.auto.IPointInfoService;
 import com.gyee.runeconomy.util.realtimesource.IEdosUtil;
@@ -27,9 +29,10 @@ import org.springframework.web.bind.annotation.RequestParam;
 import org.springframework.web.bind.annotation.RestController;
 
 import javax.annotation.Resource;
-import java.text.SimpleDateFormat;
-import java.time.LocalDate;
+import java.text.DecimalFormat;
+import java.time.Instant;
 import java.time.LocalDateTime;
+import java.time.ZoneId;
 import java.time.format.DateTimeFormatter;
 import java.util.*;
 
@@ -58,7 +61,7 @@ public class AgcDeviateController {
     @Resource
     private IPointInfoService pointInfoService;
     @Resource
-    private PowerLossesMapper powerLossesMapper;
+    private PowerLossesService powerLossService;
 
     /**
      * 获取偏差信息
@@ -85,26 +88,51 @@ public class AgcDeviateController {
 
     /**
      * 限电时间列表
+     *
      * @return
      */
     @GetMapping("/xdloss")
-    public List<PowerLossesDTO> getxdData() {
-        QueryWrapper<PowerLosses> queryWrapper = new QueryWrapper<>();
-        List<PowerLosses> powerLosses = powerLossesMapper.selectList(queryWrapper);
+    public List<PowerLossesDTO> getxdData(@RequestParam(value = "startTs") long startTs,
+                                          @RequestParam(value = "endTs") long endTs,
+                                          @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
+                                          @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
+        // 将时间戳转换为LocalDateTime
+        LocalDateTime startDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(startTs), ZoneId.systemDefault());
+        LocalDateTime endDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(endTs), ZoneId.systemDefault());
+        Page<PowerLosses> page = new Page<>(pageNum, pageSize);
+        IPage<PowerLosses> list = powerLossService.pageList(page, startDateTime, endDateTime);
+        list.setTotal(list.getRecords().size());
+        List<PowerLosses> List = list.getRecords();
         List<PowerLossesDTO> dtos = new ArrayList<>();
-        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
-        for (PowerLosses pl : powerLosses) {
+
+        DecimalFormat df = new DecimalFormat("0.0000000"); // 创建一个保留8位小数的 DecimalFormat 实例
+
+        for (PowerLosses pl : List) {
             PowerLossesDTO dto = new PowerLossesDTO();
             dto.setId(pl.getId());
-            dto.setId(dto.getId());
+
+            // 使用DateTimeFormatter格式化日期时间
+            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
             dto.setStartTime(pl.getStartTime().format(formatter));
             dto.setEndTime(pl.getEndTime().format(formatter));
-            dto.setXdss(pl.getXdss());
+
+            try {
+                double xddl = Double.parseDouble(pl.getXdss()); // 将字符串转换为double
+                double xddlDivided = xddl / 10000; // 进行除法运算
+                String xd = df.format(xddlDivided); // 使用DecimalFormat格式化结果
+                dto.setXdss(xd); // 设置格式化后的值
+            } catch (NumberFormatException e) {
+                // 处理无法解析为double的情况
+                dto.setXdss("0.0000000"); // 假设默认值为0.0000000,与DecimalFormat的格式一致
+            }
+
             dto.setRecordDate(pl.getRecordDate());
             dtos.add(dto);
         }
+
         return dtos;
     }
+
     /**
      * 获取配置
      *
@@ -118,9 +146,9 @@ public class AgcDeviateController {
 
     @GetMapping("/windturbine/curve")
     public Map<String, List<SpeedPowerAnalysis>> getWindturbineData(@RequestParam(value = "startTs") long startTs,
-                                                       @RequestParam(value = "endTs") long endTs,
-                                                       @RequestParam(value = "windturbineId") String windturbineId,
-                                                       @RequestParam(value = "interval", defaultValue = "60", required = false) int interval) throws Exception {
+                                                                    @RequestParam(value = "endTs") long endTs,
+                                                                    @RequestParam(value = "windturbineId") String windturbineId,
+                                                                    @RequestParam(value = "interval", defaultValue = "60", required = false) int interval) throws Exception {
         //切换数据库
         LettuceConnectionFactory factory = (LettuceConnectionFactory) stringRedisTemplate.getConnectionFactory();
         int database = factory.getDatabase();
@@ -169,8 +197,8 @@ public class AgcDeviateController {
 
     @GetMapping("/windtur")
     public Map<String, List<StatusTime>> getTestData(@RequestParam(value = "startTs") long startTs,
-                                                    @RequestParam(value = "endTs") long endTs,
-                                                    @RequestParam(value = "uniformcode") String uniformcode) throws Exception {
+                                                     @RequestParam(value = "endTs") long endTs,
+                                                     @RequestParam(value = "uniformcode") String uniformcode) throws Exception {
 
         Map<String, List<StatusTime>> map = new HashMap<>();
         QueryWrapper<PointInfo> qw = new QueryWrapper<>();
@@ -182,7 +210,7 @@ public class AgcDeviateController {
             for (PointData pointData : historyDatasRaw) {
                 StatusTime st = new StatusTime();
                 st.setName(ls.getName());
-                Date da=new Date(pointData.getPointTime());
+                Date da = new Date(pointData.getPointTime());
                 st.setTime(DateUtil.formatDateTime(da));
                 String getstatus = getstatus((int) pointData.getPointValueInDouble());
                 st.setStatus(getstatus);
@@ -202,32 +230,32 @@ public class AgcDeviateController {
 
     private String getstatus(int number) {
         String statusText = "";
-            switch (number) {
-                case 0:
-                    statusText = "待机";
-                    break;
-                case 1:
-                    statusText = "停机";
-                    break;
-                case 2:
-                    statusText = "并网";
-                    break;
-                case 4:
-                    statusText = "故障";
-                    break;
-                case 6:
-                    statusText = "检修";
-                    break;
-                case 8:
-                    statusText = "限电";
-                    break;
-                case 12:
-                    statusText = "离线";
-                    break;
-                default:
-                    statusText = "未知";
-                    break;
-            }
+        switch (number) {
+            case 0:
+                statusText = "待机";
+                break;
+            case 1:
+                statusText = "停机";
+                break;
+            case 2:
+                statusText = "并网";
+                break;
+            case 4:
+                statusText = "故障";
+                break;
+            case 6:
+                statusText = "检修";
+                break;
+            case 8:
+                statusText = "限电";
+                break;
+            case 12:
+                statusText = "离线";
+                break;
+            default:
+                statusText = "未知";
+                break;
+        }
         return statusText;
     }
 

+ 5 - 0
runeconomy-xk/src/main/java/com/gyee/runeconomy/service/PowerLossesService.java

@@ -1,8 +1,12 @@
 package com.gyee.runeconomy.service;
 
+import com.baomidou.mybatisplus.core.metadata.IPage;
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.gyee.runeconomy.model.PowerLosses;
 import com.baomidou.mybatisplus.extension.service.IService;
 
+import java.time.LocalDateTime;
+
 /**
  * <p>
  *  服务类
@@ -13,4 +17,5 @@ import com.baomidou.mybatisplus.extension.service.IService;
  */
 public interface PowerLossesService extends IService<PowerLosses> {
 
+    IPage<PowerLosses> pageList(Page<PowerLosses> page, LocalDateTime startTs, LocalDateTime endTs);
 }

+ 17 - 0
runeconomy-xk/src/main/java/com/gyee/runeconomy/service/impl/PowerLossesServiceImpl.java

@@ -1,11 +1,17 @@
 package com.gyee.runeconomy.service.impl;
 
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.baomidou.mybatisplus.core.metadata.IPage;
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.gyee.runeconomy.model.PowerLosses;
 import com.gyee.runeconomy.mapper.PowerLossesMapper;
 import com.gyee.runeconomy.service.PowerLossesService;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import org.springframework.stereotype.Service;
 
+import javax.annotation.Resource;
+import java.time.LocalDateTime;
+
 /**
  * <p>
  *  服务实现类
@@ -16,5 +22,16 @@ import org.springframework.stereotype.Service;
  */
 @Service
 public class PowerLossesServiceImpl extends ServiceImpl<PowerLossesMapper, PowerLosses> implements PowerLossesService {
+    @Resource
+    private PowerLossesMapper powerLossesMapper;
 
+    @Override
+    public IPage<PowerLosses> pageList(Page<PowerLosses> page, LocalDateTime startTs, LocalDateTime endTs) {
+        QueryWrapper<PowerLosses> queryWrapper = new QueryWrapper<>();
+        queryWrapper.lambda()
+                .ge(PowerLosses::getStartTime, startTs)
+                .le(PowerLosses::getEndTime, endTs)
+                .orderByAsc(PowerLosses::getId); // 明确指定排序
+        return powerLossesMapper.selectPage(page, queryWrapper);
+    }
 }