package com.atguigu.ssyx.acl.controller;


import com.atguigu.ssyx.acl.service.AdminService;
import com.atguigu.ssyx.acl.service.RoleService;
import com.atguigu.ssyx.common.result.Result;
import com.atguigu.ssyx.common.utils.MD5;
import com.atguigu.ssyx.model.acl.Admin;
import com.atguigu.ssyx.vo.acl.AdminQueryVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * ClassName: AdminController
 * Package: com.atguigu.ssyx.acl.controller
 * 用户管理 前端控制器
 *
 * @author yovinchen
 * @Create 2023/9/14 10:38
 */
@RestController
@RequestMapping("/admin/acl/user")
@Api(tags = "用户管理")
public class AdminController {

    @Autowired
    private AdminService adminService;

    @Autowired
    private RoleService roleService;

    @ApiOperation(value = "获取管理用户分页列表")
    @GetMapping("{page}/{limit}")
    public Result index(@ApiParam(name = "page", value = "当前页码", required = true) @PathVariable Long page,
                        @ApiParam(name = "limit", value = "每页记录数", required = true) @PathVariable Long limit,
                        @ApiParam(name = "userQueryVo", value = "查询对象", required = false) AdminQueryVo userQueryVo) {
        try {
            Page<Admin> pageParam = new Page<>(page, limit);
            IPage<Admin> pageModel = adminService.selectPage(pageParam, userQueryVo);
            return Result.ok(pageModel);
        } catch (Exception e) {
            throw new RuntimeException("获取管理用户分页列表异常", e);
        }
    }

    @ApiOperation(value = "获取管理用户")
    @GetMapping("get/{id}")
    public Result get(@PathVariable Long id) {
        try {
            Admin user = adminService.getById(id);
            return Result.ok(user);
        } catch (Exception e) {
            throw new RuntimeException("获取管理用户异常", e);
        }

    }

    @ApiOperation(value = "新增管理用户")
    @PostMapping("save")
    public Result save(@RequestBody Admin user) {
        try {
            //对密码进行MD5处理
            user.setPassword(MD5.encrypt(user.getPassword()));
            boolean result = adminService.save(user);
            return result ? Result.ok(null) : Result.fail(null);
        } catch (Exception e) {
            throw new RuntimeException("新增管理用户异常", e);
        }
    }

    @ApiOperation(value = "修改管理用户")
    @PutMapping("update")
    public Result updateById(@RequestBody Admin user) {
        try {
            boolean result = adminService.updateById(user);
            return result ? Result.ok(null) : Result.fail(null);
        } catch (Exception e) {
            throw new RuntimeException("修改管理用户异常", e);
        }
    }

    @ApiOperation(value = "删除管理用户")
    @DeleteMapping("remove/{id}")
    public Result remove(@PathVariable Long id) {
        try {
            boolean result = adminService.removeById(id);
            return result ? Result.ok(null) : Result.fail(null);
        } catch (Exception e) {
            throw new RuntimeException("删除管理用户异常", e);
        }
    }

    @ApiOperation(value = "根据id列表删除管理用户")
    @DeleteMapping("batchRemove")
    public Result batchRemove(@RequestBody List<Long> idList) {
        try {
            boolean result = adminService.removeByIds(idList);
            return result ? Result.ok(null) : Result.fail(null);
        } catch (Exception e) {
            throw new RuntimeException("根据id列表删除管理用户异常", e);
        }
    }

    @ApiOperation(value = "根据用户获取角色数据")
    @GetMapping("/toAssign/{adminId}")
    public Result toAssign(@PathVariable Long adminId) {
        try {
            Map<String, Object> roleMap = roleService.findRoleByUserId(adminId);
            return StringUtils.isNotEmpty(roleMap.toString()) ? Result.ok(roleMap) : Result.fail(roleMap);
        } catch (Exception e) {
            throw new RuntimeException("根据用户获取角色数据异常", e);
        }
    }

    @ApiOperation(value = "根据用户分配角色")
    @PostMapping("/doAssign")
    public Result doAssign(@RequestParam Long adminId, @RequestParam Long[] roleId) {
        try {
            roleService.saveUserRoleRealtionShip(adminId, roleId);
            return Result.ok(null);
        } catch (Exception e) {
            throw new RuntimeException("根据用户分配角色异常", e);
        }
    }
}
