package com.ruoyi.web.controller.api;
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import cn.hutool.core.io.resource.ClassPathResource;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.basic.PageInfo;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.WebUtils;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.domain.TShop;
import com.ruoyi.system.export.SumGeneratorClient;
import com.ruoyi.system.query.ProfitDetailsQuery;
import com.ruoyi.system.query.TDataStatisticsQuery;
import com.ruoyi.system.service.TOrderMealService;
import com.ruoyi.system.service.TOrderSaleService;
import com.ruoyi.system.service.TShopService;
import com.ruoyi.system.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;
/**
*
* 营业数据统计 前端控制器
*
*
* @author xiaochen
* @since 2024-08-27
*/
@Api(tags = "营业数据统计")
@RestController
@RequestMapping("/t-data-statistics")
public class TDataStatisticsController {
private final TOrderMealService orderMealService;
private final TOrderSaleService orderSaleService;
private final TokenService tokenService;
private final TShopService shopService;
@Autowired
public TDataStatisticsController(TOrderMealService orderMealService, TOrderSaleService orderSaleService, TokenService tokenService, TShopService shopService) {
this.orderMealService = orderMealService;
this.orderSaleService = orderSaleService;
this.tokenService = tokenService;
this.shopService = shopService;
}
@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(StringUtils.isEmpty(query.getStartTime()) && StringUtils.isEmpty(query.getEndTime())){
if(Objects.isNull(query.getTimeType())){
query.setTimeType(1);
}
LocalDateTime startTime = null;
LocalDateTime endTime = null;
switch (query.getTimeType()){
case 1:
// 今日
startTime = DateUtils.getDayStart(LocalDateTime.now());
endTime = DateUtils.getDayEnd(LocalDateTime.now());
break;
case 2:
// 昨日
startTime = DateUtils.getDayStart(LocalDateTime.now().minusDays(1));
endTime = DateUtils.getDayEnd(LocalDateTime.now().minusDays(1));
break;
case 3:
// 近7天
startTime = DateUtils.getDayStart(LocalDateTime.now().minusDays(7));
endTime = DateUtils.getDayEnd(LocalDateTime.now());
break;
case 4:
// 近30天
startTime = DateUtils.getDayStart(LocalDateTime.now().minusDays(30));
endTime = DateUtils.getDayEnd(LocalDateTime.now());
break;
}
query.setStartTime(DateUtils.localDateTimeToDate(startTime));
query.setEndTime(DateUtils.localDateTimeToDate(endTime));
}
SalesVolumeVO salesVolumeVO;
if(roleType == 2){
salesVolumeVO = orderMealService.salesVolume(query);
}else {
salesVolumeVO = orderSaleService.salesVolume(query);
}
return AjaxResult.success(salesVolumeVO);
}
@ApiOperation( value = "盈利明细")
@PostMapping(value = "/profitDetails")
public AjaxResult profitDetails(@RequestBody ProfitDetailsQuery query) {
Long objectId = tokenService.getLoginUser().getObjectId();
Integer roleType = tokenService.getLoginUser().getRoleType();
query.setShopId(objectId);
if(StringUtils.isEmpty(query.getStartTime()) && StringUtils.isEmpty(query.getEndTime())){
if(Objects.nonNull(query.getTimeType())){
LocalDateTime startTime = null;
LocalDateTime endTime = null;
switch (query.getTimeType()){
case 1:
// 今日
startTime = DateUtils.getDayStart(LocalDateTime.now());
endTime = DateUtils.getDayEnd(LocalDateTime.now());
break;
case 2:
// 昨日
startTime = DateUtils.getDayStart(LocalDateTime.now().minusDays(1));
endTime = DateUtils.getDayEnd(LocalDateTime.now().minusDays(1));
break;
case 3:
// 近7天
startTime = DateUtils.getDayStart(LocalDateTime.now().minusDays(7));
endTime = DateUtils.getDayEnd(LocalDateTime.now());
break;
case 4:
// 近30天
startTime = DateUtils.getDayStart(LocalDateTime.now().minusDays(30));
endTime = DateUtils.getDayEnd(LocalDateTime.now());
break;
}
query.setStartTime(DateUtils.localDateTimeToDate(startTime));
query.setEndTime(DateUtils.localDateTimeToDate(endTime));
}
}
ProfitDetailsVO profitDetailsVO;
if(roleType.equals(2)){
profitDetailsVO = orderMealService.profitDetails(query);
}else {
profitDetailsVO = orderSaleService.profitDetails(query);
}
return AjaxResult.success(profitDetailsVO);
}
@ApiOperation( value = "盈利明细导出")
@PostMapping(value = "/profitDetailsExport")
public void profitDetailsExport(@RequestBody ProfitDetailsQuery query) {
Long objectId = tokenService.getLoginUser().getObjectId();
Integer roleType = tokenService.getLoginUser().getRoleType();
query.setShopId(objectId);
if(StringUtils.isEmpty(query.getStartTime()) && StringUtils.isEmpty(query.getEndTime())){
if(Objects.nonNull(query.getTimeType())){
LocalDateTime startTime = null;
LocalDateTime endTime = null;
switch (query.getTimeType()){
case 1:
// 今日
startTime = DateUtils.getDayStart(LocalDateTime.now());
endTime = DateUtils.getDayEnd(LocalDateTime.now());
break;
case 2:
// 昨日
startTime = DateUtils.getDayStart(LocalDateTime.now().minusDays(1));
endTime = DateUtils.getDayEnd(LocalDateTime.now().minusDays(1));
break;
case 3:
// 近7天
startTime = DateUtils.getDayStart(LocalDateTime.now().minusDays(7));
endTime = DateUtils.getDayEnd(LocalDateTime.now());
break;
case 4:
// 近30天
startTime = DateUtils.getDayStart(LocalDateTime.now().minusDays(30));
endTime = DateUtils.getDayEnd(LocalDateTime.now());
break;
}
query.setStartTime(DateUtils.localDateTimeToDate(startTime));
query.setEndTime(DateUtils.localDateTimeToDate(endTime));
}
}
List list;
if(roleType.equals(2)){
list = orderMealService.profitDetailsExport(query);
}else {
list = orderSaleService.profitDetailsExport(query);
}
Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), GoodsProfitVO.class, list);
HttpServletResponse response = WebUtils.response();
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
ServletOutputStream outputStream = null;
try {
String fileName = URLEncoder.encode("盈利明细.xls", "utf-8");
response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
response.setContentType("application/vnd.ms-excel;charset=UTF-8");
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
outputStream = response.getOutputStream();
workbook.write(outputStream);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
@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(StringUtils.isEmpty(query.getStartTime()) && StringUtils.isEmpty(query.getEndTime())){
if(Objects.isNull(query.getTimeType())){
query.setTimeType(1);
}
LocalDateTime startTime = null;
LocalDateTime endTime = null;
switch (query.getTimeType()){
case 1:
// 今日
startTime = DateUtils.getDayStart(LocalDateTime.now());
endTime = DateUtils.getDayEnd(LocalDateTime.now());
break;
case 2:
// 昨日
startTime = DateUtils.getDayStart(LocalDateTime.now().minusDays(1));
endTime = DateUtils.getDayEnd(LocalDateTime.now().minusDays(1));
break;
case 3:
// 近7天
startTime = DateUtils.getDayStart(LocalDateTime.now().minusDays(7));
endTime = DateUtils.getDayEnd(LocalDateTime.now());
break;
case 4:
// 近30天
startTime = DateUtils.getDayStart(LocalDateTime.now().minusDays(30));
endTime = DateUtils.getDayEnd(LocalDateTime.now());
break;
}
query.setStartTime(DateUtils.localDateTimeToDate(startTime));
query.setEndTime(DateUtils.localDateTimeToDate(endTime));
}
PageInfo 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 = DateUtils.getDayStart(LocalDateTime.now());
endTime = DateUtils.getDayEnd(LocalDateTime.now());
break;
case 2:
// 昨日
startTime = DateUtils.getDayStart(LocalDateTime.now().minusDays(1));
endTime = DateUtils.getDayEnd(LocalDateTime.now().minusDays(1));
break;
case 3:
// 近7天
startTime = DateUtils.getDayStart(LocalDateTime.now().minusDays(7));
endTime = DateUtils.getDayEnd(LocalDateTime.now());
break;
case 4:
// 近30天
startTime = DateUtils.getDayStart(LocalDateTime.now().minusDays(30));
endTime = DateUtils.getDayEnd(LocalDateTime.now());
break;
}
query.setStartTime(DateUtils.localDateTimeToDate(startTime));
query.setEndTime(DateUtils.localDateTimeToDate(endTime));
List orderTrendsVOS;
if(roleType == 2){
orderTrendsVOS = orderMealService.orderingTrends(query);
}else{
orderTrendsVOS = orderSaleService.orderingTrends(query);
}
if (!CollectionUtils.isEmpty(orderTrendsVOS) && Objects.isNull(orderTrendsVOS.get(0).getDayTime())){
orderTrendsVOS.get(0).setDayTime(LocalDate.now().toString());
}
return AjaxResult.success(orderTrendsVOS);
}
@ApiOperation( value = "接待人数统计")
@PostMapping(value = "/personnelStatistics")
public AjaxResult personnelStatistics(@RequestBody TDataStatisticsQuery query) {
Long objectId = tokenService.getLoginUser().getObjectId();
query.setShopId(objectId);
if(StringUtils.isEmpty(query.getStartTime()) && StringUtils.isEmpty(query.getEndTime())){
if(Objects.isNull(query.getTimeType())){
query.setTimeType(1);
}
LocalDateTime startTime = null;
LocalDateTime endTime = null;
switch (query.getTimeType()){
case 1:
// 今日
startTime = DateUtils.getDayStart(LocalDateTime.now());
endTime = DateUtils.getDayEnd(LocalDateTime.now());
break;
case 2:
// 昨日
startTime = DateUtils.getDayStart(LocalDateTime.now().minusDays(1));
endTime = DateUtils.getDayEnd(LocalDateTime.now().minusDays(1));
break;
case 3:
// 近7天
startTime = DateUtils.getDayStart(LocalDateTime.now().minusDays(7));
endTime = DateUtils.getDayEnd(LocalDateTime.now());
break;
case 4:
// 近30天
startTime = DateUtils.getDayStart(LocalDateTime.now().minusDays(30));
endTime = DateUtils.getDayEnd(LocalDateTime.now());
break;
}
query.setStartTime(DateUtils.localDateTimeToDate(startTime));
query.setEndTime(DateUtils.localDateTimeToDate(endTime));
}
PersonnelStatisticsAndSumVO personnelStatisticsAndSumVO = new PersonnelStatisticsAndSumVO();
List personnelStatisticsVOS = orderMealService.personnelStatistics(query);
personnelStatisticsAndSumVO.setPersonnelStatisticsVOS(personnelStatisticsVOS);
if(!CollectionUtils.isEmpty(personnelStatisticsVOS)){
int sum = personnelStatisticsVOS.stream().filter(e -> e.getPersonCount() != null).mapToInt(PersonnelStatisticsVO::getPersonCount).sum();
personnelStatisticsAndSumVO.setPersonCountSum(sum);
// 计算列表中平均金额乘以人数的总和
personnelStatisticsAndSumVO.setTotalMoney(personnelStatisticsVOS.stream().filter(e->e.getPersonCount() != null).mapToDouble(personnelStatisticsVO -> (personnelStatisticsVO.getAvgAmount().doubleValue() * personnelStatisticsVO.getPersonCount()) / sum).sum());
}
return AjaxResult.success(personnelStatisticsAndSumVO);
}
@ApiOperation( value = "汇总导出")
@PostMapping(value = "/sumExport")
public void sumExport(@RequestBody ProfitDetailsQuery query) {
Long objectId = tokenService.getLoginUser().getObjectId();
Map result = new HashMap<>();
SumGeneratorClient sumGeneratorClient = new SumGeneratorClient();
query.setShopId(objectId);
if(StringUtils.isEmpty(query.getStartTime()) && StringUtils.isEmpty(query.getEndTime())){
if(Objects.nonNull(query.getTimeType())){
LocalDateTime startTime = null;
LocalDateTime endTime = null;
switch (query.getTimeType()){
case 1:
// 今日
startTime = DateUtils.getDayStart(LocalDateTime.now());
endTime = DateUtils.getDayEnd(LocalDateTime.now());
break;
case 2:
// 昨日
startTime = DateUtils.getDayStart(LocalDateTime.now().minusDays(1));
endTime = DateUtils.getDayEnd(LocalDateTime.now().minusDays(1));
break;
case 3:
// 近7天
startTime = DateUtils.getDayStart(LocalDateTime.now().minusDays(7));
endTime = DateUtils.getDayEnd(LocalDateTime.now());
break;
case 4:
// 近30天
startTime = DateUtils.getDayStart(LocalDateTime.now().minusDays(30));
endTime = DateUtils.getDayEnd(LocalDateTime.now());
break;
}
query.setStartTime(DateUtils.localDateTimeToDate(startTime));
query.setEndTime(DateUtils.localDateTimeToDate(endTime));
}
}
sumGeneratorClient = orderMealService.sumExport(query);
sumGeneratorClient.setGeneratorTime(DateUtils.stringToLocalDateTime(query.getStartTime()).toLocalDate() + " ~ " + DateUtils.stringToLocalDateTime(query.getEndTime()).toLocalDate());
TShop shop = shopService.getById(objectId);
if(Objects.nonNull(shop)){
sumGeneratorClient.setShopName(shop.getShopName());
}
result.put("sumGeneratorClient",sumGeneratorClient);
//1.获取excel模板
ClassPathResource classPathResource = new ClassPathResource("template/营业统计报表.xls");
TemplateExportParams params = new TemplateExportParams(classPathResource.getPath());
Workbook workbook = ExcelExportUtil.exportExcel(params, result);
HttpServletResponse response = WebUtils.response();
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
ServletOutputStream outputStream = null;
try {
String fileName = URLEncoder.encode("营业统计报表.xls", "utf-8");
response.setHeader("Content-dispodition", "attachment;filename=" + fileName);
outputStream = response.getOutputStream();
workbook.write(outputStream);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}