浏览代码

首页库存展示相关

wukai 6 月之前
父节点
当前提交
ec0e25e2ae

+ 21 - 0
ruoyi-admin/src/main/java/com/ruoyi/biz/controller/ApiController.java

@@ -347,6 +347,27 @@ public class ApiController extends BaseController {
         }
     }
 
+    @ApiOperation("能耗统计报表")
+    @GetMapping("/export/tmp")
+    @CrossOrigin(origins = "*")
+    public void tmp(HttpServletResponse response) {
+        try (InputStream inputStream = getClass().getClassLoader().getResourceAsStream("tpl/tmp.xlsx"); XSSFWorkbook wb = new XSSFWorkbook(inputStream); OutputStream outputStream = new BufferedOutputStream(response.getOutputStream())) {
+            // 清空response
+            response.reset();
+            // 设置response的Header
+            response.setCharacterEncoding("UTF-8");
+            //Content-Disposition的作用:告知浏览器以何种方式显示响应返回的文件,用浏览器打开还是以附件的形式下载到本地保存
+            //attachment表示以附件方式下载 inline表示在线打开 "Content-Disposition: inline; filename=文件名.mp3"
+            // filename表示文件的默认名称,因为网络传输只支持URL编码的相关支付,因此需要将文件名URL编码后进行传输,前端收到后需要反编码才能获取到真正的名称
+            response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("能耗统计报表" + DateUtils.dateTimeNow() + ".xlsx", "UTF-8"));
+            response.setContentType("application/octet-stream");
+            wb.write(outputStream);
+            outputStream.flush();
+        } catch (IOException ex) {
+            ex.printStackTrace();
+        }
+    }
+
 
     @ApiOperation("平方米克重")
     @GetMapping("/gram-mass/total")

+ 84 - 0
ruoyi-admin/src/main/java/com/ruoyi/biz/task/OrderTask.java

@@ -1,10 +1,19 @@
 package com.ruoyi.biz.task;
 
+import com.ruoyi.common.constant.Constants;
+import com.ruoyi.common.utils.CacheUtils;
+import com.ruoyi.order.domain.MesStock;
+import com.ruoyi.order.domain.MesStockCalc;
 import com.ruoyi.order.service.ITwinOrderService;
