package com.ruoyi.bst.order.service.impl;

import com.ruoyi.bst.order.domain.OrderQuery;
import com.ruoyi.bst.order.domain.enums.OrderStatus;

import com.ruoyi.bst.order.domain.query.OrderRefundQuery;
import com.ruoyi.bst.order.domain.vo.*;
import com.ruoyi.bst.order.mapper.OrderMapper;
import com.ruoyi.bst.order.service.OrderDashboard;
import com.ruoyi.bst.refund.domain.RefundQuery;
import com.ruoyi.common.domain.vo.LongIntegerVO;
import com.ruoyi.common.domain.vo.StringIntegerVO;
import com.ruoyi.common.utils.MathUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.dashboard.constants.StatKeys;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class OrderDashboardImpl implements OrderDashboard {

    @Autowired
    private OrderMapper orderMapper;


    @Override
    public Map<Long,Integer> selectUserCountGroupByStore(OrderQuery query) {
        List<LongIntegerVO> list = orderMapper.selectUserCountGroupByStore(query);
        return  list.stream().collect(Collectors.toMap(LongIntegerVO::getKey, LongIntegerVO::getValue));
    }

    @Override
    public Integer selectUserCount(OrderQuery query) {
        return MathUtils.dv(orderMapper.selectUserCount(query));
    }

    @Override
    public Integer selectCount(OrderQuery query) {
        return MathUtils.dv(orderMapper.selectCount(query));
    }

    @Override
    public BigDecimal selectPayAmount(OrderQuery query) {
        return MathUtils.dv(orderMapper.selectSumOfPayAmount(query));
    }

    @Override
    public Map<String, Integer> selectStatusCount(OrderQuery query) {
        List<StringIntegerVO> list = orderMapper.selectStatusCount(query);
        return list.stream().collect(Collectors.toMap(StringIntegerVO::getKey, StringIntegerVO::getValue));
    }

    @Override
    public OrderRefundStatVO selectRefundStat(OrderRefundQuery query, List<String> keys) {
        OrderRefundStatVO vo = new OrderRefundStatVO();
        // 退款金额
        if (keys.contains(StatKeys.ORDER_REFUND_AMOUNT)) {
            vo.setAmount(this.selectRefundAmount(query));
        }
        return vo;
    }

    @Override
    public BigDecimal selectRefundAmount(OrderRefundQuery query) {
        return MathUtils.dv(orderMapper.selectSumOfRefundAmount(query));
    }

    @Override
    public List<OrderDailyStatVO> selectDailyStat(OrderQuery query, List<String> keys) {
        return orderMapper.selectDailyStat(query, keys);
    }

    @Override
    public List<OrderDailyRefundStatVO> selectDailyRefundStat(RefundQuery query, List<String> keys) {
        return orderMapper.selectDailyRefundStat(query, keys);
    }

    @Override
    public List<OrderRankVO> selectRank(OrderQuery query) {
        return orderMapper.selectRank(query);
    }

    @Override
    public BigDecimal selectSumOfActualAmount(OrderQuery query) {
        return MathUtils.dv(orderMapper.selectSumOfActualAmount(query));
    }


    @Override
    public BigDecimal selectSumOfActualTotalAmount(OrderQuery query) {
        return MathUtils.dv(orderMapper.selectSumOfActualTotalAmount(query));
    }
}
