You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
iot/projects/IoT.Shared/Areas/IoTCenter/Controlls/DeviceController.cs

78 lines
3.8 KiB

using Infrastructure.Application;
using Infrastructure.Data;
using Infrastructure.Extensions;
using IoT.Shared.Application.Domain.Entities;
using IoT.Shared.Application.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Primitives;
using System;
using System.Linq;
namespace IoT.Shared.Areas.IoTCenter.Controlls
{
public class DeviceController : SharedController<IoTDevice, EditDeviceModel>
{
private readonly AjaxBaseController _ajax;
public DeviceController(IRepository<IoTDevice> repo, AjaxBaseController ajax, IServiceProvider sp) : base(repo, sp)
{
this._ajax = ajax;
}
public override IQueryable<IoTDevice> Include(IQueryable<IoTDevice> query)
{
return query.Include(o => o.Product).Include(o => o.Node);
}
public override IQueryable<IoTDevice> Query(PagedListModel<EditDeviceModel> model, IQueryable<IoTDevice> query)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (this.HttpContext.Request.Query.TryGetValue("query.organNumber", out StringValues value))
{
var organNumber = value.ToString();
query = query.WhereIf(!string.IsNullOrWhiteSpace(organNumber), o => o.Node.BuildingIoTGateways.Any(o => o.Building.Organ.Number == organNumber));
}
return query
.WhereIf(model.Query.NodeId.HasValue, o => o.NodeId == model.Query.NodeId.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, EditDeviceModel model)
{
ViewData.Add(model.NodeId, entity.Node.Name);
ViewData.Add(model.ProductId, entity.Product.Name);
}
public override void ToEditModel(IoTDevice entity, EditDeviceModel model)
{
if (entity != null)
{
this.ToDisplayModel(entity, model);
}
ViewData.SelectList(o => model.NodeId, () => this._ajax.GetNodeSelectList(model.NodeId));
ViewData.SelectList(o => model.ProductId, () => this._ajax.GetProductSelectList(model.ProductId));
}
public override string GetNodeNumber(EditDeviceModel model)
{
return this.Repo.ReadOnlyTable().Where(o => o.NodeId == model.NodeId).Select(o => o.Node.Number).FirstOrDefault();
}
}
}