+import com.ruoyi.order.utils.MssqlUtil;
 import org.springframework.stereotype.Component;
 
 import javax.annotation.Resource;
 import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.util.*;
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.stream.Collectors;
 
 /**
  * 定时任务调度测试
@@ -21,4 +30,79 @@ public class OrderTask {
         orderService.getOrder(localDate);
     }
 
+    public void stock() {
+        //减少7个小时,班组统计时间
+        LocalDate localDate = LocalDateTime.now().minusHours(7).toLocalDate();
+        List<MesStock> list = MssqlUtil.mesStock(localDate);
+        Map<String, Double> currStock = MssqlUtil.currStock();
+        HashMap<String, Object> initMap = new HashMap<>(16);
+        initMap.put("d-in", 0L);
+        initMap.put("d-out", 0L);
+        initMap.put("m-in", 0L);
+        initMap.put("m-out", 0L);
+        Map<String, Object> mapping = new HashMap<>(16);
+        mapping.put("WH01", "ycl");
+        mapping.put("WH03", "pt");
+        mapping.put("WH04", "bpb");
+        mapping.put("WH08", "cp");
+        Map<String, Object> result = new HashMap<>(16);
+        result.put("ycl", initMap.clone());
+        result.put("cp", initMap.clone());
+        result.put("bpb", initMap.clone());
+        result.put("pt", initMap.clone());
+
+        for (String key : currStock.keySet()) {
+            HashMap<String, Object> map = (HashMap<String, Object>) result.get(key);
+            map.put("curr", currStock.get(key));
+        }
+
+        //先按仓库分组
+        Map<String, List<MesStock>> codeMap = list.stream().collect(Collectors.groupingBy(MesStock::getCode));
+
+        codeMap.forEach((code, codeList) -> {
+            //按时间分组
+            Map<String, Object> map = (Map<String, Object>) result.get(mapping.get(code));
+            AtomicLong dIn = new AtomicLong((Long) map.get("d-in"));
+            AtomicLong dOut = new AtomicLong((Long) map.get("d-out"));
+            AtomicLong mIn = new AtomicLong((Long) map.get("m-in"));
+            AtomicLong mOut = new AtomicLong((Long) map.get("m-out"));
+
+            List<MesStockCalc> calcList = new ArrayList<>();
+            Map<LocalDate, List<MesStock>> dateMap = codeList.stream().collect(Collectors.groupingBy(MesStock::getDate));
+            dateMap.forEach((date, dateList) -> {
+                List<MesStock> inList = dateList.stream().filter(MesStock::getFlag).collect(Collectors.toList());
+                List<MesStock> outList = dateList.stream().filter(o -> !o.getFlag()).collect(Collectors.toList());
+                //统计入库
+                long inNum = inList.stream().mapToLong(MesStock::getTotal).sum();
+                //统计出库
+                long outNum = outList.stream().mapToLong(MesStock::getTotal).sum();
+
+                MesStockCalc calc = new MesStockCalc();
+                calc.setCode(code);
+                calc.setDate(date);
+                calc.setInNum(inNum);
+                calc.setOutNum(outNum);
+                calcList.add(calc);
+                if (localDate.isEqual(date)) {
+                    dIn.addAndGet(inNum);
+                    dOut.addAndGet(outNum);
+                    map.put("in", inList);
+                    map.put("out", outList);
+                }
+                if (localDate.getYear() == date.getYear() && localDate.getMonthValue() == date.getMonthValue()) {
+                    mIn.addAndGet(inNum);
+                    mOut.addAndGet(outNum);
+                }
+                map.put("d-in", dIn.longValue());
+                map.put("d-out", dOut.longValue());
+                map.put("m-in", mIn.longValue());
+                map.put("m-out", mOut.longValue());
+            });
+            calcList.sort(Comparator.comparing(MesStockCalc::getDate));
+            map.put("trend", calcList);
+        });
+
+        CacheUtils.put(Constants.IOT_TOKEN, Constants.VMS_STOCK, result);
+    }
+
 }

+ 9 - 14
ruoyi-admin/src/main/java/com/ruoyi/order/controller/OrderApiController.java

@@ -4,9 +4,10 @@ import com.ruoyi.biz.domain.TwinCalcDay;
 import com.ruoyi.biz.domain.TwinFormulaInfo;
 import com.ruoyi.biz.service.ITwinCalcDayService;
 import com.ruoyi.biz.service.ITwinFormulaInfoService;
+import com.ruoyi.common.constant.Constants;
 import com.ruoyi.common.core.controller.BaseController;
+import com.ruoyi.common.utils.CacheUtils;
 import com.ruoyi.common.utils.DateUtils;
-import com.ruoyi.order.domain.MesStock;
 import com.ruoyi.order.domain.TwinOrder;
 import com.ruoyi.order.domain.TwinOrderDetail;
 import com.ruoyi.order.domain.VmsStock;
@@ -63,19 +64,13 @@ public class OrderApiController extends BaseController {
     @CrossOrigin(origins = "*")
     @ResponseBody
     public Map<String, Object> mesStock() {
-        LocalDate localDate = LocalDate.now();
-        return MssqlUtil.mesStock(localDate);
-    }
-
-    @ApiOperation("查看当前库存")
-    @GetMapping("/api/mes-trend")
-    @CrossOrigin(origins = "*")
-    @ResponseBody
-    public Map<String, MesStock> mesTrend() {
-        LocalDate localDate = LocalDate.now();
-        Map<String, MesStock> map = MssqlUtil.mesTrend(localDate);
-        List<MesStock> mesList = new ArrayList<>(map.values());
-        return map;
+        Object d = CacheUtils.get(Constants.IOT_TOKEN, Constants.VMS_STOCK);
+        if (d != null) {
+            Map<String, Object> map = (Map<String, Object>) d;
+            return map;
+        } else {
+            return null;
+        }
     }
 
     @ApiOperation("导出白坯预测")

+ 11 - 14
ruoyi-admin/src/main/java/com/ruoyi/order/domain/MesStock.java

@@ -1,10 +1,11 @@
 package com.ruoyi.order.domain;
 
-import com.ruoyi.common.core.domain.BaseEntity;
 import io.swagger.annotations.ApiModel;
 import io.swagger.annotations.ApiModelProperty;
 import lombok.Data;
 
+import java.time.LocalDate;
+
 /**
  * mes库存
  *
@@ -13,26 +14,22 @@ import lombok.Data;
  */
 @ApiModel(value = "MesStock", description = "mes库存")
 @Data
