package com.corewing.app.modules.admin.biz;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.corewing.app.common.Result;
import com.corewing.app.dto.biz.user.BizUserBatchDeleteRequest;
import com.corewing.app.dto.biz.user.BizUserIdRequest;
import com.corewing.app.dto.biz.user.BizUserStatusRequest;
import com.corewing.app.dto.biz.user.ResetPasswordRequest;
import com.corewing.app.entity.User;
import com.corewing.app.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/biz/user")
public class BizUserController {

    @Resource
    private UserService userService;

    /**
     * 跳转到用户管理页面
     * @return 页面名称
     */
    @GetMapping("/index")
    public String index() {
        return "admin/biz/user/index";
    }

    /**
     * 获取分页信息
     * @param user 用户搜索对象
     * @return 用户信息集合
     */
    @GetMapping("/page")
    @ResponseBody
    public Result<Page<User>> page(User user) {
        Page<User> sysUserPage =  userService.page(user);
        return Result.success(sysUserPage);
    }

    /**
     * 修改密码
     * @param resetPasswordRequest 修改密码DTO
     * @return 成功 or 失败
     */
    @PutMapping("/resetPassword")
    @ResponseBody
    public Result<String> resetPassword(@RequestBody ResetPasswordRequest resetPasswordRequest) {
        boolean flag = userService.resetPassword(resetPasswordRequest);
        return Result.isBoolAsMsg(flag, "修改密码成功");
    }

    /**
     * 新增用户
     * @param user
     * @return
     */
    @PostMapping("/save")
    @ResponseBody
    public Result<String> save(@RequestBody User user) {
        return Result.isBool(userService.save(user));
    }

    /**
     * 修改用户
     * @param user
     * @return
     */
    @PostMapping("/update")
    @ResponseBody
    public Result<String> update(@RequestBody User user) {
        return Result.isBool(userService.update(user));
    }

    /**
     * 单删用户
     * @param bizUserIdRequest
     * @return
     */
    @PostMapping("/delete")
    @ResponseBody
    public Result<String> delete(@RequestBody BizUserIdRequest bizUserIdRequest) {
        return Result.isBool(userService.removeById(bizUserIdRequest.getId()));
    }

    /**
     * 批量删除用户
     * @return
     */
    @PostMapping("/batchDelete")
    @ResponseBody
    public Result<String> batchDelete(@RequestBody BizUserBatchDeleteRequest bizUserBatchDeleteRequest) {
        return Result.isBool(userService.removeByIds(bizUserBatchDeleteRequest.getIds()));
    }

    @PostMapping("/batchStatus")
    @ResponseBody
    public Result<String> batchStatus(@RequestBody BizUserStatusRequest bizUserStatusRequests) {
        return Result.isBool(userService.batchStatus(bizUserStatusRequests));
    }

}
