using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Bowin.Common.ServiceToken.Permission; using Bowin.Common.WebModels; using YLShipBuildLandMap.Entity; using YLShipBuildLandMap.Entity.ViewModel; using YLShipBuildLandMap.Entity.ViewModel.SystemSetting; using YLShipBuildLandMap.Services.SystemSetting; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Newtonsoft.Json.Linq; namespace YLShipBuildLandMap.Web.Controllers.SystemSetting { [Route("api/systemsetting/[controller]/[action]")] [ApiController] [Authorize] public class RoleController : ControllerBase { private IRoleService RoleService { get; set; } private IMenuService MenuService { get; set; } private IFunctionCodeService FunctionCodeService { get; set; } public RoleController(IRoleService roleService, IFunctionCodeService functionCodeService) { RoleService = roleService; FunctionCodeService = functionCodeService; } [HttpPost] public ResultMessage GetRolesForSelect([FromBody] dynamic inputObj) { try { string roleName = inputObj.roleName; int? pageIndex = inputObj.pageIndex; int? pageSize = inputObj.pageSize; return ResultMessage.Success(RoleService.GetRoleViewsForSelect(roleName, pageIndex, pageSize)); } catch (Exception ex) { return ResultMessage.GetError(ex.Message); } } [HttpPost] [FunctionCode("9902")] public ResultMessage GetRoleList([FromBody] dynamic inputObj) { try { string roleName = inputObj.roleName; int? pageIndex = inputObj.pageIndex; int? pageSize = inputObj.pageSize; var roleList = RoleService.GetRoleViewList(roleName, pageIndex, pageSize); return ResultMessage.Success(roleList); } catch (Exception ex) { return ResultMessage.GetError(ex.Message); } } [HttpPost] [FunctionCode("9902")] public ResultMessage GetRole([FromBody] dynamic inputObj) { try { Guid roleID = inputObj.roleID; var role = RoleService.GetRoleView(roleID); return ResultMessage.Success(role); } catch (Exception ex) { return ResultMessage.GetError(ex.Message); } } [HttpPost] [FunctionCode("990201")] public async Task Add([FromBody] JObject inputObj) { try { RoleView role = inputObj["role"].ToObject(); List functionCodes = inputObj["functionCodes"].ToObject>(); RoleView result = RoleService.GetRoleViewByName(role.RoleName); if (result != null) { return ResultMessage.GetError("角色已存在"); } role.RoleID = null; await RoleService.Save(role, functionCodes, LoginUser.Current.UserID); return ResultMessage.Success(); } catch (Exception ex) { return ResultMessage.GetError(ex.Message); } } [HttpPost] [FunctionCode("990202")] public async Task Edit([FromBody] dynamic inputObj) { try { RoleView role = inputObj["role"].ToObject(); List functionCodes = inputObj["functionCodes"].ToObject>(); if (!role.RoleID.HasValue) { throw new Exception("实体ID不能为空。"); } await RoleService.Save(role, functionCodes, LoginUser.Current.UserID); return ResultMessage.Success(); } catch (Exception ex) { return ResultMessage.GetError(ex.Message); } } [HttpPost] [FunctionCode("990203")] public async Task Delete([FromBody] dynamic inputObj) { try { List roleIDList = inputObj.roleIDs.ToObject>(); await RoleService.Delete(roleIDList); return ResultMessage.Success(); } catch (Exception ex) { return ResultMessage.GetError(ex.Message); } } [HttpPost] [FunctionCode("990202")] public ResultMessage GetFunctionCodeList() { try { var functionCodeList = FunctionCodeService.GetFunctionCodeList(); return ResultMessage.Success(functionCodeList); } catch (Exception ex) { return ResultMessage.GetError(ex.Message); } } [HttpPost] [FunctionCode("990202")] public ResultMessage GetRoleFunctionCode([FromBody] dynamic inputObj) { try { Guid roleID = inputObj.roleID; List roleFunctionCodeList = FunctionCodeService.GetRoleFunctionCode(roleID); return ResultMessage.Success(roleFunctionCodeList); } catch (Exception ex) { return ResultMessage.GetError(ex.Message); } } } }