当前位置: 首页 > news >正文

营业额统计-02.代码开发及功能测试

一.Controller层

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")
@Slf4j
@Api(tags = "数据统计相关接口")
public class ReportController {@Autowiredprivate 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);TurnoverReportVO turnoverReport = reportService.getTurnoverStatistic(begin, end);return Result.success(turnoverReport);}
}

营业额统计要根据时间周期计算每天的营业额是多少,因此前端要传递过来的两个参数分别是开始时间和结束时间,其中要返回的两个数据分别为每天的日期(封装在一个列表中)和每天的营业额(封装在一个列表中)。

二.Service层

接口

package com.sky.service;import com.sky.vo.TurnoverReportVO;
import org.springframework.stereotype.Service;import java.time.LocalDate;@Service
public interface ReportService {/*** 营业额统计接口* @param beginTime* @param endTime* @return*/TurnoverReportVO getTurnoverStatistic(LocalDate beginTime, LocalDate endTime);}

实现类

package com.sky.service.impl;import com.sky.entity.Orders;
import com.sky.mapper.OrdersMapper;
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 {@Autowiredprivate OrdersMapper ordersMapper;/*** 营业额统计接口* @param beginTime* @param endTime* @return*/@Overridepublic TurnoverReportVO getTurnoverStatistic(LocalDate beginTime, LocalDate endTime) {// 当前集合用于存放从begin到end范围内的每天的日期List<LocalDate> dateList = new ArrayList<>();dateList.add(beginTime);while (!beginTime.equals(endTime)) {beginTime = beginTime.plusDays(1);dateList.add(beginTime);}String dateListString = StringUtils.join(dateList, ",");List<Double> turnoverList = new ArrayList<>();for (LocalDate date : dateList) {// 将每天的最早时间(每天的0点0分0秒)和最晚时间(每天的23.59.59.9999999...)算出作为范围LocalDateTime begin = LocalDateTime.of(date, LocalTime.MIN);LocalDateTime end = LocalDateTime.of(date, LocalTime.MAX);// 计算每天的营业额  select sum(amount) from order where status = 5 and order_time < ? and order_time > ?; 状态为“已完成”的订单金额合计Map map = new HashMap<>();map.put("begin", begin);map.put("end", end);map.put("status", Orders.COMPLETED);Double turnover = ordersMapper.sumByMap(map);// 如果当天营业额为0,从数据库中查出来的会是null,那么就将其转为0.0;否则还是它本身turnover = turnover == null ? 0.0 : turnover;turnoverList.add(turnover);}String turnoverListString = StringUtils.join(turnoverList, ",");TurnoverReportVO turnoverReportVO = TurnoverReportVO.builder().dateList(dateListString).turnoverList(turnoverListString).build();return turnoverReportVO;}
}

三.Mapper层

接口

package com.sky.mapper;import com.github.pagehelper.Page;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.entity.Orders;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.core.annotation.Order;import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;@Mapper
public interface OrdersMapper {/*** 用户下单* @param orders*/void insert(Orders orders);/*** 用于替换微信支付更新数据库状态的问题* @param orderStatus* @param orderPaidStatus*/@Update("update orders set status = #{orderStatus},pay_status = #{orderPaidStatus} ,checkout_time = #{check_out_time} " +"where number = #{orderNumber}")void updateStatus(Integer orderStatus, Integer orderPaidStatus, LocalDateTime check_out_time, String orderNumber);/*** 根据id查询订单信息* @param id* @return*/@Select("select * from orders where id = #{id}")Orders getById(Long id);/*** 分页条件查询并按下单时间排序* @param ordersPageQueryDTO* @return*/Page<Orders> pageQuery(OrdersPageQueryDTO ordersPageQueryDTO);/*** 各个状态的订单数量统计* @param status* @return*/@Select("select count(*) from orders where status = #{status}")Integer countByStatus(Integer status);/*** 修改订单状态* @param orders*/void update(Orders orders);/*** 定时查询订单状态* @param status* @param time* @return*/@Select("select * from orders where status = #{status} and order_time < #{time}")List<Orders> getByStatusAndOrderTimeLT(Integer status, LocalDateTime time);/*** 根据订单号查询订单id* @param number* @return*/@Select("select id from orders where number = #{number}")Long getByOrderNumber(String number);/*** 根据动态条件统计营业额数据* @param map* @return*/Double sumByMap(Map map);
}

XML映射文件

<?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.OrdersMapper"><insert id="insert" useGeneratedKeys="true" keyProperty="id">insert into orders(number, status, user_id, address_book_id, order_time, checkout_time, pay_method, pay_status,amount, remark, phone, address, user_name, consignee, cancel_reason, rejection_reason,cancel_time, estimated_delivery_time, delivery_status, delivery_time, pack_amount,tableware_number, tableware_status)VALUES (#{number}, #{status}, #{userId}, #{addressBookId}, #{orderTime}, #{checkoutTime}, #{payMethod},#{payStatus}, #{amount}, #{remark}, #{phone}, #{address}, #{userName}, #{consignee}, #{cancelReason},#{rejectionReason}, #{cancelTime}, #{estimatedDeliveryTime}, #{deliveryStatus}, #{deliveryTime},#{packAmount}, #{tablewareNumber}, #{tablewareStatus})</insert><update id="update">update orders<set><if test="payStatus != null">pay_status = #{payStatus},</if><if test="status != null">status = #{status},</if><if test="cancelReason != null and cancelReason != ''">cancel_reason = #{cancelReason},</if><if test="rejectionReason != null and rejectionReason != ''">rejection_reason = #{rejectionReason},</if><if test="cancelTime != null">cancel_time = #{cancelTime},</if><if test="checkoutTime != null">checkout_time = #{checkoutTime},</if><if test="deliveryTime != null">delivery_time = #{deliveryTime},</if><if test="payMethod != null">pay_method = #{payMethod}</if></set>where id = #{id}</update><select id="pageQuery" resultType="com.sky.entity.Orders">select * from orders<where><if test="number != null and number != ''">and number = #{number}</if><if test="phone != null and phone != ''">and phone = #{phone}</if><if test="status != null">and status = #{status}</if><if test="beginTime != null">and order_time = #{beginTime}</if><if test="endTime != null">and order_time = #{endTime}</if><if test="userId != null">and user_id = #{userId}</if></where>order by order_time desc</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>

四.功能测试 

相关文章:

  • 邢台做网站的天津关键词优化网站
  • 新手建设什么网站好关键词排名怎么查
  • 住建城乡建设部网站证件查询seo网站推广工作内容
  • centos7安装 wordpress旺道seo工具
  • 怎么做防劫持网站营销网络
  • 成都网站运营长春网站快速优化排名
  • 命名数据网络 | 兴趣包(Interest Packet)
  • GitLab 18.1 正式发布Maven 虚拟仓库、密码泄露检测等功能,可升级体验!
  • 广州华锐互动:技术与创意双驱动的 VR 先锋​
  • Claude 3.7 的 token 预算机制详解:可控深度的混合推理范式
  • HDFS(Hadoop分布式文件系统)总结
  • 【缓存技术】深入分析如果使用好缓存及注意事项
  • 基于SpringBoot和Leaflet的区域冲突可视化-以伊以冲突为例
  • 6.26_JAVA_微服务_Elasticsearch
  • CRON表达式编辑器与定时任务实现技术文档
  • Linux 统一方式安装多版本 JDK 指南
  • LINUX 626 DNS报错
  • 【工具推荐】WaybackLister——发现潜在目录列表
  • JavaEE:分布式session
  • 2025学年湖北省职业院校技能大赛 “信息安全管理与评估”赛项 样题卷(五)
  • centos 7 安装NVIDIA Container Toolkit
  • 【unity游戏开发——网络】计算机网络中的三种数据管理模型(分散式、集中式、分布式)和三大通信模型(C/S、B/S、P2P)
  • 环境太多?不好管理怎么办?TakMll 工具帮你快速切换和管理多语言、多版本情况下的版本切换。
  • Tailwind CSS 重用样式
  • 内测分发平台是否支持应用的微服务化部署
  • Spring Boot使用Redis常用场景