每日统计
This commit is contained in:
@ -73,4 +73,6 @@ public class BusinessRespVO {
|
||||
private BigDecimal withdrawal;
|
||||
|
||||
private BigDecimal reduce;
|
||||
|
||||
private String time;
|
||||
}
|
@ -0,0 +1,56 @@
|
||||
package cn.iocoder.yudao.module.member.controller.app.business;
|
||||
|
||||
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
|
||||
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
||||
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
|
||||
import cn.iocoder.yudao.module.member.controller.admin.business.vo.BusinessRespVO;
|
||||
import cn.iocoder.yudao.module.member.dal.dataobject.business.BusinessDO;
|
||||
import cn.iocoder.yudao.module.member.service.business.BusinessService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
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 javax.validation.Valid;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
|
||||
|
||||
|
||||
@Tag(name = "管理后台 - 营业统计")
|
||||
@RestController
|
||||
@RequestMapping("/t/business")
|
||||
@Validated
|
||||
public class AppBusinessController {
|
||||
|
||||
@Resource
|
||||
private BusinessService businessService;
|
||||
|
||||
|
||||
|
||||
@GetMapping("/data")
|
||||
@Operation(summary = "获得营业统计")
|
||||
public CommonResult<List<BusinessRespVO>> getBusinessPage(String time,Long carteenId,Integer type) {
|
||||
List<BusinessRespVO> result = new ArrayList<>();
|
||||
switch (type){
|
||||
case 0:
|
||||
result = businessService.getDay(time, carteenId);
|
||||
break;
|
||||
case 1:
|
||||
result = businessService.getWeek(time, carteenId);
|
||||
break;
|
||||
case 2:
|
||||
result = businessService.getMonth(time, carteenId);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return success(result);
|
||||
}
|
||||
}
|
@ -2,11 +2,13 @@ package cn.iocoder.yudao.module.member.service.business;
|
||||
|
||||
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
||||
import cn.iocoder.yudao.module.member.controller.admin.business.vo.BusinessPageReqVO;
|
||||
import cn.iocoder.yudao.module.member.controller.admin.business.vo.BusinessRespVO;
|
||||
import cn.iocoder.yudao.module.member.controller.admin.business.vo.BusinessSaveReqVO;
|
||||
import cn.iocoder.yudao.module.member.controller.admin.business.vo.StatisticsVo;
|
||||
import cn.iocoder.yudao.module.member.dal.dataobject.business.BusinessDO;
|
||||
|
||||
import javax.validation.*;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 营业统计 Service 接口
|
||||
@ -56,4 +58,9 @@ public interface BusinessService {
|
||||
|
||||
void updateStatistics(StatisticsVo vo);
|
||||
|
||||
List<BusinessRespVO> getDay(String time,Long carteenId);
|
||||
|
||||
List<BusinessRespVO> getWeek(String time,Long carteenId);
|
||||
|
||||
List<BusinessRespVO> getMonth(String time,Long carteenId);
|
||||
}
|
@ -4,6 +4,7 @@ import cn.hutool.core.collection.CollectionUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
||||
import cn.iocoder.yudao.module.member.controller.admin.business.vo.BusinessPageReqVO;
|
||||
import cn.iocoder.yudao.module.member.controller.admin.business.vo.BusinessRespVO;
|
||||
import cn.iocoder.yudao.module.member.controller.admin.business.vo.BusinessSaveReqVO;
|
||||
import cn.iocoder.yudao.module.member.controller.admin.business.vo.StatisticsVo;
|
||||
import cn.iocoder.yudao.module.member.dal.dataobject.business.BusinessDO;
|
||||
@ -15,7 +16,9 @@ import cn.iocoder.yudao.module.member.dal.mysql.orderdetail.OrderDetailMapper;
|
||||
import cn.iocoder.yudao.module.member.enums.CostTypeEnum;
|
||||
import cn.iocoder.yudao.module.member.enums.TimePeriodEnum;
|
||||
import cn.iocoder.yudao.module.member.service.orderdetail.OrderDetailService;
|
||||
import cn.iocoder.yudao.module.member.util.MemberTimeUtils;
|
||||
import cn.iocoder.yudao.module.system.enums.ErrorCodeConstants;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@ -26,10 +29,15 @@ import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
|
||||
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.RoundingMode;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.LocalTime;
|
||||
import java.util.List;
|
||||
import java.time.YearMonth;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.time.temporal.WeekFields;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
||||
|
||||
@ -121,6 +129,12 @@ public class BusinessServiceImpl implements BusinessService {
|
||||
if(ObjectUtil.isNotEmpty(vo.getTotalMoney())){
|
||||
businessDO.setTurnover(businessDO.getTurnover().add(vo.getTotalMoney()));
|
||||
}
|
||||
|
||||
//客单价
|
||||
if(businessDO.getOrderSum() != 0){
|
||||
businessDO.setPriceAvg(businessDO.getTurnover().divide(new BigDecimal(businessDO.getOrderSum()), 2, RoundingMode.HALF_UP));
|
||||
}
|
||||
|
||||
//减免金额
|
||||
if(ObjectUtil.isNotEmpty(vo.getReduceMoney())){
|
||||
businessDO.setReduce(businessDO.getReduce().add(vo.getReduceMoney()));
|
||||
@ -142,7 +156,164 @@ public class BusinessServiceImpl implements BusinessService {
|
||||
businessMapper.updateById(businessDO);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<BusinessRespVO> getDay(String time,Long carteenId) {
|
||||
|
||||
LambdaQueryWrapper<BusinessDO> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(BusinessDO::getCarteenId,carteenId);
|
||||
wrapper.orderByAsc(BusinessDO::getCreateTime);
|
||||
wrapper.apply("DATE_FORMAT(create_time, '%Y-%m') = {0}", time);
|
||||
List<BusinessDO> businessDOS = businessMapper.selectList(wrapper);
|
||||
|
||||
Map<String, BusinessDO> map = businessDOS.stream().collect(Collectors.toMap(vo -> vo.getCreateTime().format(DateTimeFormatter.ofPattern("dd"))
|
||||
, vo -> vo, (existing, replacement) -> existing));
|
||||
|
||||
// 创建一个Map来存储每天的数据
|
||||
Map<String, BusinessRespVO> monthData = new LinkedHashMap<>();
|
||||
|
||||
|
||||
// 定义日期格式化器
|
||||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM");
|
||||
|
||||
// 将字符串解析为 YearMonth 对象
|
||||
YearMonth yearMonth = YearMonth.parse(time, formatter);
|
||||
|
||||
// 获取当月的总天数
|
||||
int daysInMonth = yearMonth.lengthOfMonth();
|
||||
|
||||
// 定义一个格式化器,格式为 "dd"(两位数的日期)
|
||||
DateTimeFormatter dayFormatter = DateTimeFormatter.ofPattern("dd");
|
||||
|
||||
// 循环遍历该月的每一天
|
||||
for (int day = 1; day <= daysInMonth; day++) {
|
||||
// 获取当前日期
|
||||
LocalDate date = yearMonth.atDay(day);
|
||||
|
||||
// 格式化日期为两位数的形式
|
||||
String key = date.format(dayFormatter);
|
||||
|
||||
if(map.get(key) == null){
|
||||
BusinessRespVO bean = new BusinessRespVO();
|
||||
bean.setTurnover(BigDecimal.ZERO).setOrderSum(0).setReduce(BigDecimal.ZERO).setWeigh(BigDecimal.ZERO)
|
||||
.setBreakfast(BigDecimal.ZERO).setLunch(BigDecimal.ZERO).setDinner(BigDecimal.ZERO)
|
||||
.setTime(key);
|
||||
monthData.put(key,bean);
|
||||
}else {
|
||||
BusinessRespVO bean = BeanUtils.toBean(map.get(key), BusinessRespVO.class);
|
||||
bean.setTime(key);
|
||||
monthData.put(key,bean);
|
||||
}
|
||||
|
||||
}
|
||||
return new ArrayList<>(monthData.values());
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<BusinessRespVO> getWeek(String time,Long carteenId) {
|
||||
|
||||
LambdaQueryWrapper<BusinessDO> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(BusinessDO::getCarteenId,carteenId);
|
||||
wrapper.orderByAsc(BusinessDO::getCreateTime);
|
||||
wrapper.apply("DATE_FORMAT(create_time, '%Y-%m') = {0}", time);
|
||||
List<BusinessDO> businessDOS = businessMapper.selectList(wrapper);
|
||||
|
||||
Map<Integer,BusinessRespVO> map = new HashMap<>();
|
||||
|
||||
int totalWeeksOfMonth = MemberTimeUtils.getTotalWeeksOfMonth(time);
|
||||
for (int i = 1; i <= totalWeeksOfMonth; i++) {
|
||||
BusinessRespVO bean = new BusinessRespVO();
|
||||
bean.setTurnover(BigDecimal.ZERO).setOrderSum(0).setReduce(BigDecimal.ZERO).setWeigh(BigDecimal.ZERO)
|
||||
.setBreakfast(BigDecimal.ZERO).setLunch(BigDecimal.ZERO).setDinner(BigDecimal.ZERO)
|
||||
.setTime(String.valueOf(i));
|
||||
map.put(i,bean);
|
||||
}
|
||||
|
||||
for (BusinessDO businessDO : businessDOS) {
|
||||
|
||||
LocalDate date = businessDO.getCreateTime().toLocalDate();
|
||||
|
||||
// 获取区域的周定义(可以是ISO标准或其他区域标准)
|
||||
WeekFields weekFields = WeekFields.of(Locale.getDefault());
|
||||
|
||||
// 使用weekFields获取当前日期属于当月的第几周
|
||||
int i = date.get(weekFields.weekOfMonth());
|
||||
BusinessRespVO businessRespVO = map.get(i);
|
||||
|
||||
//营业额
|
||||
businessRespVO.setTurnover(businessRespVO.getTurnover().add(businessDO.getTurnover()));
|
||||
//订单数
|
||||
businessRespVO.setOrderSum(businessRespVO.getOrderSum()+businessDO.getOrderSum());
|
||||
//均单价
|
||||
businessRespVO.setPriceAvg(businessDO.getTurnover().divide(new BigDecimal(businessDO.getOrderSum()), 2, RoundingMode.HALF_UP));
|
||||
//减免金额
|
||||
businessRespVO.setReduce(businessRespVO.getReduce().add(businessDO.getReduce()));
|
||||
//重量
|
||||
businessRespVO.setWeigh(businessRespVO.getWeigh().add(businessDO.getWeigh()));
|
||||
//早
|
||||
businessRespVO.setBreakfast(businessRespVO.getBreakfast().add(businessDO.getBreakfast()));
|
||||
//中
|
||||
businessRespVO.setLunch(businessRespVO.getLunch().add(businessDO.getLunch()));
|
||||
//晚
|
||||
businessRespVO.setDinner(businessRespVO.getDinner().add(businessDO.getDinner()));
|
||||
//门店
|
||||
businessRespVO.setCarteenId(businessDO.getCarteenId());
|
||||
}
|
||||
return new ArrayList<>(map.values());
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<BusinessRespVO> getMonth(String time,Long carteenId) {
|
||||
|
||||
int year = MemberTimeUtils.getYearFromYearMonthString(time);
|
||||
|
||||
LambdaQueryWrapper<BusinessDO> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(BusinessDO::getCarteenId,carteenId);
|
||||
wrapper.orderByAsc(BusinessDO::getCreateTime);
|
||||
wrapper.apply("DATE_FORMAT(create_time, '%Y') = {0}", year);
|
||||
List<BusinessDO> businessDOS = businessMapper.selectList(wrapper);
|
||||
|
||||
Map<Integer,BusinessRespVO> map = new HashMap<>();
|
||||
|
||||
for (int i = 1; i <= 12; i++) {
|
||||
BusinessRespVO bean = new BusinessRespVO();
|
||||
bean.setTurnover(BigDecimal.ZERO).setOrderSum(0).setReduce(BigDecimal.ZERO).setWeigh(BigDecimal.ZERO)
|
||||
.setBreakfast(BigDecimal.ZERO).setLunch(BigDecimal.ZERO).setDinner(BigDecimal.ZERO)
|
||||
.setTime(String.valueOf(i));
|
||||
map.put(i,bean);
|
||||
}
|
||||
for (BusinessDO businessDO : businessDOS) {
|
||||
|
||||
LocalDate date = businessDO.getCreateTime().toLocalDate();
|
||||
|
||||
// 使用weekFields获取当前日期属于当月的第几周
|
||||
int i = date.getMonthValue();
|
||||
|
||||
BusinessRespVO businessRespVO = map.get(i);
|
||||
|
||||
//营业额
|
||||
businessRespVO.setTurnover(businessRespVO.getTurnover().add(businessDO.getTurnover()));
|
||||
//订单数
|
||||
businessRespVO.setOrderSum(businessRespVO.getOrderSum()+businessDO.getOrderSum());
|
||||
//均单价
|
||||
businessRespVO.setPriceAvg(businessDO.getTurnover().divide(new BigDecimal(businessDO.getOrderSum()), 2, RoundingMode.HALF_UP));
|
||||
//减免金额
|
||||
businessRespVO.setReduce(businessRespVO.getReduce().add(businessDO.getReduce()));
|
||||
//重量
|
||||
businessRespVO.setWeigh(businessRespVO.getWeigh().add(businessDO.getWeigh()));
|
||||
//早
|
||||
businessRespVO.setBreakfast(businessRespVO.getBreakfast().add(businessDO.getBreakfast()));
|
||||
//中
|
||||
businessRespVO.setLunch(businessRespVO.getLunch().add(businessDO.getLunch()));
|
||||
//晚
|
||||
businessRespVO.setDinner(businessRespVO.getDinner().add(businessDO.getDinner()));
|
||||
//门店
|
||||
businessRespVO.setCarteenId(businessDO.getCarteenId());
|
||||
}
|
||||
return new ArrayList<>(map.values());
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
package cn.iocoder.yudao.module.member.util;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.YearMonth;
|
||||
import java.time.temporal.WeekFields;
|
||||
import java.util.Locale;
|
||||
|
||||
public class MemberTimeUtils {
|
||||
|
||||
// 获取指定月份总共有几周
|
||||
public static int getTotalWeeksOfMonth(String yearMonthString) {
|
||||
// 获取该月的第一天和最后一天
|
||||
LocalDate firstDayOfMonth = convertToLocalDate(yearMonthString, true);
|
||||
LocalDate lastDayOfMonth = firstDayOfMonth.withDayOfMonth(firstDayOfMonth.lengthOfMonth());
|
||||
|
||||
// 获取区域的周定义(可以是ISO标准或其他区域标准)
|
||||
WeekFields weekFields = WeekFields.of(Locale.getDefault());
|
||||
|
||||
// 获取第一天和最后一天的周数
|
||||
int firstWeek = firstDayOfMonth.get(weekFields.weekOfMonth());
|
||||
int lastWeek = lastDayOfMonth.get(weekFields.weekOfMonth());
|
||||
|
||||
// 计算总周数
|
||||
return lastWeek - firstWeek + 1;
|
||||
}
|
||||
|
||||
// 将"yyyy-MM"字符串转换为LocalDate,并决定是该月的第一天还是最后一天
|
||||
public static LocalDate convertToLocalDate(String yearMonthString, boolean isFirstDay) {
|
||||
// 使用YearMonth类处理"yyyy-MM"格式
|
||||
YearMonth yearMonth = YearMonth.parse(yearMonthString);
|
||||
|
||||
// 如果isFirstDay为true,则返回该月的第一天,否则返回最后一天
|
||||
return isFirstDay ? yearMonth.atDay(1) : yearMonth.atEndOfMonth();
|
||||
}
|
||||
|
||||
// 从"yyyy-MM"字符串中提取年份
|
||||
public static int getYearFromYearMonthString(String yearMonthString) {
|
||||
// 使用YearMonth类解析字符串
|
||||
YearMonth yearMonth = YearMonth.parse(yearMonthString);
|
||||
|
||||
// 返回年份
|
||||
return yearMonth.getYear();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user