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{ @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 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 listTerms(Map terms){ return mapper.listTerms(terms); } /** * 按条件统计 * @param terms 条件 * @return long */ public long countTerms(Map terms){ return mapper.countTerms(terms); } /** * 统计判决案例和调解案例数量 * @return List */ public List typeCount(Map terms){ try{ List 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 */ public List categoryCount(Map terms){ try{ // 判断 dyhCauseId 是否存在,如果不存在则不进行拼接 if (terms.get("dyhCauseId") != null) { // 获取 dyhCause String dyhCauseId = (String) terms.get("dyhCauseId"); QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.eq("dyh_cause_id", dyhCauseId); List causeMappings = causeMappingService.list(queryWrapper); if (causeMappings.isEmpty() || causeMappings == null) { terms.put("cpwsCauseIds", null); }else { List 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 yearCount = this.yearCount(terms); for (CategoryCountDTO category : yearCount) { DictionaryDTO dictionary = new DictionaryDTO(); BeanUtils.copyProperties(category, dictionary); yearCountDictionaryDTO.getChildren().add(dictionary); } List 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 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 */ public List yearCount(Map terms){ try{ // List res = new ArrayList<>(); // List 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 */ public List areaCount(Map terms){ try{ return mapper.areaCount(terms); }catch (Exception e){ log.error("[CpwsCaseInfoService.areaCount]调用失败,异常信息:"+e, e); throw new ServiceException("CpwsCaseInfoService.areaCount", e); } } /** * 区域统计-全数据树形结构 * @return List */ public List areaCountAll(Map terms){ try{ // 执行SQL查询,获取所有数据 List allChildren = mapper.areaCountAll(terms); // 使用Map按value分组 Map 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 pageQuery(PageRequest page, Map terms){ try{ // 判断 dyhCauseId 是否存在,如果不存在则不进行拼接 if (terms.get("dyhCauseId") != null) { // 获取 dyhCause String dyhCauseId = (String) terms.get("dyhCauseId"); QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.eq("dyh_cause_id", dyhCauseId); List causeMappings = causeMappingService.list(queryWrapper); if (causeMappings.isEmpty() || causeMappings == null) { terms.put("cpwsCauseIds", null); }else { List 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 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); } } }