using Infrastructure.Application; using Infrastructure.Data; using Infrastructure.Extensions; using Infrastructure.Web.Mvc; using Application.Domain.Entities; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using Platform.Application.Models.IoTCenter; using Platform.Areas.IoTCenter.Controllers; using System; using System.Linq; namespace IoT.Shared.Areas.IoTCenter.Controlls { [Authorize] [ApiController] [Route("IoTCenter/[controller]/[action]")] [Area("IoTCenter")] [ControllerScope(ControllerScopeType.Platform)] public class IoTDeviceController : IoTSharedController { private readonly AjaxController _ajax; private readonly IRepository _categoryRepo; private readonly IRepository _organRepo; private readonly IRepository _buildingRepo; public IoTDeviceController(IRepository repo, AjaxController ajax, IRepository categoryRepo, IRepository organRepo, IRepository buildingRepo, IServiceProvider sp) : base(repo, sp) { this._ajax = ajax; this._categoryRepo = categoryRepo; this._organRepo = organRepo; this._buildingRepo = buildingRepo; } public override IQueryable Include(IQueryable query) { return query.Include(o => o.IoTProduct).ThenInclude(o => o.IoTProductCategory) .Include(o => o.IoTGateway).ThenInclude(o => o.Building).ThenInclude(o => o.Organ); } public override IQueryable Query(PagedListModel model, IQueryable query) { return query .WhereIf(model.Query.OrganId.HasValue, o => o.IoTGateway.Building.OrganId == model.Query.OrganId.Value) .WhereIf(model.Query.BuildingId.HasValue, o => o.IoTGateway.BuildingId == model.Query.BuildingId.Value) .WhereIf(model.Query.IoTGatewayId.HasValue, o => o.IoTGatewayId == model.Query.IoTGatewayId.Value) .WhereIf(model.Query.CategoryId.HasValue, o => o.IoTProduct.IoTProductCategoryId == model.Query.CategoryId.Value) .WhereIf(model.Query.IoTProductId.HasValue, o => o.IoTProductId == model.Query.IoTProductId.Value) .WhereIf(!string.IsNullOrEmpty(model.Query.Name), o => o.Name.Contains(model.Query.Name)) .WhereIf(!string.IsNullOrEmpty(model.Query.DisplayName), o => o.DisplayName.Contains(model.Query.DisplayName)) .WhereIf(!string.IsNullOrEmpty(model.Query.Gateway), o => o.Gateway.Contains(model.Query.Gateway)) .WhereIf(!string.IsNullOrEmpty(model.Query.Tag), o => o.Tag.Contains(model.Query.Tag)) .WhereIf(!string.IsNullOrEmpty(model.Query.Ip), o => o.Ip.Contains(model.Query.Ip)) .WhereIf(!string.IsNullOrEmpty(model.Query.UserName), o => o.UserName.Contains(model.Query.UserName)) .WhereIf(!string.IsNullOrEmpty(model.Query.Number), o => o.Number.Contains(model.Query.Number)) .WhereIf(model.Query.IsOnline.HasValue, o => o.IsOnline == model.Query.IsOnline.Value) .WhereIf(model.Query.Disabled.HasValue, o => o.Disabled == model.Query.Disabled.Value); } public override void EntityToModel(IoTDevice entity, EditPlatformIoTDeviceModel model) { model.BuildingId = entity.IoTGateway?.BuildingId; model.OrganId = entity.IoTGateway?.Building?.OrganId; model.CategoryId = entity.IoTProduct?.IoTProductCategoryId; } public override void ToDisplayModel(IoTDevice entity, EditPlatformIoTDeviceModel model) { if (model.BuildingId.HasValue) { var name = this._buildingRepo.ReadOnlyTable() .Where(o => o.ParentId != null) .Where(o => o.Left <= entity.IoTGateway.Building.Left && o.Right >= entity.IoTGateway.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.IoTGateway.Building.Organ.Left && o.Right >= entity.IoTGateway.Building.Organ.Right) .ToList() .ToTree() .FirstOrDefault(o => o.Id == entity.IoTGateway.Building.OrganId)?.GetDisplayName(); ViewData.Add(model.OrganId, name); } if (model.CategoryId.HasValue) { var name = this._categoryRepo.ReadOnlyTable() .Where(o => o.ParentId != null) .Where(o => o.Left <= entity.IoTProduct.IoTProductCategory.Left && o.Right >= entity.IoTProduct.IoTProductCategory.Right) .ToList() .ToTree() .FirstOrDefault(o => o.Id == model.CategoryId.Value)?.GetDisplayName(); ViewData.Add(model.CategoryId, name); } if (model.IoTProductId.HasValue) { ViewData.Add(model.IoTProductId, entity.IoTProduct.Name); } if (model.IoTGatewayId.HasValue) { ViewData.Add(model.IoTGatewayId, entity.IoTGateway.Name); } } public override void ToEditModel(IoTDevice entity, EditPlatformIoTDeviceModel model) { 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); ViewData.SelectList(o => model.CategoryId, () => this._ajax.GetIoTProductCategory(model.CategoryId).SelectList()); ViewData.SelectList(o => model.IoTProductId, () => this._ajax.GetIoTProductByCategory(model.CategoryId.Value, model.IoTProductId).SelectList(), model.CategoryId.HasValue); } public override string GetNodeNumber(EditPlatformIoTDeviceModel model) { return this.Repo.ReadOnlyTable().Where(o => o.IoTGatewayId == model.IoTGatewayId).Select(o => o.IoTGateway.Number).FirstOrDefault(); } } }