营业额数据统计功能代码开发
This commit is contained in:
@@ -0,0 +1,44 @@
|
|||||||
|
package com.sky.controller.admin;
|
||||||
|
|
||||||
|
|
||||||
|
import com.sky.result.Result;
|
||||||
|
import com.sky.service.ReportService;
|
||||||
|
import com.sky.vo.TurnoverReportVO;
|
||||||
|
import io.swagger.annotations.Api;
|
||||||
|
import io.swagger.annotations.ApiOperation;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.format.annotation.DateTimeFormat;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
|
|
||||||
|
@RestController()
|
||||||
|
@RequestMapping("/admin/report")
|
||||||
|
@Api(tags = "数据统计相关接口")
|
||||||
|
@Slf4j
|
||||||
|
public class ReportController {
|
||||||
|
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private ReportService reportService;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 营业额统计
|
||||||
|
* @param begin
|
||||||
|
* @param end
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@GetMapping("/turnoverStatistics")
|
||||||
|
@ApiOperation("营业额统计")
|
||||||
|
public Result<TurnoverReportVO> turnoverStatistics(
|
||||||
|
@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
|
||||||
|
@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end){
|
||||||
|
log.info("营业额数据统计:{},{}", begin, end);
|
||||||
|
return Result.success(reportService.getTurnoverStatistics(begin, end));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -9,6 +9,7 @@ import org.apache.ibatis.annotations.Select;
|
|||||||
|
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
@Mapper
|
@Mapper
|
||||||
public interface OrderMapper {
|
public interface OrderMapper {
|
||||||
@@ -66,4 +67,13 @@ public interface OrderMapper {
|
|||||||
*/
|
*/
|
||||||
@Select("select * from orders where status = #{status} and order_time < #{orderTime}")
|
@Select("select * from orders where status = #{status} and order_time < #{orderTime}")
|
||||||
List<Orders> getByStatusAndOrderTimeLT(Integer status, LocalDateTime orderTime);
|
List<Orders> getByStatusAndOrderTimeLT(Integer status, LocalDateTime orderTime);
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据动态条件统计营业额数据
|
||||||
|
* @param map
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
//select sum(amount) from orders where order_time < ? and order_time > ? and status = 5
|
||||||
|
Double sumByMap(Map map);
|
||||||
}
|
}
|
||||||
|
|||||||
17
sky-server/src/main/java/com/sky/service/ReportService.java
Normal file
17
sky-server/src/main/java/com/sky/service/ReportService.java
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
package com.sky.service;
|
||||||
|
|
||||||
|
import com.sky.vo.TurnoverReportVO;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
|
|
||||||
|
public interface ReportService {
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
*统计指定时间内的营业额数据
|
||||||
|
* @param begin
|
||||||
|
* @param end
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
TurnoverReportVO getTurnoverStatistics(LocalDate begin, LocalDate end);
|
||||||
|
}
|
||||||
@@ -0,0 +1,73 @@
|
|||||||
|
package com.sky.service.impl;
|
||||||
|
|
||||||
|
import com.sky.entity.Orders;
|
||||||
|
import com.sky.mapper.OrderMapper;
|
||||||
|
import com.sky.service.ReportService;
|
||||||
|
import com.sky.vo.TurnoverReportVO;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.time.LocalTime;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
|
||||||
|
@Service
|
||||||
|
@Slf4j
|
||||||
|
public class ReportServiceImpl implements ReportService {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private OrderMapper orderMapper;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 统计指定时间内的营业额数据
|
||||||
|
* @param begin
|
||||||
|
* @param end
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public TurnoverReportVO getTurnoverStatistics(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);
|
||||||
|
}
|
||||||
|
|
||||||
|
//设置营业额集合
|
||||||
|
List<Double> turnoverList = new ArrayList<>();
|
||||||
|
for(LocalDate date:dateList){
|
||||||
|
//查询date对应的营业额 状态为“已完成”的订单的金额总和
|
||||||
|
LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
|
||||||
|
LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);
|
||||||
|
|
||||||
|
Map map = new HashMap();
|
||||||
|
map.put("beginTime",beginTime);
|
||||||
|
map.put("endTime",endTime);
|
||||||
|
map.put("status", Orders.COMPLETED);
|
||||||
|
Double turnover = orderMapper.sumByMap(map);
|
||||||
|
turnover = turnover == null ? 0.0 : turnover;
|
||||||
|
turnoverList.add(turnover);
|
||||||
|
}
|
||||||
|
|
||||||
|
//将日期转为字符串
|
||||||
|
String join = StringUtils.join(dateList, ",");
|
||||||
|
|
||||||
|
TurnoverReportVO turnoverReportVO = TurnoverReportVO.builder()
|
||||||
|
.dateList(join)
|
||||||
|
.turnoverList(StringUtils.join(turnoverList, ","))
|
||||||
|
.build();
|
||||||
|
|
||||||
|
return turnoverReportVO;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -67,4 +67,20 @@
|
|||||||
</where>
|
</where>
|
||||||
order by order_time desc
|
order by order_time desc
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
|
|
||||||
|
<select id="sumByMap" resultType="java.lang.Double">
|
||||||
|
select sum(amount) from orders
|
||||||
|
<where>
|
||||||
|
<if test="beginTime != null">
|
||||||
|
and order_time > #{beginTime}
|
||||||
|
</if>
|
||||||
|
<if test="endTime != null">
|
||||||
|
and order_time < #{endTime}
|
||||||
|
</if>
|
||||||
|
<if test="status != null">
|
||||||
|
and status = #{status}
|
||||||
|
</if>
|
||||||
|
</where>
|
||||||
|
</select>
|
||||||
</mapper>
|
</mapper>
|
||||||
|
|||||||
Reference in New Issue
Block a user