用户数据统计功能代码开发
This commit is contained in:
@@ -4,6 +4,7 @@ package com.sky.controller.admin;
|
|||||||
import com.sky.result.Result;
|
import com.sky.result.Result;
|
||||||
import com.sky.service.ReportService;
|
import com.sky.service.ReportService;
|
||||||
import com.sky.vo.TurnoverReportVO;
|
import com.sky.vo.TurnoverReportVO;
|
||||||
|
import com.sky.vo.UserReportVO;
|
||||||
import io.swagger.annotations.Api;
|
import io.swagger.annotations.Api;
|
||||||
import io.swagger.annotations.ApiOperation;
|
import io.swagger.annotations.ApiOperation;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
@@ -41,4 +42,21 @@ public class ReportController {
|
|||||||
log.info("营业额数据统计:{},{}", begin, end);
|
log.info("营业额数据统计:{},{}", begin, end);
|
||||||
return Result.success(reportService.getTurnoverStatistics(begin, end));
|
return Result.success(reportService.getTurnoverStatistics(begin, end));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用户统计
|
||||||
|
* @param begin
|
||||||
|
* @param end
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@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);
|
||||||
|
return Result.success(reportService.getUserStatistics(begin, end));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,8 @@ import com.sky.entity.User;
|
|||||||
import org.apache.ibatis.annotations.Mapper;
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
import org.apache.ibatis.annotations.Select;
|
import org.apache.ibatis.annotations.Select;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
@Mapper
|
@Mapper
|
||||||
public interface UserMapper {
|
public interface UserMapper {
|
||||||
/**
|
/**
|
||||||
@@ -27,4 +29,12 @@ public interface UserMapper {
|
|||||||
|
|
||||||
@Select("select * from user where id = #{id}")
|
@Select("select * from user where id = #{id}")
|
||||||
User getById(Long userId);
|
User getById(Long userId);
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据动态条件统计用户数量
|
||||||
|
* @param map
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
Integer countByMap(Map map);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package com.sky.service;
|
package com.sky.service;
|
||||||
|
|
||||||
import com.sky.vo.TurnoverReportVO;
|
import com.sky.vo.TurnoverReportVO;
|
||||||
|
import com.sky.vo.UserReportVO;
|
||||||
|
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
|
|
||||||
@@ -14,4 +15,13 @@ public interface ReportService {
|
|||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
TurnoverReportVO getTurnoverStatistics(LocalDate begin, LocalDate end);
|
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.entity.Orders;
|
||||||
import com.sky.mapper.OrderMapper;
|
import com.sky.mapper.OrderMapper;
|
||||||
|
import com.sky.mapper.UserMapper;
|
||||||
import com.sky.service.ReportService;
|
import com.sky.service.ReportService;
|
||||||
import com.sky.vo.TurnoverReportVO;
|
import com.sky.vo.TurnoverReportVO;
|
||||||
|
import com.sky.vo.UserReportVO;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
@@ -24,6 +26,8 @@ public class ReportServiceImpl implements ReportService {
|
|||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private OrderMapper orderMapper;
|
private OrderMapper orderMapper;
|
||||||
|
@Autowired
|
||||||
|
private UserMapper userMapper;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -70,4 +74,54 @@ public class ReportServiceImpl implements ReportService {
|
|||||||
|
|
||||||
return turnoverReportVO;
|
return turnoverReportVO;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 统计指定时间内的用户数据
|
||||||
|
* @param begin
|
||||||
|
* @param end
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public UserReportVO getUserStatistics(LocalDate begin, LocalDate end) {
|
||||||
|
|
||||||
|
//当前集合用于存放从begin到end范围内每天的日期
|
||||||
|
List<LocalDate> dateList = new ArrayList();
|
||||||
|
dateList.add(begin);
|
||||||
|
while(!begin.equals(end)){
|
||||||
|
//日期计算 计算指定日期的后一天对应的日期
|
||||||
|
begin = begin.plusDays(1);
|
||||||
|
dateList.add(begin);
|
||||||
|
}
|
||||||
|
|
||||||
|
//存放每天的新增用户数量 select count(id) from user where create_time < ? and create_time > ?
|
||||||
|
List<Integer> newUserList = new ArrayList<>();
|
||||||
|
//存放每天的总用户数量 select count(id) from user where create_time < ?
|
||||||
|
List<Integer> totalUserList = new ArrayList<>();
|
||||||
|
|
||||||
|
//设置用户数据集合 用户总量 新增用户
|
||||||
|
for(LocalDate date:dateList){
|
||||||
|
LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
|
||||||
|
LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);
|
||||||
|
|
||||||
|
Map map = new HashMap();
|
||||||
|
map.put("end", endTime);
|
||||||
|
Integer totalUser = userMapper.countByMap(map);
|
||||||
|
|
||||||
|
map.put("begin", beginTime);
|
||||||
|
Integer newUser = userMapper.countByMap(map);
|
||||||
|
|
||||||
|
totalUserList.add(totalUser);
|
||||||
|
newUserList.add(newUser);
|
||||||
|
}
|
||||||
|
|
||||||
|
//封装结果数据
|
||||||
|
UserReportVO userReportVO = UserReportVO.builder()
|
||||||
|
.dateList(StringUtils.join(dateList, ","))
|
||||||
|
.totalUserList(StringUtils.join(totalUserList, ","))
|
||||||
|
.newUserList(StringUtils.join(newUserList, ","))
|
||||||
|
.build();
|
||||||
|
|
||||||
|
return userReportVO;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,4 +9,17 @@
|
|||||||
values
|
values
|
||||||
(#{openid}, #{name}, #{phone}, #{sex}, #{idNumber}, #{avatar}, #{createTime})
|
(#{openid}, #{name}, #{phone}, #{sex}, #{idNumber}, #{avatar}, #{createTime})
|
||||||
</insert>
|
</insert>
|
||||||
|
|
||||||
|
|
||||||
|
<select id="countByMap" resultType="java.lang.Integer">
|
||||||
|
select count(id) from user
|
||||||
|
<where>
|
||||||
|
<if test="begin != null">
|
||||||
|
and create_time > #{begin}
|
||||||
|
</if>
|
||||||
|
<if test="end != null">
|
||||||
|
and create_time < #{end}
|
||||||
|
</if>
|
||||||
|
</where>
|
||||||
|
</select>
|
||||||
</mapper>
|
</mapper>
|
||||||
|
|||||||
Reference in New Issue
Block a user