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.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
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.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;

/**
 * 用户信息数量统计
 *
 * @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")
    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));
    }

    /**
     * 新增用户信息数量统计
     */
    @SaCheckPermission("cai:userCount:add")
    @Log(title = "用户信息数量统计", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody UserCount bo) {
        return toAjax(userCountService.save(bo));
    }

    /**
     * 修改用户信息数量统计
     */
    @SaCheckPermission("cai:userCount:edit")
    @Log(title = "用户信息数量统计", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody UserCount bo) {
        return toAjax(userCountService.updateById(bo));
    }

    /**
     * 删除用户信息数量统计
     *
     * @param userIds 主键串
     */
    @SaCheckPermission("cai:userCount:remove")
    @Log(title = "用户信息数量统计", businessType = BusinessType.DELETE)
    @DeleteMapping("/{userIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Integer[] userIds) {
        return toAjax(userCountService.removeBatchByIds(Arrays.asList(userIds), true));
    }
}
