using Infrastructure.Application; using Infrastructure.Data; using Infrastructure.Extensions; using Infrastructure.Web.Mvc; using IoT.Shared.Application.Domain.Entities; using IoT.Shared.Application.Models; 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.Product).ThenInclude(o=>o.Category) .Include(o => o.Node).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.Node.Building.OrganId == model.Query.OrganId.Value) .WhereIf(model.Query.BuildingId.HasValue, o => o.Node.BuildingId == model.Query.BuildingId.Value) .WhereIf(model.Query.NodeId.HasValue, o => o.NodeId == model.Query.NodeId.Value) .WhereIf(model.Query.CategoryId.HasValue, o => o.Product.CategoryId == model.Query.CategoryId.Value) .WhereIf(model.Query.ProductId.HasValue, o => o.ProductId == model.Query.ProductId.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.Password), o => o.Password.Contains(model.Query.Password)) .WhereIf(!string.IsNullOrEmpty(model.Query.Number), o => o.Number.Contains(model.Query.Number)) .WhereIf(!string.IsNullOrEmpty(model.Query.Icon), o => o.Icon.Contains(model.Query.Icon)) .WhereIf(!string.IsNullOrEmpty(model.Query.ConnectId), o => o.ConnectId.Contains(model.Query.ConnectId)) .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 ToDisplayModel(IoTDevice entity, EditPlatformIoTDeviceModel model) { if(entity!=null) { model.BuildingId = entity.Node.BuildingId; model.OrganId = entity.Node.Building.OrganId; model.CategoryId = entity.Product.CategoryId; } if(model.BuildingId.HasValue) { var name = this._buildingRepo.ReadOnlyTable() .Where(o => o.ParentId != null) .Where(o => o.Left <= entity.Node.Building.Left && o.Right >= entity.Node.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.Node.Building.Organ.Left && o.Right >= entity.Node.Building.Organ.Right) .ToList() .ToTree() .FirstOrDefault(o => o.Id == entity.Node.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.Product.Category.Left && o.Right >= entity.Product.Category.Right) .ToList() .ToTree() .FirstOrDefault(o => o.Id == model.CategoryId.Value)?.GetDisplayName(); ViewData.Add(model.CategoryId, name); } if (model.ProductId.HasValue) { ViewData.Add(model.ProductId, entity.Product.Name); } if(model.NodeId.HasValue) { ViewData.Add(model.NodeId, entity.Node.Name); } } public override void ToEditModel(IoTDevice entity, EditPlatformIoTDeviceModel model) { if (entity != null) { model.BuildingId = entity.Node.BuildingId; model.OrganId = entity.Node.Building.OrganId; model.CategoryId = entity.Product.CategoryId; } 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.ProductId, () => this._ajax.GetIoTProductByCategory(model.CategoryId.Value, model.ProductId).SelectList(), model.CategoryId.HasValue); } public override string GetNodeNumber(EditPlatformIoTDeviceModel model) { return this.Repo.ReadOnlyTable().Where(o => o.NodeId == model.NodeId).Select(o => o.Node.Number).FirstOrDefault(); } } }