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 OrganUserController : CrudController { private readonly AjaxController _ajax; private readonly IRepository _organRepo; public OrganUserController(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) .Include(o => o.User) .Include(o => o.UserRoles).ThenInclude(o => o.OrganRole); } public override IQueryable Query(PagedListModel model, IQueryable query) { return query .WhereIf(model.Query.OrganId.HasValue, o => o.OrganId == model.Query.OrganId.Value) .WhereIf(model.Query.UserId.HasValue, o => o.UserId == model.Query.UserId.Value) .WhereIf(model.Query.IsDefault.HasValue, o => o.IsDefault == model.Query.IsDefault.Value) .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(OrganUser entity, EditOrganUserModel model) { if (entity != null) { var name = this._organRepo.ReadOnlyTable() .Where(o => o.Left <= entity.Organ.Left && o.Left > 1) .ToList() .ToTree() .FirstOrDefault(o => o.Id == entity.OrganId)?.GetDisplayName(); ViewData.Add(model.OrganId, name); ViewData.Add(model.UserId, $"{entity.User?.UserName} {entity.User?.RealName}"); model.Roles = entity.UserRoles.Select(o => o.OrganRoleId).ToList(); entity.UserRoles.ForEach(o => ViewData.Add(o.OrganRoleId, o.OrganRole.Name)); } } public override void ToEditModel(OrganUser entity, EditOrganUserModel model) { this.ViewData.SelectList(o => model.OrganId, () => this._ajax.GetOrgan(model.OrganId).SelectList()); ViewData.SelectList(o => model.UserId, () => this._ajax.GetUser(model.OrganId, model.UserId).SelectList()); if (entity != null) { model.Roles = entity.UserRoles.Select(o => o.OrganRoleId).ToList(); } if (model.OrganId.HasValue) { ViewData.MultiSelectList(o => model.Roles, () => this._ajax.GetRole(model.OrganId.Value, model.Roles).MultiSelectList()); } } public override void ToEntity(EditOrganUserModel model, OrganUser entity) { foreach (var id in entity.UserRoles.Select(o => o.OrganRoleId).ToList()) { if (!model.Roles.Any(o => o == id)) { entity.UserRoles.RemoveAll(o => !o.IsReadOnly && o.OrganRoleId == id); } } foreach (var id in model.Roles) { if (!entity.UserRoles.Any(o => o.OrganRoleId == id)) { entity.UserRoles.Add(new OrganUserRole { OrganRoleId = id }); } } } } }