package com.css.simulation.resource.server.adapter.controller.system_manage.account_manage;

import api.common.pojo.common.ResponseBodyVO;
import api.common.pojo.param.system.RoleParam;
import api.common.pojo.po.system.RolePO;
import api.common.pojo.vo.system.RoleVO;
import api.common.util.ObjectUtil;
import com.css.simulation.resource.server.app.service.RoleService;
import com.css.simulation.resource.server.domain.service.UserDomainService;
import com.css.simulation.resource.server.infra.util.AuthUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 角色管理
 */
@RestController
@RequestMapping("/role")
public class RoleController {

    @Resource
    private RoleService roleService;
    @Resource
    private UserDomainService userDomainService;


    @RequestMapping("/isAdmin")
    public ResponseBodyVO<Boolean> isAdmin() {
        return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS, userDomainService.isAdmin(AuthUtil.getCreateUserId(), AuthUtil.getCurrentUserRoleCode()));
    }

    /**
     * 获取角色列表
     */
    @RequestMapping("/getRolePageList")
    public ResponseBodyVO<PageInfo<RoleVO>> getRolePageList(@RequestBody RoleParam pageParam) {
        ResponseBodyVO<PageInfo<RoleVO>> response = new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS);
        response.setInfo(roleService.getRolePageList(pageParam));
        return response;
    }

    /**
     * 新增、修改
     */
    @RequestMapping("/saveRole")
    public ResponseBodyVO<RolePO> saveRole(@RequestBody RoleVO roleVO) {
        if (ObjectUtil.isNull(roleVO)) {
            return new ResponseBodyVO<>(false, 500, "参数必传！", null);
        }
        ResponseBodyVO<RolePO> response = new ResponseBodyVO<RolePO>(ResponseBodyVO.Response.SUCCESS);
        response.setInfo(roleService.saveRole(roleVO));
        return response;
    }

    /**
     * 删除
     */
    @RequestMapping("/delRoleById")
    public ResponseBodyVO<String> delRoleById(@RequestBody RolePO rolePO) {
        int i = roleService.delRoleById(rolePO);
        if (i > 0) {
            return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS);
        } else {
            return new ResponseBodyVO<>(false, 500, "删除失败！", null);
        }
    }

    /**
     * 授权菜单
     */
    @RequestMapping("/saveMenusToRole")
    public ResponseBodyVO<RoleVO> saveMenusToRole(@RequestBody RoleVO roleVO) {
        if (ObjectUtil.isNull(roleVO)) {
            return new ResponseBodyVO<>(false, 500, "参数必传！", null);
        }
        ResponseBodyVO<RoleVO> response = new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS);
        response.setInfo(roleService.saveMenusToRole(roleVO));
        return response;
    }

}
