package com.ruoyi.dashboard.service;

import java.util.ArrayList;
import java.util.List;

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

import com.ruoyi.bst.area.service.AreaDashboard;
import com.ruoyi.bst.areaJoin.service.AreaJoinDashboard;
import com.ruoyi.bst.bonus.domain.vo.BonusDailyStatVO;
import com.ruoyi.bst.bonus.service.BonusDashboard;
import com.ruoyi.bst.bonusRefund.domain.vo.BonusRefundDailyStatVO;
import com.ruoyi.bst.bonusRefund.service.BonusRefundDashboard;
import com.ruoyi.bst.device.service.DeviceDashboard;
import com.ruoyi.bst.fault.service.FaultDashboard;
import com.ruoyi.bst.mchApply.service.MchApplyDashboard;
import com.ruoyi.bst.model.service.ModelDashboard;
import com.ruoyi.bst.order.domain.vo.OrderDailyRefundStatVO;
import com.ruoyi.bst.order.domain.vo.OrderDailyStatVO;
import com.ruoyi.bst.order.service.OrderDashboard;
import com.ruoyi.bst.withdraw.service.WithdrawDashboard;
import com.ruoyi.common.utils.collection.CollectionUtils;
import com.ruoyi.dashboard.domain.dailyStat.DailyStatQuery;
import com.ruoyi.dashboard.domain.dailyStat.DailyStatVO;
import com.ruoyi.dashboard.domain.stat.StatQuery;
import com.ruoyi.dashboard.domain.stat.StatVO;
import com.ruoyi.dashboard.utils.DashboardUtil;
import com.ruoyi.system.user.service.UserDashboard;

@Service
public class DashboardService {

    @Autowired
    private OrderDashboard orderDashboard;

    @Autowired
    private BonusDashboard bonusDashboard;

    @Autowired
    private UserDashboard userDashboard;

    @Autowired
    private DeviceDashboard deviceDashboard;

    @Autowired
    private AreaDashboard areaDashboard;

    @Autowired
    private ModelDashboard modelDashboard;

    @Autowired
    private BonusRefundDashboard bonusRefundDashboard;

    @Autowired
    private AreaJoinDashboard areaJoinDashboard;

    @Autowired
    private WithdrawDashboard withdrawDashboard;

    @Autowired
    private MchApplyDashboard mchApplyDashboard;

    @Autowired
    private FaultDashboard faultDashboard;

    public StatVO selectStat(StatQuery query) {
        StatVO vo = new StatVO();
        vo.setOrder(orderDashboard.selectStat(DashboardUtil.toOrderQuery(query), query.getKeys()));
        vo.setOrderRefund(orderDashboard.selectRefundStat(DashboardUtil.toOrderRefundQuery(query), query.getKeys()));
        vo.setBonus(bonusDashboard.selectStat(DashboardUtil.toBonusQuery(query), query.getKeys()));
        vo.setBonusRefund(bonusRefundDashboard.selectStat(DashboardUtil.toBonusRefundQuery(query), query.getKeys()));
        vo.setUser(userDashboard.selectStat(DashboardUtil.toUserQuery(query), query.getKeys()));
        vo.setUserAgent(userDashboard.selectAgentStat(DashboardUtil.toUserAgentQuery(query), query.getKeys()));
        vo.setDevice(deviceDashboard.selectStat(DashboardUtil.toDeviceQuery(query), query.getKeys()));
        vo.setArea(areaDashboard.selectStat(DashboardUtil.toAreaQuery(query), query.getKeys()));
        vo.setModel(modelDashboard.selectStat(DashboardUtil.toModelQuery(query), query.getKeys()));
        vo.setAreaJoin(areaJoinDashboard.selectStat(DashboardUtil.toAreaJoinQuery(query), query.getKeys()));
        vo.setWithdraw(withdrawDashboard.selectStat(DashboardUtil.toWithdrawQuery(query), query.getKeys()));
        vo.setMchApply(mchApplyDashboard.selectStat(DashboardUtil.toMchApplyQuery(query), query.getKeys()));
        vo.setFault(faultDashboard.selectStat(DashboardUtil.toFaultQuery(query), query.getKeys()));
        return vo;
    }

    public List<DailyStatVO> selectDailyStat(DailyStatQuery query) {
        List<OrderDailyStatVO> orderList = orderDashboard.selectDailyStat(DashboardUtil.toOrderQuery(query), query.getOrderKeys());
        List<OrderDailyRefundStatVO> orderRefundList = orderDashboard.selectDailyRefundStat(DashboardUtil.toOrderRefundQuery(query), query.getOrderRefundKeys());
        List<BonusDailyStatVO> bonusList = bonusDashboard.selectDailyStat(DashboardUtil.toBonusQuery(query), query.getBonusKeys());
        List<BonusRefundDailyStatVO> bonusRefundList = bonusRefundDashboard.selectDailyStat(DashboardUtil.toBonusRefundQuery(query), query.getBonusRefundKeys());

        List<DailyStatVO> result = new ArrayList<>();
        if (query.getDateRange() != null && query.getDateRange().size() >= 2) {
            result = CollectionUtils.fillVoids(result, DailyStatVO::getDate, (date) -> {
                DailyStatVO vo = new DailyStatVO();
                vo.setDate(date);
                // 订单
                OrderDailyStatVO order = orderList.stream()
                        .filter(item -> item.getDate() != null && item.getDate().isEqual(date))
                        .findFirst().orElse(new OrderDailyStatVO());
                vo.setOrder(order);
                // 订单退款
                OrderDailyRefundStatVO orderRefund = orderRefundList.stream()
                        .filter(item -> item.getDate() != null && item.getDate().isEqual(date))
                        .findFirst().orElse(new OrderDailyRefundStatVO());
                vo.setOrderRefund(orderRefund);
                // 分成
                BonusDailyStatVO bonus = bonusList.stream()
                        .filter(item -> item.getDate() != null && item.getDate().isEqual(date))
                        .findFirst().orElse(new BonusDailyStatVO());
                vo.setBonus(bonus);
                // 分成退款
                BonusRefundDailyStatVO bonusRefund = bonusRefundList.stream()
                        .filter(item -> item.getDate() != null && item.getDate().isEqual(date))
                        .findFirst().orElse(new BonusRefundDailyStatVO());
                vo.setBonusRefund(bonusRefund);

                return vo;
            }, query.getDateRange().get(0), query.getDateRange().get(1));
        }

        return result;
    }


}
