DictionaryService.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. using Bowin.Common.Linq;
  2. using Bowin.Common.Linq.Entity;
  3. using Bowin.Common.Utility;
  4. using YLShipBuildLandMap.Entity;
  5. using YLShipBuildLandMap.Entity.ViewModel;
  6. using YLShipBuildLandMap.Entity.ViewModel.SystemSetting;
  7. /*using Microsoft.EntityFrameworkCore.Query.ExpressionTranslators.Internal;*/
  8. using Microsoft.Extensions.Caching.Memory;
  9. using Microsoft.Extensions.Options;
  10. using Newtonsoft.Json.Linq;
  11. using System;
  12. using System.Collections.Generic;
  13. using System.Linq;
  14. using System.Linq.Expressions;
  15. using System.Text;
  16. using System.Threading.Tasks;
  17. using Z.EntityFramework.Plus;
  18. using Microsoft.Extensions.DependencyInjection;
  19. namespace YLShipBuildLandMap.Services.SystemSetting
  20. {
  21. public class DictionaryService : IDictionaryService
  22. {
  23. private YLShipBuildLandMapContext DbContext { get; set; }
  24. readonly MemoryCacheEntryOptions mCacheOp;
  25. public DictionaryService(YLShipBuildLandMapContext dbContext, IOptions<MemoryCacheEntryOptions> iopCacheOp)
  26. {
  27. DbContext = dbContext;
  28. mCacheOp = iopCacheOp.Value;
  29. }
  30. private IQueryable<DictionaryItemView> GetDictionaryItemQuery(Expression<Func<SysDictionary, bool>> dexp, Expression<Func<SysDictionaryItem, bool>> iexp)
  31. {
  32. var query = from r in DbContext.SysDictionaryItem.Where(iexp).Where(x => x.RecordStatus > 0)
  33. from d in DbContext.SysDictionary.Where(dexp).Where(x => x.DictionaryCode == r.DictionaryCode && x.RecordStatus > 0 && x.IsEditable == true)
  34. select new DictionaryItemView
  35. {
  36. DictionaryItemID = r.DictionaryItemId,
  37. Code = r.Code,
  38. DictionaryCode = r.DictionaryCode,
  39. Value = r.Value,
  40. Name = r.Name,
  41. DictionaryName = d.Name,
  42. OrderNo = r.OrderNo
  43. };
  44. return query;
  45. }
  46. private IQueryable<SysDictionary> GetDictionaryQuery(Expression<Func<SysDictionary, bool>> exp)
  47. {
  48. var query = from r in DbContext.SysDictionary.Where(exp).Where(x => x.RecordStatus > 0 && x.IsEditable == true)
  49. select r;
  50. return query;
  51. }
  52. public Task<IGridResultSet<DictionaryItemView>> GetDictionaryItemList(int? pageIndex, int? pageSize, string dictionaryName, string dictionaryCode, string name)
  53. {
  54. Expression<Func<SysDictionary, bool>> dexp = (e => true);
  55. Expression<Func<SysDictionaryItem, bool>> iexp = (e => true);
  56. if (!string.IsNullOrEmpty(dictionaryName))
  57. {
  58. dexp = dexp.And(e => e.Name.Contains(dictionaryName.Trim()));
  59. }
  60. if (!string.IsNullOrEmpty(name))
  61. {
  62. iexp = iexp.And(e => e.Name.Contains(name.Trim()));
  63. }
  64. if (!string.IsNullOrEmpty(dictionaryCode))
  65. {
  66. iexp = iexp.And(e => e.DictionaryCode.Contains(dictionaryCode.Trim()));
  67. }
  68. var result = GetDictionaryItemQuery(dexp, iexp).OrderBy(e => e.DictionaryCode).ThenBy(e => e.OrderNo).ToGridResultSet(pageIndex, pageSize);
  69. return Task.FromResult(result);
  70. }
  71. public List<SysDictionaryItem> GetDictionaryItemList(string dictionaryCode, string name)
  72. {
  73. var sql = this.DbContext.SysDictionaryItem.AsQueryable();
  74. sql = sql.WhereIf(!string.IsNullOrEmpty(dictionaryCode), x => x.DictionaryCode == dictionaryCode);
  75. sql = sql.WhereIf(!string.IsNullOrEmpty(name), x => x.Name == name);
  76. return sql.Where(x => x.RecordStatus > 0).OrderBy(x => x.DictionaryCode).ThenBy(x => x.Value).ToList();
  77. }
  78. public Task<IEnumerable<SysDictionaryItem>> GetDictionaryItemsbyCache(string dictionaryCode)
  79. {
  80. //查询 需要一个参数
  81. var dics = DbContext.SysDictionaryItem.Where(x => x.RecordStatus > 0 && x.DictionaryCode == dictionaryCode).OrderBy(o=>o.OrderNo).FromCacheAsync(mCacheOp);
  82. return dics;
  83. }
  84. public Task<IEnumerable<SysDictionaryItem>> GetTugboatTitleDictionary(string dictionaryCode)
  85. {
  86. var dics = from a in DbContext.SysDictionaryItem.Where(x => x.RecordStatus > 0 && x.DictionaryCode == "Title")
  87. from b in DbContext.SysDictionaryItem.Where(x => x.RecordStatus > 0 && x.DictionaryCode == dictionaryCode && x.Value == a.Value)
  88. select a;
  89. //var dics = DbContext.SysDictionaryItem.Where(x => x.RecordStatus > 0 && x.DictionaryCode == dictionaryCode).OrderBy(o => o.OrderNo).FromCacheAsync(mCacheOp);
  90. return dics.OrderBy(x => x.OrderNo).FromCacheAsync(mCacheOp);
  91. }
  92. public Task<IEnumerable<SysDictionaryItem>> GetDepartmentTitleDictionary(string dictionaryCode)
  93. {
  94. var dics = from a in DbContext.SysDictionaryItem.Where(x => x.RecordStatus > 0 && x.DictionaryCode == "Title")
  95. from b in DbContext.SysDictionaryItem.Where(x => x.RecordStatus > 0 && x.DictionaryCode == dictionaryCode && x.Value == a.Value).DefaultIfEmpty()
  96. where b.DictionaryItemId == null
  97. select a;
  98. //var dics = DbContext.SysDictionaryItem.Where(x => x.RecordStatus > 0 && x.DictionaryCode == dictionaryCode).OrderBy(o => o.OrderNo).FromCacheAsync(mCacheOp);
  99. return dics.OrderBy(x => x.OrderNo).FromCacheAsync(mCacheOp);
  100. }
  101. public string GetDictionaryName(IEnumerable<SysDictionaryItem> dics,int? value)
  102. {
  103. string name = "";
  104. var item= dics.FirstOrDefault(w => w.Value == value);
  105. if (item != null) {
  106. name = item.Name;
  107. }
  108. return name;
  109. }
  110. public Task<IGridResultSet<SysDictionary>> GetDictionaryList(int? pageIndex, int? pageSize, string dictionaryName, string dictionaryCode)
  111. {
  112. Expression<Func<SysDictionary, bool>> exp = (e => true);
  113. if (!string.IsNullOrEmpty(dictionaryName))
  114. {
  115. exp = exp.And(e => e.Name.Contains(dictionaryName.Trim()));
  116. }
  117. if (!string.IsNullOrEmpty(dictionaryCode))
  118. {
  119. exp = exp.And(e => e.DictionaryCode.Contains(dictionaryCode.Trim()));
  120. }
  121. var result = GetDictionaryQuery(exp).OrderBy(e => e.OrderNo).ThenBy(e => e.DictionaryCode).ToGridResultSet(pageIndex, pageSize);
  122. return Task.FromResult(result);
  123. }
  124. public SysDictionaryItem GetDicItem(Guid? dicItemID)
  125. {
  126. return DbContext.SysDictionaryItem.FirstOrDefault(x => x.DictionaryItemId == dicItemID);
  127. }
  128. public async Task Save(SysDictionaryItem dicItem, Guid userID)
  129. {
  130. SysDictionaryItem dbDicItem = DbContext.SysDictionaryItem.FirstOrDefault(x => x.DictionaryItemId == dicItem.DictionaryItemId);
  131. if (dbDicItem != null)
  132. {
  133. dbDicItem.DictionaryCode = dicItem.DictionaryCode;
  134. dbDicItem.Value = dicItem.Value;
  135. dbDicItem.Name = dicItem.Name;
  136. dbDicItem.OrderNo = dicItem.OrderNo;
  137. DbContext.SysDictionaryItem.Update(dbDicItem);
  138. }
  139. else
  140. {
  141. dicItem.DictionaryItemId = Guid.NewGuid();
  142. dicItem.Code = "";
  143. dicItem.RecordStatus = 1;
  144. dicItem.IsEditable = true;
  145. this.DbContext.Add(dicItem);
  146. }
  147. await this.DbContext.SaveChangesAsync();
  148. }
  149. public async Task Delete(List<Guid> dicItemIDList)
  150. {
  151. await this.DbContext.SysDictionaryItem.Where(x => dicItemIDList.Contains(x.DictionaryItemId)).DeleteAsync();
  152. }
  153. public List<int?> GetEmployeeStatus()
  154. {
  155. var list = new List<int?>() { 1,2,4,5,6 };
  156. return list;
  157. }
  158. }
  159. }