package com.ruoyi.web.controller.app;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.domain.vo.LocalDateDecimalVO;
import com.ruoyi.common.utils.collection.CollectionUtils;
import com.ruoyi.dashboard.service.DashboardService;
import com.ruoyi.ss.bonus.domain.BonusQuery;
import com.ruoyi.ss.bonus.domain.enums.BonusArrivalType;
import com.ruoyi.ss.bonus.domain.vo.BonusDailyAmountVO;
import com.ruoyi.ss.bonus.service.BonusService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

/**
 * @author wjh
 * 2024/11/28
 */
@RestController
@RequestMapping("/app/dashboard")
public class AppDashboardController extends BaseController {

    @Autowired
    private BonusService bonusService;

    @ApiOperation("按日查询本人分成列表")
    @GetMapping("/bonusDailyAmount")
    public AjaxResult bonusDailyAmount(BonusQuery query) {
        query.setArrivalId(getUserId());
        query.setArrivalTypes(BonusArrivalType.userList());
        List<LocalDateDecimalVO> list = bonusService.selectDailyPayedAmount(query);
        list = list.stream().filter(item -> Objects.nonNull(item.getKey())).collect(Collectors.toList());
        if (query.getPayDateStart() != null && query.getPayDateEnd() != null) {
            list = CollectionUtils.fillVoids(list, LocalDateDecimalVO::getKey, (date) -> {
                LocalDateDecimalVO vo = new LocalDateDecimalVO();
                vo.setKey(date);
                vo.setValue(BigDecimal.ZERO);
                return vo;
            }, query.getPayDateStart(), query.getPayDateEnd());
        }
        AjaxResult ajax = AjaxResult.success(list);

        BonusQuery totalQuery = new BonusQuery();
        totalQuery.setArrivalId(getUserId());
        totalQuery.setArrivalTypes(BonusArrivalType.userList());
        ajax.put("total", bonusService.selectSumOfPayedAmount(totalQuery));

        return ajax;
    }

}
