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.
156 lines
5.7 KiB
156 lines
5.7 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;
|
|
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 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);
|
|
}
|
|
|
|
public override IQueryable<Organ> Query(PagedListModel<EditOrganModel> model, IQueryable<Organ> query)
|
|
{
|
|
return base.Query(model, query)
|
|
.Where(o => o.ParentId != null)
|
|
.WhereIf(model.Query.AreaId.HasValue, o => o.AreaId == model.Query.AreaId.Value)
|
|
.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(Organ entity, EditOrganModel model)
|
|
{
|
|
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)
|
|
{
|
|
this.ViewData.SelectList(o => model.AreaId, () => this._ajax.GetArea(model.AreaId).SelectList());
|
|
this.ViewData.SelectList(o => model.ParentId, () => this._ajax.GetOrgan(model.ParentId, model.Id).SelectList());
|
|
}
|
|
|
|
public override void ToEntity(EditOrganModel model, Organ 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;
|
|
}
|
|
}
|
|
|
|
public override IActionResult Add(EditOrganModel model)
|
|
{
|
|
if (ModelState.IsValid)
|
|
{
|
|
try
|
|
{
|
|
var entity = new Organ();
|
|
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(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);
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
var root = this.Repo.Table().ToList().FirstOrDefault(o => o.ParentId == null);
|
|
root.Update();
|
|
this.Repo.SaveChanges();
|
|
}
|
|
}
|
|
}
|