using Cronos; using Infrastructure.Data; using Infrastructure.Extensions; using Infrastructure.Web.Mvc; using Application.Domain.Entities; using IoT.Shared.Areas.IoTCenter.Controlls; using Jint; using Jint.Native; 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.Order) .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.Order) .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.Order) .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.Order) .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.Order) .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 GetIoTData(Guid parentId, Guid? selected = null) { using var serviceScope = this._services.CreateScope(); var repo = serviceScope.ServiceProvider.GetRequiredService>(); var list = repo.ReadOnlyTable() .Where(o => !o.Hidden && o.IoTDeviceId == parentId) .Select(o => new { o.Id, Name = $"{o.Name}[{o.ValueType}]" }) .ToList(); return new JsonResult(new SelectList(list, "Id", "Name", selected)); } public JsonResult GetOperator(Guid parentId, IoTOperationType? selected) { using var serviceScope = this._services.CreateScope(); var repo = serviceScope.ServiceProvider.GetRequiredService>(); var data = repo.ReadOnlyTable().FirstOrDefault(o => o.Id == parentId); var list = new List(); if (data.ValueType != IoTValueType.String) { list.Add(new SelectListItem { Value = IoTOperationType.GreaterThan.GetValue().ToString(), Text = "大于" }); list.Add(new SelectListItem { Value = IoTOperationType.LessThan.GetValue().ToString(), Text = "小于" }); list.Add(new SelectListItem { Value = IoTOperationType.Equal.GetValue().ToString(), Text = "等于" }); list.Add(new SelectListItem { Value = IoTOperationType.NotEqual.GetValue().ToString(), Text = "不等于" }); } else { list.Add(new SelectListItem { Value = IoTOperationType.Equal.GetValue().ToString(), Text = "等于" }); list.Add(new SelectListItem { Value = IoTOperationType.NotEqual.GetValue().ToString(), Text = "不等于" }); } return new JsonResult(new SelectList(list, "Value", "Text", 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.Order) .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.Order) .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.IoTProductCategoryId == 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.IoTProduct.Name != "网关") .Where(o => o.IoTProduct.IoTApis.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.IoTProduct) .SelectMany(o => o.IoTApis) .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 JsonResult CronValid(string cron) { try { CronExpression.Parse(cron, CronFormat.IncludeSeconds); return new JsonResult(true); } catch (Exception ex) { this._logger.LogError(ex.ToString()); return new JsonResult(ex.Message); } } public JsonResult ConditionValid(Guid iotDataId, IoTOperationType type, string condition) { try { using var scope = this._services.CreateScope(); var repo = scope.ServiceProvider.GetRequiredService>(); var data = repo.ReadOnlyTable().FirstOrDefault(o => o.Id == iotDataId); var value = data.Value; var expression = ""; if (type != IoTOperationType.Other) { if (data.ValueType == IoTValueType.String) { condition = $"\"{condition}\""; } expression = $"value {type.GetDisplayName()}{(type.GetDisplayName().EndsWith("=") ? "=" : "")} {condition}"; } else { expression = condition; } var engine = new Engine().Execute($"function valid(value){{return {expression};}}"); var result = engine.Invoke("valid", value); if (result == JsValue.True || result == JsValue.False) { return new JsonResult(true); } else { return new JsonResult($"表达式运算结果无效:{result}"); } } catch (Exception ex) { ex.PrintStack(); return new JsonResult($"输入格式错误[{ex.Message}]"); } } public SelectList GetCameraSelectList(string deviceNumber) { using var scope = this._services.CreateScope(); var repo = scope.ServiceProvider.GetRequiredService>(); var list = repo.ReadOnlyTable() .Where(o => o.IoTProduct.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); } } }