liujie
2026-01-29 8ab97da36f98d75daed63cb795f037a13c530024
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
package com.ruoyi.system.service.impl;
 
import com.alibaba.fastjson2.JSONArray;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.system.mapper.ProjectInfoMapper;
import com.ruoyi.system.model.Project;
import com.ruoyi.system.model.ProjectInfo;
import com.ruoyi.system.model.Record;
import com.ruoyi.system.query.ProjectDangerousTrendQuery;
import com.ruoyi.system.query.ProjectHiddenDangerTrendListQuery;
import com.ruoyi.system.service.ProjectCategoryCalculator;
import com.ruoyi.system.service.ProjectInfoService;
import com.ruoyi.system.service.ProjectService;
import com.ruoyi.system.vo.ProjectDangerousTrendVo;
import com.ruoyi.system.vo.ProjectHiddenDangerList;
import com.ruoyi.system.vo.ProjectHiddenDangerNextList;
import com.ruoyi.system.vo.ProjectHiddenDangerTrendVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.YearMonth;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
 
import static java.util.stream.Collectors.groupingBy;
 
/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author l
 * @since 2026-01-12
 */
@Service
public class ProjectInfoServiceImpl extends ServiceImpl<ProjectInfoMapper, ProjectInfo> implements ProjectInfoService {
 
 
    @Autowired
    private ProjectCategoryCalculator projectCategoryCalculator;
 
    @Autowired
    private ProjectService projectService;
 
    @Override
    public List<ProjectHiddenDangerTrendVo> getProjectHiddenDangerTrend(Integer projectId) {
        // 获取上月第一天的时间
        LocalDate lastMonth = LocalDate.now().minusMonths(1).with(TemporalAdjusters.firstDayOfMonth());
        List<Record> list = this.baseMapper.getProjectHiddenDangerTrend(projectId,lastMonth+" 00:00:00",LocalDate.now()+" 23:59:59");
        for (Record record : list) {
            String hierarchyNames = record.getHierarchyNames();
            JSONArray jsonArray = JSONArray.parseArray(hierarchyNames);
            if(record.getDeviceId()!=null){
                // 取出倒数第3个
                String name = jsonArray.getString(jsonArray.size()-3);
 
                record.setHierarchyName( name);
            }else {
                // 取出倒数第4个
                String name = jsonArray.getString(jsonArray.size()-4);
 
                record.setHierarchyName( name);
            }
 
 
        }
        LocalDate thisMonth = LocalDate.now().minusMonths(0).with(TemporalAdjusters.firstDayOfMonth());
        Map<String, List<Record>> map = list.stream().collect(groupingBy(Record::getHierarchyName));
        List<ProjectHiddenDangerTrendVo> resultList = new ArrayList<>();
        for (Map.Entry<String, List<Record>> entry : map.entrySet()) {
            ProjectHiddenDangerTrendVo vo = new ProjectHiddenDangerTrendVo();
            vo.setName(entry.getKey());
            List<Record> value = entry.getValue();
            long count = value.stream().filter(record -> record.getCreateTime().isAfter(thisMonth.atStartOfDay())).count();
            // 根据value集合中的createTime 选出本月的数据
            vo.setThisMonth((int) count);
            vo.setLastMonth(value.size()-(int) count);
 
            int num = vo.getThisMonth() - vo.getLastMonth();
            if (num != 0) {
                if(vo.getLastMonth()>0){
                    Double rate = BigDecimal.valueOf(num).divide(BigDecimal.valueOf(vo.getLastMonth()), 4, RoundingMode.HALF_UP).multiply(BigDecimal.valueOf(100)).setScale(2, RoundingMode.HALF_UP).doubleValue();
                    vo.setRate(rate);
                }else {
                    vo.setRate(100.00d);
                }
            }else {
                vo.setRate(0.00d);
            }
            resultList.add(vo) ;
        }
 
 
        return resultList;
    }
 
