package com.qmth.paper.library.api;

import com.qmth.boot.api.constant.ApiConstant;
import com.qmth.paper.library.common.bean.params.SysRoleGroupMemberParams;
import com.qmth.paper.library.common.contant.ApiPrefixConstant;
import com.qmth.paper.library.common.contant.SystemConstant;
import com.qmth.paper.library.common.entity.SysRoleGroupMember;
import com.qmth.paper.library.common.service.SysRoleGroupMemberService;
import com.qmth.paper.library.common.util.Result;
import com.qmth.paper.library.common.util.ResultUtil;
import io.swagger.annotations.*;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * <p>
 * 角色组成员表 前端控制器
 * </p>
 */
@Api(tags = "角色组成员Controller")
@RestController
@RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + ApiPrefixConstant.PREFIX_SYS + "/role/group")
@Validated
public class SysRoleGroupMemberController {

    @Resource
    SysRoleGroupMemberService sysRoleGroupMemberService;

    @ApiOperation(value = "新增/修改")
    @ApiResponses({@ApiResponse(code = 200, message = "常规信息", response = ResultUtil.class)})
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Result save(@Valid @RequestBody SysRoleGroupMemberParams roleGroupMemberParams, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultUtil.error(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        return ResultUtil.ok(sysRoleGroupMemberService.saveOrUpdate(roleGroupMemberParams));
    }

    @ApiOperation(value = "成员管理查询")
    @ApiResponses({@ApiResponse(code = 200, message = "角色组成员信息", response = SysRoleGroupMember.class)})
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public Result list(@ApiParam(value = "角色id", required = true) @RequestParam String roleId,
                       @ApiParam(value = "成员姓名", required = false) @RequestParam(required = false) String name) {
        return ResultUtil.ok(sysRoleGroupMemberService.findByRoleIdAndName(SystemConstant.convertIdToLong(roleId), name));
    }
}