-public class MesStock extends BaseEntity {
+public class MesStock {
     private static final long serialVersionUID = 1L;
     @ApiModelProperty("仓库编码")
     private String code;
     @ApiModelProperty("仓库名称")
     private String name;
-    @ApiModelProperty("仓库类型")
+    @ApiModelProperty("物料类型")
+    private String clas;
+    @ApiModelProperty("单据类型")
     private String type;
-    @ApiModelProperty("入库数量")
-    private Integer inNum;
-    @ApiModelProperty("出库数量")
-    private Integer outNum;
-    @ApiModelProperty("年")
-    private Integer year;
-    @ApiModelProperty("月")
-    private Integer month;
-    @ApiModelProperty("日")
-    private Integer day;
+    @ApiModelProperty("出/入库数量")
+    private Long total;
+    @ApiModelProperty("入库/出库 true:入库 false:出库")
+    private Boolean flag;
     @ApiModelProperty("时间")
-    private String date;
+    private LocalDate date;
     @ApiModelProperty("key")
     private String key;
 }

+ 29 - 0
ruoyi-admin/src/main/java/com/ruoyi/order/domain/MesStockCalc.java

@@ -0,0 +1,29 @@
+package com.ruoyi.order.domain;
+
+import io.swagger.annotations.ApiModel;
+import io.swagger.annotations.ApiModelProperty;
+import lombok.Data;
+
+import java.time.LocalDate;
+
+/**
+ * mes库存
+ *
+ * @author ruoyi
+ * @date 2024-12-30
+ */
+@ApiModel(value = "MesStockCalc", description = "mes库存统计")
+@Data
+public class MesStockCalc {
+    private static final long serialVersionUID = 1L;
+    @ApiModelProperty("仓库编码")
+    private String code;
+    @ApiModelProperty("入库数量")
+    private Long inNum;
+    @ApiModelProperty("出库数量")
+    private Long outNum;
+    @ApiModelProperty("时间")
+    private LocalDate date;
+    @ApiModelProperty("key")
+    private String key;
+}

+ 45 - 120
ruoyi-admin/src/main/java/com/ruoyi/order/utils/MssqlUtil.java

@@ -1,5 +1,6 @@
 package com.ruoyi.order.utils;
 
+import com.ruoyi.common.utils.StringUtils;
 import com.ruoyi.order.domain.MesStock;
 import com.ruoyi.order.domain.VmsStock;
 
@@ -100,40 +101,16 @@ public class MssqlUtil {
      * @param localDate 当前时间
      * @return 结果
      */
-    public static Map<String, Object> mesStock(LocalDate localDate) {
-        HashMap<String, Object> initMap = new HashMap<>();
-        initMap.put("d-in", 0);
-        initMap.put("d-out", 0);
-        initMap.put("m-in", 0);
-        initMap.put("m-out", 0);
-        Map<String, Object> mapping = new HashMap<>();
-        mapping.put("WH08", "cp");
-        mapping.put("WH01", "ycl");
-        mapping.put("WH04", "bpb");
-        mapping.put("WH03", "pt");
-        Map<String, Object> result = new HashMap<>(16);
-        Map<String, Object> ycl = new HashMap<>();
-        ycl.put("fdy", initMap.clone());
-        ycl.put("poy", initMap.clone());
-        ycl.put("dty", initMap.clone());
-
-        result.put("ycl", ycl);
-        result.put("cp", initMap.clone());
-        result.put("bpb", initMap.clone());
-        result.put("pt", initMap.clone());
-        String[] sqls = new String[4];
-        String inPrefix = "select 仓库编码 as code,仓库名称 as name,物料类型 as type,sum(当日入库量) as num,sum(try_cast(单托_支数_米数 as decimal)) as n2 from V_MES_SHANGJIA";
-        String outPrefix = "select 仓库编码 as code,仓库名称 as name,物料类型 as type,sum(当日出库量) as num,sum(try_cast(单托_支数_米数 as decimal)) as n2 from V_MES_FAYUN";
-        String where = " where DATEPART(year, 时间)=" + localDate.getYear() + " and DATEPART(month, 时间)=" + localDate.getMonthValue();
-        String suffix = " group by 仓库编码,仓库名称,物料类型";
-        //当日入库
-        sqls[0] = inPrefix + where + " and DATEPART(day, 时间)=" + localDate.getDayOfMonth() + suffix;
-        //当日出库
-        sqls[1] = outPrefix + where + " and DATEPART(day, 时间)=" + localDate.getDayOfMonth() + suffix;
-        //当月入库
-        sqls[2] = inPrefix + where + suffix;
-        //当月出库
-        sqls[3] = outPrefix + where + suffix;
+    public static List<MesStock> mesStock(LocalDate localDate) {
+        List<MesStock> list = new ArrayList<>();
+        LocalDate start = localDate.minusMonths(11).withDayOfMonth(1);
+        String outView = "SELECT [仓库编码] AS CODE,[仓库名称] AS NAME,[物料类型] AS CLASS,CAST(DATEADD( HOUR,- 7,时间 ) AS DATE) AS TIME,[当日出库量] AS TOTAL,TRY_CAST(单托_支数_米数 AS DECIMAL) AS NUM,[单重_KG] WEIGHT,[单据类型] TYPE FROM V_MES_FAYUN WHERE DATEADD(HOUR,- 7,时间)>='" + start + "'";
+        String inView = "SELECT [仓库编码] AS CODE,[仓库名称] AS NAME,[物料类型] AS CLASS,CAST(DATEADD( HOUR,- 7,时间 ) AS DATE) AS TIME,[当日入库量] AS TOTAL,TRY_CAST(单托_支数_米数 AS DECIMAL) AS NUM,[单重_KG] WEIGHT,[单据类型] TYPE FROM V_MES_SHANGJIA WHERE DATEADD(HOUR,- 7,时间)>='" + start + "'";
+        String[] sqls = new String[2];
+        //入库查询
+        sqls[0] = "SELECT CODE,NAME,CLASS,TYPE,TIME,SUM(TOTAL) AS TOTAL FROM (" + inView + ") T GROUP BY CODE,NAME,CLASS,TYPE,TIME";
+        //出库查询
+        sqls[1] = "SELECT CODE,NAME,CLASS,TYPE,TIME,SUM(TOTAL) AS TOTAL FROM (" + outView + ") T GROUP BY CODE,NAME,CLASS,TYPE,TIME";
         try {
             //第一步:加载数据库驱动程序,此时不需要实例化,因为会由容器自己负责
             Class.forName(DB_DRIVER);
@@ -145,46 +122,22 @@ public class MssqlUtil {
                 for (int i = 0; i < sqls.length; i++) {
                     try (ResultSet rs = statement.executeQuery(sqls[i])) {
                         while (rs.next()) {
-                            String code = rs.getString("code");
-                            String name = rs.getString("name");
-                            Map<String, Object> map = (Map<String, Object>) result.get(mapping.get(code));
-                            String type = rs.getString("type");
-                            String key = "";
-                            switch (i + 1) {
-                                case 1:
-                                    key = "d-in";
-                                    break;
-                                case 2:
-                                    key = "d-out";
-                                    break;
-                                case 3:
-                                    key = "m-in";
-                                    break;
-                                case 4:
-                                    key = "m-out";
-                                    break;
-                                default:
-                                    break;
-                            }
-                            Integer num = rs.getInt("num");
-                            Integer num2 = rs.getInt("n2");
-                            double num3 = rs.getDouble("n2");
-                            System.err.println(code+"\t"+num+"\t"+num2+"\t"+num3);
-                            if ("WH01".equals(code)) {
-                                Map<String, Object> cm;
-                                if (type.startsWith("FDY")) {
-                                    cm = (Map<String, Object>) map.get("fdy");
-                                } else if (type.startsWith("DTY")) {
-                                    cm = (Map<String, Object>) map.get("dty");
-                                } else {
-                                    cm = (Map<String, Object>) map.get("poy");
-                                }
-                                cm.put(key, num);
-                            } else {
-                                int oldNum = (int) map.get(key);
-                                map.put(key, num + oldNum);
-                            }
+                            String code = rs.getString("CODE");
+                            String name = rs.getString("NAME");
+                            String clas = rs.getString("CLASS");
+                            String type = rs.getString("TYPE");
+                            String date = rs.getString("TIME");
+                            Long total = rs.getLong("TOTAL");
 
+                            MesStock mesStock = new MesStock();
+                            mesStock.setCode(code);
+                            mesStock.setName(name);
+                            mesStock.setClas(clas);
+                            mesStock.setType(type);
+                            mesStock.setDate(LocalDate.parse(date));
+                            mesStock.setTotal(total);
+                            mesStock.setFlag(i == 0);
+                            list.add(mesStock);
                         }
                     }
                 }
@@ -192,23 +145,21 @@ public class MssqlUtil {
         } catch (Exception e) {
             e.printStackTrace();
         }
-        return result;
+        return list;
     }
 
     /**
-     * mes趋势
+     * 查询当前库存
      *
-     * @param localDate 当前时间
-     * @return 结果
+     * @return
      */
-    public static Map<String, MesStock> mesTrend(LocalDate localDate) {
-        Map<String, MesStock> map = new HashMap<>();
-        LocalDate start = localDate.minusMonths(11).withDayOfMonth(1);
-        String[] sqls = new String[2];
-        sqls[0] = "select 仓库编码 as code,仓库名称 as name,物料类型 as type,时间 as date,sum(当日出库量) num from V_MES_FAYUN where convert(date,时间)>=convert(date,'" + start + "')" +
-                " group by 仓库编码,仓库名称,物料类型,时间  order by 时间";
-        sqls[1] = "select 仓库编码 as code,仓库名称 as name,物料类型 as type,时间 as date,sum(当日入库量) num from V_MES_SHANGJIA where convert(date,时间)>=convert(date,'" + start + "')" +
-                " group by 仓库编码,仓库名称,物料类型,时间  order by 时间";
+    public static Map<String, Double> currStock() {
+        Map<String, Double> result = new HashMap<>();
+        Map<String, String> mapping = new HashMap<>();
+        mapping.put("WH01-原料仓库", "ycl");
+        mapping.put("WH03-盘头仓库", "pt");
+        mapping.put("WH04-白坯仓库", "bpb");
+        mapping.put("WH08-成辅仓库", "cp");
         try {
             //第一步:加载数据库驱动程序,此时不需要实例化,因为会由容器自己负责
             Class.forName(DB_DRIVER);
@@ -217,37 +168,14 @@ public class MssqlUtil {
                 //第三步:进行数据库的数据操作
                 //取得Statement对象
                 //当日出库
-                for (int i = 0; i < sqls.length; i++) {
-                    try (ResultSet rs = statement.executeQuery(sqls[i])) {
-                        while (rs.next()) {
-                            String code = rs.getString("code");
-                            String name = rs.getString("name");
-                            String type = rs.getString("type");
-                            String date = rs.getString("date");
-                            Integer num = rs.getInt("num");
-                            String key = code + type + date;
-                            String[] tmp = date.split("-");
-                            int year = Integer.parseInt(tmp[0]);
-                            int month = Integer.parseInt(tmp[1]);
-                            int day = Integer.parseInt(tmp[2]);
-                            MesStock mesStock = map.get(key);
-                            if (mesStock == null) {
-                                mesStock = new MesStock();
-                            }
-                            mesStock.setKey(key);
-                            mesStock.setDate(date);
-                            mesStock.setYear(year);
-                            mesStock.setMonth(month);
-                            mesStock.setDay(day);
-                            mesStock.setCode(code);
-                            mesStock.setName(name);
-                            mesStock.setType(type);
-                            if (i == 0) {
-                                mesStock.setOutNum(num);
-                            } else {
-                                mesStock.setInNum(num);
-                            }
-                            map.put(key, mesStock);
+                String sql = "SELECT WAREHOUSE_ID AS CODE,SUM(QTY) AS TOTAL FROM V_WMS_STOCK GROUP BY WAREHOUSE_ID";
+                try (ResultSet rs = statement.executeQuery(sql)) {
+                    while (rs.next()) {
+                        String code = rs.getString("CODE");
+                        Double total = rs.getDouble("TOTAL");
+                        String key = mapping.get(code);
+                        if (StringUtils.isNotEmpty(key)) {
+                            result.put(key, total);
                         }
                     }
                 }
@@ -255,12 +183,9 @@ public class MssqlUtil {
         } catch (Exception e) {
             e.printStackTrace();
         }
-
-        return map;
+        return result;
     }
 
     public static void main(String[] args) throws Exception {
-        Map<String, Object> map = mesStock(LocalDate.now());
-        System.err.println(map);
     }
 }

二进制
ruoyi-admin/src/main/resources/tpl/tmp.xlsx


+ 140 - 100
ruoyi-admin/src/test/java/com/jjt/MssqlTest.java

@@ -1,14 +1,17 @@
 package com.jjt;
 
 import com.ruoyi.order.domain.MesStock;
+import com.ruoyi.order.domain.MesStockCalc;
 
 import java.sql.Connection;
 import java.sql.DriverManager;
 import java.sql.ResultSet;
 import java.sql.Statement;
 import java.time.LocalDate;
-import java.util.HashMap;
-import java.util.Map;
+import java.time.LocalDateTime;
+import java.util.*;
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.stream.Collectors;
 
 /**
  * @author wukai
@@ -32,29 +35,17 @@ public class MssqlTest {
      */
     private static final String PASSWORD = "MES";
 
-    public static Map<String, Object> mesStock(LocalDate localDate) {
-        Map<String, Object> mapping = new HashMap<>();
-        mapping.put("成辅仓库", "cp");
-        mapping.put("原料仓库", "ycl");
-        mapping.put("白坯仓库", "bpb");
-        mapping.put("盘头仓库", "pt");
-        Map<String, Object> result = new HashMap<>(16);
-        Map<String, Object> ycl = new HashMap<>();
-        ycl.put("fdy", new HashMap<>());
-        ycl.put("poy", new HashMap<>());
-        result.put("ycl", ycl);
-        result.put("cp", new HashMap<>());
-        result.put("bpb", new HashMap<>());
-        result.put("pt", new HashMap<>());
-        String[] sqls = new String[4];
-        //当日入库
-        sqls[0] = "select 仓库名称 as name,物料类型 as type,sum(当日入库量) as num from V_MES_SHANGJIA where DATEPART(year, 时间)=" + localDate.getYear() + " and DATEPART(month, 时间)=" + localDate.getMonthValue() + " and DATEPART(day, 时间)=" + localDate.getDayOfMonth() + " group by 仓库名称,物料类型";
-        //当日出库
-        sqls[1] = "select 仓库名称 as name,物料类型 as type,sum(当日出库量) as num from V_MES_FAYUN where DATEPART(year, 时间)=" + localDate.getYear() + " and DATEPART(month, 时间)=" + localDate.getMonthValue() + " and DATEPART(day, 时间)=" + localDate.getDayOfMonth() + " group by 仓库名称,物料类型";
-        //当月入库
-        sqls[2] = "select 仓库名称 as name,物料类型 as type,sum(当日入库量) as num from V_MES_SHANGJIA where DATEPART(year, 时间)=" + localDate.getYear() + " and DATEPART(month, 时间)=" + localDate.getMonthValue() + " group by 仓库名称,物料类型";
-        //当月出库
-        sqls[3] = "select 仓库名称 as name,物料类型 as type,sum(当日出库量) as num from V_MES_FAYUN where DATEPART(year, 时间)=" + localDate.getYear() + " and DATEPART(month, 时间)=" + localDate.getMonthValue() + " group by 仓库名称,物料类型";
+
+    public static void trend(LocalDate localDate) {
+        Map<String, MesStock> map = new HashMap<>();
+//        LocalDate start = localDate.minusMonths(11).withDayOfMonth(1);
+        LocalDate start = localDate.minusDays(1);
+        System.err.println(start);
+        String[] sqls = new String[2];
+        sqls[0] = "select 仓库编码 as code,仓库名称 as name,物料类型 as type,时间 as date,sum(当日出库量) num from V_MES_FAYUN where convert(date,时间)>=convert(date,'" + start + "')" +
+                " group by 仓库编码,仓库名称,物料类型,时间  order by 时间";
+        sqls[1] = "select 仓库编码 as code,仓库名称 as name,物料类型 as type,时间 as date,sum(当日入库量) num from V_MES_SHANGJIA where convert(date,时间)>=convert(date,'" + start + "')" +
+                " group by 仓库编码,仓库名称,物料类型,时间  order by 时间";
         try {
             //第一步:加载数据库驱动程序,此时不需要实例化,因为会由容器自己负责
             Class.forName(DB_DRIVER);
@@ -66,40 +57,34 @@ public class MssqlTest {
                 for (int i = 0; i < sqls.length; i++) {
                     try (ResultSet rs = statement.executeQuery(sqls[i])) {
                         while (rs.next()) {
-                            String name = rs.getString("name");
-                            Map<String, Object> map = (Map<String, Object>) result.get(mapping.get(name));
-                            String type = rs.getString("type");
-                            String key = "";
-                            switch (i + 1) {
-                                case 1:
-                                    key = "d-in";
-                                    break;
-                                case 2:
-                                    key = "d-out";
-                                    break;
-                                case 3:
-                                    key = "m-in";
-                                    break;
-                                case 4:
-                                    key = "m-out";
-                                    break;
-                                default:
-                                    break;
-                            }
-                            Integer num = rs.getInt("num");
-
-                            if ("原料仓库".equals(name)) {
-                                Map<String, Object> cm;
-                                if (type.startsWith("FDY")) {
-                                    cm = (Map<String, Object>) map.get("fdy");
-                                } else {
-                                    cm = (Map<String, Object>) map.get("poy");
-                                }
-                                cm.put(key, num);
-                            } else {
-                                map.put(key, num);
-                            }
-
+//                            String code = rs.getString("code");
+//                            String name = rs.getString("name");
+//                            String type = rs.getString("type");
+//                            String date = rs.getString("date");
+//                            Long num = rs.getInt("num");
+//                            String key = code + type + date;
+//                            String[] tmp = date.split("-");
+//                            int year = Integer.parseInt(tmp[0]);
+//                            int month = Integer.parseInt(tmp[1]);
+//                            int day = Integer.parseInt(tmp[2]);
+//                            MesStock mesStock = map.get(key);
+//                            if (mesStock == null) {
+//                                mesStock = new MesStock();
+//                            }
+//                            mesStock.setKey(key);
+//                            mesStock.setDate(LocalDate.parse(date));
+//                            mesStock.setYear(year);
+//                            mesStock.setMonth(month);
+//                            mesStock.setDay(day);
+//                            mesStock.setCode(code);
+//                            mesStock.setName(name);
+//                            mesStock.setType(type);
+//                            if (i == 0) {
+//                                mesStock.setOutNum(num);
+//                            } else {
+//                                mesStock.setInNum(num);
+//                            }
+//                            map.put(key, mesStock);
                         }
                     }
                 }
@@ -107,19 +92,20 @@ public class MssqlTest {
         } catch (Exception e) {
             e.printStackTrace();
         }
-        return result;
+
+        System.err.println(map.size());
     }
 
-    public static void trend(LocalDate localDate) {
-        Map<String, MesStock> map = new HashMap<>();
-//        LocalDate start = localDate.minusMonths(11).withDayOfMonth(1);
-        LocalDate start = localDate.minusDays(1);
-        System.err.println(start);
-        String[] sqls = new String[2];
-        sqls[0] = "select 仓库编码 as code,仓库名称 as name,物料类型 as type,时间 as date,sum(当日出库量) num from V_MES_FAYUN where convert(date,时间)>=convert(date,'" + start + "')" +
-                " group by 仓库编码,仓库名称,物料类型,时间  order by 时间";
-        sqls[1] = "select 仓库编码 as code,仓库名称 as name,物料类型 as type,时间 as date,sum(当日入库量) num from V_MES_SHANGJIA where convert(date,时间)>=convert(date,'" + start + "')" +
-                " group by 仓库编码,仓库名称,物料类型,时间  order by 时间";
+    public static List<MesStock> mesStock(LocalDate localDate) {
+        List<MesStock> trendList = new ArrayList<>();
+        LocalDate start = localDate.minusMonths(11).withDayOfMonth(1);
+        String outView = "SELECT [仓库编码] AS CODE,[仓库名称] AS NAME,[物料类型] AS CLASS,CAST(DATEADD( HOUR,- 7,时间 ) AS DATE) AS TIME,[当日出库量] AS TOTAL,TRY_CAST(单托_支数_米数 AS DECIMAL) AS NUM,[单重_KG] WEIGHT,[单据类型] TYPE FROM V_MES_FAYUN WHERE DATEADD(HOUR,- 7,时间)>='" + start + "'";
+        String inView = "SELECT [仓库编码] AS CODE,[仓库名称] AS NAME,[物料类型] AS CLASS,CAST(DATEADD( HOUR,- 7,时间 ) AS DATE) AS TIME,[当日入库量] AS TOTAL,TRY_CAST(单托_支数_米数 AS DECIMAL) AS NUM,[单重_KG] WEIGHT,[单据类型] TYPE FROM V_MES_SHANGJIA WHERE DATEADD(HOUR,- 7,时间)>='" + start + "'";
+        String[] trendSql = new String[2];
+        //入库查询
+        trendSql[0] = "SELECT CODE,NAME,CLASS,TYPE,TIME,SUM(TOTAL) AS TOTAL FROM (" + inView + ") T GROUP BY CODE,NAME,CLASS,TYPE,TIME";
+        //出库查询
+        trendSql[1] = "SELECT CODE,NAME,CLASS,TYPE,TIME,SUM(TOTAL) AS TOTAL FROM (" + outView + ") T GROUP BY CODE,NAME,CLASS,TYPE,TIME";
         try {
             //第一步:加载数据库驱动程序,此时不需要实例化,因为会由容器自己负责
             Class.forName(DB_DRIVER);
@@ -128,37 +114,25 @@ public class MssqlTest {
                 //第三步:进行数据库的数据操作
                 //取得Statement对象
                 //当日出库
-                for (int i = 0; i < sqls.length; i++) {
-                    try (ResultSet rs = statement.executeQuery(sqls[i])) {
+                for (int i = 0; i < trendSql.length; i++) {
+                    try (ResultSet rs = statement.executeQuery(trendSql[i])) {
                         while (rs.next()) {
-                            String code = rs.getString("code");
-                            String name = rs.getString("name");
-                            String type = rs.getString("type");
-                            String date = rs.getString("date");
-                            Integer num = rs.getInt("num");
-                            String key = code + type + date;
-                            String[] tmp = date.split("-");
-                            int year = Integer.parseInt(tmp[0]);
-                            int month = Integer.parseInt(tmp[1]);
-                            int day = Integer.parseInt(tmp[2]);
-                            MesStock mesStock = map.get(key);
-                            if (mesStock == null) {
-                                mesStock = new MesStock();
-                            }
-                            mesStock.setKey(key);
-                            mesStock.setDate(date);
-                            mesStock.setYear(year);
-                            mesStock.setMonth(month);
-                            mesStock.setDay(day);
+                            String code = rs.getString("CODE");
+                            String name = rs.getString("NAME");
+                            String clas = rs.getString("CLASS");
+                            String type = rs.getString("TYPE");
+                            String date = rs.getString("TIME");
+                            Long total = rs.getLong("TOTAL");
+
+                            MesStock mesStock = new MesStock();
                             mesStock.setCode(code);
                             mesStock.setName(name);
+                            mesStock.setClas(clas);
                             mesStock.setType(type);
-                            if (i == 0) {
-                                mesStock.setOutNum(num);
-                            } else {
-                                mesStock.setInNum(num);
-                            }
-                            map.put(key, mesStock);
+                            mesStock.setDate(LocalDate.parse(date));
+                            mesStock.setTotal(total);
+                            mesStock.setFlag(i == 0);
+                            trendList.add(mesStock);
                         }
                     }
                 }
@@ -167,12 +141,78 @@ public class MssqlTest {
             e.printStackTrace();
         }
 
-        System.err.println(map.size());
+        return trendList;
     }
 
     public static void main(String[] args) throws Exception {
-        LocalDate localDate = LocalDate.now();
-        trend(localDate);
+        //减少7个小时,班组统计时间
+        LocalDate localDate = LocalDateTime.now().minusHours(7).toLocalDate();
+        List<MesStock> list = mesStock(localDate);
+        HashMap<String, Object> initMap = new HashMap<>(16);
+        initMap.put("d-in", 0L);
+        initMap.put("d-out", 0L);
+        initMap.put("m-in", 0L);
+        initMap.put("m-out", 0L);
+        Map<String, Object> mapping = new HashMap<>(16);
+        mapping.put("WH01", "ycl");
+        mapping.put("WH03", "pt");
+        mapping.put("WH04", "bpb");
+        mapping.put("WH08", "cp");
+        Map<String, Object> result = new HashMap<>(16);
+        result.put("ycl", initMap.clone());
+        result.put("cp", initMap.clone());
+        result.put("bpb", initMap.clone());
+        result.put("pt", initMap.clone());
+        //先按仓库分组
+        Map<String, List<MesStock>> codeMap = list.stream().collect(Collectors.groupingBy(MesStock::getCode));
+
+        codeMap.forEach((code, codeList) -> {
+            //按时间分组
+            Map<String, Object> map = (Map<String, Object>) result.get(mapping.get(code));
+            AtomicLong dIn = new AtomicLong((Long) map.get("d-in"));
+            AtomicLong dOut = new AtomicLong((Long) map.get("d-out"));
+            AtomicLong mIn = new AtomicLong((Long) map.get("m-in"));
+            AtomicLong mOut = new AtomicLong((Long) map.get("m-out"));
+
+            List<MesStockCalc> calcList = new ArrayList<>();
+            Map<LocalDate, List<MesStock>> dateMap = codeList.stream().collect(Collectors.groupingBy(MesStock::getDate));
+            dateMap.forEach((date, dateList) -> {
+                List<MesStock> inList = dateList.stream().filter(MesStock::getFlag).collect(Collectors.toList());
+                List<MesStock> outList = dateList.stream().filter(o -> !o.getFlag()).collect(Collectors.toList());
+                //统计入库
+                long inNum = inList.stream().mapToLong(MesStock::getTotal).sum();
+                //统计出库
+                long outNum = outList.stream().mapToLong(MesStock::getTotal).sum();
+
+                MesStockCalc calc = new MesStockCalc();
+                calc.setCode(code);
+                calc.setDate(date);
+                calc.setInNum(inNum);
+                calc.setOutNum(outNum);
+                calcList.add(calc);
+                if (localDate.isEqual(date)) {
+                    dIn.addAndGet(inNum);
+                    dOut.addAndGet(outNum);
+                    map.put("in", inList);
+                    map.put("out", outList);
+                }
+                if (localDate.getYear() == date.getYear() && localDate.getMonthValue() == date.getMonthValue()) {
+                    mIn.addAndGet(inNum);
+                    mOut.addAndGet(outNum);
+                }
+                map.put("d-in", dIn.longValue());
+                map.put("d-out", dOut.longValue());
+                map.put("m-in", mIn.longValue());
+                map.put("m-out", mOut.longValue());
+            });
+            calcList.sort(Comparator.comparing(MesStockCalc::getDate));
+            map.put("trend", calcList);
+        });
+        System.err.println(result);
+//        //按仓库和时间分组
+//        Map<String, List<MesStock>> map = list.stream().collect(Collectors.groupingBy(o -> o.getCode() + ";" + o.getDate()));
+//        List<MesStockCalc> calcList = new ArrayList<>();
+
 
     }
 }

+ 5 - 0
ruoyi-common/src/main/java/com/ruoyi/common/constant/Constants.java

@@ -97,6 +97,11 @@ public class Constants {
      * 停机明细
      */
     public static final String STOP_DETAIL = "stopDetail";
+
+    /**
+     * 库存情况
+     */
+    public static final String VMS_STOCK = "vmsStock";
     /**
      * 平方米克重
      */