lxw
2023-10-25 c80cf92221bb3dca7871e06dc814fdaa72aed902
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
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;
 
 
    //查询历史实时数据-优化-多线程
    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);
                List<PwrdevHistorydataGw> list = new ArrayList();
                if (tableNum > 0) {
                    List<Integer> calcNumList = mapper.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);
                }
                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);
    }
}