package cn.reghao.autodop.dmaster.rbac.controller;

import cn.reghao.jdkutil.result.WebBody;
import cn.reghao.autodop.dmaster.rbac.model.dto.UserInfo;
import cn.reghao.autodop.dmaster.rbac.model.dto.UserRole;
import cn.reghao.autodop.dmaster.rbac.model.po.UserAuthority;
import cn.reghao.autodop.dmaster.rbac.model.po.Role;
import cn.reghao.autodop.dmaster.rbac.model.po.User;
import cn.reghao.autodop.dmaster.rbac.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author reghao
 * @date 2021-04-04 21:24:18
 */
@Api(tags = "用户接口")
@RequestMapping("/api/rbac/user")
@RestController
public class UserController {
    private UserService userService;

    public UserController(UserService userService) {
        this.userService = userService;
    }

    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @ApiOperation(value = "创建用户")
    @PostMapping(produces = MediaType.APPLICATION_JSON_VALUE)
    public String createUser(@Validated User user, @RequestParam("roles") Set<Role> roles) {
        Set<UserAuthority> authorities = roles.stream()
                .map(role -> new UserAuthority(role.getTitle()))
                .collect(Collectors.toSet());
        user.setAuthorities(authorities);
        userService.createUser(user);
        return WebBody.success();
    }

    @ApiOperation(value = "修改用户信息")
    @PostMapping(value = "/modify", produces = MediaType.APPLICATION_JSON_VALUE)
    public String modifyUserInfo(@Validated UserInfo userInfo) {
        userService.modifyUserInfo(userInfo);
        return WebBody.success();
    }

    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @ApiOperation(value = "删除用户")
    @DeleteMapping(value = "/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    public String deleteUser(@PathVariable("id") Integer userId) {
        userService.deleteUser(userId);
        return WebBody.success();
    }

    @ApiOperation(value = "修改用户密码")
    @PostMapping(value = "/passwd", produces = MediaType.APPLICATION_JSON_VALUE)
    public String modifyPassword(@NotNull Integer id, @NotNull String newPassword) {
        userService.modifyUserPassword(id, newPassword);
        return WebBody.success();
    }

    @ApiOperation(value = "分配用户角色")
    @PostMapping(value = "/role", produces = MediaType.APPLICATION_JSON_VALUE)
    public String assignRole(@Validated UserRole userRole) {
        userService.setUserRoles(userRole);
        return WebBody.success();
    }

    // TODO 暂不启用本功能
    @ApiOperation(value = "启用/禁用用户")
    @PostMapping(value = "/status/{enable}", produces = MediaType.APPLICATION_JSON_VALUE)
    public String setUserStatus(@PathVariable("enable") Boolean enable,
                                @RequestParam(value = "ids") List<Integer> userIds) {
        userIds.forEach(userId -> userService.setUserStatus(userId, enable));
        return WebBody.success();
    }
}
