package com.ruoyi.web.dashboard;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

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.withdraw.constants.WithdrawDailyKeys;
import com.ruoyi.bst.withdraw.domain.WithdrawQuery;
import com.ruoyi.bst.withdraw.domain.vo.WithdrawDailyStatVO;
import com.ruoyi.bst.withdraw.service.WithdrawDashboard;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.collection.CollectionUtils;

import io.swagger.annotations.ApiModelProperty;

@RestController
@RequestMapping("/dashboard/withdraw")
public class DashboardWithdrawController extends BaseController {
    
    @Autowired
    private WithdrawDashboard withdrawDashboard;

    
    @ApiModelProperty("获取每日提现统计")
    @PreAuthorize("@ss.hasAnyPermi('dashboard:withdraw:daily')")
    @GetMapping("/dailyWithdraw")
    public AjaxResult getDailyWithdraw(WithdrawQuery query) {
        query.setScope(true);
        List<WithdrawDailyStatVO> list = withdrawDashboard.selectDailyStat(
            query, 
            Arrays.asList(
                WithdrawDailyKeys.CREATE_DATE, 
                WithdrawDailyKeys.WITHDRAW_SERVICE_CHARGE, 
                WithdrawDailyKeys.WITHDRAW_COUNT, 
                WithdrawDailyKeys.WITHDRAW_AMOUNT, 
                WithdrawDailyKeys.WITHDRAW_SUCCESS_AMOUNT
            )
        );

        if (query.getCreateDateRange() != null && query.getCreateDateRange().size() >= 2) {
            list = CollectionUtils.fillVoids(list, WithdrawDailyStatVO::getDate, (date) -> {
                WithdrawDailyStatVO vo = new WithdrawDailyStatVO();
                vo.setDate(date);
                vo.setAmount(BigDecimal.ZERO);
                vo.setSuccessAmount(BigDecimal.ZERO);
                vo.setServiceCharge(BigDecimal.ZERO);
                vo.setCount(0);
                return vo;
            }, query.getCreateDateRange().get(0), query.getCreateDateRange().get(1));
        }

        return success(list);
    }
}
