springboot+mybatis3.5.2 dynamically queries the statistical information of a certain field within a certain period of time (line chart)

need:
Dynamically query the statistical line chart information of a certain statistical field within a period of time

  1. controller layer
 @ApiOperation(value = "getStatisticDetail", notes = "Statistical Line Chart")
   @GetMapping("/detail")
   @ResponseStatus(HttpStatus.OK)
   @AccessLogAnnotation(ignoreRequestArgs = "loginUser")
   @ApiImplicitParams({
           @ApiImplicitParam(name = "startTime", value = "StartTime", dataTypeClass = String.class),
           @ApiImplicitParam(name = "endTime", value = "End Time", dataTypeClass = String.class),
           @ApiImplicitParam(name = "taskType", value = "Query Type", required = true, dataTypeClass = Enum.class)
   })
   public Result getStatisticDetail(@ApiIgnore @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
                                    @RequestParam(value = "taskType", required = true) String taskType,
                                    @RequestParam(value = "startTime", required = true) String startTime,
                                    @RequestParam(value = "endTime", required = true) String endTime) {
       return statisticAnalysisService.getStatisticDetail(startTime, endTime, Long.valueOf(projectCode), taskType);
   }
  1. service layer biz
 public Result<StatisticDetailResponse> getStatisticDetail( String startTime, String endTime String taskType) {
      Result<StatisticDetailResponse> result = new Result<>();
      StatisticDetailResponse statisticDetailResponse = new StatisticDetailResponse();
      //Calculate the span of time and days
      String[] days = new String[getDayDiffer(startTime, endTime)];
      startTime = "'" + startTime + "'";
      endTime = "'" + endTime + "'";
      StatisticDetailResponse responseList = getStatisticTasks(statisticDetailResponse, startTime, endTime,Objects.requireNonNull(StatisticDetailType.getStatisticDetailByType(taskType)), days);
       result.setData(responseList);
      putMsg(result, Status.SUCCESS);
      return result;
  }

  private StatisticDetailResponse getStatisticTasks(StatisticDetailResponse statisticDetailResponse, String startTime, String endTime, StatisticDetailType statisticDetailType, String[] days) {
      String filed = statisticDetailType.getFeildName();
      List<StatisticAnalysis> details = statisticAnalysisMapper.getStatisticDetail(startTime, endTime, filed, days);
      List<String> xAxisData = new ArrayList<>(12);
      List<StatisticTask> statisticTaskList = new ArrayList<>(12);
      if (ObjectUtils.isNotEmpty(statisticDetailResponse.getList())) {
          statisticTaskList = statisticDetailResponse.getList();
      }
      List data = new ArrayList<>(12);
      for (StatisticAnalysis detail : details) {
          String key = detail.getName();
          Object value = detail.getNameValue();
          xAxisData.add(key.replaceAll(String.valueOf(Constants.SUBTRACT_CHAR), ""));
          data.add(value);
      }
      StatisticTask statisticTask = new StatisticTask();
      statisticTask.setData(data);
      statisticTask.setName(statisticDetailType.getType());
      statisticTaskList.add(statisticTask);
      statisticDetailResponse.setXAxisData(xAxisData);
      statisticDetailResponse.setList(statisticTaskList);
      return statisticDetailResponse;
  }

2.1. Query start time – number of days between end time

 /**
    * Calculate start time, end time, interval days
    *
    * @param startTime start time
    * @param endTime end time
    * @return the number of days between intervals
    */
   public static int getDayDiffer(String startTime, String endTime) {
       SimpleDateFormat formatter = new SimpleDateFormat(Constants.DATE_PATTERN1);
       long ts2 = 0;
       try {
           Date date1 = null;
           Date date = formatter.parse(startTime);
           long ts = date.getTime();
           date1 = formatter.parse(endTime);
           long ts1 = date1.getTime();
           ts2 = ts1 - ts;
       } catch (ParseException e) {
           e.printStackTrace();
       }
       int totalTime = 0;
       totalTime = (int) (ts2 / (24 * 3600 * 1000) + 1);
       return totalTime;
   }

2.2. taskType enumeration type

public enum StatisticDetailType {

   /**
    * Remaining storage space on the day (unit P)
    */
   freeSpace("freeSpace", "free_space"),

   /**
    * Space usage ratio on the day (unit %)
    */
   useRatio("useRatio", "use_ratio");
   private String type;
   private String feildName;

   StatisticDetailType() {
   }

   StatisticDetailType(String type, String feildName) {
       this.type = type;
       this.feildName = feildName;
   }

   public String getType() {
       return type;
   }

   public void setType(String type) {
       this.type = type;
   }

   public String getFeildName() {
       return feildName;
   }

   public void setFeildName(String feildName) {
       this.feildName = feildName;
   }

   /**
    * Return the enumeration object based on the enumeration value
    *
    * @param key
    * @return
    */
   public static StatisticDetailType getStatisticDetailByType(String key) {
       for (StatisticDetailType statisticDetailType : values()) {
           if (Objects.equals(statisticDetailType.type, key)) {
               return statisticDetailType;
           }
       }
       return null;
   }
}

  1. Mapper
 /**
    * @param startTime end time
    * @param endTime start time
    * @param filed query field
    * @param days Query the number of days
    * @return query results
    */
   List<StatisticAnalysis> getStatisticDetail(@Param("startTime") String startTime, @Param("endTime") String endTime, @Param("filed") String filed, @Param("days") String[] days);
  1. Mapper.xml
 <select id="getStatisticDetail" resultType="entity.StatisticAnalysis">
       select d.name, IFNULL(T.${filed},0) nameValue
       from (
           select date_add(${startTime},interval @i:=@i + 1 day) as name
            from (
                   select 1
                      <foreach item="index" collection="days">
                        union all select 1
                      </foreach>
                 ) as tmp,(select @i:= -1) t
           ) d
       left join (
           select ${filed},DATE_FORMAT(statistic_date,'%Y-%m-%d') statistic_date
           from s_statistic_analysis
           where statistic_date & amp;gt;= ${startTime} and statistic_date & amp;lt;=${endTime}
           )T on T.statistic_date = d.name
   </select>
  1. entity
public class StatisticDetailResponse {

   private List<String> xAxisData;

   private List<StatisticTask> list;

}

public class StatisticTask {

   private String name;

   private List data;


}
  1. result
{
   "code": 0,
   "msg": "success",
   "data": {
       "list": [
           {
               "name": "freeSpace",
               "data": [
                   "0.00",
                   "0.00",
                   "0.00",
                   "0.00",
                   "0.00"
               ]
           }
       ],
       "xaxisData": [
           "20231023",
           "20231024",
           "20231025",
           "20231026",
           "20231027"
       ]
   },
   "success": true,
   "failed": false
}