package com.ruoyi.mall.service;

import com.ruoyi.mall.domain.query.GoodsStatisticsQuery;
import com.ruoyi.mall.domain.query.OrderStatisticsQuery;
import com.ruoyi.mall.domain.vo.OrderAndAftersaleStatisticsVO;
import com.ruoyi.mall.domain.vo.OrderStatisticsVO;
import com.ruoyi.mall.domain.vo.ProductTopVO;
import com.ruoyi.mall.mapper.AftersaleMapper;
import com.ruoyi.mall.mapper.IndexStatisticsMapper;
import com.ruoyi.mall.mapper.OrderMapper;
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.List;

/**
 * 管理端,首页统计数据Service业务层处理

 */
@Service
public class IndexStatisticsService {
    @Autowired
    private AftersaleMapper aftersaleMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private IndexStatisticsMapper indexStatisticsMapper;

    public List<ProductTopVO> goodsStatistics(GoodsStatisticsQuery goodsStatisticsQuery) {
        if (goodsStatisticsQuery.getStatType() == 1){
            return indexStatisticsMapper.goodsSkuStatistics(goodsStatisticsQuery);
        }else {
            return indexStatisticsMapper.goodsStatistics(goodsStatisticsQuery);
        }
    }

    public OrderAndAftersaleStatisticsVO orderAndAftersaleStatistics() {
        //统计售后
        OrderAndAftersaleStatisticsVO vo = aftersaleMapper.statPendingAndProcessing();
        //统计未发货数
        vo.setWaitDeliveredCount(orderMapper.statWaitDelivered());
        //统计今日订单数，成交金额，发货数
        LocalDateTime startTime = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
        LocalDateTime endTime = LocalDateTime.of(LocalDate.now(), LocalTime.MAX);
        OrderAndAftersaleStatisticsVO todayData = orderMapper.statTodayData(startTime, endTime);
        vo.setTodayOrderCount(todayData.getTodayOrderCount());
        vo.setTodayHasDeliveredCount(todayData.getTodayHasDeliveredCount());
        vo.setTodayTransactionAmount(todayData.getTodayTransactionAmount());
        return vo;
    }

    public List<OrderStatisticsVO> orderStatistics(OrderStatisticsQuery param) {
        return indexStatisticsMapper.orderStatistics(param);
    }

}
