一、定义内部类
/**
* @Description 月度布控分析DTO
* @Author wpz
* @Date 2024/06/28 11:28
*/
@Data
public class CaseCountAnalysisDTO {
/**
* 时间集合
*/
private List<String> timeList;
/**
* 数据集合
*/
private List<DataInfo> dataInfoList;
//数据集合
@Data
public static class DataInfo {
/**
* 类型:查获总数;布控总数
*/
private String name;
/**
* 数量
*/
private List<Long> valueList;
}
}
二、使用
/**
* @Description 布控案件统计管理
* @Author wpz
* @Date 2024/4/20 9:24
*/
@Slf4j
@Validated
@RestController
@RequestMapping("/case/count")
@Tag(name = "布控案件统计管理")
public class CaseCountController {
@Resource
private CaseCountService caseCountService;
@Operation(summary = "一张图-月度布控分析")
@GetMapping("/image/analysis")
public CommonResult<CaseCountAnalysisDTO> analysis() {
return caseCountService.analysis();
}
}
/**
* @Description 案件统计服务
* @Author wpz
* @Date 2024/4/20 10:41
*/
@Slf4j
@Service
public class CaseCountService {
@Resource
private ControlCaseMapper controlCaseMapper;
@Resource
private DictDataMapper dictDataMapper;
@Resource
private IControlCaseService iControlCaseService;
@Resource
private PermissionService permissionService;
@Resource
private DeptMapper deptMapper;
@Resource
private ControlCarMapper controlCarMapper;
@Resource
private IWarnManageService iWarnManageService;
@Resource
private CheckpointService checkpointService;
/**
* 一张图-月度布控分析 最近一年数据
*
* @return 一张图-月度布控分析
*/
public CommonResult<CaseCountAnalysisDTO> analysis() {
CaseCountAnalysisDTO caseCountAnalysisDTO = new CaseCountAnalysisDTO();
LocalDateTime endTime = LocalDateTime.now();
LocalDateTime startTime = endTime.minusYears(1);
// 创建一个存储月份字符串的列表
List<String> timeList = new ArrayList<>();
LocalDateTime currentMonth = startTime;
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM");
while (currentMonth.isBefore(endTime) || currentMonth.equals(endTime)) {
timeList.add(currentMonth.format(formatter));
currentMonth = currentMonth.plusMonths(1);
}
caseCountAnalysisDTO.setTimeList(timeList);
Map<String, Long> totalMap = controlCarMapper.total(startTime, endTime)
.stream().collect(Collectors.toMap(TotalAndSeize::getMonth, TotalAndSeize::getCount, (n1, n2) -> n1));
Map<String, Long> map = controlCarMapper.seize(startTime, endTime)
.stream().collect(Collectors.toMap(TotalAndSeize::getMonth, TotalAndSeize::getCount, (n1, n2) -> n1));
List<CaseCountAnalysisDTO.DataInfo> dataInfoList = CollUtil.newArrayList();
CaseCountAnalysisDTO.DataInfo dataInfo = new CaseCountAnalysisDTO.DataInfo();
dataInfo.setName("布控总数");
CaseCountAnalysisDTO.DataInfo dataInfo2 = new CaseCountAnalysisDTO.DataInfo();
dataInfo2.setName("查获总数");
List<Long> valueList = CollUtil.newArrayList();
List<Long> valueList2 = CollUtil.newArrayList();
timeList.forEach(t -> {
valueList.add(totalMap.getOrDefault(t, 0L));
valueList2.add(map.getOrDefault(t, 0L));
});
dataInfo.setValueList(valueList);
dataInfo2.setValueList(valueList2);
dataInfoList.add(dataInfo);
dataInfoList.add(dataInfo2);
caseCountAnalysisDTO.setDataInfoList(dataInfoList);
return CommonResult.success(caseCountAnalysisDTO);
}
}
/**
* <p>
* 布控车辆 Mapper 接口
* </p>
*
* @author liubh
* @since 2024-03-0
*/
public interface ControlCarMapper extends MPJBaseMapper<ControlCar> {
@Select("SELECT TO_CHAR(gmt_create, 'YYYY-MM') AS month," +
"COUNT(*) AS count " +
"FROM control_car where status in (1,2,3,4) and gmt_create between #{startTime} and #{endTime} " +
"GROUP BY TO_CHAR(gmt_create, 'YYYY-MM')")
List<TotalAndSeize> total(@Param("startTime")LocalDateTime startTime, @Param("endTime")LocalDateTime endTime);
@Select("SELECT TO_CHAR(control_time, 'YYYY-MM') AS month," +
"COUNT(*) AS count " +
"FROM control_car where status = 3 and control_time between #{startTime} and #{endTime} " +
"GROUP BY TO_CHAR(control_time, 'YYYY-MM')")
List<TotalAndSeize> seize(@Param("startTime")LocalDateTime startTime, @Param("endTime")LocalDateTime endTime);
}
三、效果