using Infrastructure.Application; using Infrastructure.Data; using Infrastructure.Extensions; using Infrastructure.Web.Mvc; using Application.Domain.Entities; using IoT.Shared.Application.Models; using IoT.Shared.Areas.IoTCenter.Controlls; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Rendering; using Microsoft.EntityFrameworkCore; using Platform.Application.Models; using System; using System.Collections.Generic; using System.Linq; namespace Platform.Areas.IoTCenter.Controllers { [Authorize] [ApiController] [Route("IoTCenter/[controller]/[action]")] [Area("IoTCenter")] [ControllerScope(ControllerScopeType.Platform)] public class IoTTiggerController : CrudController { private readonly AjaxController _ajax; private readonly IRepository _organRepo; private readonly IRepository _buildingRepo; public IoTTiggerController(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.IoTData).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.IoTDeviceBuildingId.HasValue, o => o.IoTData.IoTDevice.IoTGateway.BuildingId == model.Query.IoTDeviceBuildingId.Value) .WhereIf(model.Query.IoTGatewayId.HasValue, o => o.IoTData.IoTDevice.IoTGatewayId == model.Query.IoTGatewayId.Value) .WhereIf(model.Query.IoTDeviceId.HasValue, o => o.IoTData.IoTDeviceId == model.Query.IoTDeviceId.Value) .WhereIf(model.Query.IoTDataId.HasValue, o => o.IoTDataId == model.Query.IoTDataId.Value) .WhereIf(!string.IsNullOrEmpty(model.Query.Name), o => o.Name.Contains(model.Query.Name)) .WhereIf(model.Query.Disabled.HasValue, o => o.Disabled == model.Query.Disabled.Value) .OrderBy(o => o.IoTSceneId); } public override void EntityToModel(IoTTigger entity, EditIoTTiggerModel model) { model.OrganId = entity.IoTScene?.Building?.OrganId; model.IoTSceneBuildingId = entity.IoTScene?.BuildingId; model.IoTDeviceBuildingId = entity.IoTData?.IoTDevice?.IoTGateway?.BuildingId; model.IoTDeviceId = entity.IoTData?.IoTDeviceId; model.IoTGatewayId = entity.IoTData?.IoTDevice?.IoTGatewayId; } public override void ToDisplayModel(IoTTigger entity, EditIoTTiggerModel model) { 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.IoTDeviceBuildingId.HasValue) { var name = this._buildingRepo.ReadOnlyTable() .Where(o => o.ParentId != null) .Where(o => o.Left <= entity.IoTData.IoTDevice.IoTGateway.Building.Left && o.Right >= entity.IoTData.IoTDevice.IoTGateway.Building.Right) .ToList() .ToTree() .FirstOrDefault(o => o.Id == model.IoTDeviceBuildingId.Value)?.GetDisplayName(); ViewData.Add(model.IoTDeviceBuildingId.Value, name); } ViewData.Add(model.IoTSceneId, entity?.IoTScene?.Name); ViewData.Add(model.IoTGatewayId, entity?.IoTData.IoTDevice?.IoTGateway.Name); ViewData.Add(model.IoTDeviceId, entity?.IoTData.IoTDevice?.Name); ViewData.Add(model.IoTDataId, entity?.IoTData?.Name); } public override void ToEditModel(IoTTigger entity, EditIoTTiggerModel model) { ViewData.SelectList(o => model.OrganId, () => this._ajax.GetOrgan(model.OrganId).SelectList()); ViewData.SelectList(o => model.IoTSceneBuildingId, () => this._ajax.GetBuilding(model.OrganId.Value, model.IoTSceneBuildingId).SelectList(), model.OrganId.HasValue); ViewData.SelectList(o => model.IoTSceneId, () => this._ajax.GetIoTScene(model.IoTSceneBuildingId.Value, model.IoTSceneId).SelectList(), model.IoTSceneBuildingId.HasValue); ViewData.SelectList(o => model.IoTDeviceBuildingId, () => this._ajax.GetBuildingByScene(model.IoTSceneId.Value, model.IoTDeviceBuildingId).SelectList(), model.IoTSceneId.HasValue); ViewData.SelectList(o => model.IoTGatewayId, () => this._ajax.GetIoTGatewayByBuilding(model.IoTDeviceBuildingId.Value, model.IoTGatewayId).SelectList(), model.IoTDeviceBuildingId.HasValue); ViewData.SelectList(o => model.IoTDeviceId, () => this._ajax.GetIoTDevice(model.IoTGatewayId.Value, model.IoTDeviceId).SelectList(), model.IoTGatewayId.HasValue); } } }