/******************************************************************************* * Copyright © 2020 HT.Cloud.Framework 版权所有 * Author: HT.Cloud * Description: WaterCloud快速开发平台 * Website: *********************************************************************************/ using SqlSugar; using System; using System.Collections.Generic; using System.Threading.Tasks; using HT.Cloud.Code; using HT.Cloud.DataBase; using HT.Cloud.Domain.SystemOrganize; using HT.Cloud.Service.SystemManage; namespace HT.Cloud.Service.SystemOrganize { public class RoleService : BaseService, IDenpendency { public ModuleService moduleApp { get; set; } public ModuleButtonService moduleButtonApp { get; set; } public ModuleFieldsService moduleFieldsApp { get; set; } public ItemsDataService itemsApp { get; set; } /// /// 缓存操作类 /// private string authorizecacheKey = GlobalContext.SystemConfig.ProjectPrefix + "_authorizeurldata_";// +权限 //获取类名 public RoleService(ISqlSugarClient context) : base(context) { } public async Task> GetList(string keyword = "") { var query = GetQuery(); if (!string.IsNullOrEmpty(keyword)) { query = query.Where(a => a.F_FullName.Contains(keyword) || a.F_EnCode.Contains(keyword)); } return await query.ToListAsync(); } public async Task> GetLookList(SoulPage pagination, string keyword = "") { //反格式化显示只能用"等于",其他不支持 Dictionary> dic = new Dictionary>(); Dictionary enabledTemp = new Dictionary(); enabledTemp.Add("1", "有效"); enabledTemp.Add("0", "无效"); dic.Add("F_EnabledMark", enabledTemp); var setList = await itemsApp.GetItemList("RoleType"); Dictionary messageTypeTemp = new Dictionary(); foreach (var item in setList) { messageTypeTemp.Add(item.F_ItemCode, item.F_ItemName); } dic.Add("F_Type", messageTypeTemp); pagination = ChangeSoulData(dic, pagination); //获取数据权限 var query = GetQuery(); if (!string.IsNullOrEmpty(keyword)) { query = query.Where(a => a.F_FullName.Contains(keyword) || a.F_EnCode.Contains(keyword)); } query = GetDataPrivilege("a", "", query); return await query.ToPageListAsync(pagination); } public async Task GetForm(string keyValue) { var data = await repository.FindEntity(keyValue); return data; } public async Task GetLookForm(string keyValue) { var data = await repository.FindEntity(keyValue); return GetFieldsFilterData(data); } private ISugarQueryable GetQuery() { var query = repository.Db.Queryable((a, b) => new JoinQueryInfos( JoinType.Left, a.F_CompanyId == b.F_Id )).Where(a => a.F_DeleteMark == false && a.F_Category == 1) .Select((a, b) => new RoleExtend { F_Id = a.F_Id, F_AllowDelete = a.F_AllowDelete, F_AllowEdit = a.F_AllowEdit, F_Category = a.F_Category, F_CreatorTime = a.F_CreatorTime, F_CreatorUserId = a.F_CreatorUserId, F_Description = a.F_Description, F_DeleteMark = a.F_DeleteMark, F_EnabledMark = a.F_EnabledMark, F_EnCode = a.F_EnCode, F_FullName = a.F_FullName, F_CompanyId = a.F_CompanyId, F_SortCode = a.F_SortCode, F_Type = a.F_Type, F_CompanyName = b.F_CompanyName, }).MergeTable(); return query; } public async Task DeleteForm(string keyValue) { if (await repository.Db.Queryable().Where(a => a.F_RoleId.Contains(keyValue)).AnyAsync()) { throw new Exception("角色使用中,无法删除"); } repository.Db.Ado.BeginTran(); await repository.Delete(a => a.F_Id == keyValue); await repository.Db.Deleteable(a => a.F_ObjectId == keyValue).ExecuteCommandAsync(); repository.Db.Ado.CommitTran(); await CacheHelper.RemoveAsync(authorizecacheKey + repository.Db.CurrentConnectionConfig.ConfigId + "_list"); } public async Task SubmitForm(RoleEntity roleEntity, string[] permissionIds, string[] permissionfieldsIds, string keyValue) { if (!string.IsNullOrEmpty(keyValue)) { roleEntity.F_Id = keyValue; } else { roleEntity.F_DeleteMark = false; roleEntity.F_AllowEdit = false; roleEntity.F_AllowDelete = false; roleEntity.Create(); } var moduledata = await moduleApp.GetList(); var buttondata = await moduleButtonApp.GetList(); var fieldsdata = await moduleFieldsApp.GetList(); List roleAuthorizeEntitys = new List(); foreach (var itemId in permissionIds) { RoleAuthorizeEntity roleAuthorizeEntity = new RoleAuthorizeEntity(); roleAuthorizeEntity.F_Id = Utils.GuId(); roleAuthorizeEntity.F_ObjectType = 1; roleAuthorizeEntity.F_ObjectId = roleEntity.F_Id; roleAuthorizeEntity.F_ItemId = itemId; if (moduledata.Find(a => a.F_Id == itemId) != null) { roleAuthorizeEntity.F_ItemType = 1; roleAuthorizeEntitys.Add(roleAuthorizeEntity); } if (buttondata.Find(a => a.F_Id == itemId) != null) { roleAuthorizeEntity.F_ItemType = 2; roleAuthorizeEntitys.Add(roleAuthorizeEntity); } } foreach (var itemId in permissionfieldsIds) { RoleAuthorizeEntity roleAuthorizeEntity = new RoleAuthorizeEntity(); roleAuthorizeEntity.F_Id = Utils.GuId(); roleAuthorizeEntity.F_ObjectType = 1; roleAuthorizeEntity.F_ObjectId = roleEntity.F_Id; roleAuthorizeEntity.F_ItemId = itemId; if (fieldsdata.Find(a => a.F_Id == itemId) != null) { roleAuthorizeEntity.F_ItemType = 3; roleAuthorizeEntitys.Add(roleAuthorizeEntity); } } repository.Db.Ado.BeginTran(); if (!string.IsNullOrEmpty(keyValue)) { await repository.Update(roleEntity); } else { roleEntity.F_Category = 1; await repository.Insert(roleEntity); } await repository.Db.Deleteable(a => a.F_ObjectId == roleEntity.F_Id).ExecuteCommandAsync(); await repository.Db.Insertable(roleAuthorizeEntitys).ExecuteCommandAsync(); repository.Db.Ado.CommitTran(); await CacheHelper.RemoveAsync(authorizecacheKey + repository.Db.CurrentConnectionConfig.ConfigId + "_list"); } } }