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.
130 lines
4.7 KiB
130 lines
4.7 KiB
using Application.Domain.Entities;
|
|
using Infrastructure.Application;
|
|
using Infrastructure.Data;
|
|
using Infrastructure.Extensions;
|
|
using Infrastructure.Web.Mvc;
|
|
using IoT.Shared.Areas.Admin.Controlls;
|
|
using IoTCenter.Application.Models;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using System;
|
|
using System.Linq;
|
|
|
|
namespace IoTCenter.Areas.Admin.Controllers
|
|
{
|
|
[Authorize]
|
|
[Area(nameof(Admin))]
|
|
public class OrganController : CrudController<Organ, EditOrganModel>
|
|
{
|
|
private readonly AjaxController _ajax;
|
|
|
|
public OrganController(IRepository<Organ> repo, AjaxController ajax) : base(repo)
|
|
{
|
|
this._ajax = ajax;
|
|
}
|
|
|
|
public override IQueryable<Organ> Include(IQueryable<Organ> query)
|
|
{
|
|
return query
|
|
.Include(o => o.Area)
|
|
.Include(o => o.Parent)
|
|
.Include(o => o.OrganNodes).ThenInclude(o => o.Node);
|
|
}
|
|
|
|
public override IQueryable<Organ> Query(PagedListModel<EditOrganModel> model, IQueryable<Organ> query)
|
|
{
|
|
return base.Query(model, 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));
|
|
}
|
|
|
|
public override void ToDisplayModel(Organ entity, EditOrganModel model)
|
|
{
|
|
model.Nodes = entity.OrganNodes.Select(o => o.NodeId).ToList();
|
|
entity.OrganNodes.ForEach(o => ViewData.Add(o.NodeId, o.Node.Name));
|
|
if (entity.AreaId.HasValue)
|
|
{
|
|
ViewData.Add(entity.AreaId.Value, entity.Area.Name);
|
|
}
|
|
if (entity.ParentId.HasValue)
|
|
{
|
|
ViewData.Add(entity.ParentId.Value, entity.Parent.Name);
|
|
}
|
|
}
|
|
|
|
public override void ToEditModel(Organ entity, EditOrganModel model)
|
|
{
|
|
if (entity != null)
|
|
{
|
|
model.Nodes = entity.OrganNodes.Select(o => o.NodeId).ToList();
|
|
}
|
|
this.ViewData.MultiSelectList(o => model.Nodes, () => this._ajax.GetNodeMultiSelectList(model.Nodes));
|
|
this.ViewData.SelectList(o => model.AreaId, () => this._ajax.GetAreaSelectList(model.AreaId));
|
|
this.ViewData.SelectList(o => model.ParentId, () => this._ajax.GetOrganSelectList(model.ParentId));
|
|
}
|
|
|
|
public override void ToEntity(EditOrganModel model, Organ entity)
|
|
{
|
|
foreach (var id in entity.OrganNodes.Select(o => o.NodeId).ToList())
|
|
{
|
|
if (!model.Nodes.Any(o => o == id))
|
|
{
|
|
entity.OrganNodes.RemoveAll(o => o.NodeId == id);
|
|
}
|
|
}
|
|
foreach (var id in model.Nodes)
|
|
{
|
|
if (!entity.OrganNodes.Any(o => o.NodeId == id))
|
|
{
|
|
entity.OrganNodes.Add(new OrganNode { NodeId = id });
|
|
}
|
|
}
|
|
}
|
|
public override IActionResult Edit(EditOrganModel 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);
|
|
}
|
|
|
|
}
|
|
}
|