using NCC.Common.Core.Manager;
using NCC.Common.Enum;
using NCC.Common.Extension;
using NCC.Common.Filter;
using NCC.Dependency;
using NCC.DynamicApiController;
using NCC.FriendlyException;
using NCC.Extend.Interfaces.UavPermissionRelation;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using NCC.Extend.Entitys;
using NCC.Extend.Entitys.Dto.UavPermissionRelation;
using Yitter.IdGenerator;
using NCC.Common.Helper;
using NCC.JsonSerialization;
using NCC.System.Entitys.System;
namespace NCC.Extend.UavPermissionRelation
{
///
/// 权限绑定服务
///
[ApiDescriptionSettings(Tag = "权限绑定服务", Name = "UavPermissionRelation", Order = 200)]
[Route("api/Extend/[controller]")]
public class UavPermissionRelationService : IUavPermissionRelationService, IDynamicApiController, ITransient
{
private readonly ISqlSugarRepository _uavPermissionRelationRepository;
private readonly SqlSugarScope _db;
private readonly IUserManager _userManager;
///
/// 初始化一个类型的新实例
///
public UavPermissionRelationService(
ISqlSugarRepository uavPermissionRelationRepository,
IUserManager userManager)
{
_uavPermissionRelationRepository = uavPermissionRelationRepository;
_db = _uavPermissionRelationRepository.Context;
_userManager = userManager;
}
#region 获取权限绑定列表
///
/// 获取权限绑定列表
///
/// 请求参数
///
[HttpGet("")]
public async Task GetList([FromQuery] UavPermissionRelationListQueryInput input)
{
var sidx = input.sidx == null ? "id" : input.sidx;
var data = await _db.Queryable()
.WhereIF(!string.IsNullOrEmpty(input.userId), p => p.UserId.Contains(input.userId))
.Select(it => new UavPermissionRelationListOutput
{
id = it.Id,
userId = it.UserId,
permissionId = it.PermissionId,
}).MergeTable().OrderBy(sidx + " " + input.sort).ToPagedListAsync(input.currentPage, input.pageSize);
return PageResult.SqlSugarPageResult(data);
}
#endregion
#region 新建权限绑定
///
/// 新建权限绑定
///
/// 参数
///
[HttpPost("")]
public async Task Create([FromBody] UavPermissionRelationCrInput input)
{
var entity = input.Adapt();
entity.Id = YitIdHelper.NextId().ToString();
var isOk = await _db.Insertable(entity).IgnoreColumns(ignoreNullColumn: true).ExecuteCommandAsync();
if (!(isOk > 0)) throw NCCException.Oh(ErrorCode.COM1000);
}
#endregion
#region 更新权限绑定
///
/// 更新权限绑定
///
/// 主键
/// 参数
///
[HttpPut("{id}")]
public async Task Update(string id, [FromBody] UavPermissionRelationUpInput input)
{
var entity = input.Adapt();
var isOk = await _db.Updateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
if (!(isOk > 0)) throw NCCException.Oh(ErrorCode.COM1001);
}
#endregion
#region 设置权限
///
/// 设置权限
///
///
///
///
[HttpPost("SetPermission/{userId}")]
public async Task SetPermission(string userId, [FromBody] List permissions)
{
var result = _db.Ado.UseTran(() =>
{
// 删除旧的权限绑定
_db.Deleteable().Where(p => p.UserId == userId).ExecuteCommand();
// 构造新的权限实体集合
var insertList = permissions.Select(item => new UavPermissionRelationEntity
{
Id = YitIdHelper.NextId().ToString(),
UserId = userId,
PermissionId = item,
CreateTime = DateTime.Now
}).ToList();
if (insertList.Any())
{
_db.Insertable(insertList).ExecuteCommand();
}
});
if (!result.IsSuccess)
{
throw new Exception("绑定失败:" + result.ErrorMessage);
}
}
#endregion
#region 删除权限绑定
///
/// 删除权限绑定
///
///
[HttpDelete("{id}")]
public async Task Delete(string id)
{
var entity = await _db.Queryable().FirstAsync(p => p.Id == id);
_ = entity ?? throw NCCException.Oh(ErrorCode.COM1005);
var isOk = await _db.Deleteable().Where(d => d.Id == id).ExecuteCommandAsync();
if (!(isOk > 0)) throw NCCException.Oh(ErrorCode.COM1002);
}
#endregion
#region 获取微信小程序的功能菜单
///
/// 获取微信小程序的功能菜单
///
///
[HttpGet("GetWxAppMenu")]
public async Task GetWxAppMenu()
{
var menu = await _db.Queryable().Where(p => p.Category == "App" && p.DeleteMark == null && p.ParentId != "-1").ToListAsync();
return menu;
}
#endregion
#region 获取指定用户的权限
///
/// 获取指定用户的权限
///
///
///
[HttpGet("GetUserPermissions/{userId}")]
public async Task GetUserPermissions(string userId)
{
var permissions = await _db.Queryable().Where(p => p.UserId == userId).Select(p => p.PermissionId).ToListAsync();
return permissions;
}
#endregion
#region 获取我的权限
///
/// 获取我的权限
///
///
[HttpGet("GetMyPermissions")]
public async Task GetMyPermissions()
{
var permissions = await _db.Queryable().Where(p => p.UserId == _userManager.UserId).Select(p => p.PermissionId).ToListAsync();
var menu = await _db.Queryable().Where(p => permissions.Contains(p.Id)).ToListAsync();
return menu;
}
#endregion
}
}