package com.mzl.flower.service.statistics;
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
import com.mzl.flower.config.exception.ValidationException;
|
import com.mzl.flower.config.security.SecurityUtils;
|
import com.mzl.flower.constant.Constants;
|
import com.mzl.flower.dto.request.flower.FlowerQueryDTO;
|
import com.mzl.flower.dto.request.payment.OrderItemSalesQueryDTO;
|
import com.mzl.flower.dto.request.payment.OrderQueryDTO;
|
import com.mzl.flower.dto.response.payment.OrderStatusCountDTO;
|
import com.mzl.flower.dto.response.statistics.FlowerStatisticsDTO;
|
import com.mzl.flower.dto.response.statistics.OrderStatisticsDTO;
|
import com.mzl.flower.dto.response.statistics.RateStatisticsDTO;
|
import com.mzl.flower.dto.response.statistics.SaleStatisticsDTO;
|
import com.mzl.flower.dto.response.wallet.WalletWithdrawVO;
|
import com.mzl.flower.entity.customer.Customer;
|
import com.mzl.flower.entity.ip.UserAccess;
|
import com.mzl.flower.entity.payment.Order;
|
import com.mzl.flower.entity.supplier.Supplier;
|
import com.mzl.flower.mapper.customer.CustomerMapper;
|
import com.mzl.flower.mapper.flower.FlowerMapper;
|
import com.mzl.flower.mapper.ip.UserAccessMapper;
|
import com.mzl.flower.mapper.payment.*;
|
import com.mzl.flower.mapper.supplier.SupplierMapper;
|
import com.mzl.flower.mapper.wallet.WalletMapper;
|
import com.mzl.flower.service.BaseService;
|
import com.mzl.flower.utils.IpUtil;
|
import io.micrometer.core.instrument.util.StringUtils;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.Transactional;
|
|
import javax.servlet.http.HttpServletRequest;
|
import java.io.IOException;
|
import java.math.BigDecimal;
|
import java.math.RoundingMode;
|
import java.time.LocalDate;
|
import java.time.LocalDateTime;
|
import java.util.ArrayList;
|
import java.util.List;
|
|
@Slf4j
|
@Service
|
@Transactional
|
public class StatisticsService extends BaseService {
|
|
@Autowired
|
private OrderMapper orderMapper;
|
|
@Autowired
|
private OrderItemMapper orderItemMapper;
|
|
@Autowired
|
private FlowerMapper flowerMapper;
|
|
@Autowired
|
private SupplierMapper supplierMapper;
|
|
@Autowired
|
private OrderItemSalesMapper orderItemSalesMapper;
|
|
@Autowired
|
private CustomerMapper customerMapper;
|
|
@Autowired
|
private UserAccessMapper userAccessMapper;
|
|
@Autowired
|
private WalletMapper walletMapper;
|
|
public SaleStatisticsDTO getSaleStatistics(String date){
|
if(StringUtils.isEmpty(date)){
|
throw new ValidationException("日期不能为空");
|
}
|
LocalDate localDate = parseLocalDate(date);
|
if(localDate == null){
|
throw new ValidationException("日期无效");
|
}
|
|
LocalDateTime end = localDate.atTime(17, 0, 0);
|
LocalDateTime begin = end.plusDays(-1);
|
|
SaleStatisticsDTO dto = new SaleStatisticsDTO();
|
BigDecimal a = orderMapper.getOrderSaleAmount(begin, end);//今日销售额:今日全部的实际支付金额合计
|
Integer c = orderItemMapper.getFlowerSaleNum(begin, end);//今日销售扎数:今日成功交易的订单的成交数量
|
|
dto.setSaleAmount(a);
|
dto.setSaleFlowerCount(c == null ? 0 : c.longValue());
|
|
return dto;
|
}
|
|
public FlowerStatisticsDTO getFlowerStatistics(){
|
FlowerStatisticsDTO dto = new FlowerStatisticsDTO();
|
|
Page page = new Page(1, 1);
|
FlowerQueryDTO q = new FlowerQueryDTO();
|
List<String> statusList = new ArrayList<>();
|
statusList.add(Constants.FLOWER_STATUS.PENDING.name());
|
statusList.add(Constants.FLOWER_STATUS.UP.name());
|
statusList.add(Constants.FLOWER_STATUS.OFF.name());
|
statusList.add(Constants.FLOWER_STATUS.FORCE_OFF.name());
|
q.setStatusList(statusList);
|
flowerMapper.selectFlowerList(page, q);
|
dto.setFlowerCount(page.getTotal());//商品管理:待审核+上架+下架(点击跳转到商品列表)
|
|
Integer sc = supplierMapper.selectCount(new QueryWrapper<Supplier>()
|
.eq("is_enabled", 1)
|
.eq("status", "P")
|
.eq("deleted", 0));
|
dto.setSupplierCount(sc.longValue());
|
|
Integer cc = customerMapper.selectCount(new QueryWrapper<Customer>()
|
.eq("is_enabled", 1)
|
.eq("deleted", 0));
|
dto.setCustomerCount(cc.longValue());//用户管理:统计商户列表已启用的全部用户(点击跳转到商户列表)
|
|
//订单管理:全部订单数量-待付款-已取消-已退款(点击跳转到订单列表)
|
List<OrderStatusCountDTO> ll = orderMapper.getOrderStatusCount(new OrderQueryDTO());
|
Integer oc = 0;
|
if (ll != null && ll.size() > 0) {
|
for (OrderStatusCountDTO c : ll) {
|
if(Constants.ORDER_STATUS_BACKEND.PENDING.name().equals(c.getValue())
|
|| Constants.ORDER_STATUS_BACKEND.CANCEL.name().equals(c.getValue())
|
|| Constants.ORDER_STATUS_BACKEND.REFUND.name().equals(c.getValue())){
|
continue;
|
}
|
oc += c.getOrderCount();
|
}
|
}
|
dto.setOrderCount(oc.longValue());
|
|
page = new Page(1, 1);
|
OrderQueryDTO oq = new OrderQueryDTO();
|
oq.setStatusBackend(Constants.ORDER_STATUS_BACKEND.SEND.name());
|
orderMapper.selectOrderList(page, oq);
|
dto.setOrderSendCount(page.getTotal());//待发货:统计订单列表待发货的数量(点击跳转到订单列表-待发货页面)
|
|
page = new Page(1, 1);
|
q = new FlowerQueryDTO();
|
statusList = new ArrayList<>();
|
statusList.add(Constants.FLOWER_STATUS.UP.name());
|
q.setStatusList(statusList);
|
flowerMapper.selectFlowerList(page, q);
|
dto.setFlowerUpCount(page.getTotal());//在售商品:统计商品列表上架的商品数量(点击跳转到商品列表-上架页面)
|
|
page = new Page(1, 1);
|
q = new FlowerQueryDTO();
|
statusList = new ArrayList<>();
|
statusList.add(Constants.FLOWER_STATUS.PENDING.name());
|
q.setStatusList(statusList);
|
flowerMapper.selectFlowerList(page, q);
|
dto.setFlowerPendingCount(page.getTotal());//待审核商品:跳转到商品列表-待审核页面
|
|
page = new Page(1, 1);
|
OrderItemSalesQueryDTO sq = new OrderItemSalesQueryDTO();
|
sq.setStatus(Constants.ORDER_SALES_STATUS.PENDING.name());
|
sq.setStatusList(splitParam(sq.getStatus()));
|
orderItemSalesMapper.selectItemSalesList(page, sq);
|
dto.setOrderSalesCount(page.getTotal());//待售后处理:统计售后理赔-待审核的数量(跳转到售后理赔-待审核页面)
|
|
return dto;
|
}
|
|
public OrderStatisticsDTO getOrderStatistics(){
|
OrderStatisticsDTO dto = new OrderStatisticsDTO();
|
BigDecimal a = orderMapper.getOrderSaleAmount(null, null);//总销售金额:全部有效订单的实际支付金额合计
|
Integer c = orderItemMapper.getFlowerSaleNum(null, null);//总销售扎数:成功交易订单的成交数量
|
dto.setTotalSaleAmount(a);
|
dto.setTotalSaleFlowerCount(c == null ? 0 : c.longValue());
|
|
WalletWithdrawVO withdrawTotal = walletMapper.getSumWithdrawTotal();
|
BigDecimal sumHistoryWithdrawTotal = walletMapper.getSumHistoryWithdrawTotal();
|
dto.setSupplierPendingAmount(withdrawTotal.getSupplierPendingAmount());
|
dto.setSupplierCompleteAmount(withdrawTotal.getSupplierCompleteAmount().add(sumHistoryWithdrawTotal));
|
|
// dto.setSupplierPendingAmount(new BigDecimal(0));//TODO 供应商待提现:结算列表待结算(供应商)+供应商钱包余额
|
// dto.setSupplierCompleteAmount(new BigDecimal(0));//TODO 供应商已提现:结算列表已结算(供应商)+供应商钱包已提现金额
|
|
return dto;
|
}
|
|
public RateStatisticsDTO getOrderRateStatistics(String date){
|
if(StringUtils.isEmpty(date)){
|
throw new ValidationException("日期不能为空");
|
}
|
LocalDate localDate = parseLocalDate(date);
|
if(localDate == null){
|
throw new ValidationException("日期无效");
|
}
|
|
LocalDateTime end = localDate.atTime(17, 0, 0);
|
LocalDateTime begin = end.plusDays(-1);
|
|
LocalDateTime endY = begin;
|
LocalDateTime beginY = endY.plusDays(-1);
|
|
RateStatisticsDTO dto = new RateStatisticsDTO();
|
|
//订单量:与订单管理一致(并计算新增量,计算日环比)
|
Integer oc = orderMapper.selectCount(new QueryWrapper<Order>()
|
.eq("deleted", 0)
|
.isNotNull("payment_time")
|
.isNull("cancel_time")
|
.isNull("refund_time"));
|
dto.setCount(oc.longValue());
|
|
Integer ocToday = orderMapper.selectCount(new QueryWrapper<Order>()
|
.eq("deleted", 0)
|
.isNotNull("payment_time")
|
.isNull("cancel_time")
|
.isNull("refund_time")
|
.gt("create_time", begin)
|
.le("create_time", end)
|
);
|
dto.setCountToday(ocToday.longValue());
|
|
Integer ocY = orderMapper.selectCount(new QueryWrapper<Order>()
|
.eq("deleted", 0)
|
.isNotNull("payment_time")
|
.isNull("cancel_time")
|
.isNull("refund_time")
|
.gt("create_time", beginY)
|
.le("create_time", endY)
|
);
|
dto.setCountRate(getRate(ocToday, ocY));
|
|
return dto;
|
}
|
|
public RateStatisticsDTO getSupplierRateStatistics(String date) {
|
if (StringUtils.isEmpty(date)) {
|
throw new ValidationException("日期不能为空");
|
}
|
LocalDate localDate = parseLocalDate(date);
|
if (localDate == null) {
|
throw new ValidationException("日期无效");
|
}
|
|
LocalDateTime end = localDate.atTime(23, 59, 59);
|
LocalDateTime begin = localDate.atTime(0, 0, 0);
|
|
LocalDateTime endY = end.plusDays(-1);
|
LocalDateTime beginY = begin.plusDays(-1);
|
|
RateStatisticsDTO dto = new RateStatisticsDTO();
|
|
//供应商数量:已启用的供应商数量(并计算新增量,计算日环比)
|
Integer sc = supplierMapper.selectCount(new QueryWrapper<Supplier>()
|
.eq("is_enabled", 1)
|
.eq("status", "P")
|
.eq("deleted", 0));
|
dto.setCount(sc.longValue());
|
|
Integer scT = supplierMapper.selectCount(new QueryWrapper<Supplier>()
|
.eq("is_enabled", 1)
|
.eq("status", "P")
|
.eq("deleted", 0)
|
.gt("create_time", begin)
|
.le("create_time", end)
|
);
|
dto.setCountToday(scT.longValue());
|
|
Integer scY = supplierMapper.selectCount(new QueryWrapper<Supplier>()
|
.eq("is_enabled", 1)
|
.eq("status", "P")
|
.eq("deleted", 0)
|
.gt("create_time", beginY)
|
.le("create_time", endY)
|
);
|
dto.setCountRate(getRate(scT, scY));
|
|
return dto;
|
}
|
|
public RateStatisticsDTO getCustomerRateStatistics(String date) {
|
if (StringUtils.isEmpty(date)) {
|
throw new ValidationException("日期不能为空");
|
}
|
LocalDate localDate = parseLocalDate(date);
|
if (localDate == null) {
|
throw new ValidationException("日期无效");
|
}
|
|
LocalDateTime end = localDate.atTime(23, 59, 59);
|
LocalDateTime begin = localDate.atTime(0, 0, 0);
|
|
LocalDateTime endY = end.plusDays(-1);
|
LocalDateTime beginY = begin.plusDays(-1);
|
|
RateStatisticsDTO dto = new RateStatisticsDTO();
|
//全部用户量:已启用的用户数量合计(并计算新增量,计算日环比)
|
|
Integer c = customerMapper.selectCount(new QueryWrapper<Customer>()
|
.eq("deleted", 0));
|
dto.setCount(c.longValue());
|
|
Integer cT = customerMapper.selectCount(new QueryWrapper<Customer>()
|
.eq("deleted", 0)
|
.gt("create_time", begin)
|
.le("create_time", end)
|
);
|
dto.setCountToday(cT.longValue());
|
|
Integer cY = customerMapper.selectCount(new QueryWrapper<Customer>()
|
.eq("deleted", 0)
|
.gt("create_time", beginY)
|
.le("create_time", endY)
|
);
|
dto.setCountRate(getRate(cT, cY));
|
|
return dto;
|
}
|
|
public RateStatisticsDTO getCustomerVisitRateStatistics(String date) {
|
if (StringUtils.isEmpty(date)) {
|
throw new ValidationException("日期不能为空");
|
}
|
LocalDate localDate = parseLocalDate(date);
|
if (localDate == null) {
|
throw new ValidationException("日期无效");
|
}
|
|
LocalDateTime end = localDate.atTime(23, 59, 59);
|
LocalDateTime begin = localDate.atTime(0, 0, 0);
|
|
LocalDateTime endY = end.plusDays(-1);
|
LocalDateTime beginY = begin.plusDays(-1);
|
|
RateStatisticsDTO dto = new RateStatisticsDTO();
|
//用户访问量:点击到交易大厅或者商品详情页面计算,同一个用户,每天只计算一次(包含游客)(并计算新增量,计算日环比)
|
|
Integer c = userAccessMapper.selectCount(new QueryWrapper<UserAccess>());
|
dto.setCount(c.longValue());
|
|
Integer cT = userAccessMapper.selectCount(new QueryWrapper<UserAccess>()
|
.gt("create_time", begin)
|
.le("create_time", end)
|
);
|
dto.setCountToday(cT.longValue());
|
|
Integer cY = userAccessMapper.selectCount(new QueryWrapper<UserAccess>()
|
.gt("create_time", beginY)
|
.le("create_time", endY)
|
);
|
dto.setCountRate(getRate(cT, cY));
|
return dto;
|
}
|
|
public RateStatisticsDTO getSalesRateStatistics(String date) {
|
if (StringUtils.isEmpty(date)) {
|
throw new ValidationException("日期不能为空");
|
}
|
LocalDate localDate = parseLocalDate(date);
|
if (localDate == null) {
|
throw new ValidationException("日期无效");
|
}
|
|
LocalDateTime end = localDate.atTime(23, 59, 59);
|
LocalDateTime begin = localDate.atTime(0, 0, 0);
|
|
LocalDateTime endY = end.plusDays(-1);
|
LocalDateTime beginY = begin.plusDays(-1);
|
|
RateStatisticsDTO dto = new RateStatisticsDTO();
|
//售后订单数量:统计待审核+已通过(并计算新增量,计算日环比)
|
|
List<String> statusList = new ArrayList<>();
|
statusList.add(Constants.ORDER_SALES_STATUS.PENDING.name());
|
statusList.add(Constants.ORDER_SALES_STATUS.AGREED.name());
|
|
Page page = new Page(1, 1);
|
OrderItemSalesQueryDTO sq = new OrderItemSalesQueryDTO();
|
sq.setStatusList(statusList);
|
orderItemSalesMapper.selectItemSalesList(page, sq);
|
dto.setCount(page.getTotal());
|
|
page = new Page(1, 1);
|
sq = new OrderItemSalesQueryDTO();
|
sq.setStatusList(statusList);
|
sq.setSalesStartDate(begin);
|
sq.setSalesEndDate(end);
|
orderItemSalesMapper.selectItemSalesList(page, sq);
|
dto.setCountToday(page.getTotal());
|
|
page = new Page(1, 1);
|
sq = new OrderItemSalesQueryDTO();
|
sq.setStatusList(statusList);
|
sq.setSalesStartDate(beginY);
|
sq.setSalesEndDate(endY);
|
orderItemSalesMapper.selectItemSalesList(page, sq);
|
dto.setCountRate(getRate(dto.getCountToday().intValue(), (int)page.getTotal()));
|
|
return dto;
|
}
|
|
private Double getRate(Integer today, Integer yesterday){
|
if(yesterday > 0){
|
Integer ty = today - yesterday;
|
BigDecimal ttyy = BigDecimal.valueOf(ty);
|
BigDecimal yy = BigDecimal.valueOf(yesterday);
|
BigDecimal rate = ttyy.divide(yy, 2, RoundingMode.HALF_UP).multiply(BigDecimal.valueOf(100));
|
return rate.doubleValue();
|
} else if (today <= 0) {
|
return 0D;
|
}
|
|
return 100D;
|
}
|
|
|
public void addUserAccessRecord(HttpServletRequest request){
|
String userId = SecurityUtils.getUserId();
|
String ip = null;
|
try {
|
ip = IpUtil.getIpAddress(request);
|
} catch (IOException e) {
|
ip = "127.0.0.1";
|
}
|
String ipId = ip+":"+(StringUtils.isBlank(userId)?"0":userId);
|
|
LocalDate localDate = LocalDate.now();
|
LocalDateTime end = localDate.atTime(23, 59, 59);
|
LocalDateTime begin = localDate.atTime(0, 0, 0);
|
int count = userAccessMapper.selectCount(new LambdaQueryWrapper<UserAccess>()
|
.eq(UserAccess::getIpId, ipId)
|
.gt(UserAccess::getCreateTime, begin)
|
.le(UserAccess::getCreateTime, end));
|
if(count==0){
|
UserAccess record = new UserAccess();
|
record.setIpId(ipId);
|
record.setCreateTime(LocalDateTime.now());
|
userAccessMapper.insert(record);
|
}
|
}
|
}
|