jinpengyong
2024-06-19 32cc13189371ee1e367897a64fbc22f90b53add8
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
package com.moral.api.utils;
 
import org.apache.commons.collections4.CollectionUtils;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
import com.moral.api.vo.HeatMapVo;
import com.moral.api.vo.TimeHourVo;
import com.moral.util.DateUtils;
 
/**
 * Description //todo
 *
 * @author swb
 * @ClassName HeatMapTimeUtils
 * @date 2023.12.18 13:42
 */
 
 
public class HeatMapTimeUtils {
 
    /**
     *获取某段时间的每天天数,和小时数
     */
    public static List<HeatMapVo> getTime(String startTime, String endTime, String type){
        ArrayList<HeatMapVo> heatMapVos = new ArrayList<>();
        if (type.equals("day")){
            ArrayList<TimeHourVo> timeHourVos = new ArrayList<>();
            String[] splitStart = startTime.split("-");
            String[] splitEnd = endTime.split("-");
            LocalDate startDate = LocalDate.of(Integer.parseInt(splitStart[0]), Integer.parseInt(splitStart[1]), Integer.parseInt(splitStart[2]));
            LocalDate endDate = LocalDate.of(Integer.parseInt(splitEnd[0]), Integer.parseInt(splitEnd[1]), Integer.parseInt(splitEnd[2]));
            long daysBetween = ChronoUnit.DAYS.between(startDate, endDate);
            for (int i = 0; i <= daysBetween; i++) {
                HeatMapVo heatMapVo = new HeatMapVo();
                LocalDate date = startDate.plusDays(i);
                heatMapVo.setId(i);
                heatMapVo.setName(date.toString());
                heatMapVo.setHourListTime(timeHourVos);
                heatMapVos.add(heatMapVo);
            }
        }else if (type.equals("month")){
            ArrayList<TimeHourVo> timeHourVos = new ArrayList<>();
            String[] splitStart = startTime.split("-");
            String[] splitEnd = endTime.split("-");
            //结束时间月份的天数
 
            int monthDay = DateUtils.getMonthDay(DateUtils.getDate(endTime,DateUtils.yyyy_MM_EN));
            LocalDate startDate = LocalDate.of(Integer.parseInt(splitStart[0]), Integer.parseInt(splitStart[1]),1);
            LocalDate endDate = LocalDate.of(Integer.parseInt(splitEnd[0]), Integer.parseInt(splitEnd[1]),monthDay);
            long daysBetween = ChronoUnit.DAYS.between(startDate, endDate);
            for (int i = 0; i <= daysBetween; i++) {
                HeatMapVo heatMapVo = new HeatMapVo();
                LocalDate date = startDate.plusDays(i);
                heatMapVo.setId(i);
                heatMapVo.setName(date.toString());
                heatMapVo.setHourListTime(timeHourVos);
                heatMapVos.add(heatMapVo);
            }
        }else {
            int id = 0;
            int idHour = -1;
            Date ks = DateUtils.getDate(startTime,"yyyy-MM-dd HH");
            Date js = DateUtils.getDateAddHour(DateUtils.getDate(endTime,"yyyy-MM-dd HH"),1);
            String name =  DateUtils.dateToDateString(ks,DateUtils.yyyy_MM_dd_EN);
            List<TimeHourVo> hourListTime = new ArrayList<>();
            while (DateUtils.isTimeBefor(js,ks)){
                idHour++;
                HeatMapVo heatMapVo = new HeatMapVo();
                String nameEndTime =  DateUtils.dateToDateString(ks,DateUtils.yyyy_MM_dd_EN);
                String time =  DateUtils.dateToDateString(ks,"HH");
                String ksTime =  DateUtils.dateToDateString(ks,"yyyy-MM-dd HH");
                TimeHourVo hourVo = new TimeHourVo();
                heatMapVo.setName(name);
                heatMapVo.setId(id);
                if(name.equals(nameEndTime)){
                    heatMapVo.setId(id);
                    heatMapVo.setName(name);
                    hourVo.setName(time);
                    hourVo.setId(idHour);
                    hourListTime.add(hourVo);
                    if(ksTime.equals(endTime)){
                        heatMapVo.setHourListTime(hourListTime);
                        heatMapVos.add(heatMapVo);
                        idHour++;
                        break;
                    }
                }else {
                    id++;
                    if(ksTime.equals(endTime)){
                        heatMapVo.setHourListTime(hourListTime);
                        heatMapVos.add(heatMapVo);
                        hourListTime = new ArrayList<>();
                        hourVo = new TimeHourVo();
                        heatMapVo = new HeatMapVo();
                        hourVo.setName("00");
                        hourVo.setId(idHour++);
                        hourListTime.add(hourVo);
                        heatMapVo.setId(id);
                        heatMapVo.setName(nameEndTime);
                        heatMapVo.setHourListTime(hourListTime);
                        heatMapVos.add(heatMapVo);
                        break;
                    }else {
                        name = nameEndTime;
                        heatMapVo.setHourListTime(hourListTime);
                        heatMapVos.add(heatMapVo);
                        hourListTime = new ArrayList<>();
                        hourVo.setName(time);
                        hourVo.setId(idHour);
                        hourListTime.add(hourVo);
                    }
                }
                ks = DateUtils.getDateAddHour(ks,1);
            }
            if(CollectionUtils.isNotEmpty(heatMapVos)){
                heatMapVos.get(0).setIdLength(idHour);
            }
 
/*            String[] splitStart = startTime.split("-");
            String[] s1 = splitStart[2].split(" ");
            String[] splitEnd = endTime.split("-");
            String[] s2 = splitEnd[2].split(" ");
            //获取天时间
            LocalDate startDate = LocalDate.of(Integer.parseInt(splitStart[0]), Integer.parseInt(splitStart[1]),Integer.parseInt(s1[0]));
            LocalDate endDate = LocalDate.of(Integer.parseInt(splitEnd[0]), Integer.parseInt(splitEnd[1]),Integer.parseInt(s2[0]));
            long daysBetween = ChronoUnit.DAYS.between(startDate, endDate);
            //获取小时
            LocalDateTime startDateTime = LocalDateTime.of(Integer.parseInt(splitStart[0]), Integer.parseInt(splitStart[1]), Integer.parseInt(s1[0]), Integer.parseInt(s1[1]), 0);
            LocalDateTime endDateTime = LocalDateTime.of(Integer.parseInt(splitEnd[0]), Integer.parseInt(splitEnd[1]), Integer.parseInt(s2[0]), Integer.parseInt(s2[1]), 0);
            List<String> hourList = new ArrayList<>();
            LocalDateTime currentDateTime = startDateTime;
            while (currentDateTime.isBefore(endDateTime) || currentDateTime.equals(endDateTime)) {
                hourList.add(currentDateTime.toString());
                currentDateTime = currentDateTime.plusHours(1);
            }
            int length=0;
            for (int i = 0; i <= daysBetween; i++) {
                ArrayList<TimeHourVo> timeHourVos = new ArrayList<>();
                HeatMapVo heatMapVo = new HeatMapVo();
                LocalDate date = startDate.plusDays(i);
                heatMapVo.setId(i);
                heatMapVo.setName(date.toString());
                for (int i1 = 0; i1 < hourList.size(); i1++) {
                    if (hourList.get(i1).contains(date.toString())){
                        TimeHourVo hourVo = new TimeHourVo();
                        hourVo.setName(hourList.get(i1).substring(11,13));
                        hourVo.setId(length);
                        timeHourVos.add(hourVo);
                        hourList.remove(i1);
                        i1--;
                        length++;
                    }
                }
                heatMapVo.setHourListTime(timeHourVos);
                heatMapVos.add(heatMapVo);
            }
            heatMapVos.get(0).setIdLength(length);*/
        }
 
        return heatMapVos;
    }
 
}