package com.xunmei.system.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.xunmei.common.log.annotation.Log;
import com.xunmei.common.log.enums.BusinessType;
import com.xunmei.common.security.annotation.RequiresPermissions;
import com.xunmei.system.domain.SysUserRole;
import com.xunmei.system.service.ISysUserRoleService;
import com.xunmei.common.core.web.controller.BaseController;
import com.xunmei.common.core.web.domain.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xunmei.common.core.web.page.TableDataInfo;

/**
 * 用户和角色关联Controller
 *
 * @author xunmei
 * @date 2023-08-10
 */
@Api(tags = {"SysUserRole" })
@RestController
@RequestMapping("/userRole")
public class SysUserRoleController extends BaseController {
    @Autowired
    private ISysUserRoleService sysUserRoleService;

/**
 * 查询用户和角色关联列表
 */
@ApiOperation(value = "查询SysUserRole列表")
@RequiresPermissions("system:role:list")
@GetMapping("/list")
    public TableDataInfo<SysUserRole> list(SysUserRole sysUserRole) {

        return sysUserRoleService.selectPage( sysUserRole);
    }


    /**
     * 获取用户和角色关联详细信息
     */
    @ApiOperation(value = "获取SysUserRole详细信息")
    @RequiresPermissions("system:role:query")
    @GetMapping(value = "/{userId}")
    public AjaxResult getInfo(@PathVariable("userId") Long userId) {
        return success(sysUserRoleService.selectSysUserRoleByUserId(userId));
    }

    /**
     * 新增用户和角色关联
     */
    @ApiOperation(value = "新增SysUserRole")
    @RequiresPermissions("system:role:add")
    @Log(title = "用户和角色关联" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysUserRole sysUserRole) {
        return toAjax(sysUserRoleService.insertSysUserRole(sysUserRole));
    }

    /**
     * 修改用户和角色关联
     */
    @ApiOperation(value = "修改SysUserRole")
    @RequiresPermissions("system:role:edit")
    @Log(title = "用户和角色关联" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysUserRole sysUserRole) {
        return toAjax(sysUserRoleService.updateSysUserRole(sysUserRole));
    }

    /**
     * 删除用户和角色关联
     */
    @ApiOperation(value = "删除SysUserRole")
    @RequiresPermissions("system:role:remove")
    @Log(title = "用户和角色关联" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{userIds}")
    public AjaxResult remove(@PathVariable Long[] userIds) {
        return toAjax(sysUserRoleService.deleteSysUserRoleByUserIds(userIds));
    }
}
