using Infrastructure.Application; 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.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using Platform.Application.Models; using System.Linq; namespace Platform.Areas.IoTCenter.Controllers { [Authorize] [ApiController] [Route("IoTCenter/[controller]/[action]")] [Area("IoTCenter")] [ControllerScope(ControllerScopeType.Platform)] public class IoTSceneIoTCommandController : CrudController { private readonly AjaxController _ajax; private readonly IRepository _organRepo; private readonly IRepository _buildingRepo; public IoTSceneIoTCommandController(IRepository repo, AjaxController ajax, IRepository organRepo, IRepository buildingRepo) : base(repo) { this._ajax = ajax; this._organRepo = organRepo; this._buildingRepo = buildingRepo; } public override IQueryable Include(IQueryable query) { return query .Include(o => o.IoTScene).ThenInclude(o=>o.Building).ThenInclude(o => o.Organ) .Include(o => o.IoTCommand).ThenInclude(o => o.IoTDevice).ThenInclude(o => o.IoTGateway).ThenInclude(o=>o.Building); } public override IQueryable Query(PagedListModel model, IQueryable query) { return query .WhereIf(model.Query.OrganId.HasValue,o=>o.IoTScene.Building.OrganId==model.Query.OrganId.Value) .WhereIf(model.Query.IoTSceneBuildingId.HasValue, o => o.IoTScene.BuildingId == model.Query.IoTSceneBuildingId.Value) .WhereIf(model.Query.IoTSceneId.HasValue, o => o.IoTSceneId == model.Query.IoTSceneId.Value) .WhereIf(model.Query.DeviceBuildingId.HasValue, o => o.IoTCommand.IoTDevice.IoTGateway.BuildingId == model.Query.DeviceBuildingId.Value) .WhereIf(model.Query.IoTCommandId.HasValue, o => o.IoTCommandId == model.Query.IoTCommandId.Value) .OrderBy(o => o.IoTSceneId); } public override void ToDisplayModel(IoTSceneIoTCommand entity, EditIoTSceneIoTCommandModel model) { if(entity!=null) { model.OrganId = entity.IoTScene?.Building?.OrganId; model.IoTSceneBuildingId = entity.IoTScene?.BuildingId; model.DeviceBuildingId = entity.IoTCommand?.IoTDevice?.IoTGateway?.BuildingId; } if (model.OrganId.HasValue) { var name = this._organRepo.ReadOnlyTable() .Where(o => o.ParentId != null) .Where(o => o.Left <= entity.IoTScene.Building.Organ.Left && o.Right >= entity.IoTScene.Building.Organ.Right) .ToList() .ToTree() .FirstOrDefault(o => o.Id == entity.IoTScene.Building.OrganId)?.GetDisplayName(); ViewData.Add(model.OrganId, name); } if (model.IoTSceneBuildingId.HasValue) { var name = this._buildingRepo.ReadOnlyTable() .Where(o => o.ParentId != null) .Where(o => o.Left <= entity.IoTScene.Building.Left && o.Right >= entity.IoTScene.Building.Right) .ToList() .ToTree() .FirstOrDefault(o => o.Id == model.IoTSceneBuildingId.Value)?.GetDisplayName(); ViewData.Add(model.IoTSceneBuildingId.Value, name); } if (model.DeviceBuildingId.HasValue) { var name = this._buildingRepo.ReadOnlyTable() .Where(o => o.ParentId != null) .Where(o => o.Left <= entity.IoTCommand.IoTDevice.IoTGateway.Building.Left && o.Right >= entity.IoTCommand.IoTDevice.IoTGateway.Building.Right) .ToList() .ToTree() .FirstOrDefault(o => o.Id == model.DeviceBuildingId.Value)?.GetDisplayName(); ViewData.Add(model.DeviceBuildingId.Value, name); } ViewData.Add(model.IoTSceneId, entity?.IoTScene?.Name); ViewData.Add(model.IoTCommandId, entity?.IoTCommand?.Name??entity.IoTCommand.Api.Name); } public override void ToEditModel(IoTSceneIoTCommand entity, EditIoTSceneIoTCommandModel model) { if (entity != null) { model.OrganId = entity.IoTScene?.Building?.OrganId; model.IoTSceneBuildingId = entity.IoTScene?.BuildingId; model.DeviceBuildingId = entity.IoTCommand?.IoTDevice?.IoTGateway?.BuildingId; } ViewData.SelectList(o => model.OrganId, () => this._ajax.GetOrgan(model.OrganId).SelectList()); ViewData.SelectList(o => model.IoTSceneId, () => this._ajax.GetIoTScene(model.IoTSceneBuildingId.Value, model.IoTSceneId).SelectList(), model.IoTSceneBuildingId.HasValue); ViewData.SelectList(o => model.IoTSceneBuildingId, () => this._ajax.GetBuilding(model.OrganId.Value, model.IoTSceneBuildingId).SelectList(), model.OrganId.HasValue); ViewData.SelectList(o => model.DeviceBuildingId, () => this._ajax.GetBuildingByScene(model.IoTSceneId.Value, model.DeviceBuildingId).SelectList(), model.IoTSceneId.HasValue); ViewData.SelectList(o => model.IoTCommandId, () => this._ajax.GetIoTCommand(model.DeviceBuildingId.Value, model.IoTCommandId).SelectList(), model.DeviceBuildingId.HasValue); } } }