whyclxw
2025-05-08 35bd0babece513b6ee4285265f3379cbcb050e59
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
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, "前一百比数据");
    }
}