package com.ruoyi.cai.controller.admin;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.cai.domain.UserCount;
import com.ruoyi.cai.dto.admin.vo.UserCountAdminVo;
import com.ruoyi.cai.service.UserCountService;
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.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;

/**
 * 用户信息数量统计
 *
 * @author 77
 * @date 2023-12-24
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/cai/userCount")
public class UserCountController extends BaseController {

    private final UserCountService userCountService;

    /**
     * 查询用户信息数量统计列表
     */
    @SaCheckPermission("cai:userCount:list")
    @GetMapping("/list")
    public TableDataInfo<UserCountAdminVo> list(UserCountAdminVo bo, PageQuery pageQuery) {
        Page<UserCountAdminVo> page = userCountService.pageAdmin(pageQuery, bo);
        return TableDataInfo.build(page);
    }

    @GetMapping("/reset")
    @SaCheckPermission("cai:userCount:edit")
    @RepeatSubmit()
    @Log(title = "用户信息数量统计", businessType = BusinessType.UPDATE)
    public R<Void> reset(Long userId) {
        userCountService.reset(userId);
        return R.ok();
    }


    /**
     * 获取用户信息数量统计详细信息
     *
     * @param userId 主键
     */
    @SaCheckPermission("cai:userCount:query")
    @GetMapping("/{userId}")
    public R<UserCount> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Integer userId) {
        return R.ok(userCountService.getById(userId));
    }

}
