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/Platform/Areas/IoTCenter/Controllers/IoTGatewayController.cs

109 lines
4.5 KiB

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.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 IoTGatewayController : IoTSharedController<IoTGateway, EditPlatformIoTGatewayModel>
{
private readonly IRepository<Organ> _organRepo;
private readonly IRepository<Building> _buildingRepo;
private readonly AjaxController _ajax;
public IoTGatewayController(IRepository<IoTGateway> repo,
IServiceProvider sp,
IRepository<Organ> organRepo,
IRepository<Building> buildingRepo,
AjaxController ajax) : base(repo, sp)
{
this._organRepo = organRepo;
this._buildingRepo = buildingRepo;
this._ajax = ajax;
}
public override IQueryable<IoTGateway> Include(IQueryable<IoTGateway> query)
{
return query.Include(o => o.Building).ThenInclude(o => o.Organ);
}
public override IQueryable<IoTGateway> Query(PagedListModel<EditPlatformIoTGatewayModel> model, IQueryable<IoTGateway> query)
{
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(!string.IsNullOrEmpty(model.Query.Number), o => o.Number.Contains(model.Query.Number))
.WhereIf(!string.IsNullOrEmpty(model.Query.Version), o => o.Version.Contains(model.Query.Version))
.WhereIf(model.Query.IsOnline.HasValue, o => o.IsOnline == model.Query.IsOnline.Value)
.WhereIf(model.Query.Hidden.HasValue, o => o.Hidden == model.Query.Hidden.Value);
}
public override void ToDisplayModel(IoTGateway entity, EditPlatformIoTGatewayModel 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(IoTGateway entity, EditPlatformIoTGatewayModel 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);
}
public override void ToEntity(EditPlatformIoTGatewayModel model, IoTGateway entity)
{
if (this.ControllerContext.ActionDescriptor.ActionName == "Edit")
{
entity = this.Repo.Table().FirstOrDefault(o => o.Id == model.Id);
entity.BuildingId = model.BuildingId;
this.Repo.SaveChanges();
}
}
public override string GetNodeNumber(EditPlatformIoTGatewayModel model)
{
return model.Number;
}
}
}