@GetMapping("/statistic/imAndOut")
@ApiOperation(value = " 统计出入库数量", httpMethod = HttpMethod.GET)
public List statisticImAndOut(StatisticsQO qo) {
return baseService.statisticImAndOut(qo);
}
service业务接口
ListserviceImpl业务实现类statisticImAndOut(StatisticsQO qo);
@Override public Listmapper层statisticImAndOut(StatisticsQO qo) { // 如果没传时间类型,默认按"日"统计 if (qo.getDateType() == null) { qo.setDateType(DateFormatEnum.DAY.getDateType()); } // 如果没设置当前时间,按照系统当前时间开始计算 if (qo.getCurrentTime() == null) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); qo.setCurrentTime(sdf.format(new Date())); } } Integer step = qo.getStep(); // 如如果展示时间没设置,默认查询7条 if (step == null) { step = 7; } // 获取"当前时间",即执行计算时间 final LocalDateTime now = cn.hutool.core.date.DateUtil.parseLocalDateTime(qo.getCurrentTime()); LocalDateTime start; final DateFormatEnum dateType = DateFormatEnum.getByDateType(qo.getDateType()); switch (dateType) { case MONTH: start = now.minusMonths(step - 1); break; case YEAR: start = now.minusYears(step - 1); break; default: start = now.minusDays(step - 1); } DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); String beginDate = start.format(formatter); String endDate = now.format(formatter); qo.setStartDate(beginDate).setEndDate(endDate) .setDateFormat(DateFormatEnum.getFormatByDateType(qo.getDateType())); return baseMapper.statisticImAndOut(qo); }
Listmapper.xml中的SQL语句statisticImAndOut(@Param("qo")StatisticsQO qo);
部分使用到的实体SELECT date.cdate date,IFNULL(IM.imNum,0) imNum,IFNULL(EX.exNum,0) exNum FROM ( SELECt DATE_FORMAT(@cdate := DATE_ADD( @cdate, INTERVAL + 1 ${qo.dateType} ),#{qo.dateFormat} ) cdate FROM (SELECt @cdate := DATE_ADD(#{qo.startDate}, INTERVAL - 1 ${qo.dateType} ) FROM PL_MATERIAL_INFO LIMIT #{qo.step} ) t0 ) date LEFT JOIN ( SELECt P1.MATERIAL_ID,COUNT(P1.ID) imNum,DATE_FORMAT(P1.CREATOR_TIME,#{qo.dateFormat}) date FROM PL_MATERIAL_IM_INFO P1 LEFT JOIN PL_MATERIAL_INFO M1 ON M1.MATERIAL_ID = P1.MATERIAL_ID GROUP BY date ) IM ON date.cdate = IM.date LEFT JOIN ( SELECt P2.MATERIAL_ID,COUNT(P2.ID) exNum,DATE_FORMAT(P2.CREATOR_TIME,#{qo.dateFormat}) date FROM PL_MATERIAL_EX_INFO P2 LEFT JOIN PL_MATERIAL_INFO M2 ON M2.MATERIAL_ID = P2.MATERIAL_ID AND M1.MATERIAL_TYPE = #{qo.materialType} GROUP BY date )EX ON date.cdate = EX.date ORDER BY date.cdate AND M2.MATERIAL_TYPE = #{qo.materialType}
@Data
@ApiModel(value = "入库出库实体VO")
@Accessors(chain = true)
public class ImAndExVO implements Serializable {
@ApiModelProperty(value = "入库数量")
private Integer imNum;
@ApiModelProperty(value = "出库数量")
private Integer exNum;
@ApiModelProperty(value = "时间")
private String date;
}
@Data
@ApiModel(value = "统计查询条件实体")
@Accessors(chain = true)
public class StatisticsQO implements Serializable {
@ApiModelProperty(value = "日期类型", allowableValues = "YEAR,MONTH,DAY")
private String dateType;
@ApiModelProperty(value = "时间格式")
private String dateFormat;
@ApiModelProperty(value = "数据展示条数")
private Integer step;
@ApiModelProperty(value = "物资类型编号")
private String materialType;
@ApiModelProperty(value = "开始日期,格式为 2022-10-01")
private String startDate;
@ApiModelProperty(value = "结束日期,格式为 2022-10-01")
private String endDate;
}
public enum DateFormatEnum {
DAY("%Y-%m-%d", "DAY"),
MONTH("%Y-%m", "MONTH"),
YEAR("%Y", "YEAR"),
;
private final String format;
private final String dateType;
DateFormatEnum(String format, String dateType) {
this.format = format;
this.dateType = dateType;
}
public String getFormat() {
return format;
}
public String getDateType() {
return dateType;
}
public static DateFormatEnum getByDateType(String dateType) {
String finalDateType = dateType.trim().toUpperCase();
Optional optional = Arrays.stream(DateFormatEnum.values())
.filter(item -> finalDateType.equals(item.getDateType()))
.findFirst();
if (!optional.isPresent()) {
throw BusinessException.create(ErrorMessage.PARAM_WRONG, "无效的参数:" + dateType);
}
return optional.get();
}
public static String getFormatByDateType(String dateType) {
String finalDateType = dateType.trim().toUpperCase();
Optional optional = Arrays.stream(DateFormatEnum.values())
.filter(item -> finalDateType.equals(item.getDateType()))
.findFirst();
if (!optional.isPresent()) {
throw BusinessException.create(ErrorMessage.PARAM_WRONG, "无效的参数:" + dateType);
}
return optional.get().getFormat();
}
}
部分SQL分析
查询出所有所需时间
SELECT
DATE_FORMAT(@cdate := DATE_ADD( @cdate, INTERVAL + 1 DAY ),'%Y-%m-%d' ) cdate
FROM
(SELECt @cdate := DATE_ADD('2022-05-17', INTERVAL - 1 DAY ) FROM PL_MATERIAL_INFO LIMIT 7 ) t0
1、这里的’2022-05-17’是统计开始时间。
例如,需要查2022-05-29之前12天的数据,这里在上面的serviceImpl中进行了逻辑运算,可以得到最开始的时间是2022-05-17,所以统计开始时间就是2022-05-17。
2、这里的“7”,表示查询的时间节点数是7个。
3、PL_MATERIAL_INFO这是我的项目中用到的数据表,替换为你需要统计的表即可。
注意:如果这张表里的数据量小于要查询的时间节点数,即这里的“7”,那么查询出来的时间节点数只有当前表中的数据量。



