基础服务api
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

154 lines
6.1 KiB

  1. using BPA.SAAS.Manage.Application.System.Dtos;
  2. using BPA.SAAS.Manage.Application.System.Interface;
  3. using BPA.SAAS.Manage.Comm.Enum;
  4. using BPA.SAAS.Manage.Core.Base;
  5. using BPA.SAAS.Manage.Core.system;
  6. using BPA.SAAS.Manage.Core.System;
  7. namespace BPA.SAAS.Manage.Application
  8. {
  9. public class DictDataService : IDictDataService, ITransient
  10. {
  11. /// <summary>
  12. /// 核心对象:拥有完整的SqlSugar全部功能
  13. /// </summary>
  14. private readonly ISqlSugarClient _db;
  15. /// <summary>
  16. /// construct
  17. /// </summary>
  18. /// <param name="sqlSugarRepository"></param>
  19. public DictDataService(ISqlSugarClient db)
  20. {
  21. _db=db;
  22. }
  23. /// <summary>
  24. /// 根据字典类型编码查询
  25. /// </summary>
  26. /// <param name="TypeCode"></param>
  27. /// <returns></returns>
  28. public async Task<List<BPA_DictData>> GetDicList(string TypeCode)
  29. {
  30. var res =await _db.Queryable<BPA_DictData, BPA_DictType>((a, b) => new JoinQueryInfos(JoinType.Inner, a.TypeId.ToString() == b.Id))
  31. .Where((a, b) => b.Code == TypeCode && a.IsDeleted == 0 && b.IsDeleted == 0)
  32. .Select((a, b) => new BPA_DictData
  33. {
  34. Id = a.Id.SelectAll()
  35. }).ToListAsync();
  36. return res;
  37. }
  38. /// <summary>
  39. /// 查询字典信息
  40. /// </summary>
  41. /// <param name="dto"></param>
  42. /// <returns></returns>
  43. public async Task<PageUtil> GetDictData(DictDataQueryDto dto)
  44. {
  45. List<IConditionalModel> conModels = new List<IConditionalModel>();
  46. if (!string.IsNullOrEmpty(dto.TypeId))
  47. {
  48. conModels.Add(new ConditionalModel() { FieldName = "a.TypeId", ConditionalType = ConditionalType.Like, FieldValue = dto.TypeId });
  49. }
  50. if (!string.IsNullOrEmpty(dto.Value))
  51. {
  52. conModels.Add(new ConditionalModel() { FieldName = "a.Value", ConditionalType = ConditionalType.Like, FieldValue = dto.Value });
  53. }
  54. if (!string.IsNullOrEmpty(dto.Status))
  55. {
  56. conModels.Add(new ConditionalModel() { FieldName = "a.Status", ConditionalType = ConditionalType.Equal, FieldValue = dto.Status });
  57. }
  58. if (!string.IsNullOrEmpty(dto.Code))
  59. {
  60. conModels.Add(new ConditionalModel() { FieldName = "a.Code", ConditionalType = ConditionalType.Equal, FieldValue = dto.Code });
  61. }
  62. if (!string.IsNullOrEmpty(dto.TypeCode))
  63. {
  64. conModels.Add(new ConditionalModel() { FieldName = "b.Code", ConditionalType = ConditionalType.Equal, FieldValue = dto.TypeCode });
  65. }
  66. RefAsync<int> total = 0;
  67. var res =await _db.Queryable<BPA_DictData, BPA_DictType>((a, b) => new JoinQueryInfos(JoinType.Inner, a.TypeId.ToString() == b.Id))
  68. .Where((a, b) => a.IsDeleted == 0 && b.IsDeleted == 0)
  69. .Where(conModels)
  70. .WhereIF(dto.CreateAt.HasValue, x => x.CreateAt.Date == dto.CreateAt.Value.Date)
  71. .Select((a, b) => new DictDataQueryDto
  72. {
  73. Id = a.Id.SelectAll(),
  74. TypeName = b.Name,
  75. TypeCode = b.Code
  76. }).ToPageListAsync(dto.Current, dto.PageSize, total);
  77. PageUtil util = new PageUtil()
  78. {
  79. Total = total,
  80. Data = res
  81. };
  82. return util;
  83. }
  84. /// <summary>
  85. /// 添加字典信息
  86. /// </summary>
  87. /// <param name="dto"></param>
  88. /// <returns></returns>
  89. public async Task<bool> AddDictData(DictDataDto dto)
  90. {
  91. var dicttypes = _db.Queryable<BPA_DictData>().Where(a => a.Code == dto.Code && a.IsDeleted == 0 && a.TypeId == dto.TypeId).ToList();
  92. if (dicttypes.Count() > 0)
  93. {
  94. throw Oops.Oh("编码已存在");
  95. }
  96. try
  97. {
  98. _db.Ado.BeginTran();
  99. var dictData = new BPA_DictData
  100. {
  101. TypeId = dto.TypeId,
  102. Value = dto.Value,
  103. Code = dto.Code,
  104. Remark = dto.Remark,
  105. };
  106. var res =await _db.Insertable(dictData).CallEntityMethod(t => t.Create()).ExecuteCommandAsync();
  107. _db.Ado.CommitTran();
  108. return res>0;
  109. }
  110. catch (Exception ex)
  111. {
  112. _db.Ado.RollbackTran();
  113. return false;
  114. }
  115. }
  116. /// <summary>
  117. /// 修改字典信息
  118. /// </summary>
  119. /// <param name="dto"></param>
  120. /// <returns></returns>
  121. public async Task<bool> UpdateDictData(DictDataDto dto)
  122. {
  123. var resEntity = _db.Queryable<BPA_DictData>().Where(it => it.IsDeleted == 0).First(it => it.Id == dto.Id);
  124. if (null == resEntity)
  125. {
  126. return false;
  127. }
  128. var resEntity2 = _db.Queryable<BPA_DictData>().Where(it => it.IsDeleted == 0).First(it => it.Id != dto.Id && it.Code == dto.Code);
  129. if (resEntity2 != null)
  130. {
  131. throw Oops.Oh("code已存在");
  132. }
  133. resEntity.Value = dto.Value;
  134. resEntity.Code = dto.Code;
  135. resEntity.Remark = dto.Remark;
  136. //resEntity.Status = (CommonStatus)Enum.ToObject(typeof(CommonStatus), int.Parse(dto.Status));
  137. var res =await _db.Updateable(resEntity).ExecuteCommandAsync();
  138. return res > 0;
  139. }
  140. /// <summary>
  141. /// 删除字典信息
  142. /// </summary>
  143. /// <param name="Ids"></param>
  144. /// <returns></returns>
  145. public async Task<bool> BatchDelDictData(List<string> Ids)
  146. {
  147. var resEntitites = _db.Queryable<BPA_DictData>().In(Ids).ToList();
  148. var res =await _db.Deleteable(resEntitites).ExecuteCommandAsync();
  149. return res > 0;
  150. }
  151. }
  152. }