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

145 lines
6.2 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 IRepository<BuildingIoTGateway> _buildingGatewayRepo;
private readonly AjaxController _ajax;
public IoTGatewayController(IRepository<IoTGateway> repo,
IServiceProvider sp, IRepository<Organ> organRepo,
IRepository<Building> buildingRepo,
IRepository<BuildingIoTGateway> buildingGatewayRepo,
AjaxController ajax) : base(repo, sp)
{
this._organRepo = organRepo;
this._buildingRepo = buildingRepo;
this._buildingGatewayRepo = buildingGatewayRepo;
this._ajax = ajax;
}
public override IQueryable<IoTGateway> Query(PagedListModel<EditPlatformIoTGatewayModel> model, IQueryable<IoTGateway> query)
{
query = this._buildingGatewayRepo.ReadOnlyTable()
.WhereIf(model.Query.BuildingId.HasValue, o => o.BuildingId == model.Query.BuildingId.Value)
.WhereIf(model.Query.OrganId.HasValue, o => o.Building.OrganId == model.Query.OrganId.Value)
.Select(o => o.IoTGateway);
return query
.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)
{
var building = this._buildingGatewayRepo.ReadOnlyTable()
.Where(o => o.IoTGatewayId == model.Id)
.Include(o=>o.Building).ThenInclude(o=>o.Organ)
.Select(o=>o.Building)
.FirstOrDefault();
model.BuildingId = building?.Id;
model.OrganId = building?.OrganId;
if (model.OrganId.HasValue)
{
var organ = building?.Organ;
var name = this._organRepo.ReadOnlyTable()
.Where(o => o.ParentId != null)
.Where(o => o.Left <= organ.Left && o.Right >= organ.Right).ToList()
.ToList()
.ToTree()
.FirstOrDefault(o => o.Id == model.OrganId.Value)?.GetDisplayName();
ViewData.Add(model.OrganId, name);
}
if(model.BuildingId.HasValue)
{
var name = this._buildingRepo.ReadOnlyTable()
.Where(o => o.ParentId != null)
.Where(o => o.Left <= building.Left && o.Right >= building.Right).ToList()
.ToList()
.ToTree()
.FirstOrDefault(o => o.Id == model.BuildingId.Value)?.GetDisplayName();
ViewData.Add(model.BuildingId, name);
}
}
public override void ToEditModel(IoTGateway entity, EditPlatformIoTGatewayModel model)
{
if(entity!=null)
{
var building = this._buildingGatewayRepo.ReadOnlyTable()
.Where(o => o.IoTGatewayId == model.Id)
.Include(o => o.Building)
.Select(o => o.Building)
.FirstOrDefault();
model.BuildingId = building?.Id;
model.OrganId = building?.OrganId;
}
ViewData.SelectList(o => model.OrganId, () => this._ajax.GetOrgan(model.OrganId).SelectList());
if(model.OrganId.HasValue)
{
ViewData.SelectList(o => model.BuildingId, () => this._ajax.GetBuilding(model.OrganId.Value, model.BuildingId).SelectList());
}
}
public override void ToEntity(EditPlatformIoTGatewayModel model, IoTGateway entity)
{
if (model.BuildingId.HasValue)
{
var buildingGateway = this._buildingGatewayRepo.Table().FirstOrDefault(o => o.IoTGatewayId == model.Id);
if (model.BuildingId.HasValue)
{
if (buildingGateway == null)
{
buildingGateway = new BuildingIoTGateway { BuildingId = model.BuildingId.Value, IoTGatewayId = model.Id };
this._buildingGatewayRepo.Add(buildingGateway);
}
else
{
if (buildingGateway.BuildingId != model.BuildingId.Value)
{
buildingGateway.BuildingId = model.BuildingId.Value;
}
}
}
else
{
if (buildingGateway != null)
{
this._buildingGatewayRepo.Delete(buildingGateway);
}
}
if(this.ControllerContext.ActionDescriptor.ActionName=="Edit")
{
this._buildingGatewayRepo.SaveChanges();
}
}
}
public override string GetNodeNumber(EditPlatformIoTGatewayModel model)
{
return model.Number;
}
}
}