using Infrastructure.Application; using Infrastructure.Data; using Infrastructure.Extensions; using Infrastructure.Web.Mvc; using IoT.Shared.Application.Domain.Entities; 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 IoTSceneController : CrudController { private readonly AjaxController _ajax; private readonly IRepository _organRepo; private readonly IRepository _buildingRepo; public IoTSceneController(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.Building).ThenInclude(o => o.Organ); } public override IQueryable Query(PagedListModel model, IQueryable query) { ViewData.SelectList(o => model.Query.OrganId, () => this._ajax.GetOrgan(model.Query.OrganId).SelectList()); return query .WhereIf(model.Query.OrganId.HasValue, o => o.Building.OrganId == model.Query.OrganId.Value) .WhereIf(model.Query.BuildingId.HasValue, o => o.BuildingId == model.Query.BuildingId.Value) .WhereIf(!string.IsNullOrEmpty(model.Query.Name), o => o.Name.Contains(model.Query.Name)) .WhereIf(model.Query.Hidden.HasValue, o => o.Hidden == model.Query.Hidden.Value) .OrderBy(o => o.DisplayOrder); } public override void ToDisplayModel(IoTScene entity, EditIoTSceneModel model) { if(entity!=null) { model.OrganId = entity.Building.OrganId; } if (model.BuildingId.HasValue) { var name = this._buildingRepo.ReadOnlyTable() .Where(o => o.ParentId != null) .Where(o => o.Left <= entity.Building.Left && o.Right >= entity.Building.Right) .ToList() .ToTree() .FirstOrDefault(o => o.Id == model.BuildingId.Value)?.GetDisplayName(); ViewData.Add(model.BuildingId.Value, name); } if (model.OrganId.HasValue) { var name = this._organRepo.ReadOnlyTable() .Where(o => o.ParentId != null) .Where(o => o.Left <= entity.Building.Organ.Left && o.Right >= entity.Building.Organ.Right) .ToList() .ToTree() .FirstOrDefault(o => o.Id == entity.Building.OrganId)?.GetDisplayName(); ViewData.Add(model.OrganId, name); } } public override void ToEditModel(IoTScene entity, EditIoTSceneModel model) { if (entity != null) { model.OrganId = entity.Building.OrganId; } ViewData.SelectList(o => model.OrganId, () => this._ajax.GetOrgan(model.OrganId).SelectList()); ViewData.SelectList(o => model.BuildingId, () => this._ajax.GetBuilding(model.OrganId.Value, model.BuildingId).SelectList(), model.OrganId.HasValue); } } }