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 GetBuildingByScene(Guid parentId, Guid? selected) { using var serviceScope = this._services.CreateScope(); var sceneRepo = serviceScope.ServiceProvider.GetRequiredService>(); var building = sceneRepo.ReadOnlyTable() .Where(o => o.Id == parentId) .Select(o => o.Building) .FirstOrDefault(); var repo = serviceScope.ServiceProvider.GetRequiredService>(); var list = repo.ReadOnlyTable() .Where(o => o.ParentId != null) .Where(o => o.Left >=building.Left&&o.Right<= building.Right) .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 GetIoTCommand(Guid parentId, Guid? selected) { using var serviceScope = this._services.CreateScope(); var repo = serviceScope.ServiceProvider.GetRequiredService>(); var list = repo.ReadOnlyTable() .Where(o => o.IoTDevice.IoTGateway.BuildingId == parentId) .OrderBy(o => o.ApiId) .ThenBy(o => o.DisplayOrder) .Select(o => new { o.Id, Name=$"{o.IoTDevice.IoTGateway.Name}-{o.IoTDevice.DisplayName??o.IoTDevice.Name}-{o.Name??o.Api.Name}"}) .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 JsonResult GetIoTProductByCategory(Guid parentId, Guid? selected = null) { using var scope = this._services.CreateScope(); var repo = scope.ServiceProvider.GetRequiredService>(); var list = repo.ReadOnlyTable() .Where(o => o.CategoryId == parentId) .Select(o => new { o.Id, o.Name }) .ToList(); return new JsonResult(new SelectList(list, "Id", "Name", selected)); } public JsonResult GetIoTGatewayByBuilding(Guid parentId, Guid? selected = null) { using var scope = this._services.CreateScope(); var repo = scope.ServiceProvider.GetRequiredService>(); var list = repo.ReadOnlyTable() .Where(o => o.BuildingId == parentId) .Select(o => new { o.Id, Name = $"[{o.Number}]{o.Name}" }) .ToList(); return new JsonResult(new SelectList(list, "Id", "Name", selected)); } public JsonResult GetIoTDeviceForCommand(Guid parentId, Guid? selected = null) { using var scope = this._services.CreateScope(); var repo = scope.ServiceProvider.GetRequiredService>(); var list = repo.ReadOnlyTable() .Where(o => o.IoTGatewayId == parentId) .Where(o => o.Product.Name != "网关") .Where(o => o.Product.Apis.Any()) .Select(o => new { o.Id, o.Name }) .ToList(); return new JsonResult(new SelectList(list, "Id", "Name", selected)); } public JsonResult GetIoTScene(Guid parentId, Guid? selected = null) { using var scope = this._services.CreateScope(); var repo = scope.ServiceProvider.GetRequiredService>(); var list = repo.ReadOnlyTable() .Where(o => o.BuildingId == parentId) .Select(o => new { o.Id, o.Name }) .ToList(); return new JsonResult(new SelectList(list, "Id", "Name", selected)); } public JsonResult GetIoTApiByDevice(Guid parentId, Guid? selected = null) { using var scope = this._services.CreateScope(); var repo = scope.ServiceProvider.GetRequiredService>(); var list = repo.ReadOnlyTable() .Where(o => o.Id == parentId) .Select(o => o.Product) .SelectMany(o => o.Apis) .Select(o => new { o.Id, o.Name, Product = o.Product.Name }) .ToList(); if (list.Any()) { var product = list.FirstOrDefault().Product; if (product == "串口控制器" || product == "红外转发器") { list = list.Where(o => o.Name == "发送指令").ToList(); } } return new JsonResult(new SelectList(list, "Id", "Name", selected)); } public SelectList GetCameraSelectList(string deviceNumber) { using var scope = this._services.CreateScope(); var repo = scope.ServiceProvider.GetRequiredService>(); var list = repo.ReadOnlyTable() .Where(o => o.Product.Number == "onvifcamera") .WhereIf(!string.IsNullOrEmpty(deviceNumber), o => o.Number == deviceNumber) .Select(o => new { Id = o.Number, Name = o.DisplayName }) .ToList(); return new SelectList(list, "Id", "Name", deviceNumber); } } }