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.User;
import com.ruoyi.cai.dto.admin.query.ResetPasswordReq;
import com.ruoyi.cai.dto.admin.query.UserUpdateAdminReq;
import com.ruoyi.cai.dto.admin.vo.UserAdminVo;
import com.ruoyi.cai.dto.admin.vo.UserFullAdminVo;
import com.ruoyi.cai.manager.UserAdminManager;
import com.ruoyi.cai.service.UserService;
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-19
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/cai/user")
public class UserController extends BaseController {

    private final UserService userService;
    private final UserAdminManager userAdminManager;

    /**
     * 查询用户列表
     */
    @SaCheckPermission("cai:user:list")
    @GetMapping("/list")
    public TableDataInfo<UserAdminVo> list(User query, PageQuery pageQuery) {
        Page<UserAdminVo> result = userService.pageAdmin(pageQuery, query);
        return TableDataInfo.build(result);
    }

    /**
     * 获取用户详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("cai:user:query")
    @GetMapping("/{id}")
    public R<User> getInfo(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        return R.ok(userService.getById(id));
    }

    @SaCheckPermission("cai:user:query")
    @GetMapping("/full/{id}")
    public R<UserFullAdminVo> getFullUser(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        return R.ok(userAdminManager.getUser(id));
    }

    /**
     * 新增用户
     */
    @SaCheckPermission("cai:user:add")
    @Log(title = "用户", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody User bo) {
        return toAjax(userService.save(bo));
    }

    /**
     * 修改用户
     */
    @SaCheckPermission("cai:user:edit")
    @Log(title = "用户", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody UserUpdateAdminReq bo) {
        return toAjax(userAdminManager.updateUser(bo));
    }

    @PostMapping("/resetPwd")
    public R<Void> resetPwd(@RequestBody ResetPasswordReq req) {
        User user = userService.getById(req.getUserId());
        userService.resetPassword(user.getMobile(),user.getPassword());
        return R.ok();
    }

    /**
     * 删除用户
     *
     * @param ids 主键串
     */
    @SaCheckPermission("cai:user:remove")
    @Log(title = "用户", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Integer[] ids) {
        return toAjax(userService.removeBatchByIds(Arrays.asList(ids)));
    }
}
