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.
100 lines
4.1 KiB
100 lines
4.1 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.Linq;
|
|
|
|
namespace Platform.Areas.UserCenter.Controllers
|
|
{
|
|
[Authorize]
|
|
[ApiController]
|
|
[Route("UserCenter/[controller]/[action]")]
|
|
[Area("UserCenter")]
|
|
[ControllerScope(ControllerScopeType.Platform)]
|
|
public class OrganUserController : CrudController<OrganUser, EditOrganUserModel>
|
|
{
|
|
private readonly AjaxController _ajax;
|
|
private readonly IRepository<Organ> _organRepo;
|
|
|
|
public OrganUserController(IRepository<OrganUser> repo, AjaxController ajax, IRepository<Organ> organRepo) : base(repo)
|
|
{
|
|
this._ajax = ajax;
|
|
this._organRepo = organRepo;
|
|
}
|
|
|
|
public override IQueryable<OrganUser> Include(IQueryable<OrganUser> query)
|
|
{
|
|
return query
|
|
.Include(o => o.Organ)
|
|
.Include(o => o.User)
|
|
.Include(o => o.UserRoles).ThenInclude(o => o.OrganRole);
|
|
}
|
|
|
|
public override IQueryable<OrganUser> Query(PagedListModel<EditOrganUserModel> model, IQueryable<OrganUser> 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());
|
|
if (model.OrganId.HasValue)
|
|
{
|
|
ViewData.SelectList(o => model.UserId, () => this._ajax.GetUser(model.OrganId.Value, 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 });
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} |