Commit b2c74b80 by 杨林龙

优化定时任务 一次性统计并插入数据

1 parent 811923f1
......@@ -32,7 +32,7 @@ public class StatisticsDriveController {
@SysLog(module = MODULE, value = "查询试驾统计数据", action = OperateType.QUERY)
@ApiOperation(value = "查询试驾统计数据")
public R statisticsDriveDataEveryDay() {
log.info("开始查询试驾统计数据");
log.info("开始查询试驾统计数据===");
List<StatisticsDriveDto> statisticsDriveList = statisticsDriveService.statisticsDriveDataEveryDay();
if (CollectionUtil.isEmpty(statisticsDriveList)) {
......@@ -59,4 +59,13 @@ public class StatisticsDriveController {
log.info("数据集合为空 没有数据可以保存===");
return R.ok();
}
@GetMapping(value = "/statisticsandinsertdrivedatalist")
@SysLog(module = MODULE, value = "查询并插入试驾统计数据", action = OperateType.QUERY)
@ApiOperation(value = "查询并插入试驾统计数据")
public R statisticsAndInsertDriveDataEveryDay() {
log.info("开始查询试驾统计数据并插入到统计表中===");
Integer num = statisticsDriveService.statisticsAndInsertDriveDataEveryDay();
return R.ok().data(num);
}
}
......@@ -20,4 +20,11 @@ public interface StatisticsDriveMapper extends CommonBaseMapper<StatisticsDrive>
* @return 统计数据的集合
*/
List<StatisticsDriveDto> statisticsDriveDataEveryDay(@Param("yesterDayFormat") String yesterDayFormat);
/**
* 统计并插入每一天的数据总和
*
* @param yesterDayFormat 昨天的日期
*/
Integer statisticsAndInsertDriveDataEveryDay(@Param("yesterDayFormat") String yesterDayFormat);
}
......@@ -8,6 +8,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
......@@ -35,6 +36,7 @@ public class StatisticsDriveService {
}
public void insertStatisticsDriveList(List<StatisticsDriveDto> statisticsDriveDtoList) {
List<StatisticsDrive> statisticsDriveList = new ArrayList<>();
for (StatisticsDriveDto statisticsDriveDto : statisticsDriveDtoList) {
StatisticsDrive statisticsDrive = new StatisticsDrive();
statisticsDrive.setSn(statisticsDriveDto.getSn());
......@@ -42,7 +44,24 @@ public class StatisticsDriveService {
statisticsDrive.setDriveTotal(statisticsDriveDto.getDriveTotal());
statisticsDrive.setDriveTime(statisticsDriveDto.getDriveTime());
statisticsDrive.setCreatedTime(new Date());
statisticsDriveMapper.insertSelective(statisticsDrive);
statisticsDriveList.add(statisticsDrive);
}
statisticsDriveMapper.insertList(statisticsDriveList);
}
/**
* 统计每天的数据并插入
*
* @return 成功插入的行数
*/
public Integer statisticsAndInsertDriveDataEveryDay() {
// 获取昨天的日期
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, -1);
String yesterDayFormat = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
log.info("数据统计的天数===" + yesterDayFormat);
Integer num = statisticsDriveMapper.statisticsAndInsertDriveDataEveryDay(yesterDayFormat);
log.info("插入到统计表的数据行数===" + num);
return num;
}
}
......@@ -28,4 +28,16 @@
where left(journey_time,10) = #{yesterDayFormat}
GROUP BY sn
</select>
<insert id="statisticsAndInsertDriveDataEveryDay">
INSERT t_statistics_fence_drive (sn ,drive_num ,drive_total ,drive_time ,created_time)
select sn,
sum(case when percent >= 50 then 1 else 0 end) as driveNum,
count(1) as driveTotal,
left(journey_time,10) as driveTime,
NOW()
from t_electric_fence_drive
where left(journey_time,10) = #{yesterDayFormat}
GROUP BY sn;
</insert>
</mapper>
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!