package com.kcim.web;

import com.kcim.common.util.PageUtils;
import com.kcim.common.util.Result;
import com.kcim.dao.model.dto.RoleEditDTO;
import com.kcim.dao.model.dto.RoleMenuDTO;
import com.kcim.dao.model.dto.RoleSaveDTO;
import com.kcim.dao.model.dto.RoleUserDTO;
import com.kcim.vo.CommonVO;
import com.kcim.service.RoleMenuService;
import com.kcim.service.RoleService;
import com.kcim.service.UserRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;


@Api(tags = "角色管理相关")
@RestController
@RequestMapping("/role/")
public class RoleController extends AbstractController {

    private final RoleService roleService;
    private final RoleMenuService roleMenuService;
    private final UserRoleService userRoleService;

    public RoleController(RoleService roleService, RoleMenuService roleMenuService, UserRoleService userRoleService) {
        this.roleService = roleService;
        this.roleMenuService = roleMenuService;
        this.userRoleService = userRoleService;
    }


    @ApiOperation("新增一个角色")
    @PostMapping("/save")
    public Result save(@RequestBody @Valid RoleSaveDTO roleSaveDTO) {
        roleService.saveRole(roleSaveDTO);
        return Result.ok();
    }


    @ApiOperation("查询角色列表")
    @GetMapping("/list")
    public Result list(@RequestParam(defaultValue = "1", value = "current") Integer page,
                       @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                       @RequestParam(value = "roleName", required = false) String keyword,
                       @RequestParam(value = "startDate", required = false) String startDate,
                       @RequestParam(value = "endDate", required = false) String endDate
                       ) {
        PageUtils pageUtils = roleService.selectList(page, pageSize, keyword, startDate,endDate);
        return Result.ok(pageUtils);
    }

    @ApiOperation("编辑某个角色")
    @PostMapping("/edit")
    public Result update(@RequestBody @Valid RoleEditDTO roleEditDTO) {
        roleService.updateRole(roleEditDTO, getUserId());
        return Result.ok();
    }

    @ApiOperation("删除")
    @PostMapping("/delete")
    public Result delete(@RequestBody Long[] roleIds) {
        roleService.deleteByIds(Arrays.asList(roleIds));
        return Result.ok();
    }

    @ApiOperation("角色对应的人员列表")
    @GetMapping("/roleUsers")
    public Result roleUsers(@RequestParam Long roleId) {
        List<CommonVO> roleUsers = userRoleService.getUsersByRoleId(roleId, getHospId());
        return Result.ok(roleUsers);
    }

    @ApiOperation("角色对应的菜单列表")
    @GetMapping("/roleMenus")
    public Result roleMenus(@RequestParam Long roleId) {
        List<CommonVO> roleUsers = roleMenuService.getMenuByRoleId(roleId, getUser().getHospId());
        return Result.ok(roleUsers);
    }

    @ApiOperation("绑定角色和人员关系时选择列表（可回显）")
    @GetMapping("/roleUserList")
    public Result roleUserList(@RequestParam Long roleId) {
        List<?> list = userRoleService.roleUserList(roleId, getUser().getHospId());
        return Result.ok(list);
    }

    @ApiOperation("编辑角色和人员关系")
    @PostMapping("/editUserRole")
    public Result saveRoleUser(@RequestBody @Valid RoleUserDTO roleUserDTO) {
        userRoleService.saveRoleUser(roleUserDTO, getUser().getHospId());
        return Result.ok();
    }

    @ApiOperation("绑定角色和菜单关系时选择列表可回显")
    @GetMapping("/roleMenuList")
    public Result roleMenuList(@RequestParam Long roleId) {
        List<CommonVO> list = roleMenuService.roleMenuList(roleId, getUser().getHospId());
        return Result.ok(list);
    }

    @ApiOperation("编辑角色和菜单关系")
    @PostMapping("/editRoleMenu")
    public Result saveRoleMenu(@RequestBody @Valid RoleMenuDTO roleMenuDTO) {
        roleMenuService.saveRoleMenu(roleMenuDTO, getUser().getHospId());
        return Result.ok();
    }
}
