package com.whyc.service;
|
|
import com.whyc.dto.BattTestData;
|
import com.whyc.dto.Response;
|
import com.whyc.factory.ThreadPoolExecutorFactory;
|
import com.whyc.mapper.PwrdevHistorydataGwMapper;
|
import com.whyc.pojo.PwrdevHistorydataGw;
|
import com.whyc.util.ActionUtil;
|
import com.whyc.util.DateUtil;
|
import com.whyc.util.ThreadLocalUtil;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Service;
|
|
import java.text.ParseException;
|
import java.util.*;
|
import java.util.concurrent.CountDownLatch;
|
import java.util.concurrent.ThreadPoolExecutor;
|
import java.util.concurrent.TimeUnit;
|
import java.util.stream.Collectors;
|
|
import static java.lang.Thread.sleep;
|
|
@Service
|
public class PwrdevHistorydataGwService {
|
@Autowired(required = false)
|
private PwrdevHistorydataGwMapper mapper;
|
|
@Autowired
|
private SubTablePageInfoService subService;
|
|
|
//查询历史实时数据-优化-多线程
|
public Response serchByCondition(Date recordTime1, Date recordTime2, int devId) throws ParseException, InterruptedException {
|
List<PwrdevHistorydataGw> dataList = new LinkedList<>();
|
List<List<Date>> monthTimeList = DateUtil.getMonthTime(recordTime1, recordTime2);
|
|
//线程池
|
//UserThreadFactory userThreadFactory = new UserThreadFactory("BattRealDataThread");
|
//ThreadPoolExecutor pool = new ThreadPoolExecutor(12, 12, 10, TimeUnit.MINUTES, new LinkedBlockingDeque<>(10), userThreadFactory);
|
ThreadPoolExecutor pool = ThreadPoolExecutorFactory.getPoolExecutor();
|
|
CountDownLatch latch = new CountDownLatch(monthTimeList.size());
|
for (int i = 0; i < monthTimeList.size(); i++) {
|
int finalI = i;
|
pool.execute(() -> {
|
int finalII = finalI;
|
Date startTime = monthTimeList.get(finalII).get(0);
|
Date stopTime = monthTimeList.get(finalII).get(1);
|
//String table = devId + "_" + ActionUtil.sdfwithOutday.format(startTime);
|
String table = devId + "_" + ThreadLocalUtil.format(startTime,2);
|
//System.err.println("finalII:" + finalII + ",table:" + table);
|
//判断表是否存在
|
//int tableNum = mapper.judgeTable(table);
|
int tableNum = subService.judgeTable_pwr(table);
|
List<PwrdevHistorydataGw> list = new ArrayList();
|
if (tableNum > 0) {
|
//List<Integer> calcNumList = mapper.searchMaxNum2(startTime, stopTime, table);
|
List<Integer> calcNumList = subService.searchMaxNum2(startTime, stopTime, table);
|
int maxNum = calcNumList == null || calcNumList.isEmpty() ? 0 : calcNumList.get(1) - calcNumList.get(0);
|
int roteN = 0;
|
int endN = BattTestData.RC_NUM_Real;//总笔数
|
//去除筛选,将全部数据取出
|
if (maxNum <= endN) {
|
roteN = 1;
|
} else {
|
if (maxNum % endN == 0) {
|
roteN = maxNum / endN;
|
} else {
|
roteN = maxNum / endN + 1;
|
}
|
}
|
//list = mapper.serchByCondition(startTime, stopTime, table, roteN);
|
list = subService.serchByCondition(startTime, stopTime, table, roteN);
|
}
|
dataList.addAll(list);
|
latch.countDown();
|
//System.out.println("table:"+table+"完成");
|
});
|
sleep(200);
|
}
|
//pool.shutdown();
|
//pool.awaitTermination(1,TimeUnit.HOURS);
|
latch.await(10, TimeUnit.MINUTES);
|
List dataListSorted = dataList.stream().sorted(Comparator.comparing(PwrdevHistorydataGw::getRecordTime)).collect(Collectors.toList());
|
return new Response().set(1, dataListSorted);
|
}
|
}
|