package com.ruoyi.web.controller.api;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.query.TDataStatisticsQuery;
import com.ruoyi.system.service.TOrderMealService;
import com.ruoyi.system.service.TOrderSaleService;
import com.ruoyi.system.vo.OrderTrendsVO;
import com.ruoyi.system.vo.PersonnelStatisticsVO;
import com.ruoyi.system.vo.SalesRankingVO;
import com.ruoyi.system.vo.SalesVolumeVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;
/**
*
* 营业数据统计 前端控制器
*
*
* @author xiaochen
* @since 2024-08-27
*/
@RestController
@RequestMapping("/t-data-statistics")
public class TDataStatisticsController {
private final TOrderMealService orderMealService;
private final TOrderSaleService orderSaleService;
private final TokenService tokenService;
@Autowired
public TDataStatisticsController(TOrderMealService orderMealService, TOrderSaleService orderSaleService, TokenService tokenService) {
this.orderMealService = orderMealService;
this.orderSaleService = orderSaleService;
this.tokenService = tokenService;
}
@ApiOperation( value = "销售额统计")
@PostMapping(value = "/salesVolume")
public AjaxResult salesVolume(@RequestBody TDataStatisticsQuery query) {
Integer roleType = tokenService.getLoginUser().getRoleType();
Long objectId = tokenService.getLoginUser().getObjectId();
query.setShopId(objectId);
if(Objects.isNull(query.getTimeType())){
query.setTimeType(1);
}
LocalDateTime startTime = null;
LocalDateTime endTime = null;
switch (query.getTimeType()){
case 1:
// 今日
startTime = LocalDateTime.MIN;
endTime = LocalDateTime.MAX;
break;
case 2:
// 昨日
startTime = LocalDateTime.now().minusDays(1);
endTime = LocalDateTime.now().minusDays(1);
break;
case 3:
// 近7天
startTime = LocalDateTime.now().minusDays(7);
endTime = LocalDateTime.now();
break;
case 4:
// 近30天
startTime = LocalDateTime.now().minusDays(30);
endTime = LocalDateTime.now();
break;
}
query.setStartTime(startTime);
query.setEndTime(endTime);
SalesVolumeVO salesVolumeVO;
if(roleType == 2){
salesVolumeVO = orderMealService.salesVolume(query);
}else {
salesVolumeVO = orderSaleService.salesVolume(query);
}
return AjaxResult.success(salesVolumeVO);
}
@ApiOperation( value = "商品分类销售排行")
@PostMapping(value = "/salesRanking")
public AjaxResult> salesRanking(@RequestBody TDataStatisticsQuery query) {
Integer roleType = tokenService.getLoginUser().getRoleType();
Long objectId = tokenService.getLoginUser().getObjectId();
query.setShopId(objectId);
if(Objects.isNull(query.getTimeType())){
query.setTimeType(1);
}
LocalDateTime startTime = null;
LocalDateTime endTime = null;
switch (query.getTimeType()){
case 1:
// 今日
startTime = LocalDateTime.MIN;
endTime = LocalDateTime.MAX;
break;
case 2:
// 昨日
startTime = LocalDateTime.now().minusDays(1);
endTime = LocalDateTime.now().minusDays(1);
break;
case 3:
// 近7天
startTime = LocalDateTime.now().minusDays(7);
endTime = LocalDateTime.now();
break;
case 4:
// 近30天
startTime = LocalDateTime.now().minusDays(30);
endTime = LocalDateTime.now();
break;
}
query.setStartTime(startTime);
query.setEndTime(endTime);
List salesRankingVOS;
if(roleType == 2){
salesRankingVOS = orderMealService.salesRanking(query);
}else{
salesRankingVOS = orderSaleService.salesRanking(query);
}
return AjaxResult.success(salesRankingVOS);
}
@ApiOperation( value = "下单趋势")
@PostMapping(value = "/orderingTrends")
public AjaxResult> orderingTrends(@RequestBody TDataStatisticsQuery query) {
Integer roleType = tokenService.getLoginUser().getRoleType();
Long objectId = tokenService.getLoginUser().getObjectId();
query.setShopId(objectId);
if(Objects.isNull(query.getTimeType())){
query.setTimeType(3);
}
LocalDateTime startTime = null;
LocalDateTime endTime = null;
switch (query.getTimeType()){
case 1:
// 今日
startTime = LocalDateTime.MIN;
endTime = LocalDateTime.MAX;
break;
case 2:
// 昨日
startTime = LocalDateTime.now().minusDays(1);
endTime = LocalDateTime.now().minusDays(1);
break;
case 3:
// 近7天
startTime = LocalDateTime.now().minusDays(7);
endTime = LocalDateTime.now();
break;
case 4:
// 近30天
startTime = LocalDateTime.now().minusDays(30);
endTime = LocalDateTime.now();
break;
}
query.setStartTime(startTime);
query.setEndTime(endTime);
List orderTrendsVOS;
if(roleType == 2){
orderTrendsVOS = orderMealService.orderingTrends(query);
}else{
orderTrendsVOS = orderSaleService.orderingTrends(query);
}
return AjaxResult.success(orderTrendsVOS);
}
@ApiOperation( value = "接待人数统计")
@PostMapping(value = "/personnelStatistics")
public AjaxResult> personnelStatistics(@RequestBody TDataStatisticsQuery query) {
Long objectId = tokenService.getLoginUser().getObjectId();
query.setShopId(objectId);
if(Objects.isNull(query.getTimeType())){
query.setTimeType(1);
}
LocalDateTime startTime = null;
LocalDateTime endTime = null;
switch (query.getTimeType()){
case 1:
// 今日
startTime = LocalDateTime.MIN;
endTime = LocalDateTime.MAX;
break;
case 2:
// 昨日
startTime = LocalDateTime.now().minusDays(1);
endTime = LocalDateTime.now().minusDays(1);
break;
case 3:
// 近7天
startTime = LocalDateTime.now().minusDays(7);
endTime = LocalDateTime.now();
break;
case 4:
// 近30天
startTime = LocalDateTime.now().minusDays(30);
endTime = LocalDateTime.now();
break;
}
query.setStartTime(startTime);
query.setEndTime(endTime);
return AjaxResult.success(orderMealService.personnelStatistics(query));
}
}