package com.whyc.service;
|
|
import com.github.pagehelper.PageInfo;
|
import com.whyc.abe.ABEDataParase;
|
import com.whyc.abe.ABEDataParseUtil;
|
import com.whyc.dto.BattDataDTO;
|
import com.whyc.dto.BattTestData;
|
import com.whyc.dto.Response;
|
import com.whyc.dto.result.RealDateDTO;
|
import com.whyc.factory.ThreadPoolExecutorFactory;
|
import com.whyc.mapper.BattRealdataMapper;
|
import com.whyc.pojo.Batt4815HisData;
|
import com.whyc.pojo.BattRealdata;
|
import com.whyc.util.ActionUtil;
|
import com.whyc.util.DateUtil;
|
import com.whyc.util.ReflectUtil;
|
import com.whyc.util.ThreadLocalUtil;
|
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
|
import org.apache.poi.ss.usermodel.Cell;
|
import org.apache.poi.ss.usermodel.Sheet;
|
import org.apache.poi.ss.usermodel.Workbook;
|
import org.apache.poi.ss.usermodel.WorkbookFactory;
|
import org.springframework.beans.BeanUtils;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Service;
|
import org.springframework.web.multipart.MultipartFile;
|
|
import javax.annotation.Resource;
|
import java.io.IOException;
|
import java.io.InputStream;
|
import java.text.ParseException;
|
import java.text.SimpleDateFormat;
|
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 BattRealdataService {
|
@Resource
|
private BattRealdataMapper mapper;
|
|
@Autowired
|
private SubTablePageInfoService subService;
|
|
//查询历史实时数据
|
public Response serchByCondition(BattRealdata realdata) {
|
//String table = realdata.getBattGroupId() + "_" + ActionUtil.sdfwithOutday.format(realdata.getRecrodTime());
|
String table = realdata.getBattGroupId() + "_" + ThreadLocalUtil.format(realdata.getRecrodTime(),2);
|
realdata.setTableName(table);//表名时间格式部分
|
//判断表是否存在
|
//int tableNum = mapper.judgeTable(table);
|
int tableNum = subService.judgeTable_realdata(table);
|
List<RealDateDTO> list = new ArrayList();
|
List listEnd = new ArrayList();
|
if (tableNum > 0) {
|
//int maxNum = mapper.searchMaxNum(realdata);
|
//int minNum=mapper.searchMinNum(realdata);
|
//realdata.setMaxNum(maxNum);
|
//realdata.setMinNum(minNum);
|
//int monCount=Integer.valueOf(realdata.getNote());//单体总数
|
//int number=realdata.getMaxNum()/monCount;//要取出的总数
|
//list = mapper.serchByCondition(realdata);
|
list = subService.serchByCondition(realdata);
|
int number = (list.size() == 0 ? 0 : list.size());//单个单体总数
|
int roteN = 0;
|
int endN = BattTestData.RC_NUM_Real;//总笔数
|
//去除筛选,将全部数据取出
|
if (number <= endN) {
|
roteN = 1;
|
} else {
|
if (number % endN == 0) {
|
roteN = number / endN;
|
} else {
|
roteN = number / endN + 1;
|
}
|
}
|
//roteN=roteN*monCount;
|
//realdata.setRoteN(roteN);
|
int i = 0;
|
for (RealDateDTO b : list) {
|
if (i % roteN == 0) {
|
listEnd.add(b);
|
}
|
i++;
|
}
|
}
|
PageInfo pageInfo = new PageInfo(listEnd);
|
return new Response().set(1, pageInfo);
|
}
|
|
/**
|
* 多线程处理
|
*
|
* @param realdata2
|
* @return
|
*/
|
public Response serchByCondition2(BattRealdata realdata2) throws ParseException, InterruptedException {
|
List<RealDateDTO> dataList = new LinkedList<>();
|
List<List<Date>> monthTimeList = DateUtil.getMonthTime(realdata2.getRecrodTime(), realdata2.getRecrodTime1());
|
|
//线程池
|
//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;
|
BattRealdata realdata = new BattRealdata();
|
BeanUtils.copyProperties(realdata2, realdata);
|
realdata.setRecrodTime(monthTimeList.get(finalII).get(0));
|
realdata.setRecrodTime1(monthTimeList.get(finalII).get(1));
|
|
//String table = realdata.getBattGroupId() + "_" + ActionUtil.sdfwithOutday.format(realdata.getRecrodTime());
|
String table = realdata.getBattGroupId() + "_" + ThreadLocalUtil.format(realdata.getRecrodTime(),2);
|
//System.err.println("finalII:" + finalII + ",table:" + table);
|
realdata.setTableName(table);//表名时间格式部分
|
//判断表是否存在
|
//int tableNum = mapper.judgeTable(table);
|
int tableNum = subService.judgeTable_realdata(table);
|
List<RealDateDTO> list = new ArrayList();
|
if (tableNum > 0) {
|
//List<Integer> calcNumList = mapper.searchMaxNum2(realdata);
|
List<Integer> calcNumList = subService.searchMaxNum2(realdata);
|
int maxNum = calcNumList == null || calcNumList.isEmpty() ? 0 : calcNumList.get(1) - calcNumList.get(0);
|
int number = maxNum / Integer.parseInt(realdata.getNote());//单个单体总数
|
int roteN = 0;
|
int endN = BattTestData.RC_NUM_Real;//总笔数
|
//去除筛选,将全部数据取出
|
if (number <= endN) {
|
roteN = 1;
|
} else {
|
if (number % endN == 0) {
|
roteN = number / endN;
|
} else {
|
roteN = number / endN + 1;
|
}
|
}
|
realdata.setRoteN(roteN);
|
//list = mapper.serchByCondition2(realdata);
|
list = subService.serchByCondition2(realdata);
|
}
|
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(RealDateDTO::getRecrodTime)).collect(Collectors.toList());
|
//System.out.println(realdata2.getBattGroupId()+"完成");
|
//return new Response().set(1,dataList);
|
return new Response().set(1, dataListSorted);
|
|
/*String table=realdata.getBattGroupId()+"_"+ActionUtil.sdfwithOutday.format(realdata.getRecrodTime());
|
realdata.setTableName(table);//表名时间格式部分
|
//判断表是否存在
|
int tableNum = mapper.judgeTable(table);
|
List<RealDateDTO> list = new ArrayList();
|
if(tableNum>0){
|
List<Integer> calcNumList = mapper.searchMaxNum2(realdata);
|
|
int maxNum = calcNumList == null ||calcNumList.isEmpty() ? 0:calcNumList.get(1)-calcNumList.get(0);
|
int number = maxNum/Integer.parseInt(realdata.getNote());//单个单体总数
|
int roteN = 0;
|
int endN = BattTestData.RC_NUM_Real;//总笔数
|
//去除筛选,将全部数据取出
|
if (number <= endN) {
|
roteN = 1;
|
} else {
|
if (number % endN == 0) {
|
roteN = number / endN;
|
} else {
|
roteN = number / endN + 1;
|
}
|
}
|
realdata.setRoteN(roteN);
|
list = mapper.serchByCondition2(realdata);
|
|
}
|
PageInfo pageInfo = new PageInfo(list);
|
return new Response().set(1,pageInfo);*/
|
}
|
|
public Response excelParse(MultipartFile file) throws IOException, InvalidFormatException {
|
String name=file.getOriginalFilename();
|
if(!name.endsWith(".xls") && !name.endsWith(".xlsx")) {
|
return new Response<>().set(1, false, "文件类型错误");
|
}
|
List dataList = new LinkedList();
|
Workbook workbook = null;
|
InputStream inputStream = file.getInputStream();
|
workbook = WorkbookFactory.create(inputStream);
|
Sheet sheet = workbook.getSheet("数据表");
|
|
int lastRowNum = sheet.getLastRowNum();
|
short lastCellNum = sheet.getRow(0).getLastCellNum();
|
//获取单体的单元格值
|
List<String> monKey = new LinkedList<>();
|
for (int i = 4; i < lastCellNum; i++) {
|
Cell cell = sheet.getRow(0).getCell(i);
|
if(cell==null){
|
break;
|
}
|
monKey.add(cell.getStringCellValue());
|
}
|
sheet.getRow(0);
|
//从第2行开始
|
for (int i = 1; i <= lastRowNum; i++) {
|
BattDataDTO data = new BattDataDTO();
|
Map<String, Object> addProperties = new HashMap<>();
|
String value;
|
for (int j = 0; j < lastCellNum; j++) {
|
Cell cell = sheet.getRow(i).getCell(j);
|
if(j==0) {
|
Date dateCellValue = cell.getDateCellValue();
|
value = new SimpleDateFormat("HH:mm:ss").format(dateCellValue);
|
}else {
|
//当前行的单元格为空,当前行停止赋值
|
if(cell==null){
|
break;
|
}
|
cell.setCellType(Cell.CELL_TYPE_STRING);
|
value = cell.getStringCellValue();
|
}
|
//当前行的单元格值为空,当前行停止赋值
|
if(value.equals("")){
|
break;
|
}
|
switch (j){
|
case 0:{data.setTime(value);}break;
|
case 1:{data.setTotalVol(value);}break;
|
case 2:{data.setTotalCurr(value);}break;
|
case 3:{data.setCap(value);}break;
|
default:{
|
int index = j-4;
|
addProperties.put(monKey.get(index),value);
|
}
|
}
|
}
|
Object target = ReflectUtil.getTarget(data, addProperties);
|
dataList.add(target);
|
}
|
if(inputStream!=null){
|
try {
|
inputStream.close();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
return new Response().set(1, dataList);
|
}
|
|
public Response aBEParse(MultipartFile file) throws IOException {
|
ABEDataParase abeDataParase = ABEDataParseUtil.readFBSFile(file);
|
return new Response().set(1, abeDataParase);
|
}
|
|
//FBO4815实时页面需要前一百比历史实时数据
|
public Response serchDate100(int battGroupId) {
|
//String table = battGroupId + "_" + ActionUtil.sdfwithOutday.format(new Date());
|
String table = battGroupId + "_" + ThreadLocalUtil.format(new Date(),2);
|
//List<BattRealdata> list = mapper.serchDate100(table);
|
List<BattRealdata> list = subService.serchDate100(table);
|
return new Response().setII(1, list.size() > 0, list, "前一百比数据");
|
}
|
}
|