package com.ruoyi.cai.controller.admin;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.cai.domain.DayIncomeStatistics;
import com.ruoyi.cai.service.DayIncomeStatisticsService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.time.LocalDate;

/**
 * 每日账单统计
 *
 * @author 77
 * @date 2024-01-22
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/cai/dayIncomeStatistics")
public class DayIncomeStatisticsController extends BaseController {

    private final DayIncomeStatisticsService dayIncomeStatisticsService;

    /**
     * 查询每日账单统计列表
     */
    @SaCheckPermission("cai:dayIncomeStatistics:list")
    @GetMapping("/list")
    public TableDataInfo<DayIncomeStatistics> list(DayIncomeStatistics bo, PageQuery pageQuery) {
        Page<DayIncomeStatistics> page = dayIncomeStatisticsService.page(pageQuery.build(),
                Wrappers.lambdaQuery(bo).orderByDesc(DayIncomeStatistics::getDate));
        return TableDataInfo.build(page);
    }

    /**
     * 获取每日账单统计详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("cai:dayIncomeStatistics:query")
    @GetMapping("/{id}")
    public R<DayIncomeStatistics> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(dayIncomeStatisticsService.getById(id));
    }

    @SaCheckPermission("cai:dayIncomeStatistics:edit")
    @Log(title = "刷新单天统计", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/refreshToday")
    public R<Void> refreshToday() {
        dayIncomeStatisticsService.refreshByDate(LocalDate.now());
        return R.ok();
    }

    @SaCheckPermission("cai:dayIncomeStatistics:edit")
    @Log(title = "刷新昨日统计", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/refreshLastDay")
    public R<Void> refreshLastDay() {
        dayIncomeStatisticsService.refreshByDate(LocalDate.now().plusDays(-1));
        return R.ok();
    }

}
