package com.ruoyi.web.dashboard;

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.bonus.constants.BonusDailyKeys;
import com.ruoyi.bst.bonus.domain.BonusQuery;
import com.ruoyi.bst.bonus.domain.enums.BonusStatus;
import com.ruoyi.bst.bonus.domain.vo.BonusDailyStatVO;
import com.ruoyi.bst.bonus.service.BonusDashboard;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.collection.CollectionUtils;

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

    @Autowired
    private BonusDashboard bonusDashboard;

    @PreAuthorize("@ss.hasPermi('dashboard:bonus:dailyAmount')")
    @GetMapping("/dailyAmount")
    public AjaxResult getDailyAmount(BonusQuery query) {
        query.setScope(true);
        query.setStatusList(BonusStatus.valid());
        List<String> keys = Arrays.asList(BonusDailyKeys.CREATE_DATE, BonusDailyKeys.BONUS_AMOUNT);
        List<BonusDailyStatVO> list = bonusDashboard.selectDailyStat(query, keys);
        if (query.getCreateDateRange() != null && query.getCreateDateRange().size() >= 2) {
            list = CollectionUtils.fillVoids(list, BonusDailyStatVO::getDate, (date) -> {
                BonusDailyStatVO vo = new BonusDailyStatVO();
                vo.setDate(date);
                return vo;
            }, query.getCreateDateRange().get(0), query.getCreateDateRange().get(1));
        }
        return success(list);
    }
    
}
