Browse Source

增加停机统计功能

wukai 4 months ago
parent
commit
91ba66931e

+ 86 - 33
jjt-biz/src/main/java/com/jjt/calc/controller/TwinCalcStopController.java

@@ -1,28 +1,28 @@
 package com.jjt.calc.controller;
 
-import java.util.List;
-import javax.annotation.Resource;
-import javax.servlet.http.HttpServletResponse;
-
-import io.swagger.annotations.Api;
-import io.swagger.annotations.ApiOperation;
-import org.springframework.security.access.prepost.PreAuthorize;
-import org.springframework.web.bind.annotation.GetMapping;
-import org.springframework.web.bind.annotation.PostMapping;
-import org.springframework.web.bind.annotation.PutMapping;
-import org.springframework.web.bind.annotation.DeleteMapping;
-import org.springframework.web.bind.annotation.PathVariable;
-import org.springframework.web.bind.annotation.RequestBody;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RestController;
+import com.jjt.biz.service.ITwinDeviceService;
+import com.jjt.calc.domain.TwinCalcStop;
+import com.jjt.calc.service.ITwinCalcStopService;
 import com.jjt.common.annotation.Log;
 import com.jjt.common.core.controller.BaseController;
 import com.jjt.common.core.domain.AjaxResult;
+import com.jjt.common.core.domain.R;
+import com.jjt.common.core.page.TableDataInfo;
 import com.jjt.common.enums.BusinessType;
-import com.jjt.calc.domain.TwinCalcStop;
-import com.jjt.calc.service.ITwinCalcStopService;
 import com.jjt.common.utils.poi.ExcelUtil;
-import com.jjt.common.core.page.TableDataInfo;
+import com.jjt.utils.Tools;
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiOperation;
+import org.springframework.security.access.prepost.PreAuthorize;
+import org.springframework.web.bind.annotation.*;
+
+import javax.annotation.Resource;
+import javax.servlet.http.HttpServletResponse;
+import java.math.BigDecimal;
+import java.math.RoundingMode;
+import java.util.*;
+import java.util.concurrent.atomic.AtomicReference;
+import java.util.stream.Collectors;
 
 /**
  * 停机数据统计Controller
@@ -30,12 +30,14 @@ import com.jjt.common.core.page.TableDataInfo;
  * @author wukai
  * @date 2025-01-18
  */
-@Api(tags="停机数据统计")
+@Api(tags = "停机数据统计")
 @RestController
 @RequestMapping("/calc/calcStop")
