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

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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.OrderDailyRefundStatVO;
import com.ruoyi.bst.order.domain.vo.OrderDailyStatVO;
import com.ruoyi.bst.order.domain.vo.OrderRankVO;
import com.ruoyi.bst.order.domain.vo.OrderRefundStatVO;
import com.ruoyi.bst.order.domain.vo.OrderStatVO;
import com.ruoyi.bst.order.mapper.OrderMapper;
import com.ruoyi.bst.order.service.OrderDashboard;
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;

@Service
public class OrderDashboardImpl implements OrderDashboard {

    @Autowired
    private OrderMapper orderMapper;

    @Override
    public OrderStatVO selectStat(OrderQuery query, List<String> keys) {
        OrderStatVO vo = new OrderStatVO();
        // 订单用户数量
        if (keys.contains(StatKeys.ORDER_USER_COUNT)) {
            vo.setUserCount(selectUserCount(query));
        }
        // 订单数量
        if (keys.contains(StatKeys.ORDER_COUNT)) {
            vo.setCount(selectCount(query));
        }
        // 订单支付金额
        if (keys.contains(StatKeys.ORDER_PAY_AMOUNT)) {
            vo.setPayAmount(selectPayAmount(query));
        }
        // 待审核的订单数量
        if (keys.contains(StatKeys.ORDER_WAIT_VERIFY_COUNT)) {
            vo.setWaitVerifyCount(selectWaitVerifyCount(query));
        }
        // 订单状态数量
        if (keys.contains(StatKeys.ORDER_STATUS_COUNT)) {
            vo.setStatusCount(selectStatusCount(query));
        }
        return vo;
    }

    @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 Integer selectWaitVerifyCount(OrderQuery query) {
        OrderQuery waitVerifyQuery = new OrderQuery();
        BeanUtils.copyProperties(query, waitVerifyQuery);
        waitVerifyQuery.setStatus(OrderStatus.WAIT_VERIFY.getCode());
        return MathUtils.dv(orderMapper.selectCount(waitVerifyQuery));
    }

    @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(OrderRefundQuery 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 selectSumOfActualRiddingFee(OrderQuery query) {
        return MathUtils.dv(orderMapper.selectSumOfActualRiddingFee(query));
    }

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

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

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

    @Override
    public int selectUnpaidCountByUserId(Long userId) {
        if (userId == null) {
            return 0;
        }
        OrderQuery query = new OrderQuery();
        query.setUserId(userId);
        query.setStatus(OrderStatus.RIDE_WAIT_PAY.getCode());
        return this.selectCount(query);
    }
}
