package com.ssm.controller;

import com.github.pagehelper.PageInfo;
import com.ssm.core.bean.GenericResponse;
import com.ssm.core.bean.PageResponse;
import com.ssm.core.util.SessionHelp;
import com.ssm.manage.entity.SysRole;
import com.ssm.manage.entity.SysRoleUser;
import com.ssm.manage.query.RolePowerRequest;
import com.ssm.manage.query.RoleRequest;
import com.ssm.manage.query.RoleUserRequest;
import com.ssm.manage.response.ResponseCode;
import com.ssm.manage.service.SsmSysRolePowerService;
import com.ssm.manage.service.SsmSysRoleService;
import com.ssm.manage.service.SsmSysRoleUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Api(value="角色controller",tags={"角色相关接口"})
@RestController
@Slf4j
@RequestMapping("/demay/ssm/role")
public class SsmSysRoleController {
   @Autowired
   private SsmSysRoleService ssmSysRoleService;
   @Autowired
   private SsmSysRolePowerService ssmSysRolePowerService;
   @Autowired
   private SsmSysRoleUserService ssmSysRoleUserService;

    @ApiOperation(value = "分页获取角色信息  平台端-角色-分页获取角色信息", httpMethod = "POST", response = PageResponse.class, notes = "分页获取菜单信息")
    @RequestMapping(value = "/page", method = RequestMethod.POST)
    public PageResponse<SysRole> selectByPage(@RequestBody RoleRequest roleRequest) {
        PageResponse<SysRole> pageResponse = new PageResponse<>();
        PageInfo<SysRole> pageInfo;
        try {
            pageInfo = ssmSysRoleService.selectPage(roleRequest);
        } catch (Exception e) {
            log.error("SsmSysRoleController.selectPage:", e);
            pageResponse.setCodeAndMsg(ResponseCode.FAILED);
            return pageResponse;
        }
        pageResponse.setCodeAndMsg(ResponseCode.OK);
        pageResponse.setPageInfo(pageInfo);
        return pageResponse;
    }


    @ApiOperation(value = "用户关联的角色list  平台端-角色-查看角色关联的用户", httpMethod = "POST", response = GenericResponse.class, notes = "")
    @RequestMapping(value = "/roleUserList", method = RequestMethod.POST)
    public GenericResponse roleUserList(@RequestBody RoleUserRequest roleUserRequest) {
        GenericResponse genericResponse = new GenericResponse();

//        if (roleUserRequest.getUserId() == null){
//            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S,"用户id");
//        }
        List<SysRoleUser> list = new ArrayList<>();
        try {
            list = ssmSysRoleUserService.roleUserList(roleUserRequest);
        } catch (Exception e) {
            log.error("SsmSysRoleController.roleUserList:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }

        genericResponse.setCodeAndMsg(ResponseCode.OK);
        genericResponse.setData(list);
        return genericResponse;
    }

}
