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.Linq; namespace Platform.Areas.Admin.Controllers { [Authorize] [ApiController] [Route("Admin/[controller]/[action]")] [Area("Admin")] [ControllerScope(ControllerScopeType.Platform)] public class BuildingController : TreeCrudController { private readonly AjaxController _ajax; private readonly IRepository _organRepo; public BuildingController(IRepository repo, AjaxController ajax, IRepository organRepo) : base(repo) { this._ajax = ajax; this._organRepo = organRepo; } public override IQueryable Include(IQueryable query) { return query.Include(o => o.Organ); } public override IQueryable Query(PagedListModel model, IQueryable query) { return query .Where(o => o.ParentId != null) .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.Organ.Id).ThenBy(o => o.ParentId).ThenBy(o => o.Type).ThenBy(o => o.CustomType).ThenBy(o => o.DisplayOrder); } public override void ToDisplayModel(Building entity, EditBuildingModel model) { if (entity.ParentId.HasValue) { var name = this.Repo.ReadOnlyTable() .Where(o=>o.ParentId!=null) .Where(o => o.Left < entity.Left && o.Right > entity.Right) .ToList() .ToTree() .FirstOrDefault(o => o.Id == model.ParentId.Value)?.GetDisplayName(); ViewData.Add(model.ParentId.Value, name); } if (entity != null) { var name = this._organRepo.ReadOnlyTable() .Where(o => o.ParentId != null) .Where(o => o.Left <= entity.Organ.Left && o.Right >= entity.Organ.Right) .ToList() .ToTree() .FirstOrDefault(o => o.Id == entity.OrganId)?.GetDisplayName(); ViewData.Add(model.OrganId, name); } } public override void ToEditModel(Building entity, EditBuildingModel model) { base.ToEditModel(entity, model); this.ViewData.SelectList(o => model.OrganId, () => this._ajax.GetOrgan(model.OrganId).SelectList()); if (model.OrganId.HasValue) { ViewData.SelectList(o => model.ParentId, () => this._ajax.GetOrganBuilding(model.OrganId, model.ParentId).SelectList()); } } } }