using Infrastructure.Data; using Application.Domain.Entities; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Rendering; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using System; using System.Linq; namespace IoT.Shared.Areas.IoTCenter.Controlls { public class AjaxBaseController { protected readonly IServiceProvider _services; protected readonly ILogger _logger; public AjaxBaseController(IServiceProvider services, ILogger logger) { this._services = services; this._logger = logger; } public JsonResult GetDictionaryCategory(Guid? selected) { using var scope = this._services.CreateScope(); var repo = scope.ServiceProvider.GetRequiredService>(); var list = repo.ReadOnlyTable() .Select(o => new { o.Name, Value=o.Key }) .ToList(); return new JsonResult(new SelectList(list, "Value", "Name", selected)); } public JsonResult GetDictionary(string key,string selected) { using var scope = this._services.CreateScope(); var repo = scope.ServiceProvider.GetRequiredService>(); var list = repo.ReadOnlyTable() .Where(o => o.Key == key) .SelectMany(o=>o.Items) .OrderBy(o=>o.Order) .Select(o => new { o.Name, o.Value }) .ToList(); return new JsonResult(new SelectList(list, "Value", "Name", selected)); } public JsonResult GetIoTProduct(Guid? selected) { 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 JsonResult(new SelectList(list, "Id", "Name", selected)); } public JsonResult GetIoTApi(Guid parentId, Guid? selected = null) { using var scope = this._services.CreateScope(); var repo = scope.ServiceProvider.GetRequiredService>(); var list = repo.ReadOnlyTable() .Where(o => o.IoTProductId == parentId) .Select(o => new { o.Id, o.Name }) .ToList(); return new JsonResult(new SelectList(list, "Id", "Name", selected)); } public JsonResult GetIoTGateway(Guid? selected = null) { using var scope = this._services.CreateScope(); var repo = scope.ServiceProvider.GetRequiredService>(); var list = repo.ReadOnlyTable() .Select(o => new { o.Id, Name = $"[{o.Number}]{o.Name}" }) .ToList(); return new JsonResult(new SelectList(list, "Id", "Name", selected)); } public JsonResult GetIoTDevice(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) .Select(o => new { o.Id,o.Name, o.DisplayName }) .ToList() .Select(o=>new { o.Id,Name=o.DisplayName??o.Name}) .ToList(); return new JsonResult(new SelectList(list, "Id", "Name", selected)); } } }