RoleService.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. using DapperORMCore.Context.DataContext;
  2. using DapperORMCore.Context.Extend;
  3. using DapperORMCore.Model.BaseModel;
  4. using DapperORMCore.Model.CoreModel;
  5. using DapperORMCore.String.Consts;
  6. using DapperORMCore.String.Enums;
  7. using NXWMS.IService.NXWMS;
  8. using NXWMS.Model.AppModels.Condition;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Text;
  12. using WestDistance.DapperORM.Repository.Repositorys;
  13. using DapperORMCore.Context;
  14. using NXWMS.Code;
  15. using System.Linq;
  16. using System.Security.Claims;
  17. using System.IdentityModel.Tokens.Jwt;
  18. using Microsoft.IdentityModel.Tokens;
  19. using Microsoft.IdentityModel.Logging;
  20. using Microsoft.Extensions.Configuration;
  21. using NXWMS.String.Enums;
  22. using System.Data;
  23. using NXWMS.Code.Converter;
  24. using NXWMS.DataAccess.Entity;
  25. using DapperORMCore.Repository.IRepositorys;
  26. using DapperORMCore.Dapper.BaseModel;
  27. using NXWMS.IService.NXWMS.SysSettings;
  28. using NXWMS.Model.AppModels.Condition.SysSettings;
  29. using NXWMS.Model.AppModels.Result.SysSettings;
  30. using NXWMS.Code.Extends;
  31. using NXWMS.Model.Common;
  32. namespace NXWMS.Service.NXWMS.SysSettings
  33. {
  34. /// <summary>
  35. /// 角色服务
  36. /// </summary>
  37. [AutoInject(typeof(IRoleService), InjectType.Scope)]
  38. public class RoleService : ServiceBase, IRoleService
  39. {
  40. /// <summary>
  41. /// 系统操作仓储中转
  42. /// </summary>
  43. private IDataRepositoryContext _dataContext;
  44. /// <summary>
  45. /// SQL节点仓储
  46. /// </summary>
  47. private ISQLNodeRepository _iSQLNodeRepository;
  48. /// <summary>
  49. /// 配置
  50. /// </summary>
  51. private IConfiguration _configuration;
  52. public RoleService(IDataRepositoryContext dataRepositoryContext, IConfiguration configuration, ISQLNodeRepository iSQLNodeRepository)
  53. {
  54. this._dataContext = dataRepositoryContext;
  55. this._configuration = configuration;
  56. this._iSQLNodeRepository = iSQLNodeRepository;
  57. }
  58. public OperateResultInfo<PageQueryResultInfo<RoleResult>> GetList(RoleSearchCondition info)
  59. {
  60. var sqlAndBuilder = new StringBuilder();
  61. var sql = $@"SELECT
  62. CreateName = (SELECT USER_NAME FROM SYS_USER A WHERE A.USER_ID=CREATE_BY),
  63. UpdateName = (SELECT USER_NAME FROM SYS_USER B WHERE B.USER_ID=UPDATE_BY),
  64. UsedFlagName = {_iSQLNodeRepository.GetEnumIntCaseString<UsedFlag>("USED_FLAG")},
  65. {info.ItemSQL} FROM SYS_ROLE WHERE 1=1 AND DEL_FLAG = 0 ";
  66. sqlAndBuilder = info.Id != null ?
  67. info.Id > 0 ?
  68. sqlAndBuilder.Append(_iSQLNodeRepository.GetAddCondition("ROLE_ID", info.Id, DBOperationString._Equal)) :
  69. sqlAndBuilder : sqlAndBuilder;
  70. sqlAndBuilder = info.IsUsed == null ?
  71. sqlAndBuilder :
  72. sqlAndBuilder.Append(_iSQLNodeRepository.GetAddCondition("USED_FLAG", info.IsUsed, DBOperationString._Equal));
  73. sqlAndBuilder = string.IsNullOrWhiteSpace(info.RoleCode) ?
  74. sqlAndBuilder :
  75. sqlAndBuilder.Append(_iSQLNodeRepository.GetAddCondition("ROLE_CODE", info.RoleCode, DBOperationString._ContainIn));
  76. sqlAndBuilder = string.IsNullOrWhiteSpace(info.RoleName) ?
  77. sqlAndBuilder :
  78. sqlAndBuilder.Append(_iSQLNodeRepository.GetAddCondition("ROLE_NAME", info.RoleName, DBOperationString._ContainIn));
  79. sql = sql + (sqlAndBuilder.Length > 0 ? _iSQLNodeRepository.GetAndString(sqlAndBuilder, false) : "");
  80. IEnumerable<RoleResult> result;
  81. IEnumerable<RoleResult> totalResult;
  82. totalResult = new DataRepository<RoleResult>(_dataContext).Query(sql);
  83. if (info.PageIndex == 0 || info.PageSize == 0)
  84. {
  85. result = totalResult.ToList();
  86. }
  87. else
  88. {
  89. result = new DataRepository<RoleResult>(_dataContext).QueryPage(sql,
  90. "CREATE_TIME", info.PageSize, info.PageIndex, true);
  91. }
  92. return SuccessStatus(new PageQueryResultInfo<RoleResult>
  93. {
  94. RowData = result,
  95. PageConditionInfo = info,
  96. TotalCount = totalResult.Count(),
  97. TotalPageCount = (int)Math.Ceiling((double)totalResult.Count() / info.PageSize)
  98. });
  99. }
  100. public OperateResultInfo Deleted(RoleCondition info)
  101. {
  102. var sqlAndBuilder = new StringBuilder();
  103. sqlAndBuilder = info.RoleId != null ?
  104. info.RoleId > 0 ?
  105. sqlAndBuilder.Append(_iSQLNodeRepository.GetAddCondition("ROLE_ID", info.RoleId, DBOperationString._Equal)) :
  106. sqlAndBuilder : sqlAndBuilder;
  107. sqlAndBuilder = string.IsNullOrWhiteSpace(info.RoleCode) ?
  108. sqlAndBuilder :
  109. sqlAndBuilder.Append(_iSQLNodeRepository.GetAddCondition("ROLE_CODE", info.RoleCode, DBOperationString._Equal));
  110. sqlAndBuilder = string.IsNullOrWhiteSpace(info.RoleIds) ?
  111. sqlAndBuilder :
  112. sqlAndBuilder.Append(_iSQLNodeRepository.GetAddCondition("ROLE_ID", info.RoleIds, DBOperationString._In));
  113. if (sqlAndBuilder.Length == 0)
  114. {
  115. return FailMessageStatus("参数错误!");
  116. }
  117. var now = DateTime.Now;
  118. var sql = $@"UPDATE SYS_ROLE SET DEL_FLAG=1,UPDATE_BY={info.OperationUserId},UPDATE_TIME='{now}'
  119. WHERE 1=1 {sqlAndBuilder}";
  120. var affectedRows = new DataRepository<SYS_ROLE>(_dataContext).Execute(sql);
  121. return GetStatus(affectedRows, info.RoleIds.Split(',').Length);
  122. }
  123. public OperateResultInfo Remove(RoleCondition info)
  124. {
  125. var whereList = GetFieldKeyList(info);
  126. var result = new DataRepository<SYS_ROLE>(_dataContext).Query(whereList).FirstOrDefault();
  127. if (result != null)
  128. {
  129. var affectedRows = 0;
  130. if (info.RoleId != null)
  131. {
  132. affectedRows = new DataRepository<SYS_ROLE>(_dataContext).Remove("ROLE_ID", info.RoleId.ToString());
  133. }
  134. if (!string.IsNullOrWhiteSpace(info.RoleCode))
  135. {
  136. affectedRows = new DataRepository<SYS_ROLE>(_dataContext).Remove("ROLE_CODE", info.RoleCode);
  137. }
  138. return GetStatus(affectedRows);
  139. }
  140. return FailMessageStatus("未查找到数据!");
  141. }
  142. public OperateResultInfo Add(RoleCondition info)
  143. {
  144. if (info == null)
  145. {
  146. return FailStatus();
  147. }
  148. //接口中特定错误状态不加了,,既然要简单.
  149. if (new DataRepository<SYS_ROLE>(_dataContext).Query().Where(m => m.ROLE_CODE == info.RoleCode
  150. && m.DEL_FLAG == 0 && m.USED_FLAG == 1).Any())
  151. {
  152. return FailMessageStatus("角色编码已经存在,请输入其它编码!");
  153. }
  154. _dataContext.BeginTran();
  155. var now = DateTime.Now;
  156. var entity = new SYS_ROLE();
  157. var roleId = new DataRepository<SYS_ROLE>(_dataContext).Query().Select(s => s.ROLE_ID).Max() + 1;
  158. entity.CREATE_BY = info.OperationUserId;
  159. entity.CREATE_TIME = now;
  160. entity.UPDATE_BY = info.OperationUserId;
  161. entity.UPDATE_TIME = now;
  162. entity.ROLE_CODE = info.RoleCode;
  163. entity.ROLE_NAME = info.RoleName;
  164. entity.DESCRIBE = info.Describe;
  165. entity.ROLE_ID = roleId;
  166. var affectedRows = new DataRepository<SYS_ROLE>(_dataContext).Add(entity, new string[1] { "ROLE_ID" });
  167. var whereList = new List<FieldKeyInfo>();
  168. whereList.AddFieldKeyInfo(nameof(SYS_ROLE_MENU_PERMISSION.ROLE_ID), roleId,
  169. EnumCSharpPropertyType.INT32, DBOperationString._Equal);
  170. var permissionData = new DataRepository<SYS_ROLE_MENU_PERMISSION>(_dataContext).
  171. Query(whereList).ToList();
  172. permissionData.ForEach(s =>
  173. {
  174. s.DEL_FLAG = 1;
  175. s.UPDATE_BY = info.OperationUserId;
  176. s.UPDATE_TIME = now;
  177. });
  178. affectedRows = new DataRepository<SYS_ROLE_MENU_PERMISSION>(_dataContext).Update(permissionData, "ROLE_MENU_PERMISSION_ID");
  179. if (affectedRows != permissionData.Count)
  180. {
  181. _dataContext.Rollback();
  182. return GetStatus(affectedRows, permissionData.Count);
  183. }
  184. affectedRows = 0;
  185. foreach (int item in info.PermissionMenuIdList)
  186. {
  187. var permission = new SYS_ROLE_MENU_PERMISSION
  188. {
  189. CREATE_TIME = now,
  190. UPDATE_TIME = now,
  191. CREATE_BY = info.OperationUserId,
  192. UPDATE_BY = info.OperationUserId,
  193. MENU_ID = item,
  194. ROLE_ID = roleId,
  195. USED_FLAG = 1,
  196. };
  197. affectedRows = affectedRows + new DataRepository<SYS_ROLE_MENU_PERMISSION>(_dataContext).Add(permission, new string[1] { "ROLE_MENU_PERMISSION_ID" });
  198. }
  199. if (affectedRows != info.PermissionMenuIdList.Count)
  200. {
  201. _dataContext.Rollback();
  202. return GetStatus(affectedRows, info.PermissionMenuIdList.Count);
  203. }
  204. _dataContext.Commit();
  205. return SuccessStatus();
  206. }
  207. public OperateResultInfo Edit(RoleCondition info)
  208. {
  209. if (info.RoleId == null && string.IsNullOrWhiteSpace(info.RoleIds))
  210. {
  211. return FailMessageStatus("参数错误!");
  212. }
  213. var whereList = new List<FieldKeyInfo>()
  214. .AddFieldKeyInfo(nameof(SYS_ROLE.ROLE_ID), info.RoleId, EnumCSharpPropertyType.INT32, DBOperationString._Equal,
  215. info.RoleId != null)
  216. .AddFieldKeyInfo(nameof(SYS_ROLE.ROLE_CODE), info.RoleCode, EnumCSharpPropertyType.STRING, DBOperationString._Equal,
  217. !string.IsNullOrWhiteSpace(info.RoleCode));
  218. var result = new DataRepository<SYS_ROLE>(_dataContext).Query(whereList).FirstOrDefault();
  219. if (result != null)
  220. {
  221. _dataContext.BeginTran();
  222. var now = DateTime.Now;
  223. result.USED_FLAG = Convert.ToInt32(info.IsUsed);
  224. result.ROLE_ID = result.ROLE_ID;
  225. result.UPDATE_BY = info.OperationUserId;
  226. result.UPDATE_TIME = now;
  227. result.ROLE_CODE = info.RoleCode;
  228. result.ROLE_NAME = info.RoleName;
  229. result.DESCRIBE = info.Describe;
  230. var affectedRows = new DataRepository<SYS_ROLE>(_dataContext).Update(result, "ROLE_ID", "NEWID");
  231. //菜单权限分配
  232. whereList = new List<FieldKeyInfo>();
  233. whereList.AddFieldKeyInfo(nameof(SYS_ROLE_MENU_PERMISSION.ROLE_ID), result.ROLE_ID,
  234. EnumCSharpPropertyType.INT32, DBOperationString._Equal);
  235. var permissionData = new DataRepository<SYS_ROLE_MENU_PERMISSION>(_dataContext).
  236. Query(whereList).ToList();
  237. permissionData.ForEach(s =>
  238. {
  239. s.DEL_FLAG = 1;
  240. s.UPDATE_BY = info.OperationUserId;
  241. s.UPDATE_TIME = now;
  242. });
  243. affectedRows = new DataRepository<SYS_ROLE_MENU_PERMISSION>(_dataContext).Update(permissionData, "ROLE_MENU_PERMISSION_ID");
  244. if (affectedRows != permissionData.Count)
  245. {
  246. _dataContext.Rollback();
  247. return GetStatus(affectedRows, permissionData.Count);
  248. }
  249. affectedRows = 0;
  250. if (info.PermissionMenuIdList.Any())
  251. {
  252. foreach (int item in info.PermissionMenuIdList)
  253. {
  254. if (!new DataRepository<SYS_ROLE_MENU_PERMISSION>(_dataContext).Query().
  255. Where(s => s.ROLE_ID == result.ROLE_ID && s.MENU_ID == item && s.DEL_FLAG==0).Any())
  256. {
  257. var permission = new SYS_ROLE_MENU_PERMISSION
  258. {
  259. CREATE_TIME = now,
  260. UPDATE_TIME = now,
  261. CREATE_BY = info.OperationUserId,
  262. UPDATE_BY = info.OperationUserId,
  263. MENU_ID = item,
  264. ROLE_ID = result.ROLE_ID,
  265. USED_FLAG = 1,
  266. };
  267. affectedRows = affectedRows + new DataRepository<SYS_ROLE_MENU_PERMISSION>(_dataContext).Add(permission, new string[1] { "ROLE_MENU_PERMISSION_ID" });
  268. }
  269. }
  270. }
  271. if (affectedRows != info.PermissionMenuIdList.Count)
  272. {
  273. _dataContext.Rollback();
  274. return GetStatus(affectedRows, info.PermissionMenuIdList.Count);
  275. }
  276. _dataContext.Commit();
  277. return SuccessStatus();
  278. }
  279. return FailMessageStatus("未查找到数据!");
  280. }
  281. /// <summary>
  282. /// 获取角色对应授权信息
  283. /// </summary>
  284. /// <param name="info"></param>
  285. /// <returns></returns>
  286. public OperateResultInfo<RolePermissionResult> GetPermissionInfo(RolePermissionCondition info)
  287. {
  288. if (info.RoleId == null && string.IsNullOrWhiteSpace(info.RoleCode))
  289. {
  290. return FailMessageStatus("参数错误!", new RolePermissionResult());
  291. }
  292. var whereList = new List<FieldKeyInfo>()
  293. .AddFieldKeyInfo(nameof(SYS_ROLE.ROLE_ID), info.RoleId, EnumCSharpPropertyType.INT32, DBOperationString._Equal,
  294. info.RoleId != null)
  295. .AddFieldKeyInfo(nameof(SYS_ROLE.ROLE_CODE), info.RoleCode, EnumCSharpPropertyType.STRING, DBOperationString._Equal,
  296. !string.IsNullOrWhiteSpace(info.RoleCode));
  297. var result = new DataRepository<SYS_ROLE>(_dataContext).Query(whereList).FirstOrDefault();
  298. if (result != null)
  299. {
  300. var sql = string.Format(@"SELECT
  301. MainMenuId = a.MENU_ID, MainMenuCode = a.MENU_CODE,MainMenuName=a.MENU_NAME,MainMenuOrder=a.MENU_ORDER,MainMenuURL=a.MENU_URL
  302. FROM SYS_ROLE_MENU_PERMISSION T
  303. JOIN SYS_ROLE R ON T.ROLE_ID = R.ROLE_ID AND R.DEL_FLAG = 0 AND R.USED_FLAG = 1 AND T.DEL_FLAG = 0 AND T.USED_FLAG = 1
  304. JOIN SYS_MENU A ON T.MENU_ID = A.MENU_ID AND A.DEL_FLAG = 0 AND A.USED_FLAG = 1 AND A.DEL_FLAG = 0
  305. LEFT JOIN SYS_MENU B ON T.MENU_ID = B.MENU_ID AND B.USED_FLAG = 1 AND B.DEL_FLAG = 0
  306. LEFT JOIN SYS_MENU C ON C.USED_FLAG = 1 AND C.DEL_FLAG = 0 AND C.MENU_PID != -1
  307. AND T.MENU_ID = C.MENU_ID
  308. WHERE 1 = 1 AND T.ROLE_ID = {0}", result.ROLE_ID);
  309. var roleMenuResult = new DataRepository<RoleMenuInfo>(_dataContext).Query(sql);
  310. return SuccessStatus(new RolePermissionResult
  311. {
  312. RoleCode = result.ROLE_CODE,
  313. RoleId = result.ROLE_ID,
  314. RoleName = result.ROLE_NAME,
  315. RoleMenuList = roleMenuResult.ToList()
  316. });
  317. }
  318. return FailMessageStatus<RolePermissionResult>("未查找到数据!", null);
  319. }
  320. #region Private
  321. /// <summary>
  322. /// 条件统一下,用统一筛选
  323. /// </summary>
  324. /// <param name="RoleCondition"></param>
  325. /// <returns></returns>
  326. private List<FieldKeyInfo> GetFieldKeyList(RoleCondition info)
  327. {
  328. var whereList = new List<FieldKeyInfo>()
  329. .AddFieldKeyInfo(nameof(SYS_ROLE.ROLE_ID), info.RoleId, EnumCSharpPropertyType.INT32, DBOperationString._Equal,
  330. info.RoleId != null)
  331. .AddFieldKeyInfo(nameof(SYS_ROLE.ROLE_ID), info.RoleIds, EnumCSharpPropertyType.STRING, DBOperationString._In,
  332. !string.IsNullOrWhiteSpace(info.RoleIds))
  333. .AddFieldKeyInfo(nameof(SYS_ROLE.ROLE_CODE), info.RoleCode, EnumCSharpPropertyType.STRING, DBOperationString._Equal,
  334. !string.IsNullOrWhiteSpace(info.RoleCode));
  335. return whereList;
  336. }
  337. #endregion
  338. }
  339. }