    @Override
    public List<ProjectHiddenDangerList> getProjectHiddenDangerTrendList(ProjectHiddenDangerTrendListQuery query) {
        List<Record> list = this.baseMapper.getProjectHiddenDangerTrend(query.getProjectId(),getMonthBoundaries(query.getStartTime(),true)+" 00:00:00",getMonthBoundaries(query.getEndTime(),false)+" 23:59:59");
        for (Record record : list) {
            String hierarchyNames = record.getHierarchyNames();
            JSONArray jsonArray = JSONArray.parseArray(hierarchyNames);
            if(record.getDeviceId()!=null){
                // 取出倒数第3个
                String name = jsonArray.getString(jsonArray.size()-3);
 
                record.setHierarchyName( name);
            }else {
                // 取出倒数第4个
                String name = jsonArray.getString(jsonArray.size()-4);
 
                record.setHierarchyName( name);
            }
 
        }
 
        // 算出跨度几个月
        int months = calculateMonthsBetween(query.getStartTime(),query.getEndTime());
 
 
        ArrayList<ProjectHiddenDangerList> projectHiddenDangerLists = new ArrayList<>();
 
        Map<String, List<Record>> map = list.stream().collect(groupingBy(Record::getHierarchyName));
        for (Map.Entry<String, List<Record>> stringListEntry : map.entrySet()) {
            String key = stringListEntry.getKey();
            if(query.getContent()!=null && !query.getContent().isEmpty()){
                if(!key.contains( query.getContent())){
                    continue;
                }
            }
 
            List<Record> value = stringListEntry.getValue();
            ProjectHiddenDangerList projectHiddenDangerList1 = new ProjectHiddenDangerList();
            projectHiddenDangerList1.setName(key);
            ArrayList<ProjectHiddenDangerNextList> projectHiddenDangerNextLists = new ArrayList<>();
            for (int i = 0; i < months; i++) {
                ProjectHiddenDangerNextList projectHiddenDangerNextList = new ProjectHiddenDangerNextList();
                LocalDate localDate = getMonthBoundaries(query.getStartTime(), true).plusMonths(i);
                String dateStr = localDate.toString().substring(0, 7);
                long count = value.stream().filter(record -> record.getCreateTime().toString().contains(dateStr)).count();
                ProjectHiddenDangerList projectHiddenDangerList = new ProjectHiddenDangerList();
                projectHiddenDangerList.setName(key);
                projectHiddenDangerNextList.setTime(dateStr);
                projectHiddenDangerNextList.setNum((int) count);
                projectHiddenDangerNextLists.add(projectHiddenDangerNextList);
            }
            projectHiddenDangerList1.setList(projectHiddenDangerNextLists);
            projectHiddenDangerLists.add(projectHiddenDangerList1);
        }
 
        return projectHiddenDangerLists;
 
    }
 
    @Override
    public List<ProjectDangerousTrendVo> getProjectDangerousTrend(ProjectDangerousTrendQuery query) {
        List<ProjectDangerousTrendVo> list = new ArrayList<>();
        Project project = projectService.getById(query.getProjectId());
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 算出跨度几个月
        int months = calculateMonthsBetween(query.getStartTime(),query.getEndTime());
        ArrayList<Project> projects = new ArrayList<>();
        projects.add( project);
        String temp=null;
        try {
            for (int i = 0; i < months; i++) {
                ProjectDangerousTrendVo vo = new ProjectDangerousTrendVo();
                LocalDate localDate = getMonthBoundaries(query.getStartTime(), true).plusMonths(i);
                String dateStr = localDate.toString().substring(0, 7);
                String start = localDate+" 00:00:00";
                String end = getMonthBoundaries(query.getStartTime(), false).plusMonths(i)+" 23:59:59";
                Date startDate = simpleDateFormat.parse(start);
                Date endDate = simpleDateFormat.parse(end);
                vo.setTime(dateStr);
 
                if(project.getType()==2){
                    Map<Integer, String> integerStringMap = projectCategoryCalculator.calculateFixedAssetCategories(projects, Long.valueOf(project.getClientId()), startDate, endDate);
                    String s = integerStringMap.get(query.getProjectId());
                    vo.setLevel(s);
                }else {
                    Map<Integer, String> integerStringMap = projectCategoryCalculator.calculateOngoingProjectCategories(projects, Long.valueOf(project.getClientId()), startDate, endDate);
                    String s = integerStringMap.get(query.getProjectId());
                    vo.setLevel(s);
                }
                // 如果没有则沿用上次的
                if(vo.getLevel()!=null){
                    temp=vo.getLevel();
                }else {
                    vo.setLevel(temp);
                }
                list.add( vo);
            }
        }catch (ParseException e){
            e.printStackTrace();
        }
 
        return list;
    }
 
 
    public static LocalDate getMonthBoundaries(String yearMonthStr,boolean isInclusive) {
        // 解析字符串为YearMonth
        YearMonth yearMonth = YearMonth.parse(yearMonthStr);
        if(isInclusive){
            // 获取第一天
            LocalDate firstDay = yearMonth.atDay(1);
            return firstDay;
        }else {
            // 获取最后一天
            LocalDate lastDay = yearMonth.atEndOfMonth();
            return lastDay;
        }
    }
 
 
 
    public static int calculateMonthsBetween(String startYearMonth, String endYearMonth) {
        YearMonth start = YearMonth.parse(startYearMonth);
        YearMonth end = YearMonth.parse(endYearMonth);
 
        // 计算月份差 + 1(包含结束月份)
        return (int) ChronoUnit.MONTHS.between(start, end) + 1;
    }
}