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 : TreeCrudController { private readonly AjaxController _ajax; private readonly IRepository _areaRepo; public OrganController(IRepository repo, AjaxController ajax, IRepository areaRepo) : base(repo) { this._ajax = ajax; this._areaRepo = areaRepo; } public override IQueryable Include(IQueryable query) { return query.Include(o => o.Area); } public override IQueryable Query(PagedListModel model, IQueryable 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)) .OrderBy(o=>o.AreaId).ThenBy(o=>o.ParentId).ThenBy(o=>o.DisplayOrder); } public override void ToDisplayModel(Organ entity, EditOrganModel model) { if(model.AreaId.HasValue) { var name = this._areaRepo.ReadOnlyTable() .Where(o => o.Left <= entity.Area.Left && o.Left > 1) .ToList() .ToTree().FirstOrDefault(o => o.Id == model.AreaId.Value)?.GetDisplayName(); ViewData.Add(model.AreaId.Value, name); } if(model.ParentId.HasValue) { var name = this.Repo.ReadOnlyTable() .Where(o => o.Left < entity.Left && o.Left > 1) .ToList() .ToTree() .FirstOrDefault(o => o.Id == model.ParentId.Value)?.GetDisplayName(); ViewData.Add(model.ParentId.Value, 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).SelectList()); } } }