广州市综治平台后端
xusd
2025-06-07 36306491396230522fa20585c2621a7fc899849a
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
package cn.huge.module.knowledge.service;
 
import cn.huge.base.common.exception.ServiceException;
import cn.huge.base.common.utils.DateUtils;
import cn.huge.base.common.utils.IdUtils;
import cn.huge.module.client.api.impl.UtilsClientImpl;
import cn.huge.module.knowledge.dao.mapper.CpwsCaseInfoMapper;
import cn.huge.module.knowledge.domain.dto.CaseYearCountDTO;
import cn.huge.module.knowledge.domain.dto.CategoryCountDTO;
import cn.huge.module.knowledge.domain.dto.CpwsCaseinfoDTO;
import cn.huge.module.knowledge.domain.dto.DictionaryDTO;
import cn.huge.module.knowledge.domain.po.CauseMapping;
import cn.huge.module.knowledge.domain.po.CpwsCaseInfo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.PostConstruct;
import java.util.*;
 
/**
 * @title: 裁判文书网案件信息表业务逻辑处理
 * @Description 裁判文书网案件信息表业务逻辑处理
 * @company hugeinfo
 * @author huangh
 * @Time 2024-12-10 17:13:00
 * @version 1.0.0
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class CpwsCaseInfoService extends ServiceImpl<CpwsCaseInfoMapper, CpwsCaseInfo>{
 
    @Autowired
    private CpwsCaseInfoMapper mapper;
 
    @Autowired
    private UtilsClientImpl utilsClient;
 
    @Autowired
    private CauseMappingService causeMappingService;
 
    @Autowired
    private DyhCaseInfoService dyhCaseInfoService;
 
 
 
    /**
     * 更新对象
     * @param entity 对象
     */
    public void updateCpwsCaseInfo(CpwsCaseInfo entity){
        try{
            mapper.updateCpwsCaseInfo(entity);
        }catch (Exception e){
            log.error("[CpwsCaseInfoService.updateCpwsCaseInfo]调用失败,异常信息:"+e, e);
            throw new ServiceException("CpwsCaseInfoService.updateCpwsCaseInfo", e);
        }
    }
 
    /**
     * 条件更新对象
     * @param entity 对象
     * @param terms 条件
     */
    public void updateCpwsCaseInfoTerms(CpwsCaseInfo entity, Map<String, Object> terms){
        try{
            mapper.updateCpwsCaseInfoTerms(entity, terms);
        }catch (Exception e){
            log.error("[CpwsCaseInfoService.updateCpwsCaseInfoTerms]调用失败,异常信息:"+e, e);
            throw new ServiceException("CpwsCaseInfoService.updateCpwsCaseInfoTerms", e);
        }
    }
 
    /**
     * 根据编号物理删除
     * @param id 查询条件集合
     */
    public void deleteCpwsCaseInfo(String id){
        try{
            mapper.deleteCpwsCaseInfo(id);
        }catch (Exception e){
            log.error("[CpwsCaseInfoService.deleteCpwsCaseInfo]调用失败,异常信息:"+e, e);
            throw new ServiceException("CpwsCaseInfoService.deleteCpwsCaseInfo", e);
        }
    }
 
    /**
     * 按条件查询
     * @param terms 条件
     * @return List
     */
    public List<CpwsCaseInfo> listTerms(Map<String, Object> terms){
        return mapper.listTerms(terms);
    }
 
    /**
     * 按条件统计
     * @param terms 条件
     * @return long
     */
    public long countTerms(Map<String, Object> terms){
        return mapper.countTerms(terms);
    }
 
    /**
     * 统计判决案例和调解案例数量
     * @return List<CategoryCountDTO>
     */
    public List<CategoryCountDTO> typeCount(Map<String, Object> terms){
        try{
            List<CategoryCountDTO> typeCount = new ArrayList<>();
            CategoryCountDTO judgeCount = new CategoryCountDTO();
            judgeCount.setCategory("案件类型");
            // 将 long 类型的 countTerms 转换为 int 类型的 count
            judgeCount.setName("判决文书");
            judgeCount.setCount((int) countTerms(terms));
 
            CategoryCountDTO dyhCount = new CategoryCountDTO();
            dyhCount.setCategory("案件类型");
            dyhCount.setName("调解案例");
            dyhCount.setCount((int) dyhCaseInfoService.countTerms(terms));
 
            typeCount.add(judgeCount);
            typeCount.add(dyhCount);
            return typeCount;
        }catch (Exception e){
            log.error("[CpwsCaseInfoService.yearCount]调用失败,异常信息:"+e, e);
            throw new ServiceException("CpwsCaseInfoService.yearCount", e);
        }
    }
 
    /**
     * 各类别统计
     * @return List<CategoryCountDTO>
     */
    public List<DictionaryDTO> categoryCount(Map<String, Object> terms){
        try{
            // 判断 dyhCauseId 是否存在,如果不存在则不进行拼接
            if (terms.get("dyhCauseId") != null) {
                // 获取 dyhCause
                String dyhCauseId = (String) terms.get("dyhCauseId");
                QueryWrapper<CauseMapping> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("dyh_cause_id", dyhCauseId);
                List<CauseMapping> causeMappings = causeMappingService.list(queryWrapper);
                if (causeMappings.isEmpty() || causeMappings == null) {
                    terms.put("cpwsCauseIds", null);
                }else {
                    List<String> cpwsCauseIds = new ArrayList<>();
                    for (CauseMapping causeMapping : causeMappings) {
                        cpwsCauseIds.add(causeMapping.getCpwsCauseId());
                    }
                    terms.put("cpwsCauseIds", cpwsCauseIds);
 
                }
            }
            DictionaryDTO yearCountDictionaryDTO = new DictionaryDTO();
            yearCountDictionaryDTO.setName("发生时间");
            yearCountDictionaryDTO.setChildren(new ArrayList<>());
            List<CategoryCountDTO> yearCount = this.yearCount(terms);
            for (CategoryCountDTO category : yearCount) {
                DictionaryDTO dictionary = new DictionaryDTO();
                BeanUtils.copyProperties(category, dictionary);
                yearCountDictionaryDTO.getChildren().add(dictionary);
            }
 
            List<DictionaryDTO> areaCount = this.areaCountAll(terms);
            DictionaryDTO areaCountDictionaryDTO = new DictionaryDTO();
            areaCountDictionaryDTO.setName("纠纷发生地");
            // areaCountDictionaryDTO.setChildren(new ArrayList<>());
            areaCountDictionaryDTO.setChildren(areaCount);
            // for (CategoryCountDTO category : areaCount) {
            //     DictionaryDTO dictionary = new DictionaryDTO();
            //     BeanUtils.copyProperties(category, dictionary);
            //     areaCountDictionaryDTO.getChildren().add(dictionary);
            // }
            List<DictionaryDTO> categoryCount = new ArrayList<>();
            categoryCount.add(yearCountDictionaryDTO);
            categoryCount.add(areaCountDictionaryDTO);
            return categoryCount;
        }catch (Exception e){
            log.error("[CpwsCaseInfoService.categoryCount]调用失败,异常信息:"+e, e);
            throw new ServiceException("CpwsCaseInfoService.categoryCount", e);
        }
    }
 
    /**
     * 年份统计
     * @return List<CategoryCountDTO>
     */
    public List<CategoryCountDTO> yearCount(Map<String, Object> terms){
        try{
            // List<CategoryCountDTO> res = new ArrayList<>();
            // List<CategoryCountDTO> yearCounts = mapper.yearCount(terms);
            // for (CategoryCountDTO category : yearCounts) {
            //     if (category.getCount()>=500){
            //         // 判断 category.getName() 是否以 20 开头
            //         if (category.getName().startsWith("20")) {
            //             res.add(category);
            //         }
            //     }
            //
            // }
            // return res;
            return mapper.yearCount(terms);
        }catch (Exception e){
            log.error("[CpwsCaseInfoService.yearCount]调用失败,异常信息:"+e, e);
            throw new ServiceException("CpwsCaseInfoService.yearCount", e);
        }
    }
 
    /**
     * 区域统计
     * @return List<CategoryCountDTO>
     */
    public List<CategoryCountDTO> areaCount(Map<String, Object> terms){
        try{
            return mapper.areaCount(terms);
        }catch (Exception e){
            log.error("[CpwsCaseInfoService.areaCount]调用失败,异常信息:"+e, e);
            throw new ServiceException("CpwsCaseInfoService.areaCount", e);
        }
    }
 
    /**
     * 区域统计-全数据树形结构
     * @return List<CategoryCountDTO>
     */
    public List<DictionaryDTO> areaCountAll(Map<String, Object> terms){
        try{
            // 执行SQL查询,获取所有数据
            List<DictionaryDTO> allChildren = mapper.areaCountAll(terms);
 
            // 使用Map按value分组
            Map<String, DictionaryDTO> parentMap = new HashMap<>();
 
            for (DictionaryDTO child : allChildren) {
                String value = child.getValue();
                if (!parentMap.containsKey(value)) {
                    // 创建父节点
                    DictionaryDTO parent = new DictionaryDTO();
                    parent.setName(value); // 父节点的name是value
                    // // 设置父节点的code
                    // parent.setCode("parent_" + value);
                    // parent.setValue(value); // 父节点的value也可以设置为value
                    parent.setCount(0);
                    parent.setChildren(new ArrayList<>());
                    parentMap.put(value, parent);
                }
 
                // 获取父节点
                DictionaryDTO parent = parentMap.get(value);
                // 将子节点添加到父节点的children列表
                parent.getChildren().add(child);
                // 累加父节点的count
                parent.setCount(parent.getCount() + child.getCount());
                // 移除子节点中的value
                child.setValue(null);
            }
 
            // 收集所有父节点
            return new ArrayList<>(parentMap.values());
 
        }catch (Exception e){
            log.error("[CpwsCaseInfoService.areaCountAll]调用失败,异常信息:"+e, e);
            throw new ServiceException("CpwsCaseInfoService.areaCountAll", e);
        }
    }
 
    /**
     * 按条件分页查询
     * @param page 分页对象
     * @param terms 条件
     * @return Page
     */
    public Page<CpwsCaseinfoDTO> pageQuery(PageRequest page, Map<String, Object> terms){
 
        try{
            // 判断 dyhCauseId 是否存在,如果不存在则不进行拼接
            if (terms.get("dyhCauseId") != null) {
                // 获取 dyhCause
                String dyhCauseId = (String) terms.get("dyhCauseId");
                QueryWrapper<CauseMapping> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("dyh_cause_id", dyhCauseId);
                List<CauseMapping> causeMappings = causeMappingService.list(queryWrapper);
                if (causeMappings.isEmpty() || causeMappings == null) {
                    terms.put("cpwsCauseIds", null);
                }else {
                    List<String> cpwsCauseIds = new ArrayList<>();
                    for (CauseMapping causeMapping : causeMappings) {
                        cpwsCauseIds.add(causeMapping.getCpwsCauseId());
                    }
                    terms.put("cpwsCauseIds", cpwsCauseIds);
 
                }
            }
 
            // log.info("cpwsCauseIds:{}", terms.get("cpwsCauseIds"));
            long total = mapper.countTerms(terms);
            List<CpwsCaseinfoDTO> content = mapper.pageTerms(page, terms);
            return new PageImpl<>(content, page, total);
        }catch (Exception e){
            log.error("[CpwsCaseInfoService.pageQuery]调用失败,异常信息:"+e, e);
            throw new ServiceException("CpwsCaseInfoService.pageQuery", e);
        }
    }
 
    /**
     * 新增或更新对象
     * @param cpwsCaseInfo 实体对象
     */
    public void saveCpwsCaseInfo(CpwsCaseInfo cpwsCaseInfo){
        try{
            Date nowDate = DateUtils.getNowDate();
            // 判断是否新增
            if (IdUtils.checkNewId(cpwsCaseInfo.getCpwsCaseInfoId())){
                cpwsCaseInfo.setCpwsCaseInfoId(utilsClient.getNewTimeId());
                cpwsCaseInfo.setCreateTime(nowDate);
            }
            cpwsCaseInfo.setUpdateTime(nowDate);
            this.saveOrUpdate(cpwsCaseInfo);
        }catch (Exception e){
            log.error("[CpwsCaseInfoService.saveCpwsCaseInfo]调用失败,异常信息:"+e, e);
            throw new ServiceException("CpwsCaseInfoService.saveCpwsCaseInfo", e);
        }
    }
 
    /**
     * 根据ID查询案件标题
     * @param id 案件ID
     * @return CpwsCaseInfo 仅包含id和caseName
     */
    public CpwsCaseInfo getCaseTitleById(String id) {
        try {
            return mapper.getCaseTitleById(id);
        } catch (Exception e) {
            log.error("[CpwsCaseInfoService.getCaseTitleById]调用失败,异常信息:"+e, e);
            throw new ServiceException("CpwsCaseInfoService.getCaseTitleById", e);
        }
    }
 
}