17 changed files with 502 additions and 38 deletions
@ -0,0 +1,26 @@ |
|||
package com.dreamchaser.depository_manage.scheduled; |
|||
|
|||
import com.dreamchaser.depository_manage.service.MaterialService; |
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.springframework.context.annotation.Configuration; |
|||
import org.springframework.scheduling.annotation.EnableScheduling; |
|||
import org.springframework.scheduling.annotation.Scheduled; |
|||
|
|||
|
|||
/** |
|||
* 用于执行定时任务 |
|||
*/ |
|||
@Configuration |
|||
@EnableScheduling |
|||
public class SaticScheduleTask { |
|||
|
|||
@Autowired |
|||
MaterialService materialService; |
|||
|
|||
// 每天凌晨0点执行一次
|
|||
@Scheduled(cron = "0 0 0 * * ?") |
|||
private void getTransData() throws Exception { |
|||
materialService.InitTreeMenus_Test(); |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,140 @@ |
|||
package com.dreamchaser.depository_manage; |
|||
|
|||
import com.dreamchaser.depository_manage.controller.DepositoryController; |
|||
import com.dreamchaser.depository_manage.entity.MaterialType; |
|||
import com.dreamchaser.depository_manage.service.DepositoryRecordService; |
|||
import com.dreamchaser.depository_manage.service.DepositoryService; |
|||
import com.dreamchaser.depository_manage.service.MaterialTypeService; |
|||
import com.dreamchaser.depository_manage.utils.DateUtil; |
|||
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 |
|||
@RunWith(SpringRunner.class) |
|||
public class TestForgetMapData { |
|||
|
|||
@Autowired |
|||
MaterialTypeService materialTypeService; |
|||
@Autowired |
|||
DepositoryRecordService depositoryRecordService; |
|||
|
|||
|
|||
@Test |
|||
public void Test(){ |
|||
Map<String, Object> mapData = getMapData("1"); |
|||
System.out.println(mapData); |
|||
} |
|||
|
|||
// 中国地图数据
|
|||
public Map<String, Object> getMapData(String type){ |
|||
List<Object> mapDataList = new ArrayList<>(); |
|||
Map<String, Object> previousMonth1 = getPreviousMonth(); |
|||
List<Object> sourceList1 = (List<Object>) previousMonth1.get("sourceList"); |
|||
ArrayList<Object> title = new ArrayList<>(); |
|||
title.add("product"); |
|||
for (int i = sourceList1.size() - 1; i >= 0; i--) { |
|||
title.add(((Map<String, Object>) sourceList1.get(i)).get("month")); |
|||
} |
|||
mapDataList.add(title); |
|||
|
|||
// 获取所有顶级类别
|
|||
List<MaterialType> materialTypeAll = materialTypeService.findMaterialTypeNoParent(); |
|||
// 定义对应数量的线程池
|
|||
ExecutorService exs = Executors.newFixedThreadPool(materialTypeAll.size()); |
|||
// 结果集
|
|||
List<Future<Object>> futureList = new ArrayList<Future<Object>>(); |
|||
// 1.定义CompletionService
|
|||
CompletionService<Object> completionService = new ExecutorCompletionService<Object>(exs); |
|||
|
|||
for (int i = 0; i < materialTypeAll.size(); i++) { |
|||
MaterialType mt = materialTypeAll.get(i); |
|||
Future<Object> future = completionService.submit(new findMapData(previousMonth1,type,mt)); |
|||
futureList.add(future); |
|||
} |
|||
for (int i = 0; i < materialTypeAll.size(); i++) { |
|||
Object result = null; |
|||
try { |
|||
result = completionService.take().get(); |
|||
} catch (InterruptedException | ExecutionException e) { |
|||
e.printStackTrace(); |
|||
} |
|||
mapDataList.add(result); |
|||
} |
|||
Map<String,Object> mapData =new HashMap<>(); |
|||
mapData.put("mapDataList", mapDataList); |
|||
return mapData; |
|||
} |
|||
|
|||
class findMapData implements Callable<Object>{ |
|||
String type ; |
|||
MaterialType mt; |
|||
Map<String, Object> previousMonth1; |
|||
findMapData( Map<String, Object> previousMonth1,String type ,MaterialType mt){ |
|||
this.previousMonth1 = previousMonth1; |
|||
this.type = type; |
|||
this.mt = mt; |
|||
} |
|||
|
|||
@Override |
|||
public Object call() throws Exception { |
|||
List<Object> productData = new ArrayList<>(); |
|||
List<Object> months1 = (List<Object>) previousMonth1.get("months"); |
|||
productData.add(mt.getTname()); |
|||
for (int j = months1.size() - 1; j > 0; j--) { |
|||
Map<String, Object> parm = new HashMap<>(); |
|||
parm.put("type", Integer.parseInt(type)); |
|||
if (Integer.parseInt(type) == 1) { |
|||
parm.put("state", "已入库"); |
|||
} else if (Integer.parseInt(type) == 2) { |
|||
parm.put("state", "已出库"); |
|||
} |
|||
parm.put("start", months1.get(j)); |
|||
parm.put("end", months1.get(j - 1)); |
|||
parm.put("oldId", mt.getOldId()); |
|||
//根据条件获取月份中物料的总额
|
|||
// 测试
|
|||
Integer materialCountByMonth1 = depositoryRecordService.findMaterialCountByMonth2(parm); |
|||
productData.add(materialCountByMonth1); |
|||
} |
|||
return productData; |
|||
} |
|||
} |
|||
|
|||
|
|||
/** |
|||
* 获取本月之前的月份 |
|||
* |
|||
* @return |
|||
*/ |
|||
public static Map<String, Object> getPreviousMonth() { |
|||
Calendar instance = Calendar.getInstance(); |
|||
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); |
|||
Map<String, Object> source = new HashMap<>(); |
|||
List<Object> sourceList = new ArrayList<>(); |
|||
int month = instance.get(Calendar.MONTH) + 1; |
|||
ArrayList<Object> months = new ArrayList<>(); |
|||
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.set(Calendar.DAY_OF_MONTH, 1); |
|||
months.add(DateUtil.DateTimeByMonthToTimeStamp(formatter.format(instance.getTime()))); |
|||
Map<String, Object> map = new HashMap<>(); |
|||
map.put("months", months); |
|||
map.put("sourceList", sourceList); |
|||
return map; |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,213 @@ |
|||
package com.dreamchaser.depository_manage; |
|||
|
|||
import com.dreamchaser.depository_manage.controller.DepositoryController; |
|||
import com.dreamchaser.depository_manage.entity.MaterialType; |
|||
import com.dreamchaser.depository_manage.mapper.MaterialTypeMapper; |
|||
import com.dreamchaser.depository_manage.service.DepositoryRecordService; |
|||
import com.dreamchaser.depository_manage.service.DepositoryService; |
|||
import com.dreamchaser.depository_manage.service.MaterialTypeService; |
|||
import com.dreamchaser.depository_manage.utils.DateUtil; |
|||
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 javax.swing.*; |
|||
import java.text.SimpleDateFormat; |
|||
import java.util.*; |
|||
import java.util.concurrent.*; |
|||
|
|||
@SpringBootTest |
|||
@RunWith(SpringRunner.class) |
|||
public class TestForgetSourceList { |
|||
|
|||
@Autowired |
|||
DepositoryRecordService depositoryRecordService; |
|||
|
|||
@Autowired |
|||
DepositoryService depositoryService; |
|||
|
|||
@Autowired |
|||
MaterialTypeService materialTypeService; |
|||
|
|||
@Autowired |
|||
MaterialTypeMapper materialTypeMapper; |
|||
|
|||
|
|||
@Test |
|||
public void Test(){ |
|||
// List<Object> sourceList = getSourceList("1");
|
|||
// System.out.println(sourceList);
|
|||
MaterialType materialTypeByOldId = materialTypeMapper.findMaterialTypeByOldId(Long.valueOf("25")); |
|||
System.out.println(findChildForMaterialTypeByParent(materialTypeByOldId)); |
|||
|
|||
} |
|||
|
|||
// 根据id获取子类
|
|||
public List<Long> findChildForMaterialTypeByParent(MaterialType mt) { |
|||
List<Long> result = new ArrayList<>(); |
|||
result.add(mt.getOldId()); |
|||
List<Long> parentId = new ArrayList<>(); |
|||
parentId.add(mt.getOldId()); |
|||
List<MaterialType> materialTypeAll = materialTypeMapper.findMaterialTypeAll(); |
|||
for (int i = 0; i < materialTypeAll.size(); i++) { |
|||
MaterialType materialType = materialTypeAll.get(i); |
|||
if (isTrueForParent(parentId, materialType.getParentId())) { |
|||
parentId.add(materialType.getOldId()); |
|||
result.add(materialType.getOldId()); |
|||
} |
|||
} |
|||
return result; |
|||
} |
|||
|
|||
public Boolean isTrueForParent(List<Long> parentList, Long id) { |
|||
for (Long aLong : parentList) { |
|||
if (Long.compare(aLong, id) == 0) { |
|||
return true; |
|||
} |
|||
} |
|||
return false; |
|||
} |
|||
|
|||
//获取本月及之前月份各种类别入/出库总量
|
|||
public List<Object> getSourceList(String type){ |
|||
|
|||
List<MaterialType> materialTypeAll = materialTypeService.findMaterialTypeNoParent(); |
|||
Map<String, Object> previousMonth = getPreviousMonth(); |
|||
List<Object> months = (List<Object>) previousMonth.get("months"); |
|||
List<Object> sourceList = (List<Object>) previousMonth.get("sourceList"); |
|||
ExecutorService exs = Executors.newFixedThreadPool(months.size()); |
|||
// 结果集
|
|||
List<Future<Object>> futureList = new ArrayList<Future<Object>>(); |
|||
// 1.定义CompletionService
|
|||
CompletionService<Object> completionService = new ExecutorCompletionService<Object>(exs); |
|||
for (int num = 0; num < months.size() - 1; num++) { |
|||
Map<String,Object> map = (Map<String, Object>) sourceList.get(num); |
|||
Future<Object> future = completionService.submit(new getSourceListTask(map,type,months.get(num + 1).toString(),months.get(num).toString(),materialTypeAll)); |
|||
futureList.add(future); |
|||
} |
|||
for (int i = 0; i < months.size() - 1; i++) { |
|||
Object result = null; |
|||
try { |
|||
result = completionService.take(); |
|||
} catch (InterruptedException e) { |
|||
e.printStackTrace(); |
|||
} |
|||
} |
|||
List<String> barSource = new ArrayList<>(); |
|||
barSource.add("month"); |
|||
|
|||
for (int i = 0; i < materialTypeAll.size(); i++) { |
|||
barSource.add(materialTypeAll.get(i).getTname()); |
|||
} |
|||
sourceList.add(barSource); |
|||
return sourceList; |
|||
} |
|||
|
|||
|
|||
// 具体执行getSourceList逻辑
|
|||
class getSourceListTask implements Callable<Object> { |
|||
|
|||
String type; |
|||
String start; |
|||
String end; |
|||
Map<String,Object> map; |
|||
List<MaterialType> materialTypeAll; |
|||
getSourceListTask(Map<String,Object> map, String type, String start, String end,List<MaterialType> materialTypeAll){ |
|||
this.map = map; |
|||
this.type = type; |
|||
this.start = start; |
|||
this.end = end; |
|||
this.materialTypeAll = materialTypeAll; |
|||
} |
|||
@Override |
|||
public Object call() throws Exception { |
|||
ExecutorService exs = Executors.newFixedThreadPool(materialTypeAll.size()); |
|||
// 结果集
|
|||
List<Future<Object>> futureList = new ArrayList<Future<Object>>(); |
|||
// 1.定义CompletionService
|
|||
CompletionService<Object> completionService = new ExecutorCompletionService<Object>(exs); |
|||
|
|||
for (int j = 0; j < materialTypeAll.size(); j++) { |
|||
Map<String, Object> parm = new HashMap<>(); |
|||
parm.put("type", Integer.parseInt(type)); |
|||
if (Integer.parseInt(type) == 1) { |
|||
parm.put("state", "已入库"); |
|||
} else if (Integer.parseInt(type) == 2) { |
|||
parm.put("state", "已出库"); |
|||
} |
|||
parm.put("start",start); |
|||
parm.put("end", end); |
|||
parm.put("oldId", materialTypeAll.get(j).getOldId()); |
|||
//根据条件获取月份中物料的总额
|
|||
// 测试
|
|||
|
|||
Future<Object> future = completionService.submit(new findMaterialCountTaskForSourceList(parm)); |
|||
futureList.add(future); |
|||
} |
|||
for (int i = 0; i < materialTypeAll.size(); i++) { |
|||
Object result = null; |
|||
try { |
|||
result = completionService.take().get(); |
|||
} catch (InterruptedException e) { |
|||
e.printStackTrace(); |
|||
} catch (ExecutionException e) { |
|||
e.printStackTrace(); |
|||
} |
|||
map.putAll((Map<? extends String, ?>) result); |
|||
} |
|||
return map; |
|||
} |
|||
} |
|||
|
|||
|
|||
// 根据条件获取月份中物料的总额用于sourcelist
|
|||
class findMaterialCountTaskForSourceList implements Callable<Object> { |
|||
|
|||
Map<String,Object> map; |
|||
findMaterialCountTaskForSourceList(Map<String,Object> map){ |
|||
this.map = map; |
|||
} |
|||
@Override |
|||
public Object call() throws Exception { |
|||
Map<String,Object> result = new HashMap<>(); |
|||
Integer materialCountByMonth1 = depositoryRecordService.findMaterialCountByMonth2(map); |
|||
Long oldId = Long.valueOf(map.get("oldId").toString()); |
|||
MaterialType materialTypeByOldId = materialTypeService.findMaterialTypeByOldId(oldId); |
|||
result.put(materialTypeByOldId.getTname(),materialCountByMonth1); |
|||
return result; |
|||
} |
|||
} |
|||
|
|||
|
|||
/** |
|||
* 获取本月之前的月份 |
|||
* |
|||
* @return |
|||
*/ |
|||
public static Map<String, Object> getPreviousMonth() { |
|||
Calendar instance = Calendar.getInstance(); |
|||
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); |
|||
Map<String, Object> source = new HashMap<>(); |
|||
List<Object> sourceList = new ArrayList<>(); |
|||
int month = instance.get(Calendar.MONTH) + 1; |
|||
ArrayList<Object> months = new ArrayList<>(); |
|||
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.set(Calendar.DAY_OF_MONTH, 1); |
|||
months.add(DateUtil.DateTimeByMonthToTimeStamp(formatter.format(instance.getTime()))); |
|||
Map<String, Object> map = new HashMap<>(); |
|||
map.put("months", months); |
|||
map.put("sourceList", sourceList); |
|||
return map; |
|||
} |
|||
} |
|||
|
|||
Loading…
Reference in new issue