-public class TwinCalcStopController extends BaseController{
+public class TwinCalcStopController extends BaseController {
     @Resource
     private ITwinCalcStopService twinCalcStopService;
+    @Resource
+    private ITwinDeviceService deviceService;
 
     /**
      * 查询停机数据统计列表
@@ -43,22 +45,77 @@ public class TwinCalcStopController extends BaseController{
     @ApiOperation("查询停机数据统计列表")
     @PreAuthorize("@ss.hasPermi('calc:calcStop:list')")
     @GetMapping("/list")
-    public TableDataInfo list(TwinCalcStop twinCalcStop)
-    {
+    public TableDataInfo list(TwinCalcStop twinCalcStop) {
         startPage();
         List<TwinCalcStop> list = twinCalcStopService.selectTwinCalcStopList(twinCalcStop);
         return getDataTable(list);
     }
 
     /**
+     * 查询停机数据统计列表
+     */
+    @ApiOperation("查询停机数据统计列表")
+    @GetMapping("/calc")
+    public R calc(TwinCalcStop twinCalcStop) {
+//        PageDomain pageDomain = TableSupport.buildPageRequest();
+//        Integer pageNum = pageDomain.getPageNum();
+//        int pageSize = 100000;
+//        String orderBy = SqlUtil.escapeOrderBySql(pageDomain.getOrderBy());
+//        Boolean reasonable = pageDomain.getReasonable();
+//        PageHelper.startPage(pageNum, pageSize, orderBy).setReasonable(reasonable);
+        List<TwinCalcStop> list = twinCalcStopService.selectTwinCalcStopList(twinCalcStop);
+        Map<Integer, List<TwinCalcStop>> stopDeviceGroup = list.stream().collect(Collectors.groupingBy(TwinCalcStop::getStopType, LinkedHashMap::new, Collectors.toList()));
+        int stopSize = 7;
+        Integer[] stopNum = new Integer[stopSize];
+        int totalNum = 0;
+        //停机时间
+        Long[] stopTime = new Long[stopSize];
+        Arrays.fill(stopTime, 0L);
+        AtomicReference<Long> totalTime = new AtomicReference<>(0L);
+        for (Map.Entry<Integer, List<TwinCalcStop>> entry : stopDeviceGroup.entrySet()) {
+            Integer stopType = entry.getKey();
+            int pos = stopType - 1;
+            List<TwinCalcStop> stops = entry.getValue();
+            stopNum[pos] = stops.size();
+            totalNum += stops.size();
+            stops.forEach(stop -> {
+                long t = (stop.getEndTime().getTime() - stop.getStartTime().getTime()) / 1000;
+                stopTime[pos] += t;
+                totalTime.updateAndGet(v -> v + t);
+            });
+        }
+
+        List<Map<String, Object>> calcList = new ArrayList<>();
+        for (int i = 0; i < stopNum.length; i++) {
+            if (stopNum[i] == null) {
+                continue;
+            }
+            Map<String, Object> map = new HashMap<>(16);
+            map.put("type", i + 1);
+            map.put("num", stopNum[i]);
+            map.put("time", Tools.convertHMS(stopTime[i]));
+            float percentNum = BigDecimal.valueOf(stopNum[i]).divide(BigDecimal.valueOf(totalNum), 4, RoundingMode.HALF_UP).floatValue();
+            float percentTimes = BigDecimal.valueOf(stopTime[i]).divide(BigDecimal.valueOf(totalTime.get()), 4, RoundingMode.HALF_UP).floatValue();
+            map.put("percentNum", percentNum);
+            map.put("percentTimes", percentTimes);
+            calcList.add(map);
+        }
+
+        Map<String, Object> result = new HashMap<>();
+        result.put("list", list);
+        result.put("calc", calcList);
+
+        return R.success(result);
+    }
+
+    /**
      * 导出停机数据统计列表
      */
     @ApiOperation("导出停机数据统计列表")
     @PreAuthorize("@ss.hasPermi('calc:calcStop:export')")
     @Log(title = "停机数据统计", businessType = BusinessType.EXPORT)
     @PostMapping("/export")
-    public void export(HttpServletResponse response, TwinCalcStop twinCalcStop)
-    {
+    public void export(HttpServletResponse response, TwinCalcStop twinCalcStop) {
         List<TwinCalcStop> list = twinCalcStopService.selectTwinCalcStopList(twinCalcStop);
         ExcelUtil<TwinCalcStop> util = new ExcelUtil<TwinCalcStop>(TwinCalcStop.class);
         util.exportExcel(response, list, "停机数据统计数据");
@@ -70,8 +127,7 @@ public class TwinCalcStopController extends BaseController{
     @ApiOperation("获取停机数据统计详细信息")
     @PreAuthorize("@ss.hasPermi('calc:calcStop:query')")
     @GetMapping(value = "/{id}")
-    public AjaxResult getInfo(@PathVariable("id") Long id)
-    {
+    public AjaxResult getInfo(@PathVariable("id") Long id) {
         return success(twinCalcStopService.selectTwinCalcStopById(id));
     }
 
@@ -82,8 +138,7 @@ public class TwinCalcStopController extends BaseController{
     @PreAuthorize("@ss.hasPermi('calc:calcStop:add')")
     @Log(title = "停机数据统计", businessType = BusinessType.INSERT)
     @PostMapping
-    public AjaxResult add(@RequestBody TwinCalcStop twinCalcStop)
-    {
+    public AjaxResult add(@RequestBody TwinCalcStop twinCalcStop) {
         return toAjax(twinCalcStopService.insertTwinCalcStop(twinCalcStop));
     }
 
@@ -94,8 +149,7 @@ public class TwinCalcStopController extends BaseController{
     @PreAuthorize("@ss.hasPermi('calc:calcStop:edit')")
     @Log(title = "停机数据统计", businessType = BusinessType.UPDATE)
     @PutMapping
-    public AjaxResult edit(@RequestBody TwinCalcStop twinCalcStop)
-    {
+    public AjaxResult edit(@RequestBody TwinCalcStop twinCalcStop) {
         return toAjax(twinCalcStopService.updateTwinCalcStop(twinCalcStop));
     }
 
@@ -105,9 +159,8 @@ public class TwinCalcStopController extends BaseController{
     @ApiOperation("删除停机数据统计")
     @PreAuthorize("@ss.hasPermi('calc:calcStop:remove')")
     @Log(title = "停机数据统计", businessType = BusinessType.DELETE)
-	@DeleteMapping("/{ids}")
-    public AjaxResult remove(@PathVariable Long[] ids)
-    {
+    @DeleteMapping("/{ids}")
+    public AjaxResult remove(@PathVariable Long[] ids) {
         return toAjax(twinCalcStopService.deleteTwinCalcStopByIds(ids));
     }
 }