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