using Furion.DependencyInjection;
using Furion.DistributedIDGenerator;
using Furion.DynamicApiController;
using Microsoft.AspNetCore.Mvc;
using Nirvana.Common;
using Nirvana.Common.ApiBase;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Waste.Domain;
namespace Waste.Application
{
///
/// 账户管理
///
public class RoleService : IRoleService, IDynamicApiController, ITransient
{
private readonly ISqlSugarRepository repository;
private readonly SqlSugarClient dbClient;
public RoleService(ISqlSugarRepository sqlSugarRepository
)
{
repository = sqlSugarRepository;
dbClient = repository.Context;
}
///
/// 删除角色
///
///
///
[HttpGet]
public async Task DeleteFormAsync(Guid keyValue)
{
await dbClient.Deleteable().Where(e => e.Id == keyValue).ExecuteCommandAsync();
await repository.Change().Context.Deleteable().Where(e => e.RoleId == keyValue).ExecuteCommandAsync();
return new ResultInfo { code = ResultState.SUCCESS, message = "删除成功", data = null };
}
///
/// 角色详情
///
///
///
[HttpGet]
public async Task DetailAsync(Guid id)
{
if (id == Guid.Empty)
{
return new RoleSubmitModel() { permissionId = new List() };
}
var data = await dbClient.Queryable().FirstAsync(x => x.Id == id);
return new RoleSubmitModel
{
Id = data.Id,
EnCode = data.EnCode,
Name = data.Name,
Remark = data.Remark,
permissionId = await repository.Change().Context.Queryable().Where(x => x.RoleId == id).Select(x => x.MenuId).ToListAsync()
};
}
///
/// 角色列表
///
///
///
[HttpPost]
public async Task> GetListAsync(QueryParams param)
{
RefAsync totalnum = 0;
var temquery = dbClient.Queryable();
if (param.queryParam != null && param.queryParam.Count > 0)
{
List conModels = new List();
param.queryParam.ForEach(x =>
{
if (!string.IsNullOrEmpty(x.Value))
{
conModels.Add(new ConditionalModel()
{
FieldName = x.Name,
ConditionalType = (ConditionalType)x.Type,
FieldValue = x.Value.Trim()
});
}
});
if (conModels.Count > 0)
{
temquery = temquery.Where(conModels);
}
}
string sorts = string.Format("{0} {1}", param.sort, param.order);
var query = await temquery.OrderBy(sorts)
.ToPageListAsync(param.offset, param.limit, totalnum);
return new PageParms
{
page = param.offset,
Items = query,
totalnum = totalnum,
limit = param.limit
};
}
///
/// 信息提交
///
///
///
public async Task SubmitFormAsync(RoleSubmitModel role)
{
if (role.Id == Guid.Empty)
{
role.Id = IDGen.NextID();
role.CreateTime = DateTime.Now;
role.Remark = role.Remark.ToStr();
await dbClient.Insertable(role).ExecuteCommandAsync();
}
else
{
await dbClient.Updateable()
.SetColumns(x => new W_Role
{
Name = role.Name,
Remark = role.Remark
})
.Where(x => x.Id == role.Id).ExecuteCommandAsync();
}
List roleAuthorizeEntitys = new List();
foreach (var item in role.permissionId)
{
var itemId = item;
W_RoleAuthorize roleAuthorizeEntity = new W_RoleAuthorize
{
CreateTime = DateTime.Now,
RoleId = role.Id,
MenuId = itemId
};
roleAuthorizeEntitys.Add(roleAuthorizeEntity);
}
await repository.Change().Context.Deleteable(t => t.RoleId == role.Id).ExecuteCommandAsync();
if (roleAuthorizeEntitys.Count > 0)
{
await repository.Change().Context.Insertable(roleAuthorizeEntitys).ExecuteCommandAsync();
}
return new ResultInfo { code = ResultState.SUCCESS, message = "成功" };
}
}
}