using Infrastructure.Data; using Infrastructure.Extensions; using Infrastructure.Web.Mvc; using IoT.Shared.Application.Domain.Entities; using IoT.Shared.Areas.IoTCenter.Controlls; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Rendering; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; using System.Linq; namespace Platform.Areas.IoTCenter.Controllers { public class AjaxController : AjaxBaseController { public AjaxController(IServiceProvider services,ILogger logger): base(services, logger) { } 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 GetOrgan(Guid? selected) { using var serviceScope = this._services.CreateScope(); var repo = serviceScope.ServiceProvider.GetRequiredService>(); var list = repo.ReadOnlyTable() .Where(o => o.ParentId != null) .OrderBy(o => o.ParentId) .ThenBy(o => o.DisplayOrder) .ToList() .ToTree() .Select(o => new Organ { Id = o.Id, Name = o.GetDisplayName() }) .ToList(); return new JsonResult(new SelectList(list, "Id", "Name", selected)); } public JsonResult GetBuilding(Guid parentId, Guid? selected) { using var serviceScope = this._services.CreateScope(); var repo = serviceScope.ServiceProvider.GetRequiredService>(); var list = repo.ReadOnlyTable() .Where(o => o.ParentId != null) .Where(o => o.OrganId == parentId) .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(List selected, ControllerScopeType? scope = null) { using var serviceScope = this._services.CreateScope(); var categoryRepo = serviceScope.ServiceProvider.GetRequiredService>(); var categoryList = categoryRepo.ReadOnlyTable().Where(o => o.ParentId != null).ToList().ToTree(); var repo = serviceScope.ServiceProvider.GetRequiredService>(); var list = repo.ReadOnlyTable() .Include(o => o.Category) .WhereIf(scope.HasValue && scope.Value == ControllerScopeType.Organ, o => o.Type == PermissionType.Organ) .ToList() .Select(o => new { o.Id, o.Name, Group = categoryList.FirstOrDefault(c => c.Id == o.CategoryId.Value)?.GetDisplayName() }); return new JsonResult(new MultiSelectList(list, "Id", "Name", selected, "Group")); } [ApiExplorerSettings(IgnoreApi = true)] public JsonResult GetPermissionCategory(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 Building { Id = o.Id, Name = o.GetDisplayName() }) .ToList(); return new JsonResult(new SelectList(list, "Id", "Name", selected)); } public JsonResult GetIoTProductCategory(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 IoTProductCategory { Id = o.Id, Name = o.GetDisplayName() }) .ToList(); return new JsonResult(new SelectList(list, "Id", "Name", selected)); } public SelectList GetOrganNodeSelectList(Guid parentId, Guid? selected = null) { using var scope = this._services.CreateScope(); var repo = scope.ServiceProvider.GetRequiredService>(); var list = repo.ReadOnlyTable() .Where(o => o.Building.OrganId == parentId) .Select(o => o.IoTGateway) .Select(o => new { o.Id, Name = $"{o.Name}({o.Number})" }) .ToList(); return new SelectList(list, "Id", "Name", selected); } public SelectList GetOrganSceneSelectList(Guid parentId, Guid? 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, o.Name }) .ToList(); return new SelectList(list, "Id", "Name", selected); } public JsonResult GetOrganSceneJson(Guid parentId, Guid? selected) { return new JsonResult(this.GetOrganSceneSelectList(parentId, selected)); } } }