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.
iot/projects/Platform/Areas/UserCenter/Controllers/PermissionController.cs

76 lines
3.3 KiB

using Infrastructure.Application;
using Infrastructure.Data;
using Infrastructure.Extensions;
using Infrastructure.Web.Mvc;
using IoT.Shared.Application.Domain.Entities;
using IoT.Shared.Application.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Platform.Areas.IoTCenter.Controllers;
using System.Linq;
namespace IoT.Shared.Areas.UserCenter.Controllers
{
[Authorize]
[ApiController]
[Route("UserCenter/[controller]/[action]")]
[Area("UserCenter")]
[ControllerScope(ControllerScopeType.Platform)]
public class PermissionController : CrudController<Permission, EditPermissionModel>
{
private readonly AjaxController _ajax;
private readonly IRepository<Organ> _organRepo;
private readonly IRepository<PermissionCategory> _permissionCategoryRepo;
public PermissionController(IRepository<Permission> repo, AjaxController ajax, IRepository<Organ> organRepo, IRepository<PermissionCategory> permissionCategoryRepo) : base(repo)
{
this._ajax = ajax;
this._organRepo = organRepo;
this._permissionCategoryRepo = permissionCategoryRepo;
}
public override IQueryable<Permission> Include(IQueryable<Permission> query)
{
return query.Include(o => o.Category);
}
public override IQueryable<Permission> Query(PagedListModel<EditPermissionModel> model, IQueryable<Permission> query)
{
return query
.WhereIf(model.Query.OrganId.HasValue, o => o.OrganId == model.Query.OrganId.Value)
.WhereIf(model.Query.CategoryId.HasValue, o => o.CategoryId == model.Query.CategoryId.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))
.OrderBy(o => o.OrganId).ThenBy(o => o.CategoryId).ThenBy(o => o.Number);
}
public override void ToDisplayModel(Permission entity, EditPermissionModel model)
{
if (entity.CategoryId.HasValue)
{
var name = this._permissionCategoryRepo.ReadOnlyTable()
.Where(o => o.Left < entity.Category.Left && o.Left > 1)
.ToList()
.ToTree()
.FirstOrDefault(o => o.Id == model.CategoryId.Value)?.GetDisplayName();
ViewData.Add(model.CategoryId.Value, name);
}
if (entity.OrganId.HasValue)
{
var name = this._organRepo.ReadOnlyTable()
.Where(o => o.Left <= entity.Organ.Left && o.Left > 1)
.ToList()
.ToTree()
.FirstOrDefault(o => o.Id == entity.OrganId.Value)?.GetDisplayName();
ViewData.Add(model.OrganId, name);
}
}
public override void ToEditModel(Permission entity, EditPermissionModel model)
{
this.ViewData.SelectList(o => model.OrganId, () => this._ajax.GetOrgan(model.OrganId).SelectList());
this.ViewData.SelectList(o => model.CategoryId, () => this._ajax.GetPermissionCategory(null, model.CategoryId).SelectList());
}
}
}