营业额统计

This commit is contained in:
Bunny 2024-01-11 10:42:04 +08:00
parent 6076e2b081
commit 9c27154677
6 changed files with 138 additions and 5 deletions

View File

@ -96,6 +96,7 @@
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<!-- websocket -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
@ -122,11 +123,6 @@
<artifactId>minio</artifactId>
<version>8.5.2</version>
</dependency>
<!-- websocket -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
</dependencies>
<build>

View File

@ -0,0 +1,40 @@
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.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 javax.annotation.Resource;
import java.time.LocalDate;
@RestController
@RequestMapping("/admin/report")
@Api(tags = "数据相关接口")
@Slf4j
public class ReportController {
@Resource
private ReportService reportService;
/**
* 营业额统计
*
* @param begin 看起始时间
* @param end 结束时间
* @return TurnoverReportVO
*/
@ApiOperation("营业额统计")
@GetMapping("/turnoverStatistics")
public Result<TurnoverReportVO> turnoverReport(@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {
log.info("营业额统计,开始时间:{};结束时间:{}", begin, end);
TurnoverReportVO turnoverStatistics = reportService.getTurnoverStatistics(begin, end);
return Result.success(turnoverStatistics);
}
}

View File

@ -6,6 +6,7 @@ import com.sky.entity.Orders;
import org.apache.ibatis.annotations.Mapper;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
@Mapper
@ -70,4 +71,12 @@ public interface OrderMapper {
* @param status
*/
Integer countStatus(Integer status);
/**
* 根据动态条件统计营业额数据
*
* @param map 集合
* @return Double
*/
Double sumByMap(HashMap map);
}

View File

@ -0,0 +1,16 @@
package com.sky.service;
import com.sky.vo.TurnoverReportVO;
import java.time.LocalDate;
public interface ReportService {
/**
* 营业额统计
*
* @param begin 看起始时间
* @param end 结束时间
* @return TurnoverReportVO
*/
TurnoverReportVO getTurnoverStatistics(LocalDate begin, LocalDate end);
}

View File

@ -0,0 +1,56 @@
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 org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.HashMap;
@Service
public class ReportServiceImpl implements ReportService {
@Resource
private OrderMapper orderMapper;
/**
* 营业额统计
*
* @param begin 看起始时间
* @param end 结束时间
* @return TurnoverReportVO
*/
@Override
public TurnoverReportVO getTurnoverStatistics(LocalDate begin, LocalDate end) {
ArrayList<LocalDate> dateArrayList = new ArrayList<>();
dateArrayList.add(begin);
// 循环加天
while (!begin.equals(end)) {
begin = begin.plusDays(1);
dateArrayList.add(begin);
}
// 查询date日期营业额
ArrayList<Double> turnoverList = new ArrayList<>();
for (LocalDate date : dateArrayList) {
// 查询date日期对应营业额数据营业额是状态已完成订单金额合计
LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);
HashMap map = new HashMap<>();
map.put("begin", beginTime);
map.put("end", endTime);
map.put("status", Orders.COMPLETED);
Double turnover = orderMapper.sumByMap(map);
turnover = turnover == null ? 0.0 : turnover;
turnoverList.add(turnover);
}
return TurnoverReportVO.builder().dateList(StringUtils.join(dateArrayList, ",")).turnoverList(StringUtils.join(turnoverList, ",")).build();
}
}

View File

@ -109,4 +109,20 @@
from orders
where status = #{status}
</select>
<!-- 根据动态条件统计营业额数据 -->
<select id="sumByMap" resultType="java.lang.Double">
select sum(amount) from orders
<where>
<if test="begin != null">
and order_time &gt; #{begin}
</if>
<if test="end != null">
and order_time &lt; #{end}
</if>
<if test="status != null">
and status = #{status}
</if>
</where>
</select>
</mapper>