using JNPF.Common.Core.Manager;
using JNPF.Common.Enums;
using JNPF.Common.Extension;
using JNPF.Common.Filter;
using JNPF.Common.Security;
using JNPF.DependencyInjection;
using JNPF.DynamicApiController;
using JNPF.FriendlyException;
using JNPF.Systems.Entitys.Dto.ModuleButton;
using JNPF.Systems.Entitys.Permission;
using JNPF.Systems.Entitys.System;
using JNPF.Systems.Interfaces.System;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
namespace JNPF.Systems;
///
/// 功能按钮
/// 版 本:V3.2
/// 版 权:拓通智联科技有限公司(http://www.tuotong-tech.com)
/// 日 期:2021-06-01.
///
[ApiDescriptionSettings(Tag = "System", Name = "ModuleButton", Order = 212)]
[Route("api/system/[controller]")]
public class ModuleButtonService : IModuleButtonService, IDynamicApiController, ITransient
{
///
/// 服务基础仓储.
///
private readonly ISqlSugarRepository _repository;
///
/// 用户管理器.
///
private readonly IUserManager _userManager;
///
/// 初始化一个类型的新实例.
///
public ModuleButtonService(
ISqlSugarRepository repository,
IUserManager userManager)
{
_repository = repository;
_userManager = userManager;
}
#region GET
///
/// 获取按钮权限列表.
///
/// 功能id.
/// 参数.
///
[HttpGet("{moduleId}/List")]
public async Task GetList(string moduleId, [FromQuery] KeywordInput input)
{
var list = await GetList(moduleId);
if (input.keyword.IsNotEmptyOrNull())
list = list.FindAll(t => t.EnCode.Contains(input.keyword) || t.FullName.Contains(input.keyword));
var treeList = list.Adapt>();
return new { list = treeList.ToTree("-1") };
}
///
/// 获取按钮权限下拉列表.
///
/// 菜单ID.
///
[HttpGet("{moduleId}/Selector")]
public async Task GetSelector(string moduleId)
{
var treeList = (await GetList(moduleId)).Adapt>();
return new { list = treeList.ToTree("-1") };
}
///
/// 获取按钮信息.
///
/// 主键id.
///
[HttpGet("{id}")]
public async Task GetInfo(string id)
{
var data = await _repository.GetFirstAsync(x => x.Id == id && x.DeleteMark == null);
return data.Adapt();
}
#endregion
#region Post
///
/// 添加按钮.
///
/// 参数.
///
[HttpPost("")]
public async Task Create([FromBody] ModuleButtonCrInput input)
{
var entity = input.Adapt();
if (await _repository.IsAnyAsync(x => (x.EnCode == input.enCode || x.FullName == input.fullName) && x.DeleteMark == null && x.ModuleId == input.moduleId))
throw Oops.Oh(ErrorCode.COM1004);
var isOk = await Create(entity);
if (isOk < 1)
throw Oops.Oh(ErrorCode.COM1000);
}
///
/// 修改按钮.
///
/// 主键值.
/// 参数.
///
[HttpPut("{id}")]
public async Task Update(string id, [FromBody] ModuleButtonUpInput input)
{
var entity = input.Adapt();
var isOk = await _repository.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).CallEntityMethod(m => m.LastModify()).ExecuteCommandHasChangeAsync();
if (!isOk)
throw Oops.Oh(ErrorCode.COM1001);
}
///
/// 删除按钮.
///
/// 主键值.
///
[HttpDelete("{id}")]
public async Task Delete(string id)
{
if (!await _repository.IsAnyAsync(r => r.Id == id && r.DeleteMark == null))
throw Oops.Oh(ErrorCode.COM1005);
if (await _repository.IsAnyAsync(x => x.ParentId == id && x.DeleteMark == null))
throw Oops.Oh(ErrorCode.D1007);
var isOk = await _repository.AsUpdateable().SetColumns(it => new ModuleButtonEntity()
{
DeleteMark = 1,
DeleteUserId = _userManager.UserId,
DeleteTime = SqlFunc.GetDate()
}).Where(it => it.Id == id).ExecuteCommandHasChangeAsync();
if (!isOk) throw Oops.Oh(ErrorCode.COM1003);
}
///
/// 更新按钮状态.
///
/// 主键值.
///
[HttpPut("{id}/Actions/State")]
public async Task ActionsState(string id)
{
if (!await _repository.IsAnyAsync(r => r.Id == id && r.DeleteMark == null))
throw Oops.Oh(ErrorCode.COM1005);
var isOk = await _repository.AsUpdateable().SetColumns(it => new ModuleButtonEntity()
{
EnabledMark = SqlFunc.IIF(it.EnabledMark == 1, 0, 1),
LastModifyUserId = _userManager.UserId,
LastModifyTime = SqlFunc.GetDate()
}).Where(it => it.Id == id).ExecuteCommandHasChangeAsync();
if (!isOk) throw Oops.Oh(ErrorCode.COM1003);
}
#endregion
#region PublicMethod
///
/// 列表.
///
///
[NonAction]
public async Task> GetList(string? moduleId = default)
{
return await _repository.AsQueryable().Where(x => x.DeleteMark == null).WhereIF(moduleId.IsNotEmptyOrNull(), it => it.ModuleId == moduleId).OrderBy(o => o.SortCode).ToListAsync();
}
///
/// 创建.
///
/// 实体对象.
///
[NonAction]
public async Task Create(ModuleButtonEntity entity)
{
return await _repository.AsInsertable(entity).IgnoreColumns(ignoreNullColumn: true).CallEntityMethod(m => m.Creator()).ExecuteCommandAsync();
}
///
/// 获取用户模块按钮.
///
///
[NonAction]
public async Task GetUserModuleButtonList()
{
var output = new List();
if (!_userManager.IsAdministrator)
{
var roles = _userManager.Roles;
if (roles.Any())
{
var items = await _repository.AsSugarClient().Queryable().In(a => a.ObjectId, roles).Where(a => a.ItemType == "button").Select(a => a.ItemId).ToListAsync();
var buttons = await _repository.AsQueryable().Where(a => items.Contains(a.Id)).Where(a => a.EnabledMark == 1 && a.DeleteMark == null).Select().OrderBy(q => q.SortCode).ToListAsync();
output = buttons.Adapt>();
}
}
else
{
var buttons = await _repository.AsQueryable().Where(a => a.EnabledMark == 1 && a.DeleteMark == null).Select().OrderBy(q => q.SortCode).ToListAsync();
output = buttons.Adapt>();
}
return output;
}
#endregion
}