package com.ruoyi.web.dashboard;

import java.util.Arrays;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.bst.bonus.constants.BonusDailyKeys;
import com.ruoyi.bst.bonus.domain.enums.BonusStatus;
import com.ruoyi.bst.bonusRefund.constants.BonusRefundDailyKeys;
import com.ruoyi.bst.order.constants.OrderDailyKeys;
import com.ruoyi.bst.order.domain.enums.OrderStatus;
import com.ruoyi.bst.refund.domain.enums.RefundStatus;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.dashboard.domain.dailySettleStat.DailySettleStatQuery;
import com.ruoyi.dashboard.domain.dailyStat.DailyStatQuery;
import com.ruoyi.dashboard.domain.platformIncomeDaily.PlatformDailyIncomeQuery;
import com.ruoyi.dashboard.domain.revenueStat.RevenueStatQuery;
import com.ruoyi.dashboard.domain.stat.StatQuery;
import com.ruoyi.dashboard.service.DashboardService;

import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/dashboard")
public class DashboardController extends BaseController {

    @Autowired
    private DashboardService dashboardService;

    @ApiOperation("获取统计数据（即将弃用）")
    @PreAuthorize("@ss.hasAnyPermi('dashboard:stat')")
    @GetMapping("/stat")
    public AjaxResult getStat(StatQuery query) {
        query.setScope(true);
        query.setOrderStatusList(OrderStatus.valid());
        query.setBonusStatusList(BonusStatus.valid());
        query.setRefundStatusList(RefundStatus.valid());
        return success(dashboardService.selectStat(query));
    }

    @ApiOperation("获取每日金额统计数据")
    @PreAuthorize("@ss.hasAnyPermi('dashboard:dailyAmount')")
    @GetMapping("/dailyAmount")
    public AjaxResult getDailyAmount(DailyStatQuery query) {
        query.setScope(true);
        query.setOrderStatusList(OrderStatus.valid());
        query.setRefundStatusList(RefundStatus.valid());
        query.setBonusStatusList(BonusStatus.valid());

        query.setOrderKeys(
                Arrays.asList(OrderDailyKeys.CREATE_DATE, OrderDailyKeys.PAY_AMOUNT, OrderDailyKeys.ORDER_COUNT));
        query.setBonusKeys(Arrays.asList(BonusDailyKeys.CREATE_DATE, BonusDailyKeys.BONUS_AMOUNT));
        query.setOrderRefundKeys(Arrays.asList(OrderDailyKeys.REFUND_CREATE_DATE, OrderDailyKeys.REFUND_AMOUNT));
        query.setBonusRefundKeys(
                Arrays.asList(BonusRefundDailyKeys.CREATE_DATE, BonusRefundDailyKeys.BONUS_REFUND_AMOUNT));

        return success(dashboardService.selectDailyStat(query));
    }

    @ApiOperation("获取每日结算金额统计数据")
    @PreAuthorize("@ss.hasAnyPermi('dashboard:dailySettleAmount')")
    @GetMapping("/dailySettleAmount")
    public AjaxResult getDailySettleAmount(DailySettleStatQuery query) {
        query.setScope(true);
        return success(dashboardService.selectDailySettleStat(query));
    }

    @ApiOperation("获取商户营收统计")
    @PreAuthorize("@ss.hasAnyPermi('dashboard:revenue')")
    @GetMapping("/revenueStat")
    public AjaxResult getRevenueStat(RevenueStatQuery query) {
        query.setScope(true);
        return success(dashboardService.selectRevenueStat(query));
    }

    @ApiOperation("获取平台每日收益统计")
    @PreAuthorize("@ss.hasAnyPermi('dashboard:platformDailyIncome')")
    @GetMapping("/platformDailyIncome")
    public AjaxResult getPlatformDailyIncome(PlatformDailyIncomeQuery query) {
        return success(dashboardService.selectPlatformDailyIncome(query));
    }

}
