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/UserCenter/Controllers/BuildingController.cs

180 lines
6.8 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.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using Platform.Application.Models;
using Platform.Areas.IoTCenter.Controllers;
using System;
using System.Linq;
namespace Platform.Areas.UserCenter.Controllers
{
[Authorize]
[ApiController]
[Route("UserCenter/[controller]/[action]")]
[Area("UserCenter")]
[ControllerScope(ControllerScopeType.Platform)]
public class BuildingController : CrudController<Building, EditBuildingModel>
{
private readonly AjaxController _ajax;
public BuildingController(IRepository<Building> repo, AjaxController ajax) : base(repo)
{
this._ajax = ajax;
}
public override IQueryable<Building> Include(IQueryable<Building> query)
{
return query
.Include(o => o.Parent)
.Include(o => o.Organ)
.Include(o => o.BuildingIoTGateways).ThenInclude(o => o.IoTGateway);
}
public override IQueryable<Building> Query(PagedListModel<EditBuildingModel> model, IQueryable<Building> query)
{
return query
.WhereIf(model.Query.ParentId.HasValue, o => o.ParentId == model.Query.ParentId.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(model.Query.Type.HasValue, o => o.Type == model.Query.Type.Value)
.WhereIf(!string.IsNullOrEmpty(model.Query.CustomType), o => o.CustomType.Contains(model.Query.CustomType));
}
public override void ToDisplayModel(Building entity, EditBuildingModel model)
{
model.IoTGateways = entity.BuildingIoTGateways.Select(o => o.IoTGatewayId).ToList();
entity.BuildingIoTGateways.ForEach(o => ViewData.Add(o.IoTGatewayId, o.IoTGateway.Name));
if (entity.ParentId.HasValue)
{
ViewData.Add(model.ParentId.Value, entity.Parent.Name);
}
if (entity.OrganId.HasValue)
{
ViewData.Add(model.OrganId.Value, entity.Organ.Name);
}
}
public override void ToEditModel(Building entity, EditBuildingModel model)
{
if (entity != null)
{
model.IoTGateways = entity.BuildingIoTGateways.Select(o => o.IoTGatewayId).ToList();
}
this.ViewData.MultiSelectList(o => model.IoTGateways, () => this._ajax.GetNodeMultiSelectList(model.IoTGateways));
ViewData.SelectList(o => model.OrganId, () => this._ajax.GetOrganSelectList(model.OrganId));
if (model.OrganId.HasValue)
{
ViewData.SelectList(o => model.ParentId, () => this._ajax.GetOrganBuilding(model.OrganId.Value, model.ParentId, model.Id).Value as SelectList);
}
}
public override void ToEntity(EditBuildingModel model, Building entity)
{
if (!model.ParentId.HasValue)
{
var root = this.Repo.ReadOnlyTable().FirstOrDefault(o => o.ParentId == null);
if (root == null)
{
throw new Exception("根节点没有初始化");
}
entity.ParentId = root.Id;
}
foreach (var id in entity.BuildingIoTGateways.Select(o => o.IoTGatewayId).ToList())
{
if (!model.IoTGateways.Any(o => o == id))
{
entity.BuildingIoTGateways.RemoveAll(o => o.IoTGatewayId == id);
}
}
foreach (var id in model.IoTGateways)
{
if (!entity.BuildingIoTGateways.Any(o => o.IoTGatewayId == id))
{
entity.BuildingIoTGateways.Add(new BuildingIoTGateway { IoTGatewayId = id });
}
}
}
public override IActionResult Add(EditBuildingModel model)
{
if (ModelState.IsValid)
{
try
{
var entity = new Building();
entity.From(model);
this.ToEntity(model, entity);
this.Repo.Add(entity);
this.Repo.SaveChanges();
Update();
return this.Success();
}
catch (DbUpdateException ex)
{
ex.PrintStack();
ModelState.AddModelError("", $"{ex.Message}{ex.InnerException?.Message}");
}
}
this.ToEditModel(null, model);
return Result(model);
}
public override IActionResult Edit(EditBuildingModel model)
{
var id = model.Id;
var query = this.Repo.Table();
query = this.Include(query);
var entity = query.FirstOrDefault(o => o.Id == id);
if (ModelState.IsValid)
{
try
{
if (model.ParentId != null)
{
if (model.ParentId == model.Id)
{
throw new Exception("上级节点不能是当前节点");
}
var parent = this.Repo.Table().ToList().FirstOrDefault(o => o.Id == model.ParentId);
while (parent.ParentId.HasValue)
{
if (parent.ParentId == model.Id)
{
throw new Exception("上级节点不能是当前节点的下级节点");
}
else
{
parent = parent.Parent;
}
}
}
entity.From(model, skipReadonly: true);
this.ToEntity(model, entity);
this.Repo.SaveChanges();
return this.Success();
}
catch (Exception ex)
{
ex.PrintStack();
ModelState.AddModelError("", ex.Message);
}
}
this.ToEditModel(entity, model);
return Result(model);
}
private void Update()
{
var root = this.Repo.Table().ToList().FirstOrDefault(o => o.ParentId == null);
root.Update();
this.Repo.SaveChanges();
}
}
}