Browse Source

修改本地库存折现图数据算法

lwx_dev
erdanergou 3 years ago
parent
commit
1ff17cacf7
  1. 639
      src/main/java/com/dreamchaser/depository_manage/controller/DepositoryController.java
  2. 2
      src/main/java/com/dreamchaser/depository_manage/service/impl/DepositoryRecordServiceImpl.java
  3. 222
      src/test/java/com/dreamchaser/depository_manage/InventoryLineChartTest.java
  4. 25
      src/test/java/com/dreamchaser/depository_manage/InventorySunburstDataTest.java

639
src/main/java/com/dreamchaser/depository_manage/controller/DepositoryController.java

@ -48,217 +48,6 @@ public class DepositoryController {
PlaceService placeService; PlaceService placeService;
/**
* 获取之前仓库的库存值
*
* @return
*/
public Map<String, Object> getBeforeInventoryByDName(DepositoryService depositoryService, DepositoryRecordService depositoryRecordService, UserByPort user) {
List<Integer> result = new ArrayList<>();
List<Long> days = DateUtil.getLastTimeInterval();
Map<String, Integer> depositoryAllNameAndId = depositoryService.findDepositoryAllNameAndId(user);
Iterator it = depositoryAllNameAndId.keySet().iterator();
Map<String, Object> map = new HashMap<>();
// 获取本周仓库库存
Map<String, Object> thisWeekInventoryByDName = getThisWeekInventoryByDName(depositoryService, depositoryRecordService, user);
while (it.hasNext()) {
int i;
Object key = it.next();
Integer thisDayInventory = 0;
// 本周周一的总量
thisDayInventory = ((List<Integer>) 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<Integer>) result).clone());
result.clear();
}
return map;
}
/**
* 获取本周库存容量
*
* @param depositoryService
* @param depositoryRecordService
* @return
*/
public Map<String, Object> 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<String, Integer> depositoryAllNameAndId = depositoryService.findDepositoryAllNameAndId(user);
Iterator it = depositoryAllNameAndId.keySet().iterator();
//result用来接收每个仓库每天的库存
ArrayList<Double> result = new ArrayList<>();
// map用来存储仓库与库存的映射
Map<String, Object> map = new HashMap<>();
// 只获取当天
List<InventoryByDname> 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<Long> 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<Object> getBeforeInventoryByMonth(DepositoryService depositoryService, Map<String, Object> todayInventory, DepositoryRecordService depositoryRecordService, UserByPort userByPort) {
List<Depository> depositoryAll = depositoryService.findDepositoryByAdminorgAndUser(userByPort);
// 获取该用户管理的仓库
Map<String, Object> previousMonth = DateUtil.getPreviousMonth();
List<Object> months = (List<Object>) previousMonth.get("months");
List<Object> sourceList = (List<Object>) 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<Double> result = new ArrayList<>();
String month = formatter.format(instance.getTime());
List<InventoryByDname> 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<String, Object>) 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<String, Object> parmIn = new HashMap<>();
Map<String, Object> 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<String, Object>) 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<String, Object>) sourceList.get(0)).put(dname, todayDepositoryInventory);
}
List<String> 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) @RequestMapping(value = "/layui/echart_back_invetory", method = RequestMethod.GET)
@ResponseBody @ResponseBody
public Map<String, Object> echart_back_MaterialTypeInvetory(@RequestParam Map<String, Object> map, HttpServletRequest request) { public Map<String, Object> echart_back_MaterialTypeInventory(@RequestParam Map<String, Object> map, HttpServletRequest request) {
String typeName = (String) map.get("typeName"); String typeName = (String) map.get("typeName");
String type = (String) map.get("type"); String type = (String) map.get("type");
// 获取本月及之前月份各种类别入/出库总量 // 获取本月及之前月份各种类别入/出库总量
@ -599,9 +388,32 @@ public class DepositoryController {
depository_data.put("week", show_weekday); depository_data.put("week", show_weekday);
if ("0".equals(type)) { if ("0".equals(type)) {
// 如果是获取库存 ExecutorService exs = Executors.newFixedThreadPool(depositoryAllNameAndId.size());
depository_data.put("ThisWeekInventory", getThisWeekInventoryByDName(depositoryService, depositoryRecordService, userByPort)); // 结果集
depository_data.put("BeforeInventory", getBeforeInventoryByDName(depositoryService, depositoryRecordService, userByPort)); List<Future<Object>> futureList = new ArrayList<Future<Object>>();
// 1.定义CompletionService
CompletionService<Object> completionService = new ExecutorCompletionService<Object>(exs);
// 获取当前各仓库的库存数
List<InventoryByDname> toDayInventoryByDName = depositoryService.getToDayInventoryByDName();
while (it.hasNext()) {
Object next = it.next();
getInventoryByDate getApplicationRecordByDate = new getInventoryByDate(next.toString(), days, depositoryAllNameAndId, toDayInventoryByDName);
Future<Object> 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{ else{
// 定义线程 // 定义线程
@ -667,8 +479,8 @@ public class DepositoryController {
depository_data.put("week", show_weekday); depository_data.put("week", show_weekday);
if ("0".equals(type)) { if ("0".equals(type)) {
// 如果是获取库存 // 如果是获取库存
depository_data.put("ThisWeekInventory", getThisWeekInventoryByDName(depositoryService, depositoryRecordService, userByPort)); Map<Object, Object> inventoryLineChartData = getInventoryLineChartData(depositoryAllNameAndId);
depository_data.put("BeforeInventory", getBeforeInventoryByDName(depositoryService, depositoryRecordService, userByPort)); depository_data.put("data",inventoryLineChartData);
} else { } else {
Map<Object, Object> lineChartData = getLineChartData(depositoryAllNameAndId, type); Map<Object, Object> lineChartData = getLineChartData(depositoryAllNameAndId, type);
depository_data.put("data", lineChartData); depository_data.put("data", lineChartData);
@ -684,7 +496,7 @@ public class DepositoryController {
*/ */
@RequestMapping(value = "/layui/echart_back", method = RequestMethod.GET) @RequestMapping(value = "/layui/echart_back", method = RequestMethod.GET)
@ResponseBody @ResponseBody
public Map<String, Object> learnLayuiAndEchart_back(@RequestParam("type") String type, HttpServletRequest request) { public Map<String, Object> echart_back(@RequestParam("type") String type, HttpServletRequest request) {
// 获取当前登录用户 // 获取当前登录用户
String token = request.getHeader("user-token"); String token = request.getHeader("user-token");
if (token == null) { if (token == null) {
@ -716,8 +528,6 @@ public class DepositoryController {
Map<Object, Object> lineChartData = new HashMap<>(); Map<Object, Object> lineChartData = new HashMap<>();
// 旭日图数据 // 旭日图数据
List<Object> sunburstChartData = new ArrayList<>(); List<Object> sunburstChartData = new ArrayList<>();
// 各仓库当前库存数目
Map<String, Object> todayInventory = new HashMap<>();
// 用于标准开启了几个线程 // 用于标准开启了几个线程
int taskNum = 0; int taskNum = 0;
@ -726,49 +536,35 @@ public class DepositoryController {
// 获取库存图 // 获取库存图
if ("0".equals(type)) { if ("0".equals(type)) {
// 如果是获取库存 // 获取库存旭日图
Future<Object> inventoryFutureForSunburstChartData = completionService.submit(new distributeTasks(depositoryAllNameAndId, type, 4, userByPort, depositoryName)); Future<Object> futureForSunburstData = completionService.submit(new distributeTasks(depositoryAllNameAndId, type, 5, userByPort, depositoryName));
futureList.add(inventoryFutureForSunburstChartData); futureList.add(futureForSunburstData);
distributeTasks distributeTasks1 = new distributeTasks(depositoryAllNameAndId, type, 5, userByPort, depositoryName); taskNum += 1;
distributeTasks1.setTodayInventory(todayInventory);
distributeTasks1.setDepositoryService(depositoryService); // 获取本周库存图
distributeTasks1.setDepositoryRecordService(depositoryRecordService); Future<Object> futureForLineData = completionService.submit(new distributeTasks(depositoryAllNameAndId, type, 3, userByPort, depositoryName));
distributeTasks distributeTasks2 = new distributeTasks(depositoryAllNameAndId, type, 6, userByPort, depositoryName); futureList.add(futureForLineData);
distributeTasks2.setDepositoryService(depositoryService); taskNum += 1;
distributeTasks2.setDepositoryRecordService(depositoryRecordService);
distributeTasks distributeTasks3 = new distributeTasks(depositoryAllNameAndId, type, 7, userByPort, depositoryName);
distributeTasks3.setDepositoryService(depositoryService);
distributeTasks3.setDepositoryRecordService(depositoryRecordService);
Future<Object> future1 = completionService.submit(distributeTasks1); for (int i = 0; i < taskNum; i++) {
Future<Object> future2 = completionService.submit(distributeTasks2);
Future<Object> future3 = completionService.submit(distributeTasks3);
futureList.add(future1);
futureList.add(future2);
futureList.add(future3);
List<Object> beforeInventoryByMonth = null;
Map<String, Object> thisWeekInventoryByDName = null;
Map<String, Object> beforeInventoryOnMap = null;
for (int i = 0; i < 3; i++) {
Object result = null; Object result = null;
try { try {
result = completionService.take().get(); result = completionService.take().get();
} catch (InterruptedException | ExecutionException e) { } catch (InterruptedException | ExecutionException e) {
e.printStackTrace(); e.printStackTrace();
} }
if (beforeInventoryByMonth == null || beforeInventoryByMonth.size() == 0) { if (sunburstChartData == null || sunburstChartData.size() == 0) {
beforeInventoryByMonth = (List<Object>) ((Map<String, Object>) result).get("sourceListByMonth"); sunburstChartData = (List<Object>) ((Map<String, Object>) result).get("sunburstChartDataForInventory");
}
if (thisWeekInventoryByDName == null || thisWeekInventoryByDName.size() == 0) {
thisWeekInventoryByDName = (Map<String, Object>) ((Map<String, Object>) result).get("ThisWeekInventory");
} }
if (beforeInventoryOnMap == null || beforeInventoryOnMap.size() == 0) { if (lineChartData == null || lineChartData.size() == 0) {
beforeInventoryOnMap = (Map<String, Object>) ((Map<String, Object>) result).get("MapInventory"); lineChartData = (Map<Object, Object>) ((Map<String, Object>) result).get("inventoryLineChartData");
} }
} }
depository_data.put("todayInventory", todayInventory);
depository_data.put("sourceListByMonth", beforeInventoryByMonth);
depository_data.put("ThisWeekInventory", thisWeekInventoryByDName);
depository_data.put("MapInventory", beforeInventoryOnMap);
} }
else { else {
@ -899,6 +695,197 @@ public class DepositoryController {
} }
/**
* 用于获取当前用户所拥有的仓库库存及各类别的库存
* @param userByPort 带获取用户
* @return
*/
public List<Object> getInventorySunburstData(UserByPort userByPort){
Map<String, Object> previousMonth = DateUtil.getPreviousMonth();
// 获取所有月份
List<Object> months = (List<Object>) previousMonth.get("months");
// 获取月份名称
List<Object> sourceList = (List<Object>) previousMonth.get("sourceList");
Calendar instance = Calendar.getInstance();
instance.add(Calendar.MONTH, 1);
instance.set(Calendar.DATE, 1);
// 获取当前用于可见的仓库列表
List<Depository> depositoryList = depositoryService.findDepositoryByAdminorgAndUser(userByPort);
// 定义每次开启的线程数
int threadSize = depositoryList.size();
// 开启对应数量的线程
ExecutorService exs = Executors.newFixedThreadPool(threadSize);
// 定义线程结果集
List<Future<Object>> futureList = new ArrayList<Future<Object>>();
// 定义CompletionService
CompletionService<Object> completionService = new ExecutorCompletionService<>(exs);
// 用于存储当前仓库各物料类型的库存数
Map<Integer, List<InventoryByMTAndDepository>> inventoryForMTAndDepository = new HashMap<>();
// 定义结果集
List<Object> 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<String, Object>) sourceList.get(num)).get("month");
for (Depository depository : depositoryList
) {
// 获取当前仓库id
Integer depositoryId = depository.getId();
// 判断当前当前仓库各物料类型的库存是否存在
List<InventoryByMTAndDepository> inventoryByMTAndDepositories = inventoryForMTAndDepository.get(depositoryId);
if (inventoryByMTAndDepositories == null || inventoryByMTAndDepositories.size() == 0) {
// 如果不存在则获取
inventoryByMTAndDepositories = depositoryService.getTodayInventoryInfoByDidAndMt(depositoryId);
}
// 开启线程计算结果
Future<Object> submit = completionService.submit(new inventoryByMaterialTypeForMonth(start, end, depository, inventoryByMTAndDepositories));
futureList.add(submit);
}
List<Object> childItems = new ArrayList<>();
double value = 0.0;
for (int i = 0; i < depositoryList.size(); i++) {
Object obj = null;
try {
obj = completionService.take().get();
Map<String, Object> objectMap = (Map<String, Object>) obj;
Integer id = ObjectFormatUtil.toInteger((objectMap).get("id"));
List<InventoryByMTAndDepository> inventoryByMtAndD = (List<InventoryByMTAndDepository>)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<String,Object> 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<String, String> itemStyle = new HashMap<>();
itemStyle.put("color", "rgb(" + r + "," + g + "," + b + ")");
stringObjectMap.put("itemStyle", itemStyle);
result.add(stringObjectMap);
}
return result;
}
/**
* 用于计算当前时间段内当前仓库下各物料类型的库存
*/
class inventoryByMaterialTypeForMonth implements Callable<Object> {
// 开始时间
Object start;
// 结束时间
Object end;
// 查询仓库
Depository depository;
// 当前仓库各物料类型库存数
List<InventoryByMTAndDepository> todayInventoryInfoByDidAndMt;
public inventoryByMaterialTypeForMonth(Object start, Object end, Depository depository, List<InventoryByMTAndDepository> todayInventoryInfoByDidAndMt) {
this.start = start;
this.end = end;
this.depository = depository;
this.todayInventoryInfoByDidAndMt = todayInventoryInfoByDidAndMt;
}
@Override
public Object call() throws Exception {
// 定义当前返回结果
Map<String, Object> 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<String, String> itemStyle = new HashMap<>();
itemStyle.put("color", "rgb(" + r + "," + g + "," + b + ")");
result.put("itemStyle", itemStyle);
// 获取所有顶级物料类型
List<MaterialType> materialTypeNoParent = materialTypeService.findMaterialTypeNoParent();
Map<String, Object> paramIn = new HashMap<>();
Map<String, Object> 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<Object> children = new ArrayList<>();
for (MaterialType materialType : materialTypeNoParent
) {
// 生成子数据
Map<String, Object> resultItem = new HashMap<>();
resultItem.put("name", materialType.getTname());
// 用于生产随机的颜色
Map<String, String> 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 @Override
public Object call() throws Exception { public Object call() throws Exception {
// taskType 1为条形图2为旭日图3为库存可视化中的折线图4为库存可视化中的旭日图 // taskType 1为条形图2为旭日图3为库存可视化中的本周折线图4为库存可视化中上周折线图图5为库存可视化中的旭日图
Map<String, Object> result = new HashMap<>(); Map<String, Object> result = new HashMap<>();
if (1 == taskType) { if (1 == taskType) {
@ -1358,11 +1345,13 @@ public class DepositoryController {
List<Object> sunburstChartData = getSunburstChartData(type); List<Object> sunburstChartData = getSunburstChartData(type);
result.put("sunburstChartData", sunburstChartData); result.put("sunburstChartData", sunburstChartData);
} else if (3 == taskType) { } else if (3 == taskType) {
List<Object> beforeInventoryByMonth = getBeforeInventoryByMonth(depositoryService, todayInventory, depositoryRecordService, userByPort); Map<Object, Object> inventoryLineChartData = getInventoryLineChartData(depositoryAllNameAndId);
result.put("sourceListByMonth", beforeInventoryByMonth); result.put("inventoryLineChartData", inventoryLineChartData);
} else if (4 == taskType) { } else if (4 == taskType) {
Map<String, Object> thisWeekInventoryByDName = getThisWeekInventoryByDName(depositoryService, depositoryRecordService, userByPort);
result.put("ThisWeekInventory", thisWeekInventoryByDName); }else if(5 == taskType){
List<Object> inventorySunburstData = getInventorySunburstData(userByPort);
result.put("sunburstChartDataForInventory",inventorySunburstData);
} }
return result; return result;
} }
@ -1376,8 +1365,6 @@ public class DepositoryController {
String key; String key;
List<Long> days; List<Long> days;
Map<String, Integer> depositoryAllNameAndId; Map<String, Integer> depositoryAllNameAndId;
String type; String type;
@ -1411,6 +1398,136 @@ public class DepositoryController {
} }
} }
/**
* 获取本周各仓库库存的折线图数据
* @param depositoryAllNameAndId 仓库与Id对应
* @return
*/
// 获取柱状图数据
public Map<Object, Object> getInventoryLineChartData(Map<String, Integer> 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<Long> 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<InventoryByDname> toDayInventoryByDName = depositoryService.getToDayInventoryByDName();
// 定义线程
ExecutorService exs = Executors.newFixedThreadPool(depositoryAllNameAndId.size());
// 结果集
List<Future<Object>> futureList = new ArrayList<Future<Object>>();
// 1.定义CompletionService
CompletionService<Object> completionService = new ExecutorCompletionService<Object>(exs);
// 每天各仓库库存数目
Map<Object, Object> show_data = new HashMap<>();
while (it.hasNext()) {
Object next = it.next();
getInventoryByDate getApplicationRecordByDate = new getInventoryByDate(next.toString(), days, depositoryAllNameAndId, toDayInventoryByDName);
Future<Object> 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<Object> {
String key;
List<Long> days;
Map<String, Integer> depositoryAllNameAndId;
List<InventoryByDname> inventoryByDnameList;
getInventoryByDate(String key, List<Long> days, Map<String, Integer> depositoryAllNameAndId, List<InventoryByDname> inventoryByDnameList) {
this.key = key;
this.depositoryAllNameAndId = depositoryAllNameAndId;
this.days = days;
this.inventoryByDnameList = inventoryByDnameList;
}
@Override
public Object call() throws Exception {
Map<Integer, Object> result = new HashMap<>();
Map<String, Object> map = new HashMap<>();
map.put("type", "line");
Map<String, Object> areaStyleItem = new HashMap<>();
map.put("areaStyle", areaStyleItem);
int i;
List<Double> 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逻辑 // 具体执行getSourceList逻辑
class getSunburstChartTask implements Callable<Object> { class getSunburstChartTask implements Callable<Object> {

2
src/main/java/com/dreamchaser/depository_manage/service/impl/DepositoryRecordServiceImpl.java

@ -4635,7 +4635,7 @@ public class DepositoryRecordServiceImpl implements DepositoryRecordService {
return quantity; return quantity;
} else { } else {
map.put("state", "已出库"); map.put("state", "已出库");
quantity = (double) depositoryRecordMapper.findApplicationOutRecordByDate(map); quantity = (double) depositoryRecordMapper.findApplicationOutRecordByDate(map) / 100;
return quantity; return quantity;
} }
} }

222
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<String, Object> getPreviousMonth() {
Calendar instance = Calendar.getInstance();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM");
Map<String, Object> source = new HashMap<>();
List<Object> sourceList = new ArrayList<>();
int month = instance.get(Calendar.MONTH) + 1;
// 获取下个月
instance.add(Calendar.MONTH, 1);
Long nextMonth = DateUtil.DateTimeByMonthToTimeStamp(formatter.format(instance.getTime()));
ArrayList<Object> 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<String, Object>) source).clone());
}
instance.set(Calendar.MONTH, month);
instance.add(Calendar.MONTH, 1);
Map<String, Object> map = new HashMap<>();
map.put("months", months);
map.put("sourceList", sourceList);
return map;
}
@Test
public void main() {
UserByPort userByPort = LinkInterfaceUtil.FindUserById(87, null);
Map<String, Integer> depositoryAllNameAndId = depositoryService.findDepositoryAllNameAndId(userByPort);
Map<Object, Object> barChartData = getInventoryLineChartData(depositoryAllNameAndId);
System.out.println(JSONObject.toJSONString(barChartData));
}
// 获取柱状图数据
public Map<Object, Object> getInventoryLineChartData(Map<String, Integer> 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<Long> 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<InventoryByDname> toDayInventoryByDName = depositoryService.getToDayInventoryByDName();
// 定义线程
ExecutorService exs = Executors.newFixedThreadPool(depositoryAllNameAndId.size());
// 结果集
List<Future<Object>> futureList = new ArrayList<Future<Object>>();
// 1.定义CompletionService
CompletionService<Object> completionService = new ExecutorCompletionService<Object>(exs);
// 每天各仓库入库数目
Map<Object, Object> show_data = new HashMap<>();
while (it.hasNext()) {
Object next = it.next();
getInventoryByDate getApplicationRecordByDate = new getInventoryByDate(next.toString(), days, depositoryAllNameAndId, toDayInventoryByDName);
Future<Object> 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<Object> {
String key;
List<Long> days;
Map<String, Integer> depositoryAllNameAndId;
List<InventoryByDname> inventoryByDnameList;
getInventoryByDate(String key, List<Long> days, Map<String, Integer> depositoryAllNameAndId, List<InventoryByDname> inventoryByDnameList) {
this.key = key;
this.depositoryAllNameAndId = depositoryAllNameAndId;
this.days = days;
this.inventoryByDnameList = inventoryByDnameList;
}
@Override
public Object call() throws Exception {
Map<Integer, Object> result = new HashMap<>();
Map<String, Object> map = new HashMap<>();
map.put("type", "line");
Map<String, Object> areaStyleItem = new HashMap<>();
map.put("areaStyle", areaStyleItem);
int i;
List<Double> 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<InventoryByDname> list) {
for (InventoryByDname inventoryByDname : list) {
if (dname.equals(inventoryByDname.getDname())) {
return inventoryByDname.getInventory();
}
}
return 0.0;
}
}

25
src/test/java/com/dreamchaser/depository_manage/InventorySunburstDataTest.java

@ -79,6 +79,10 @@ public class InventorySunburstDataTest {
UserByPort userByPort = LinkInterfaceUtil.FindUserById(78, null); UserByPort userByPort = LinkInterfaceUtil.FindUserById(78, null);
}
public List<Object> getInventorySunburstData(UserByPort userByPort){
Map<String, Object> previousMonth = DateUtil.getPreviousMonth(); Map<String, Object> previousMonth = DateUtil.getPreviousMonth();
// 获取所有月份 // 获取所有月份
List<Object> months = (List<Object>) previousMonth.get("months"); List<Object> months = (List<Object>) previousMonth.get("months");
@ -158,27 +162,8 @@ public class InventorySunburstDataTest {
itemStyle.put("color", "rgb(" + r + "," + g + "," + b + ")"); itemStyle.put("color", "rgb(" + r + "," + g + "," + b + ")");
stringObjectMap.put("itemStyle", itemStyle); stringObjectMap.put("itemStyle", itemStyle);
result.add(stringObjectMap); result.add(stringObjectMap);
}
System.out.println(JSONObject.toJSONString(result));
}
/**
* 获取当前仓库下的库存
*
* @param dname
* @param list
* @return
*/
public Double getInventoryByDname(String dname, List<InventoryByDname> list) {
for (InventoryByDname inventoryByDname : list) {
if (dname.equals(inventoryByDname.getDname())) {
return inventoryByDname.getInventory();
}
} }
return 0.0; return result;
} }
/** /**

Loading…
Cancel
Save