forked from gzzfw/backEnd/gz-dyh

huangh
2024-10-28 1f5f3c84cb9b35d65caf576c0d46d26bda291875
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
package cn.huge.module.analysis.controller;
 
import cn.huge.base.common.bo.ReturnBO;
import cn.huge.base.common.utils.ReturnFailUtils;
import cn.huge.base.common.utils.ReturnSucUtils;
import cn.huge.base.config.CurrentUser;
import cn.huge.module.analysis.domain.dto.CountRepeateDto;
import cn.huge.module.analysis.domain.vo.CountRepeateVo;
import cn.huge.module.analysis.service.AnalysisService;
import cn.huge.module.client.api.impl.CustClientImpl;
import cn.huge.module.cust.dto.CtUnitDTO;
import cn.huge.module.cust.dto.CtUserDTO;
import dm.jdbc.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
 
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
 
/**
 * @title: 纠纷信息主表接口api-web端
 * @description: 纠纷信息主表接口api-web端
 * @company: hugeinfo
 * @author: wangwh
 * @time: 2024-08-27 10:00:57
 * @version: 1.0.0
 */
@Slf4j
@RestController
@RequestMapping("/api/web/analysis")
public class AnalysisWebController {
 
    @Autowired(required = false)
    private HttpServletRequest request;
 
    @Autowired
    private AnalysisService service;
    @Autowired
    private CustClientImpl custClient;
 
 
    /**
     * 统计重复来访案件数量-根据案件类型分组
     * @url {ctx}/api/web/analysis/countRepeatedCasesByCaseType
     * @param
     * @return Object
     */
    @GetMapping("/countRepeatedCasesByCaseType")
    public ReturnBO countRepeatedCasesByCaseType(@RequestBody CountRepeateVo countRepeateVo) {
        try{
            countRepeateVo.setGroupByCaseType("true");
            List<CountRepeateDto> countRepeateDtoList = service.countRepeatedCases(countRepeateVo);
            return ReturnSucUtils.getRepInfo("请求成功",countRepeateDtoList);
        }catch (Exception e){
            log.error("统计重复来访案件数量异常",e);
            return ReturnFailUtils.getRepInfo(e.getMessage());
        }
    }
 
    /**
     * 统计重复来访案件数量-根据地区分组
     * @url {ctx}/api/web/analysis/countRepeatedCasesByArea
     * @param
     * @return Object
     */
    @GetMapping("/countRepeatedCasesByArea")
    public ReturnBO countRepeatedCasesByArea(@RequestBody CountRepeateVo countRepeateVo) {
        try{
 
            // 判断 countRepeateVo 的 queRoad 是否为空
            // 如果 queRoad 和 queArea 都为空且 queCity 不为空,则设置 groupByArea 为 true 根据 区 聚合
            if (StringUtil.isEmpty(countRepeateVo.getQueRoad()) &&
                    StringUtil.isEmpty(countRepeateVo.getQueArea()) &&
                    ! StringUtil.isEmpty(countRepeateVo.getQueCity())) {
                countRepeateVo.setGroupByArea("true");
            }
            // 如果 queRoad 为空且 queCity 和 queArea 不为空,则设置 groupByRoad 为 true 根据 街道 聚合
            else if (StringUtil.isEmpty(countRepeateVo.getQueRoad()) &&
                    ! StringUtil.isEmpty(countRepeateVo.getQueArea()) &&
                    ! StringUtil.isEmpty(countRepeateVo.getQueCity())) {
                countRepeateVo.setGroupByRoad("true");
            }
            List<CountRepeateDto> countRepeateDtoList = service.countRepeatedCases(countRepeateVo);
            if (countRepeateDtoList.size() == 1 && StringUtil.isEmpty(countRepeateDtoList.get(0).getGroupByType())) {
                countRepeateDtoList.get(0).setGroupByType("本级");
            }
            return ReturnSucUtils.getRepInfo("请求成功",countRepeateDtoList);
        }catch (Exception e){
            log.error("统计重复来访案件数量异常",e);
            return ReturnFailUtils.getRepInfo(e.getMessage());
        }
    }
 
