SysDictDataService.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. // Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
  2. //
  3. // 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
  4. //
  5. // 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
  6. namespace Admin.NET.Core.Service;
  7. /// <summary>
  8. /// 系统字典值服务 🧩
  9. /// </summary>
  10. [ApiDescriptionSettings(Order = 420, Description = "系统字典值")]
  11. public class SysDictDataService : IDynamicApiController, ITransient
  12. {
  13. private readonly SqlSugarRepository<SysDictData> _sysDictDataRep;
  14. public readonly ISugarQueryable<SysDictData> VSysDictData;
  15. private readonly SysCacheService _sysCacheService;
  16. private readonly UserManager _userManager;
  17. private readonly SysLangTextCacheService _sysLangTextCacheService;
  18. public SysDictDataService(SqlSugarRepository<SysDictData> sysDictDataRep,
  19. SysCacheService sysCacheService,
  20. UserManager userManager,
  21. SysLangTextCacheService sysLangTextCacheService)
  22. {
  23. _userManager = userManager;
  24. _sysDictDataRep = sysDictDataRep;
  25. _sysCacheService = sysCacheService;
  26. _sysLangTextCacheService = sysLangTextCacheService;
  27. VSysDictData = _sysDictDataRep.Context.UnionAll(
  28. _sysDictDataRep.AsQueryable(),
  29. _sysDictDataRep.Change<SysDictDataTenant>().AsQueryable().WhereIF(_userManager.SuperAdmin, d => d.TenantId == _userManager.TenantId).Select<SysDictData>());
  30. }
  31. /// <summary>
  32. /// 获取字典值分页列表 🔖
  33. /// </summary>
  34. /// <param name="input"></param>
  35. /// <returns></returns>
  36. [DisplayName("获取字典值分页列表")]
  37. public async Task<SqlSugarPagedList<SysDictData>> Page(PageDictDataInput input)
  38. {
  39. var langCode = _userManager.LangCode;
  40. var baseQuery = VSysDictData
  41. .Where(u => u.DictTypeId == input.DictTypeId)
  42. .WhereIF(!string.IsNullOrEmpty(input.Code?.Trim()), u => u.Code.Contains(input.Code))
  43. .WhereIF(!string.IsNullOrEmpty(input.Label?.Trim()), u => u.Label.Contains(input.Label))
  44. .OrderBy(u => new { u.OrderNo, u.Code });
  45. var pageList = await baseQuery.ToPagedListAsync(input.Page, input.PageSize);
  46. var list = pageList.Items;
  47. var ids = list.Select(d => d.Id).Distinct().ToList();
  48. var translations = await _sysLangTextCacheService.GetTranslations(
  49. "SysDictData",
  50. "Label",
  51. ids,
  52. langCode);
  53. foreach (var item in list)
  54. {
  55. if (translations.TryGetValue(item.Id, out var translatedLabel) && !string.IsNullOrEmpty(translatedLabel))
  56. {
  57. item.Label = translatedLabel;
  58. }
  59. }
  60. pageList.Items = list;
  61. return pageList;
  62. }
  63. /// <summary>
  64. /// 获取字典值列表 🔖
  65. /// </summary>
  66. /// <returns></returns>
  67. [DisplayName("获取字典值列表")]
  68. public async Task<List<SysDictData>> GetList([FromQuery] GetDataDictDataInput input)
  69. {
  70. var langCode = _userManager.LangCode;
  71. var list = await GetDictDataListByDictTypeId(input.DictTypeId);
  72. var ids = list.Select(d => d.Id).Distinct().ToList();
  73. var translations = await _sysLangTextCacheService.GetTranslations(
  74. "SysDictData",
  75. "Label",
  76. ids,
  77. langCode);
  78. foreach (var item in list)
  79. {
  80. if (translations.TryGetValue(item.Id, out var translatedLabel) && !string.IsNullOrEmpty(translatedLabel))
  81. {
  82. item.Label = translatedLabel;
  83. }
  84. }
  85. return await GetDictDataListByDictTypeId(input.DictTypeId);
  86. }
  87. /// <summary>
  88. /// 增加字典值 🔖
  89. /// </summary>
  90. /// <param name="input"></param>
  91. /// <returns></returns>
  92. [ApiDescriptionSettings(Name = "Add"), HttpPost]
  93. [DisplayName("增加字典值")]
  94. public async Task AddDictData(AddDictDataInput input)
  95. {
  96. var isExist = await VSysDictData.AnyAsync(u => u.Value == input.Value && u.DictTypeId == input.DictTypeId);
  97. if (isExist) throw Oops.Oh(ErrorCodeEnum.D3003);
  98. var dictType = await _sysDictDataRep.Change<SysDictType>().GetByIdAsync(input.DictTypeId);
  99. if (dictType.SysFlag == YesNoEnum.Y && !_userManager.SuperAdmin) throw Oops.Oh(ErrorCodeEnum.D3008);
  100. Remove(dictType);
  101. dynamic dictData = dictType.IsTenant == YesNoEnum.Y ? input.Adapt<SysDictDataTenant>() : input.Adapt<SysDictData>();
  102. await _sysDictDataRep.Context.Insertable(dictData).ExecuteCommandAsync();
  103. }
  104. /// <summary>
  105. /// 更新字典值 🔖
  106. /// </summary>
  107. /// <param name="input"></param>
  108. /// <returns></returns>
  109. [UnitOfWork]
  110. [ApiDescriptionSettings(Name = "Update"), HttpPost]
  111. [DisplayName("更新字典值")]
  112. public async Task UpdateDictData(UpdateDictDataInput input)
  113. {
  114. var isExist = await VSysDictData.AnyAsync(u => u.Id == input.Id);
  115. if (!isExist) throw Oops.Oh(ErrorCodeEnum.D3004);
  116. isExist = await VSysDictData.AnyAsync(u => u.Value == input.Value && u.DictTypeId == input.DictTypeId && u.Id != input.Id);
  117. if (isExist) throw Oops.Oh(ErrorCodeEnum.D3003);
  118. var dictType = await _sysDictDataRep.Change<SysDictType>().GetByIdAsync(input.DictTypeId);
  119. if (dictType.SysFlag == YesNoEnum.Y && !_userManager.SuperAdmin) throw Oops.Oh(ErrorCodeEnum.D3009);
  120. Remove(dictType);
  121. dynamic dictData = dictType.IsTenant == YesNoEnum.Y ? input.Adapt<SysDictDataTenant>() : input.Adapt<SysDictData>();
  122. await _sysDictDataRep.Context.Updateable(dictData).ExecuteCommandAsync();
  123. }
  124. /// <summary>
  125. /// 删除字典值 🔖
  126. /// </summary>
  127. /// <param name="input"></param>
  128. /// <returns></returns>
  129. [UnitOfWork]
  130. [ApiDescriptionSettings(Name = "Delete"), HttpPost]
  131. [DisplayName("删除字典值")]
  132. public async Task DeleteDictData(DeleteDictDataInput input)
  133. {
  134. var dictData = await VSysDictData.FirstAsync(u => u.Id == input.Id) ?? throw Oops.Oh(ErrorCodeEnum.D3004);
  135. var dictType = await _sysDictDataRep.Change<SysDictType>().GetByIdAsync(dictData.DictTypeId);
  136. if (dictType.SysFlag == YesNoEnum.Y && !_userManager.SuperAdmin) throw Oops.Oh(ErrorCodeEnum.D3010);
  137. Remove(dictType);
  138. dynamic entity = dictType.IsTenant == YesNoEnum.Y ? input.Adapt<SysDictDataTenant>() : input.Adapt<SysDictData>();
  139. await _sysDictDataRep.Context.Deleteable(entity).ExecuteCommandAsync();
  140. }
  141. /// <summary>
  142. /// 获取字典值详情 🔖
  143. /// </summary>
  144. /// <param name="input"></param>
  145. /// <returns></returns>
  146. [DisplayName("获取字典值详情")]
  147. public async Task<SysDictData> GetDetail([FromQuery] DictDataInput input)
  148. {
  149. return (await VSysDictData.FirstAsync(u => u.Id == input.Id))?.Adapt<SysDictData>();
  150. }
  151. /// <summary>
  152. /// 修改字典值状态 🔖
  153. /// </summary>
  154. /// <param name="input"></param>
  155. /// <returns></returns>
  156. [UnitOfWork]
  157. [DisplayName("修改字典值状态")]
  158. public async Task SetStatus(DictDataInput input)
  159. {
  160. var dictData = await VSysDictData.FirstAsync(u => u.Id == input.Id) ?? throw Oops.Oh(ErrorCodeEnum.D3004);
  161. var dictType = await _sysDictDataRep.Change<SysDictType>().GetByIdAsync(dictData.DictTypeId);
  162. if (dictType.SysFlag == YesNoEnum.Y && !_userManager.SuperAdmin) throw Oops.Oh(ErrorCodeEnum.D3009);
  163. Remove(dictType);
  164. dictData.Status = input.Status;
  165. dynamic entity = dictType.IsTenant == YesNoEnum.Y ? input.Adapt<SysDictDataTenant>() : input.Adapt<SysDictData>();
  166. await _sysDictDataRep.Context.Updateable(entity).ExecuteCommandAsync();
  167. }
  168. /// <summary>
  169. /// 根据字典类型Id获取字典值集合
  170. /// </summary>
  171. /// <param name="dictTypeId"></param>
  172. /// <returns></returns>
  173. [NonAction]
  174. public async Task<List<SysDictData>> GetDictDataListByDictTypeId(long dictTypeId)
  175. {
  176. return await GetDataListByIdOrCode(dictTypeId, null);
  177. }
  178. /// <summary>
  179. /// 根据字典类型编码获取字典值集合 🔖
  180. /// </summary>
  181. /// <param name="code"></param>
  182. /// <returns></returns>
  183. [DisplayName("根据字典类型编码获取字典值集合")]
  184. public async Task<List<SysDictData>> GetDataList(string code)
  185. {
  186. return await GetDataListByIdOrCode(null, code);
  187. }
  188. /// <summary>
  189. /// 获取字典值集合 🔖
  190. /// </summary>
  191. /// <param name="typeId"></param>
  192. /// <param name="code"></param>
  193. /// <returns></returns>
  194. [NonAction]
  195. public async Task<List<SysDictData>> GetDataListByIdOrCode(long? typeId, string code)
  196. {
  197. if (string.IsNullOrWhiteSpace(code) && typeId == null ||
  198. !string.IsNullOrWhiteSpace(code) && typeId != null)
  199. throw Oops.Oh(ErrorCodeEnum.D3011);
  200. var dictType = await _sysDictDataRep.Change<SysDictType>().AsQueryable()
  201. .Where(u => u.Status == StatusEnum.Enable)
  202. .WhereIF(!string.IsNullOrWhiteSpace(code), u => u.Code == code)
  203. .WhereIF(typeId != null, u => u.Id == typeId)
  204. .FirstAsync();
  205. if (dictType == null) return null;
  206. string dicKey = dictType.IsTenant == YesNoEnum.N ? $"{CacheConst.KeyDict}{dictType.Code}" : $"{CacheConst.KeyTenantDict}{_userManager}:{dictType?.Code}";
  207. var dictDataList = _sysCacheService.Get<List<SysDictData>>(dicKey);
  208. if (dictDataList == null)
  209. {
  210. //平台字典和租户字典分开缓存
  211. if (dictType.IsTenant == YesNoEnum.Y)
  212. {
  213. dictDataList = await _sysDictDataRep.Change<SysDictDataTenant>().AsQueryable()
  214. .Where(u => u.DictTypeId == dictType.Id)
  215. .Where(u => u.Status == StatusEnum.Enable)
  216. .WhereIF(_userManager.SuperAdmin, d => d.TenantId == _userManager.TenantId).Select<SysDictData>()
  217. .OrderBy(u => new { u.OrderNo, u.Value, u.Code })
  218. .ToListAsync();
  219. }
  220. else
  221. {
  222. dictDataList = await _sysDictDataRep.AsQueryable()
  223. .Where(u => u.DictTypeId == dictType.Id)
  224. .Where(u => u.Status == StatusEnum.Enable)
  225. .OrderBy(u => new { u.OrderNo, u.Value, u.Code })
  226. .ToListAsync();
  227. }
  228. _sysCacheService.Set(dicKey, dictDataList);
  229. }
  230. return dictDataList;
  231. }
  232. /// <summary>
  233. /// 根据查询条件获取字典值集合 🔖
  234. /// </summary>
  235. /// <param name="input"></param>
  236. /// <returns></returns>
  237. [DisplayName("根据查询条件获取字典值集合")]
  238. public async Task<List<SysDictData>> GetDataList([FromQuery] QueryDictDataInput input)
  239. {
  240. var dataList = await GetDataList(input.Value);
  241. if (input.Status.HasValue) return dataList.Where(u => u.Status == (StatusEnum)input.Status.Value).ToList();
  242. return dataList;
  243. }
  244. /// <summary>
  245. /// 根据字典类型Id删除字典值
  246. /// </summary>
  247. /// <param name="dictTypeId"></param>
  248. /// <returns></returns>
  249. [NonAction]
  250. public async Task DeleteDictData(long dictTypeId)
  251. {
  252. var dictType = await _sysDictDataRep.Change<SysDictType>().AsQueryable().Where(u => u.Id == dictTypeId).FirstAsync();
  253. Remove(dictType);
  254. if (dictType?.IsTenant == YesNoEnum.Y)
  255. await _sysDictDataRep.Change<SysDictDataTenant>().DeleteAsync(u => u.DictTypeId == dictTypeId);
  256. else
  257. await _sysDictDataRep.DeleteAsync(u => u.DictTypeId == dictTypeId);
  258. }
  259. /// <summary>
  260. /// 通过字典数据Value查询显示文本Label
  261. /// 适用于列表中根据字典数据值找文本的子查询 _sysDictDataService.MapDictValueToLabel(() =>obj.Type, "org_type",obj);
  262. /// </summary>
  263. /// <typeparam name="T"></typeparam>
  264. /// <param name="mappingFiled"></param>
  265. /// <param name="dictTypeCode"></param>
  266. /// <param name="parameter"></param>
  267. /// <returns></returns>
  268. public string MapDictValueToLabel<T>(Expression<Func<object>> mappingFiled, string dictTypeCode, T parameter)
  269. {
  270. return VSysDictData.InnerJoin<SysDictType>((d, dt) => d.DictTypeId.Equals(dt.Id) && dt.Code == dictTypeCode)
  271. .SetContext(d => d.Value, mappingFiled, parameter).FirstOrDefault()?.Label;
  272. }
  273. /// <summary>
  274. /// 通过字典数据显示文本Label查询Value
  275. /// 适用于列表数据导入根据字典数据文本找值的子查询 _sysDictDataService.MapDictLabelToValue(() => obj.Type, "org_type",obj);
  276. /// </summary>
  277. /// <typeparam name="T"></typeparam>
  278. /// <param name="mappingFiled"></param>
  279. /// <param name="dictTypeCode"></param>
  280. /// <param name="parameter"></param>
  281. /// <returns></returns>
  282. public string MapDictLabelToValue<T>(Expression<Func<object>> mappingFiled, string dictTypeCode, T parameter)
  283. {
  284. return VSysDictData.InnerJoin<SysDictType>((d, dt) => d.DictTypeId.Equals(dt.Id) && dt.Code == dictTypeCode)
  285. .SetContext(d => d.Label, mappingFiled, parameter).FirstOrDefault()?.Value;
  286. }
  287. /// <summary>
  288. /// 清理字典数据缓存
  289. /// </summary>
  290. /// <param name="dictType"></param>
  291. private void Remove(SysDictType dictType)
  292. {
  293. _sysCacheService.Remove($"{CacheConst.KeyDict}{dictType?.Code}");
  294. _sysCacheService.Remove($"{CacheConst.KeyTenantDict}{_userManager}:{dictType?.Code}");
  295. }
  296. }