using Infrastructure.Data; using Infrastructure.Extensions; using Infrastructure.Web.Mvc; using IoT.Shared.Application.Domain.Entities; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Rendering; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; using System.Linq; namespace Platform.Areas.IoTCenter.Controllers { [Area("IoTCenter")] public class AjaxController : IoT.Shared.Areas.IoTCenter.Controlls.AjaxBaseController { public AjaxController(IServiceProvider services, ILogger logger, IRepository categoryRepo, IRepository productRepo, IRepository organRepo, IRepository buildingIoTGatewayRepo, IRepository organSceneRepo, IRepository nodeRepo, IRepository sceneRepo, IRepository commandRepo, IRepository deviceRepo, IRepository dataRepo, IRepository apiRepo, IRepository sceneTiggerRepo) : base(services, logger, categoryRepo, productRepo, organRepo, buildingIoTGatewayRepo, organSceneRepo, nodeRepo, sceneRepo, commandRepo, deviceRepo, dataRepo, apiRepo, sceneTiggerRepo) { } public JsonResult GetArea(Guid? selected) { using var scope = this._services.CreateScope(); var repo = scope.ServiceProvider.GetRequiredService>(); var list = repo.ReadOnlyTable() .Where(o => o.ParentId != null) .OrderBy(o => o.ParentId) .ThenBy(o => o.DisplayOrder) .ToList() .ToTree() .Select(o => new Area { Id = o.Id, Name = o.GetDisplayName() }) .ToList(); return new JsonResult(new SelectList(list, "Id", "Name", selected)); } public JsonResult GetOrganBuilding(Guid? parentId, Guid? selected) { using var scope = this._services.CreateScope(); var repo = scope.ServiceProvider.GetRequiredService>(); var list = repo.ReadOnlyTable() .Where(o => o.ParentId != null) .WhereIf(parentId.HasValue,o => o.OrganId == parentId.Value) .OrderBy(o => o.ParentId) .ThenBy(o => o.DisplayOrder) .ToList() .ToTree().Select(o => new Building { Id = o.Id, Name = o.GetDisplayName() }) .ToList(); return new JsonResult(new SelectList(list, "Id", "Name", selected)); } public JsonResult GetUser(Guid? parentId, Guid? selected = null, string search = null, ControllerScopeType? range = null) { using var scope = this._services.CreateScope(); var repo = scope.ServiceProvider.GetRequiredService>(); var organId = scope.ServiceProvider.GetRequiredService().HttpContext.User.GetOrganId(); var list = repo.ReadOnlyTable() .WhereIf(range.HasValue && range == ControllerScopeType.Organ && organId.HasValue, o => o.OrganUsers.Any(o => o.OrganId == organId.Value)) .WhereIf(parentId.HasValue, o => o.OrganUsers.Any(o => o.OrganId == parentId.Value)) .WhereIf(!string.IsNullOrEmpty(search), o => o.UserName.Contains(search) || o.NickName.Contains(search) || o.Email.Contains(search) || o.PhoneNumber.Contains(search) || o.RealName.Contains(search) || o.IdentityNumber.Contains(search)) .WhereIf(selected.HasValue, o => o.Id == selected.Value) .Select(o => new { o.Id, Name = $"{o.UserName} {o.RealName}" }) .Take(20) .ToList(); return new JsonResult(new SelectList(list, "Id", "Name", selected)); } public JsonResult GetRole(Guid parentId, List selected = null) { using var scope = this._services.CreateScope(); var repo = scope.ServiceProvider.GetRequiredService>(); var list = repo.ReadOnlyTable() .Where(o => o.OrganId == parentId) .Select(o => new { o.Id, Name = $"{o.Name} {o.Number}" }) .ToList(); return new JsonResult(new MultiSelectList(list, "Id", "Name", selected)); } public JsonResult GetPermission(Guid parentId, List selected = null) { using var scope = this._services.CreateScope(); var repo = scope.ServiceProvider.GetRequiredService>(); var list = repo.ReadOnlyTable() //.Where(o => o.Permission.OrganId == parentId) .Select(o => new { o.Permission.Id, o.Permission.Name }) .ToList(); return new JsonResult(new MultiSelectList(list, "Id", "Name", selected)); } [ApiExplorerSettings(IgnoreApi = true)] public JsonResult GetPermissionCategory(Guid? organId,Guid? selected) { using var scope = this._services.CreateScope(); var repo = scope.ServiceProvider.GetRequiredService>(); //var organId = scope.ServiceProvider.GetRequiredService().HttpContext.User.GetOrganId(); var list = repo.ReadOnlyTable() .Where(o => o.ParentId != null) .WhereIf(organId.HasValue,o => o.OrganId == organId.Value) .OrderBy(o => o.ParentId) .ThenBy(o => o.DisplayOrder) .ToList() .ToTree().Select(o => new Building { Id = o.Id, Name = o.GetDisplayName() }) .ToList(); return new JsonResult(new SelectList(list, "Id", "Name", selected)); } public MultiSelectList GetOrganMultiSelectList(List selected) { if (selected == null) { selected = new List(); } var list = this._organRepo.ReadOnlyTable() .Where(o => o.ParentId != null) .Select(o => new { o.Id, o.Name }) .ToList(); return new MultiSelectList(list, "Id", "Name", selected); } [ApiExplorerSettings(IgnoreApi = true)] public MultiSelectList GetRoleMultiSelectList(IEnumerable selected) { if (selected == null) { selected = new List(); } using var scope = this._services.CreateScope(); var repo = scope.ServiceProvider.GetRequiredService>(); var list = repo.ReadOnlyTable() .Select(o => new { o.Id, o.Name }) .ToList(); return new MultiSelectList(list, "Id", "Name", selected); } } }