diff --git a/src/main/java/com/dreamchaser/depository_manage/controller/DepositoryController.java b/src/main/java/com/dreamchaser/depository_manage/controller/DepositoryController.java index 13c1922e..eca4a84d 100644 --- a/src/main/java/com/dreamchaser/depository_manage/controller/DepositoryController.java +++ b/src/main/java/com/dreamchaser/depository_manage/controller/DepositoryController.java @@ -48,217 +48,6 @@ public class DepositoryController { PlaceService placeService; - /** - * 获取之前仓库的库存值 - * - * @return - */ - public Map getBeforeInventoryByDName(DepositoryService depositoryService, DepositoryRecordService depositoryRecordService, UserByPort user) { - List result = new ArrayList<>(); - List days = DateUtil.getLastTimeInterval(); - Map depositoryAllNameAndId = depositoryService.findDepositoryAllNameAndId(user); - Iterator it = depositoryAllNameAndId.keySet().iterator(); - Map map = new HashMap<>(); - // 获取本周仓库库存 - Map thisWeekInventoryByDName = getThisWeekInventoryByDName(depositoryService, depositoryRecordService, user); - while (it.hasNext()) { - int i; - Object key = it.next(); - Integer thisDayInventory = 0; - // 本周周一的总量 - thisDayInventory = ((List) thisWeekInventoryByDName.get(key.toString())).get(0); - result.add(thisDayInventory); - int j = 0; - for (i = days.size() - 1; i > 0; i--) { - // 遍历 Map并计算各仓库的入库数 - Integer val = (Integer) depositoryAllNameAndId.get(key); - // 获取一段时间内的入库额度 - Integer depositoryRecordByDateByIn1 = depositoryRecordService.findApplicationInRecordByDate(days.get(i), days.get(i - 1), val); - // 获取一段时间内的出库库额度 - Integer depositoryRecordByDateByOut1 = depositoryRecordService.findApplicationOutRecordByDate(days.get(i), days.get(i - 1), val); - Integer wareHouseCount1 = result.get(j++) - depositoryRecordByDateByIn1 + depositoryRecordByDateByOut1; - result.add(wareHouseCount1); - } - result.remove(0); - Collections.reverse(result); - map.put(key.toString(), ((ArrayList) result).clone()); - result.clear(); - } - return map; - } - - /** - * 获取本周库存容量 - * - * @param depositoryService - * @param depositoryRecordService - * @return - */ - public Map getThisWeekInventoryByDName(DepositoryService depositoryService, DepositoryRecordService depositoryRecordService, UserByPort user) { - Calendar instance = Calendar.getInstance(); - SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); - int weekDay = instance.get(Calendar.DAY_OF_WEEK); - weekDay = weekDay - 1; - // 获取所有仓库名称 - Map depositoryAllNameAndId = depositoryService.findDepositoryAllNameAndId(user); - Iterator it = depositoryAllNameAndId.keySet().iterator(); - //result用来接收每个仓库每天的库存 - ArrayList result = new ArrayList<>(); - // map用来存储仓库与库存的映射 - Map map = new HashMap<>(); - // 只获取当天 - List toDayInventoryByDNameTest = depositoryService.getToDayInventoryByDName(); - if (weekDay == 1) { - while (it.hasNext()) { - int i; - String key = it.next().toString(); - String dname = key.split(",")[0]; - Double inventoryByDname = getInventoryByDname(dname, toDayInventoryByDNameTest); - result.add(inventoryByDname); - map.put(key, result.clone()); - result.clear(); - } - } else { - int now = instance.get(Calendar.DAY_OF_WEEK) - 1 == 0 ? 7 : instance.get(Calendar.DAY_OF_WEEK) - 1; - List days = new ArrayList<>(); - days.add(DateUtil.DateTimeByDayToTimeStamp(formatter.format(instance.getTime()))); - Boolean flag = false; - while (now - 1 > 0) { - now--; - instance.add(Calendar.DATE, -1); - long format = DateUtil.DateTimeByDayToTimeStamp(formatter.format(instance.getTime())); - days.add(format); - } - while (it.hasNext()) { - int i; - String key = it.next().toString(); - int j = 0; - - // 获取今天容量 - String dname = key.split(",")[0]; - Double inventoryByDname = getInventoryByDname(dname, toDayInventoryByDNameTest); - result.add(inventoryByDname); - // 判断当天是否有出入口记录 - - Calendar IsInOut = Calendar.getInstance(); - Long start = DateUtil.DateTimeByDayToTimeStamp(formatter.format(IsInOut.getTime())); - IsInOut.add(Calendar.DATE, 1); - Long end = DateUtil.DateTimeByDayToTimeStamp(formatter.format(IsInOut.getTime())); - // 判断今天是否有出入库记录 - Integer inCount = depositoryRecordService.findApplicationInRecordByDate(end, start, null); - Integer OutCount = depositoryRecordService.findApplicationOutRecordByDate(end, start, null); - if (inCount > 0 || OutCount > 0) { - flag = true; - } - for (i = 0; i < days.size() - 1; i++) { - // 获取本周周一日期 - String mondayOnThisWeek = DateUtil.getMondayOnThisWeek(); - if (mondayOnThisWeek.equals(DateUtil.TimeStampToDateTime(days.get(i)))) { - break; - } - // 当前日期额度 - Double aDouble = result.get(j++); - // 获取当前仓库 - Integer val = (Integer) depositoryAllNameAndId.get(key); - // 如果今天有出入库记录 - if (flag) { - Integer inRecordByDate = depositoryRecordService.findApplicationInRecordByDate(end, start, val); - Integer OutRecordByDate = depositoryRecordService.findApplicationOutRecordByDate(end, start, val); - aDouble = aDouble - inRecordByDate + OutRecordByDate; - flag = false; - } - // 获取一段时间内的入库额度 - //测试 - Integer depositoryRecordByDateByIn1 = depositoryRecordService.findApplicationInRecordByDate(days.get(i), days.get(i + 1), val); - // 获取一段时间内的出库额度 - Integer depositoryRecordByDateByOut1 = depositoryRecordService.findApplicationOutRecordByDate(days.get(i), days.get(i + 1), val); - Double warehouserCount1 = aDouble - depositoryRecordByDateByIn1 + depositoryRecordByDateByOut1; - result.add(warehouserCount1); - } - Collections.reverse(result); - map.put(key.toString(), result.clone()); - result.clear(); - } - } - return map; - } - - /** - * 获取之前各月的库存容量 - * - * @param depositoryService - * @param depositoryRecordService - * @return - */ - public List getBeforeInventoryByMonth(DepositoryService depositoryService, Map todayInventory, DepositoryRecordService depositoryRecordService, UserByPort userByPort) { - List depositoryAll = depositoryService.findDepositoryByAdminorgAndUser(userByPort); - // 获取该用户管理的仓库 - Map previousMonth = DateUtil.getPreviousMonth(); - List months = (List) previousMonth.get("months"); - List sourceList = (List) previousMonth.get("sourceList"); - Calendar instance = Calendar.getInstance(); - SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); - instance.add(Calendar.MONTH, 1); - instance.set(Calendar.DATE, 1); - List result = new ArrayList<>(); - String month = formatter.format(instance.getTime()); - List toDayInventoryByDNameTest = depositoryService.getToDayInventoryByDName(); - for (int i = 0; i < depositoryAll.size(); i++) { - String dname = depositoryAll.get(i).getDname(); - Double todayDepositoryInventory = getInventoryByDname(dname, toDayInventoryByDNameTest); - result.add(todayDepositoryInventory); - todayInventory.put(dname, todayDepositoryInventory); - ((Map) sourceList.get(0)).put(dname, result.get(i)); - } - for (int num = 0; num < months.size() - 1; num++) { - int k = 0; - for (Depository depository : depositoryAll) { - // 获取该仓库本月库存 - Map parmIn = new HashMap<>(); - Map parmOut = new HashMap<>(); - // 获取每月入库总额 - parmIn.put("type", 1); - parmIn.put("state", "已入库"); - parmIn.put("depository_id", depository.getId()); - parmIn.put("start", months.get(num + 1)); - parmIn.put("end", months.get(num)); - // 获取每月出库总额 - parmOut.put("type", 2); - parmOut.put("state", "已出库"); - parmOut.put("depository_id", depository.getId()); - parmOut.put("start", months.get(num + 1)); - parmOut.put("end", months.get(num)); - // 获取月份中入库物料的总额 - // 测试 - Double wareHouseInCountByMonth1 = depositoryRecordService.calMaterialTypeTopCount(parmIn); - // 获取月份中出库物料的总额 - // 测试 - Double wareHouseOutCountByMonth1 = depositoryRecordService.calMaterialTypeTopCount(parmOut); - // 获取当前月库存容量 - // 测试 - Double wareHouseCount1 = result.get(k++) - wareHouseInCountByMonth1 + wareHouseOutCountByMonth1; - result.add(wareHouseCount1); - ((Map) sourceList.get(num)).put(depository.getDname(), wareHouseCount1); - } - for (int i = 0; i < depositoryAll.size(); i++) { - result.remove(0); - } - } - for (int i = 0; i < depositoryAll.size(); i++) { - String dname = depositoryAll.get(i).getDname(); - Double todayDepositoryInventory = getInventoryByDname(dname, toDayInventoryByDNameTest); - ((Map) sourceList.get(0)).put(dname, todayDepositoryInventory); - } - List barSource = new ArrayList<>(); - barSource.add("month"); - for (int i = 0; i < depositoryAll.size(); i++) { - barSource.add(depositoryAll.get(i).getDname()); - } - sourceList.add(barSource); - return sourceList; - } - - /** * 获取当前仓库下的库存 * @@ -527,7 +316,7 @@ public class DepositoryController { // 获取各类别库存容量 @RequestMapping(value = "/layui/echart_back_invetory", method = RequestMethod.GET) @ResponseBody - public Map echart_back_MaterialTypeInvetory(@RequestParam Map map, HttpServletRequest request) { + public Map echart_back_MaterialTypeInventory(@RequestParam Map map, HttpServletRequest request) { String typeName = (String) map.get("typeName"); String type = (String) map.get("type"); // 获取本月及之前月份各种类别入/出库总量 @@ -599,9 +388,32 @@ public class DepositoryController { depository_data.put("week", show_weekday); if ("0".equals(type)) { - // 如果是获取库存 - depository_data.put("ThisWeekInventory", getThisWeekInventoryByDName(depositoryService, depositoryRecordService, userByPort)); - depository_data.put("BeforeInventory", getBeforeInventoryByDName(depositoryService, depositoryRecordService, userByPort)); + ExecutorService exs = Executors.newFixedThreadPool(depositoryAllNameAndId.size()); + // 结果集 + List> futureList = new ArrayList>(); + // 1.定义CompletionService + CompletionService completionService = new ExecutorCompletionService(exs); + + + // 获取当前各仓库的库存数 + List toDayInventoryByDName = depositoryService.getToDayInventoryByDName(); + while (it.hasNext()) { + Object next = it.next(); + getInventoryByDate getApplicationRecordByDate = new getInventoryByDate(next.toString(), days, depositoryAllNameAndId, toDayInventoryByDName); + Future future = completionService.submit(getApplicationRecordByDate); + futureList.add(future); + } + + for (int i = 0; i < depositoryAllNameAndId.size(); i++) { + Object result = null; + try { + result = completionService.take().get(); + } catch (InterruptedException | ExecutionException e) { + e.printStackTrace(); + } + show_data.putAll((Map) result); + } + depository_data.put("data", show_data); } else{ // 定义线程 @@ -667,8 +479,8 @@ public class DepositoryController { depository_data.put("week", show_weekday); if ("0".equals(type)) { // 如果是获取库存 - depository_data.put("ThisWeekInventory", getThisWeekInventoryByDName(depositoryService, depositoryRecordService, userByPort)); - depository_data.put("BeforeInventory", getBeforeInventoryByDName(depositoryService, depositoryRecordService, userByPort)); + Map inventoryLineChartData = getInventoryLineChartData(depositoryAllNameAndId); + depository_data.put("data",inventoryLineChartData); } else { Map lineChartData = getLineChartData(depositoryAllNameAndId, type); depository_data.put("data", lineChartData); @@ -684,7 +496,7 @@ public class DepositoryController { */ @RequestMapping(value = "/layui/echart_back", method = RequestMethod.GET) @ResponseBody - public Map learnLayuiAndEchart_back(@RequestParam("type") String type, HttpServletRequest request) { + public Map echart_back(@RequestParam("type") String type, HttpServletRequest request) { // 获取当前登录用户 String token = request.getHeader("user-token"); if (token == null) { @@ -716,8 +528,6 @@ public class DepositoryController { Map lineChartData = new HashMap<>(); // 旭日图数据 List sunburstChartData = new ArrayList<>(); - // 各仓库当前库存数目 - Map todayInventory = new HashMap<>(); // 用于标准开启了几个线程 int taskNum = 0; @@ -726,49 +536,35 @@ public class DepositoryController { // 获取库存图 if ("0".equals(type)) { - // 如果是获取库存 - Future inventoryFutureForSunburstChartData = completionService.submit(new distributeTasks(depositoryAllNameAndId, type, 4, userByPort, depositoryName)); - futureList.add(inventoryFutureForSunburstChartData); - distributeTasks distributeTasks1 = new distributeTasks(depositoryAllNameAndId, type, 5, userByPort, depositoryName); - distributeTasks1.setTodayInventory(todayInventory); - distributeTasks1.setDepositoryService(depositoryService); - distributeTasks1.setDepositoryRecordService(depositoryRecordService); - distributeTasks distributeTasks2 = new distributeTasks(depositoryAllNameAndId, type, 6, userByPort, depositoryName); - distributeTasks2.setDepositoryService(depositoryService); - distributeTasks2.setDepositoryRecordService(depositoryRecordService); - distributeTasks distributeTasks3 = new distributeTasks(depositoryAllNameAndId, type, 7, userByPort, depositoryName); - distributeTasks3.setDepositoryService(depositoryService); - distributeTasks3.setDepositoryRecordService(depositoryRecordService); - Future future1 = completionService.submit(distributeTasks1); - Future future2 = completionService.submit(distributeTasks2); - Future future3 = completionService.submit(distributeTasks3); - futureList.add(future1); - futureList.add(future2); - futureList.add(future3); - List beforeInventoryByMonth = null; - Map thisWeekInventoryByDName = null; - Map beforeInventoryOnMap = null; - for (int i = 0; i < 3; i++) { + // 获取库存旭日图 + Future futureForSunburstData = completionService.submit(new distributeTasks(depositoryAllNameAndId, type, 5, userByPort, depositoryName)); + futureList.add(futureForSunburstData); + taskNum += 1; + + // 获取本周库存图 + Future futureForLineData = completionService.submit(new distributeTasks(depositoryAllNameAndId, type, 3, userByPort, depositoryName)); + futureList.add(futureForLineData); + taskNum += 1; + + + + + for (int i = 0; i < taskNum; i++) { Object result = null; try { result = completionService.take().get(); } catch (InterruptedException | ExecutionException e) { e.printStackTrace(); } - if (beforeInventoryByMonth == null || beforeInventoryByMonth.size() == 0) { - beforeInventoryByMonth = (List) ((Map) result).get("sourceListByMonth"); - } - if (thisWeekInventoryByDName == null || thisWeekInventoryByDName.size() == 0) { - thisWeekInventoryByDName = (Map) ((Map) result).get("ThisWeekInventory"); + if (sunburstChartData == null || sunburstChartData.size() == 0) { + sunburstChartData = (List) ((Map) result).get("sunburstChartDataForInventory"); } - if (beforeInventoryOnMap == null || beforeInventoryOnMap.size() == 0) { - beforeInventoryOnMap = (Map) ((Map) result).get("MapInventory"); + if (lineChartData == null || lineChartData.size() == 0) { + lineChartData = (Map) ((Map) result).get("inventoryLineChartData"); } } - depository_data.put("todayInventory", todayInventory); - depository_data.put("sourceListByMonth", beforeInventoryByMonth); - depository_data.put("ThisWeekInventory", thisWeekInventoryByDName); - depository_data.put("MapInventory", beforeInventoryOnMap); + + } else { @@ -899,6 +695,197 @@ public class DepositoryController { } + + + /** + * 用于获取当前用户所拥有的仓库库存及各类别的库存 + * @param userByPort 带获取用户 + * @return + */ + public List getInventorySunburstData(UserByPort userByPort){ + Map previousMonth = DateUtil.getPreviousMonth(); + // 获取所有月份 + List months = (List) previousMonth.get("months"); + // 获取月份名称 + List sourceList = (List) previousMonth.get("sourceList"); + Calendar instance = Calendar.getInstance(); + instance.add(Calendar.MONTH, 1); + instance.set(Calendar.DATE, 1); + // 获取当前用于可见的仓库列表 + List depositoryList = depositoryService.findDepositoryByAdminorgAndUser(userByPort); + // 定义每次开启的线程数 + int threadSize = depositoryList.size(); + // 开启对应数量的线程 + ExecutorService exs = Executors.newFixedThreadPool(threadSize); + // 定义线程结果集 + List> futureList = new ArrayList>(); + // 定义CompletionService + CompletionService completionService = new ExecutorCompletionService<>(exs); + // 用于存储当前仓库各物料类型的库存数 + Map> inventoryForMTAndDepository = new HashMap<>(); + // 定义结果集 + List result = new ArrayList<>(); + // 定义各 + for (int num = 0; num < months.size() - 1; num++) { + // 获取起始时间 + Object start = months.get(num + 1); + // 获取结束时间 + Object end = months.get(num); + // 获取当前计算的月份 + Object month = ((Map) sourceList.get(num)).get("month"); + for (Depository depository : depositoryList + ) { + // 获取当前仓库id + Integer depositoryId = depository.getId(); + // 判断当前当前仓库各物料类型的库存是否存在 + List inventoryByMTAndDepositories = inventoryForMTAndDepository.get(depositoryId); + if (inventoryByMTAndDepositories == null || inventoryByMTAndDepositories.size() == 0) { + // 如果不存在则获取 + inventoryByMTAndDepositories = depositoryService.getTodayInventoryInfoByDidAndMt(depositoryId); + } + // 开启线程计算结果 + Future submit = completionService.submit(new inventoryByMaterialTypeForMonth(start, end, depository, inventoryByMTAndDepositories)); + futureList.add(submit); + } + + List childItems = new ArrayList<>(); + double value = 0.0; + for (int i = 0; i < depositoryList.size(); i++) { + Object obj = null; + try { + + obj = completionService.take().get(); + Map objectMap = (Map) obj; + Integer id = ObjectFormatUtil.toInteger((objectMap).get("id")); + List inventoryByMtAndD = (List)objectMap.get("inventoryByMtAndD"); + value += ObjectFormatUtil.toDouble(objectMap.get("value")); + inventoryForMTAndDepository.put(id,inventoryByMtAndD); + (objectMap).remove("id"); + (objectMap).remove("inventoryByMtAndD"); + + childItems.add(obj); + + + } catch (InterruptedException | ExecutionException e) { + e.printStackTrace(); + } + } + Map stringObjectMap = new HashMap<>(); + stringObjectMap.put("children",childItems); + stringObjectMap.put("name",month); + stringObjectMap.put("value",value); + Random random = new Random(); + int r = random.nextInt(256); + int g = random.nextInt(256); + int b = random.nextInt(256); + Map itemStyle = new HashMap<>(); + itemStyle.put("color", "rgb(" + r + "," + g + "," + b + ")"); + stringObjectMap.put("itemStyle", itemStyle); + result.add(stringObjectMap); + } + return result; + } + + /** + * 用于计算当前时间段内当前仓库下各物料类型的库存 + */ + class inventoryByMaterialTypeForMonth implements Callable { + + // 开始时间 + Object start; + // 结束时间 + Object end; + // 查询仓库 + Depository depository; + // 当前仓库各物料类型库存数 + List todayInventoryInfoByDidAndMt; + + public inventoryByMaterialTypeForMonth(Object start, Object end, Depository depository, List todayInventoryInfoByDidAndMt) { + this.start = start; + this.end = end; + this.depository = depository; + this.todayInventoryInfoByDidAndMt = todayInventoryInfoByDidAndMt; + } + + @Override + public Object call() throws Exception { + // 定义当前返回结果 + Map result = new HashMap<>(); + // 定义当前仓库的总数 + double inventorySum = 0.0; + for (InventoryByMTAndDepository item : todayInventoryInfoByDidAndMt + ) { + inventorySum += item.getInventory(); + } + result.put("name", depository.getDname()); + result.put("id",depository.getId()); + result.put("value", inventorySum); + // 用于生产随机的颜色 + Random random = new Random(); + int r = random.nextInt(256); + int g = random.nextInt(256); + int b = random.nextInt(256); + Map itemStyle = new HashMap<>(); + itemStyle.put("color", "rgb(" + r + "," + g + "," + b + ")"); + result.put("itemStyle", itemStyle); + + // 获取所有顶级物料类型 + List materialTypeNoParent = materialTypeService.findMaterialTypeNoParent(); + Map paramIn = new HashMap<>(); + Map paramOut = new HashMap<>(); + // 获取每月入库总额 + paramIn.put("type", 1); + paramIn.put("state", "已入库"); + paramIn.put("depository_id", depository.getId()); + paramIn.put("start", start); + paramIn.put("end", end); + // 获取每月出库总额 + paramOut.put("type", 2); + paramOut.put("state", "已出库"); + paramOut.put("depository_id", depository.getId()); + paramOut.put("start", start); + paramOut.put("end", end); + List children = new ArrayList<>(); + for (MaterialType materialType : materialTypeNoParent + ) { + // 生成子数据 + Map resultItem = new HashMap<>(); + resultItem.put("name", materialType.getTname()); + // 用于生产随机的颜色 + Map itemStyleItem = new HashMap<>(); + int rt = random.nextInt(256); + int gt = random.nextInt(256); + int bt = random.nextInt(256); + itemStyleItem.put("color", "rgb(" + rt + "," + gt + "," + bt + ")"); + result.put("itemStyle", itemStyleItem); + long oldId = materialType.getOldId(); + paramIn.put("oldId", oldId); + paramOut.put("oldId", oldId); + // 获取当前类别在当前仓库下当前月份的入库总额 + Double wareHouseInCountByMonth = depositoryRecordService.calMaterialTypeTopCount(paramIn); + // 获取当前类别在当前仓库下当前月份的出库总额 + Double wareHouseOutCountByMonth = depositoryRecordService.calMaterialTypeTopCount(paramOut); + for (InventoryByMTAndDepository item : todayInventoryInfoByDidAndMt + ) { + long mtId = item.getMtId(); + // 如果是同一类型 + if (mtId == oldId) { + // 计算上个月该类型库存数 + Double wareHouseCount = item.getInventory() - wareHouseInCountByMonth + wareHouseOutCountByMonth; + // 修改库存记录 + item.setInventory(wareHouseCount); + resultItem.put("value", wareHouseCount); + } + } + children.add(resultItem); + } + result.put("children", children); + result.put("inventoryByMtAndD",todayInventoryInfoByDidAndMt); + return result; + } + } + + /** * 根据物料编号查询所在仓库 * @@ -1347,7 +1334,7 @@ public class DepositoryController { @Override public Object call() throws Exception { - // taskType 1为条形图2为旭日图3为库存可视化中的折线图4为库存可视化中的旭日图 + // taskType 1为条形图2为旭日图3为库存可视化中的本周折线图4为库存可视化中上周折线图图5为库存可视化中的旭日图 Map result = new HashMap<>(); if (1 == taskType) { @@ -1358,11 +1345,13 @@ public class DepositoryController { List sunburstChartData = getSunburstChartData(type); result.put("sunburstChartData", sunburstChartData); } else if (3 == taskType) { - List beforeInventoryByMonth = getBeforeInventoryByMonth(depositoryService, todayInventory, depositoryRecordService, userByPort); - result.put("sourceListByMonth", beforeInventoryByMonth); + Map inventoryLineChartData = getInventoryLineChartData(depositoryAllNameAndId); + result.put("inventoryLineChartData", inventoryLineChartData); } else if (4 == taskType) { - Map thisWeekInventoryByDName = getThisWeekInventoryByDName(depositoryService, depositoryRecordService, userByPort); - result.put("ThisWeekInventory", thisWeekInventoryByDName); + + }else if(5 == taskType){ + List inventorySunburstData = getInventorySunburstData(userByPort); + result.put("sunburstChartDataForInventory",inventorySunburstData); } return result; } @@ -1376,8 +1365,6 @@ public class DepositoryController { String key; List days; Map depositoryAllNameAndId; - - String type; @@ -1411,6 +1398,136 @@ public class DepositoryController { } } + + /** + * 获取本周各仓库库存的折线图数据 + * @param depositoryAllNameAndId 仓库与Id对应 + * @return + */ + // 获取柱状图数据 + public Map getInventoryLineChartData(Map depositoryAllNameAndId) { + + // 获取遍历器 + Iterator it = depositoryAllNameAndId.keySet().iterator(); + //获取获取系统的当前日历对象 + Calendar instance = Calendar.getInstance(); + // 获取日期 + SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); + int now = instance.get(Calendar.DAY_OF_WEEK) - 1 == 0 ? 7 : instance.get(Calendar.DAY_OF_WEEK) - 1; + List days = new ArrayList<>(); // 周一至今的每天 + instance.add(Calendar.DATE, 1); + days.add(DateUtil.DateTimeByDayToTimeStamp(formatter.format(instance.getTime()))); + instance.add(Calendar.DATE, -1); + days.add(DateUtil.DateTimeByDayToTimeStamp(formatter.format(instance.getTime()))); + while (now - 1 > 0) { + now--; + instance.add(Calendar.DATE, -1); + Long format = DateUtil.DateTimeByDayToTimeStamp(formatter.format(instance.getTime())); + days.add(format); + } + // 获取当前各仓库的库存数 + List toDayInventoryByDName = depositoryService.getToDayInventoryByDName(); + // 定义线程 + ExecutorService exs = Executors.newFixedThreadPool(depositoryAllNameAndId.size()); + // 结果集 + List> futureList = new ArrayList>(); + // 1.定义CompletionService + CompletionService completionService = new ExecutorCompletionService(exs); + + // 每天各仓库库存数目 + Map show_data = new HashMap<>(); + while (it.hasNext()) { + Object next = it.next(); + getInventoryByDate getApplicationRecordByDate = new getInventoryByDate(next.toString(), days, depositoryAllNameAndId, toDayInventoryByDName); + Future future = completionService.submit(getApplicationRecordByDate); + futureList.add(future); + } + + for (int i = 0; i < depositoryAllNameAndId.size(); i++) { + Object result = null; + try { + result = completionService.take().get(); + } catch (InterruptedException | ExecutionException e) { + e.printStackTrace(); + } + show_data.putAll((Map) result); + } + + return show_data; + } + + + /** + * 根据日期获取各仓库库存的数量 + */ + class getInventoryByDate implements Callable { + String key; + List days; + Map depositoryAllNameAndId; + List inventoryByDnameList; + + + getInventoryByDate(String key, List days, Map depositoryAllNameAndId, List inventoryByDnameList) { + this.key = key; + this.depositoryAllNameAndId = depositoryAllNameAndId; + this.days = days; + this.inventoryByDnameList = inventoryByDnameList; + } + + @Override + public Object call() throws Exception { + Map result = new HashMap<>(); + Map map = new HashMap<>(); + map.put("type", "line"); + Map areaStyleItem = new HashMap<>(); + map.put("areaStyle", areaStyleItem); + int i; + List drCountbyDrName = new ArrayList<>(); + // 获取当前仓库 + String dname = key.split(",")[0]; + // 获取当前仓库下的库存 + Double inventoryByDname = getInventoryByDname(dname, inventoryByDnameList); + // 将当前仓库的库存添加到结果集中 + drCountbyDrName.add(inventoryByDname); + Integer val = (Integer) depositoryAllNameAndId.get(key); + SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); + boolean flag = false; + // 判断当天是否有出入口记录 + Calendar IsInOut = Calendar.getInstance(); + Long start = DateUtil.DateTimeByDayToTimeStamp(formatter.format(IsInOut.getTime())); + IsInOut.add(Calendar.DATE, 1); + Long end = DateUtil.DateTimeByDayToTimeStamp(formatter.format(IsInOut.getTime())); + // 判断今天是否有出入库记录 + Integer inCount = depositoryRecordService.findApplicationInRecordByDate(end, start, null); + Integer OutCount = depositoryRecordService.findApplicationOutRecordByDate(end, start, null); + if (inCount > 0 || OutCount > 0) { + flag = true; + } + int j = 0; + for (i = days.size() - 1; i > 0; i--) { + // 当前日期额度 + Double aDouble = drCountbyDrName.get(j++); + // 如果今天有出入库记录 + if (flag) { + Integer inRecordByDate = depositoryRecordService.findApplicationInRecordByDate(end, start, val); + Integer OutRecordByDate = depositoryRecordService.findApplicationOutRecordByDate(end, start, val); + aDouble = aDouble - (inRecordByDate / 100.0) + (OutRecordByDate / 100.0); + flag = false; + } + // 获取当前仓库当前时间的入库数量 + Double depositoryRecordByDateForIn = depositoryRecordService.findApplicationRecordByDate(days.get(i - 1), days.get(i), 1, val); + // 获取当前仓库当前时间的出库数量 + Double depositoryRecordByDateForOut = depositoryRecordService.findApplicationRecordByDate(days.get(i - 1), days.get(i), 2, val); + Double warehouserCount = aDouble - depositoryRecordByDateForIn + depositoryRecordByDateForOut; + drCountbyDrName.add(warehouserCount); + } + Collections.reverse(drCountbyDrName); + map.put("data", drCountbyDrName); + result.put(val, map); + return result; + } + } + // 具体执行getSourceList逻辑 class getSunburstChartTask implements Callable { diff --git a/src/main/java/com/dreamchaser/depository_manage/service/impl/DepositoryRecordServiceImpl.java b/src/main/java/com/dreamchaser/depository_manage/service/impl/DepositoryRecordServiceImpl.java index 11dedac5..40cbd80a 100644 --- a/src/main/java/com/dreamchaser/depository_manage/service/impl/DepositoryRecordServiceImpl.java +++ b/src/main/java/com/dreamchaser/depository_manage/service/impl/DepositoryRecordServiceImpl.java @@ -4635,7 +4635,7 @@ public class DepositoryRecordServiceImpl implements DepositoryRecordService { return quantity; } else { map.put("state", "已出库"); - quantity = (double) depositoryRecordMapper.findApplicationOutRecordByDate(map); + quantity = (double) depositoryRecordMapper.findApplicationOutRecordByDate(map) / 100; return quantity; } } diff --git a/src/test/java/com/dreamchaser/depository_manage/InventoryLineChartTest.java b/src/test/java/com/dreamchaser/depository_manage/InventoryLineChartTest.java new file mode 100644 index 00000000..f61814ca --- /dev/null +++ b/src/test/java/com/dreamchaser/depository_manage/InventoryLineChartTest.java @@ -0,0 +1,222 @@ +package com.dreamchaser.depository_manage; + +import com.alibaba.fastjson.JSONObject; +import com.dreamchaser.depository_manage.entity.UserByPort; +import com.dreamchaser.depository_manage.pojo.InventoryByDname; +import com.dreamchaser.depository_manage.service.DepositoryRecordService; +import com.dreamchaser.depository_manage.service.DepositoryService; +import com.dreamchaser.depository_manage.service.MaterialService; +import com.dreamchaser.depository_manage.service.MaterialTypeService; +import com.dreamchaser.depository_manage.utils.DateUtil; +import com.dreamchaser.depository_manage.utils.LinkInterfaceUtil; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +import java.text.SimpleDateFormat; +import java.util.*; +import java.util.concurrent.*; + +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +@RunWith(SpringRunner.class) +public class InventoryLineChartTest { + + + @Autowired + MaterialTypeService materialTypeService; + + @Autowired + DepositoryRecordService depositoryRecordService; + + @Autowired + MaterialService materialService; + + @Autowired + DepositoryService depositoryService; + + /** + * 获取本月之前的月份 + * + * @return + */ + public static Map getPreviousMonth() { + Calendar instance = Calendar.getInstance(); + SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM"); + Map source = new HashMap<>(); + List sourceList = new ArrayList<>(); + int month = instance.get(Calendar.MONTH) + 1; + // 获取下个月 + instance.add(Calendar.MONTH, 1); + Long nextMonth = DateUtil.DateTimeByMonthToTimeStamp(formatter.format(instance.getTime())); + ArrayList months = new ArrayList<>(); + months.add(nextMonth); + instance.add(Calendar.MONTH, -1); + + while (month > 0) { + instance.set(Calendar.MONTH, month); + instance.set(Calendar.DAY_OF_MONTH, -1); + source.put("month", month + "月"); + months.add(DateUtil.DateTimeByMonthToTimeStamp(formatter.format(instance.getTime()))); + month--; + sourceList.add(((HashMap) source).clone()); + } + instance.set(Calendar.MONTH, month); + instance.add(Calendar.MONTH, 1); + Map map = new HashMap<>(); + map.put("months", months); + map.put("sourceList", sourceList); + return map; + } + + @Test + public void main() { + UserByPort userByPort = LinkInterfaceUtil.FindUserById(87, null); + Map depositoryAllNameAndId = depositoryService.findDepositoryAllNameAndId(userByPort); + Map barChartData = getInventoryLineChartData(depositoryAllNameAndId); + System.out.println(JSONObject.toJSONString(barChartData)); + } + + // 获取柱状图数据 + public Map getInventoryLineChartData(Map depositoryAllNameAndId) { + + // 获取遍历器 + Iterator it = depositoryAllNameAndId.keySet().iterator(); + //获取获取系统的当前日历对象 + Calendar instance = Calendar.getInstance(); + // 获取日期 + SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); + int now = instance.get(Calendar.DAY_OF_WEEK) - 1 == 0 ? 7 : instance.get(Calendar.DAY_OF_WEEK) - 1; + List days = new ArrayList<>(); // 周一至今的每天 + instance.add(Calendar.DATE, 1); + days.add(DateUtil.DateTimeByDayToTimeStamp(formatter.format(instance.getTime()))); + instance.add(Calendar.DATE, -1); + days.add(DateUtil.DateTimeByDayToTimeStamp(formatter.format(instance.getTime()))); + while (now - 1 > 0) { + now--; + instance.add(Calendar.DATE, -1); + Long format = DateUtil.DateTimeByDayToTimeStamp(formatter.format(instance.getTime())); + days.add(format); + } + // 获取当前各仓库的库存数 + List toDayInventoryByDName = depositoryService.getToDayInventoryByDName(); + // 定义线程 + ExecutorService exs = Executors.newFixedThreadPool(depositoryAllNameAndId.size()); + // 结果集 + List> futureList = new ArrayList>(); + // 1.定义CompletionService + CompletionService completionService = new ExecutorCompletionService(exs); + + // 每天各仓库入库数目 + Map show_data = new HashMap<>(); + while (it.hasNext()) { + Object next = it.next(); + getInventoryByDate getApplicationRecordByDate = new getInventoryByDate(next.toString(), days, depositoryAllNameAndId, toDayInventoryByDName); + Future future = completionService.submit(getApplicationRecordByDate); + futureList.add(future); + } + + for (int i = 0; i < depositoryAllNameAndId.size(); i++) { + Object result = null; + try { + result = completionService.take().get(); + } catch (InterruptedException | ExecutionException e) { + e.printStackTrace(); + } + show_data.putAll((Map) result); + } + + return show_data; + } + + + /** + * 根据日期获取各仓库库存的数量 + */ + class getInventoryByDate implements Callable { + String key; + List days; + Map depositoryAllNameAndId; + List inventoryByDnameList; + + + getInventoryByDate(String key, List days, Map depositoryAllNameAndId, List inventoryByDnameList) { + this.key = key; + this.depositoryAllNameAndId = depositoryAllNameAndId; + this.days = days; + this.inventoryByDnameList = inventoryByDnameList; + } + + @Override + public Object call() throws Exception { + Map result = new HashMap<>(); + Map map = new HashMap<>(); + map.put("type", "line"); + Map areaStyleItem = new HashMap<>(); + map.put("areaStyle", areaStyleItem); + int i; + List drCountbyDrName = new ArrayList<>(); + // 获取当前仓库 + String dname = key.split(",")[0]; + // 获取当前仓库下的库存 + Double inventoryByDname = getInventoryByDname(dname, inventoryByDnameList); + // 将当前仓库的库存添加到结果集中 + drCountbyDrName.add(inventoryByDname); + Integer val = (Integer) depositoryAllNameAndId.get(key); + SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); + boolean flag = false; + // 判断当天是否有出入口记录 + Calendar IsInOut = Calendar.getInstance(); + Long start = DateUtil.DateTimeByDayToTimeStamp(formatter.format(IsInOut.getTime())); + IsInOut.add(Calendar.DATE, 1); + Long end = DateUtil.DateTimeByDayToTimeStamp(formatter.format(IsInOut.getTime())); + // 判断今天是否有出入库记录 + Integer inCount = depositoryRecordService.findApplicationInRecordByDate(end, start, null); + Integer OutCount = depositoryRecordService.findApplicationOutRecordByDate(end, start, null); + if (inCount > 0 || OutCount > 0) { + flag = true; + } + int j = 0; + for (i = days.size() - 1; i > 0; i--) { + // 当前日期额度 + Double aDouble = drCountbyDrName.get(j++); + // 如果今天有出入库记录 + if (flag) { + Integer inRecordByDate = depositoryRecordService.findApplicationInRecordByDate(end, start, val); + Integer OutRecordByDate = depositoryRecordService.findApplicationOutRecordByDate(end, start, val); + aDouble = aDouble - (inRecordByDate / 100.0) + (OutRecordByDate / 100.0); + flag = false; + } + // 获取当前仓库当前时间的入库数量 + Double depositoryRecordByDateForIn = depositoryRecordService.findApplicationRecordByDate(days.get(i - 1), days.get(i), 1, val); + // 获取当前仓库当前时间的出库数量 + Double depositoryRecordByDateForOut = depositoryRecordService.findApplicationRecordByDate(days.get(i - 1), days.get(i), 2, val); + Double warehouserCount = aDouble - depositoryRecordByDateForIn + depositoryRecordByDateForOut; + drCountbyDrName.add(warehouserCount); + } + Collections.reverse(drCountbyDrName); + map.put("data", drCountbyDrName); + result.put(val, map); + return result; + } + } + + + /** + * 获取当前仓库下的库存 + * + * @param dname + * @param list + * @return + */ + public Double getInventoryByDname(String dname, List list) { + for (InventoryByDname inventoryByDname : list) { + if (dname.equals(inventoryByDname.getDname())) { + return inventoryByDname.getInventory(); + } + } + return 0.0; + } + +} diff --git a/src/test/java/com/dreamchaser/depository_manage/InventorySunburstDataTest.java b/src/test/java/com/dreamchaser/depository_manage/InventorySunburstDataTest.java index 084c1132..87922151 100644 --- a/src/test/java/com/dreamchaser/depository_manage/InventorySunburstDataTest.java +++ b/src/test/java/com/dreamchaser/depository_manage/InventorySunburstDataTest.java @@ -79,6 +79,10 @@ public class InventorySunburstDataTest { UserByPort userByPort = LinkInterfaceUtil.FindUserById(78, null); + + } + + public List getInventorySunburstData(UserByPort userByPort){ Map previousMonth = DateUtil.getPreviousMonth(); // 获取所有月份 List months = (List) previousMonth.get("months"); @@ -158,27 +162,8 @@ public class InventorySunburstDataTest { itemStyle.put("color", "rgb(" + r + "," + g + "," + b + ")"); stringObjectMap.put("itemStyle", itemStyle); result.add(stringObjectMap); - - } - System.out.println(JSONObject.toJSONString(result)); - - } - - - /** - * 获取当前仓库下的库存 - * - * @param dname - * @param list - * @return - */ - public Double getInventoryByDname(String dname, List list) { - for (InventoryByDname inventoryByDname : list) { - if (dname.equals(inventoryByDname.getDname())) { - return inventoryByDname.getInventory(); - } } - return 0.0; + return result; } /**