    /**
     * 统计重复来访案件数量-根据案件类型分组
     * @url {ctx}/api/web/analysis/countRepeatedCasesByCaseType
     * @param
     * @return Object
     */
    @GetMapping("/countRepeatedCasesByTime")
    public ReturnBO countRepeatedCasesByTime(@RequestBody CountRepeateVo countRepeateVo) {
        try{
 
            DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            String dayTypeStr = "day";
            String monthTypeStr = "month";
            String yearTypeStr = "year";
 
            LocalDate start = LocalDate.parse(countRepeateVo.getCreateTimeStart(), DATE_TIME_FORMATTER);
            LocalDate end = LocalDate.parse(countRepeateVo.getCreateTimeEnd(), DATE_TIME_FORMATTER);
 
            long daysBetween = java.time.temporal.ChronoUnit.DAYS.between(start, end);
            if (daysBetween < 15) {
                countRepeateVo.setGroupByTime(dayTypeStr);
            } else if ((daysBetween >= 15) && (daysBetween < 365)) {
                countRepeateVo.setGroupByTime(monthTypeStr);
            } else {
                countRepeateVo.setGroupByTime(yearTypeStr);
            }
            List<CountRepeateDto> countRepeateDtoList = service.countRepeatedCases(countRepeateVo);
            if (!countRepeateDtoList.isEmpty()){
                String timeFormatter = "";
                if (dayTypeStr.equals(countRepeateVo.getGroupByTime())){
                    timeFormatter = "日";
                }
                else if (monthTypeStr.equals(countRepeateVo.getGroupByTime())) {
                    timeFormatter = "月";
                }
                else if (yearTypeStr.equals(countRepeateVo.getGroupByTime())) {
                    timeFormatter = "年";
                }
                for (CountRepeateDto countRepeateDto : countRepeateDtoList) {
                    // 按 “-”切割字符串,获取最后一个元素
                    String[] split = countRepeateDto.getGroupByType().split("-");
                    String lastElement = split[split.length - 1];
                    // 判断 lastElement 是否为小于 13 的数字,如果是,则将类型转换为int
                    if (lastElement.matches("^\\d+$")) {
                        lastElement = String.valueOf(Integer.parseInt(lastElement));
                    }
                    countRepeateDto.setGroupByType(lastElement + timeFormatter);
                }
 
            }
            return ReturnSucUtils.getRepInfo("请求成功",countRepeateDtoList);
        }catch (Exception e){
            log.error("统计重复来访案件数量异常",e);
            return ReturnFailUtils.getRepInfo(e.getMessage());
        }
    }
 
    private List<String> getDaysList(LocalDate start, LocalDate end) {
        List<String> days = new ArrayList<>();
        while (!start.isAfter(end)) {
            days.add(start.toString());
            start = start.plusDays(1);
        }
        return days;
    }
 
    private List<String> getMonthsList(LocalDate start, LocalDate end) {
        List<String> months = new ArrayList<>();
        while (!start.isAfter(end)) {
            String month = start.getMonth().name().substring(0, 3); // 例如 "JAN"
            // 获取中文月份
            switch (month) {
                case "JAN":
                    month = "一月";
                    break;
                case "FEB":
                    month = "二月";
                    break;
                case "MAR":
                    month = "三月";
                    break;
                case "APR":
                    month = "四月";
                    break;
                case "MAY":
                    month = "五月";
                    break;
                case "JUN":
                    month = "六月";
                    break;
                case "JUL":
                    month = "七月";
                    break;
                case "AUG":
                    month = "八月";
                    break;
                case "SEP":
                    month = "九月";
                    break;
                case "OCT":
                    month = "十月";
                    break;
                case "NOV":
                    month = "十一月";
                    break;
                case "DEC":
                    month = "十二月";
                    break;
            }
            if (!months.contains(month)) {
                months.add(month);
            }
            start = start.plusMonths(1);
        }
        // return months.stream().map(m -> m + "月").collect(Collectors.toList());
        return months;
    }
 
    private List<String> getYearsList(LocalDate start, LocalDate end) {
        List<String> years = new ArrayList<>();
        while (!start.isAfter(end)) {
            String year = Integer.toString(start.getYear());
            if (!years.contains(year)) {
                years.add(year);
            }
            start = start.plusYears(1);
        }
        return years.stream().map(y -> y + "年").collect(Collectors.toList());
    }
}