package com.ruoyi.web.controller.ss;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.ValidGroup;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.collection.CollectionUtils;
import com.ruoyi.dashboard.service.DashboardService;
import com.ruoyi.ss.balancePeriod.domain.SmBalancePeriodQuery;
import com.ruoyi.ss.balancePeriod.service.ISmBalancePeriodService;
import com.ruoyi.ss.bonus.domain.BonusQuery;
import com.ruoyi.ss.bonus.domain.vo.BonusDailyAmountVO;
import com.ruoyi.ss.bonus.service.BonusService;
import com.ruoyi.ss.businessRecord.service.ISmBusinessRecordService;
import com.ruoyi.dashboard.domain.dto.ServiceIncomeQuery;
import com.ruoyi.ss.transactionBill.domain.TransactionBillQuery;
import com.ruoyi.ss.transactionBill.service.TransactionBillService;
import com.ruoyi.ss.transactionBill.domain.enums.TransactionBillGroupBy;
import com.ruoyi.ss.transactionBill.domain.enums.TransactionBillStatus;
import com.ruoyi.ss.businessRecord.domain.SmBusinessRecordVo;
import com.ruoyi.dashboard.domain.vo.BriefVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
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.Date;
import java.util.List;

/**
 * 报表Controller
 * @author 辉
 * 2024/3/6
 */
@RestController
@RequestMapping("/system/dashboard")
public class SmDashboardController extends BaseController {

    @Autowired
    private TransactionBillService transactionBillService;

    @Autowired
    private ISmBalancePeriodService balancePeriodService;

    @Autowired
    private ISmBusinessRecordService businessRecordService;

    @Autowired
    private DashboardService dashboardService;

    @Autowired
    private BonusService bonusService;

    /**
     * 舆情分析数据
     */
    @GetMapping("/brief")
    public AjaxResult brief() {
        BriefVo brief = businessRecordService.selectBrief();

        Date today = new Date();
        Date startDate = DateUtils.plusDate(today, -12);
        Date endDate = DateUtils.plusDate(today, -1);
        List<SmBusinessRecordVo> history = businessRecordService.selectDailyLatest(startDate, endDate);

        brief.setHistory(history);

        return AjaxResult.success(brief);
    }

    /**
     * 获取账单分时统计数据
     * @param dto 查询条件
     * @return 统计数据
     */
    @GetMapping("/hourBillCount")
    public AjaxResult hourBillCount(@Validated(ValidGroup.Query.class) TransactionBillQuery dto) {
        dto.setGroupBy(TransactionBillGroupBy.create_hour.name());
        dto.setStatusList(TransactionBillStatus.serviceIncome());
        return AjaxResult.success(transactionBillService.selectCount(dto));
    }

    /**
     * 获取余额时段分析
     */
    @ApiOperation("获取余额时段分析")
    @GetMapping("/balancePeriod")
    public AjaxResult balancePeriod(SmBalancePeriodQuery dto) {
        return AjaxResult.success(balancePeriodService.selectSmBalancePeriodList(dto));
    }

    /**
     * 获取待办事项
     */
    @GetMapping("/todoList")
    public AjaxResult getTodoList() {
        return success(dashboardService.getTodoList());
    }

    /**
     * 获取服务费、收入及成本
     */
    @GetMapping("/serviceIncome")
    public AjaxResult getServiceIncome(ServiceIncomeQuery query) {
        return success(dashboardService.getServiceIncome(query));
    }

    /**
     * 按月统计分成
     */
    @GetMapping("/bonusMonthAmount")
    public AjaxResult getBonusMonthAmount(BonusQuery query) {
        return success(bonusService.selectMonthAmount(query));
    }

    /**
     * 按天统计分成
     */
    @GetMapping("/bonusDailyAmount")
    public AjaxResult getBonusDailyAmount(BonusQuery query) {
        List<BonusDailyAmountVO> list = bonusService.selectDailyAmount(query);
        // 若开始和结束日期不为空，则补全缺失的日期
        if (query.getPayDateStart() != null && query.getPayDateEnd() != null) {
            list = CollectionUtils.fillVoids(list, BonusDailyAmountVO::getKey, (date) -> {
                BonusDailyAmountVO vo = new BonusDailyAmountVO();
                vo.setKey(date);
                vo.setSum(BigDecimal.ZERO);
                return vo;
            }, query.getPayDateStart(), query.getPayDateEnd());
        }
        return success(list);
    }

}
