苍穹外卖--用户统计功能实现
This commit is contained in:
@@ -4,6 +4,7 @@ package com.sky.controller.admin;
|
||||
import com.sky.result.Result;
|
||||
import com.sky.service.ReportService;
|
||||
import com.sky.vo.TurnoverReportVO;
|
||||
import com.sky.vo.UserReportVO;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@@ -37,4 +38,20 @@ public class ReportController {
|
||||
|
||||
return Result.success(vo);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@GetMapping("/userStatistics")
|
||||
@ApiOperation("用户统计")
|
||||
public Result<UserReportVO> userStatistics(
|
||||
@DateTimeFormat(pattern = "yyyy-MM-dd")
|
||||
LocalDate begin,
|
||||
@DateTimeFormat(pattern = "yyyy-MM-dd")
|
||||
LocalDate end){
|
||||
log.info("用户统计:{},{}",begin,end);
|
||||
//调用业务层,用户统计功能
|
||||
UserReportVO vo = reportService.getUserStatistics(begin,end);
|
||||
|
||||
return Result.success(vo);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,6 +6,8 @@ import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Options;
|
||||
import org.apache.ibatis.annotations.Select;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/*
|
||||
操作User表
|
||||
*/
|
||||
@@ -21,4 +23,6 @@ public interface UserMapper {
|
||||
|
||||
@Select("select * from user where id = #{id} ")
|
||||
User getById(Long id);
|
||||
|
||||
Integer countByMap(Map map);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.sky.service;
|
||||
|
||||
import com.sky.vo.TurnoverReportVO;
|
||||
import com.sky.vo.UserReportVO;
|
||||
|
||||
import java.time.LocalDate;
|
||||
|
||||
@@ -12,4 +13,12 @@ public interface ReportService {
|
||||
* @return
|
||||
*/
|
||||
TurnoverReportVO getTurnoverStatistics(LocalDate begin, LocalDate end);
|
||||
|
||||
/**
|
||||
* 用户统计
|
||||
* @param begin
|
||||
* @param end
|
||||
* @return
|
||||
*/
|
||||
UserReportVO getUserStatistics(LocalDate begin, LocalDate end);
|
||||
}
|
||||
|
||||
@@ -2,8 +2,10 @@ package com.sky.service.impl;
|
||||
|
||||
import com.sky.entity.Orders;
|
||||
import com.sky.mapper.OrderMapper;
|
||||
import com.sky.mapper.UserMapper;
|
||||
import com.sky.service.ReportService;
|
||||
import com.sky.vo.TurnoverReportVO;
|
||||
import com.sky.vo.UserReportVO;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
@@ -22,6 +24,9 @@ public class ReportServiceImpl implements ReportService {
|
||||
@Autowired
|
||||
private OrderMapper orderMapper;
|
||||
|
||||
@Autowired
|
||||
private UserMapper userMapper;
|
||||
|
||||
|
||||
/**
|
||||
* 统计指定时间内每天的营业额数据
|
||||
@@ -69,4 +74,49 @@ public class ReportServiceImpl implements ReportService {
|
||||
|
||||
return turnoverReportVO;
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户统计
|
||||
* @param begin
|
||||
* @param end
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public UserReportVO getUserStatistics(LocalDate begin, LocalDate end) {
|
||||
//根据对应的日期,将当天的所有已完成的订单查询出来,并统计总金额(sum)
|
||||
List<LocalDate> dateList = new ArrayList<>();
|
||||
dateList.add(begin);
|
||||
//不停地创建日期对象,直到end
|
||||
while (!begin.equals(end)){
|
||||
begin = begin.plusDays(1);
|
||||
dateList.add(begin);
|
||||
}
|
||||
|
||||
//定义2个集合,分别保存总用户数量和新用户数量
|
||||
//select count(1) from user where create_time < ?;
|
||||
//select count(1) from user where create_time < ? and create_time > ?;
|
||||
List<Integer> totalUserList = new ArrayList<>();
|
||||
List<Integer> newUserList = new ArrayList<>();
|
||||
|
||||
for (LocalDate localDate : dateList) {
|
||||
//根据指定的日期查询,当日的总用户数和新用户数
|
||||
LocalDateTime beginTime = LocalDateTime.of(localDate, LocalTime.MIN);
|
||||
LocalDateTime endTime = LocalDateTime.of(localDate, LocalTime.MAX);
|
||||
Map map = new HashMap();
|
||||
map.put("end", endTime);
|
||||
Integer totalUserCount = userMapper.countByMap(map);
|
||||
totalUserList.add(totalUserCount);
|
||||
map.put("begin", beginTime);
|
||||
Integer newUserCount = userMapper.countByMap(map);
|
||||
newUserList.add(newUserCount);
|
||||
}
|
||||
//封装返回结果
|
||||
UserReportVO userReportVO = UserReportVO.builder()
|
||||
.dateList(StringUtils.join(dateList, ","))
|
||||
.totalUserList(StringUtils.join(totalUserList, ","))
|
||||
.newUserList(StringUtils.join(newUserList, ","))
|
||||
.build();
|
||||
|
||||
return userReportVO;
|
||||
}
|
||||
}
|
||||
|
||||
15
sky-server/src/main/resources/mapper/UserMapper.xml
Normal file
15
sky-server/src/main/resources/mapper/UserMapper.xml
Normal file
@@ -0,0 +1,15 @@
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
|
||||
<mapper namespace="com.sky.mapper.UserMapper">
|
||||
<select id="countByMap" resultType="java.lang.Integer" parameterType="java.util.Map">
|
||||
select count(1) from user
|
||||
<where>
|
||||
<if test="begin != null">
|
||||
and create_time > #{begin}
|
||||
</if>
|
||||
<if test="end != null">
|
||||
and create_time < #{end}
|
||||
</if>
|
||||
</where>
|
||||
</select>
|
||||
</mapper>
|
||||
Reference in New Issue
Block a user