using Infrastructure.Data; using Infrastructure.Extensions; using IoT.Shared.Application.Domain.Entities; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Rendering; 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.AjaxController { private readonly IRepository _areaRepo; private readonly IRepository _buildingRepo; private readonly IRepository _roleRepo; public AjaxController(ILogger logger, IHttpContextAccessor httpContxt, IRepository categoryRepo, IRepository productRepo, IRepository organRepo, IRepository buildingIoTGatewayRepo, IRepository organSceneRepo, IRepository nodeRepo, IRepository sceneRepo, IRepository commandRepo, IRepository deviceRepo, IRepository dataRepo, IRepository apiRepo, IRepository sceneTiggerRepo, IRepository areaRepo, IRepository buildingRepo, IRepository roleRepo) : base(logger, httpContxt, categoryRepo, productRepo, organRepo, buildingIoTGatewayRepo, organSceneRepo, nodeRepo, sceneRepo, commandRepo, deviceRepo, dataRepo, apiRepo, sceneTiggerRepo) { this._areaRepo = areaRepo; this._buildingRepo = buildingRepo; this._roleRepo = roleRepo; } public SelectList GetAreaSelectList(Guid? selected = null, Guid? currentId = null) { var list = this._areaRepo.ReadOnlyTable() .Where(o => o.ParentId != null) .WhereIf(currentId.HasValue, o => o.Id != currentId.Value) .OrderBy(o => o.Parent) .ThenBy(o => o.DisplayOrder) .Select(o => new { o.Id, Name = $"{o.Name}({o.Number})" }) .ToList(); return new SelectList(list, "Id", "Name", selected); } public JsonResult GetOrganBuilding(Guid parentId, Guid? selected = null, Guid? currentId = null) { var list = this._buildingRepo.ReadOnlyTable() .Where(o => o.ParentId != null) .Where(o => o.OrganId == parentId) .WhereIf(currentId.HasValue, o => o.Id != currentId.Value) .OrderBy(o => o.Parent) .ThenBy(o => o.DisplayOrder) .Select(o => new { o.Id, Name = $"{o.Name}({o.Number})" }) .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(); } var list = this._roleRepo.ReadOnlyTable() .Select(o => new { o.Id, o.Name }) .ToList(); return new MultiSelectList(list, "Id", "Name", selected); } } }