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.

210 lines
8.0 KiB

1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
  1. using NPOI.POIFS.Properties;
  2. using Shentun.Utilities;
  3. using Shentun.WebPeis.Models;
  4. using Shentun.WebPeis.Questions;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Runtime.CompilerServices;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using Volo.Abp;
  12. using Volo.Abp.Domain.Entities;
  13. using Volo.Abp.Domain.Repositories;
  14. using Volo.Abp.Domain.Services;
  15. namespace Shentun.WebPeis.QuestionAnswers
  16. {
  17. public class QuestionAnswerManager : DomainService
  18. {
  19. private readonly IRepository<QuestionAnswer> _questionAnswerRepository;
  20. private readonly IRepository<QuestionRegisterAnswer> _questionRegisterAnswerRepository;
  21. public QuestionAnswerManager(
  22. IRepository<QuestionAnswer> questionAnswerRepository,
  23. IRepository<QuestionRegisterAnswer> questionRegisterAnswerRepository)
  24. {
  25. _questionAnswerRepository = questionAnswerRepository;
  26. _questionRegisterAnswerRepository = questionRegisterAnswerRepository;
  27. }
  28. /// <summary>
  29. /// 创建
  30. /// </summary>
  31. /// <returns></returns>
  32. public async Task<QuestionAnswer> CreateAsync(QuestionAnswer entity)
  33. {
  34. DataHelper.CheckEntityIsNull(entity);
  35. //DataHelper.CheckStringIsNull(entity.QuestionTypeName, "名称");
  36. return new QuestionAnswer
  37. {
  38. HealthGuidance = entity.HealthGuidance,
  39. Aliases = entity.Aliases,
  40. IsNone = entity.IsNone,
  41. Overview = entity.Overview,
  42. ChildAnswerTitle = entity.ChildAnswerTitle,
  43. AnswerResultType = entity.AnswerResultType,
  44. ChildAnswerType = entity.ChildAnswerType,
  45. QuestionAnswerId = entity.QuestionAnswerId,
  46. QuestionAnswerName = entity.QuestionAnswerName,
  47. QuestionRegisterAnswers = entity.QuestionRegisterAnswers,
  48. Reason = entity.Reason,
  49. ParentId = entity.ParentId,
  50. PathCode = await CreatePathCode(entity.ParentId),
  51. QuestionId = entity.QuestionId,
  52. SimpleCode = LanguageConverter.GetPYSimpleCode(entity.QuestionAnswerName),
  53. DisplayOrder = await EntityHelper.CreateMaxDisplayOrder<QuestionAnswer>(_questionAnswerRepository)
  54. };
  55. }
  56. /// <summary>
  57. /// 更新
  58. /// </summary>
  59. /// <param name="sourceEntity"></param>
  60. /// <param name="targetEntity"></param>
  61. /// <returns></returns>
  62. public void UpdateAsync(
  63. QuestionAnswer sourceEntity,
  64. QuestionAnswer targetEntity
  65. )
  66. {
  67. DataHelper.CheckEntityIsNull(sourceEntity);
  68. DataHelper.CheckEntityIsNull(targetEntity);
  69. //DataHelper.CheckStringIsNull(sourceEntity.QuestionTypeName, "名称");
  70. targetEntity.HealthGuidance = sourceEntity.HealthGuidance;
  71. targetEntity.Aliases = sourceEntity.Aliases;
  72. targetEntity.IsNone = sourceEntity.IsNone;
  73. targetEntity.Overview = sourceEntity.Overview;
  74. targetEntity.ChildAnswerTitle = sourceEntity.ChildAnswerTitle;
  75. targetEntity.AnswerResultType = sourceEntity.AnswerResultType;
  76. targetEntity.ChildAnswerType = sourceEntity.ChildAnswerType;
  77. targetEntity.QuestionAnswerName = sourceEntity.QuestionAnswerName;
  78. targetEntity.QuestionRegisterAnswers = sourceEntity.QuestionRegisterAnswers;
  79. targetEntity.Reason = sourceEntity.Reason;
  80. targetEntity.QuestionId = sourceEntity.QuestionId;
  81. targetEntity.SimpleCode = LanguageConverter.GetPYSimpleCode(sourceEntity.QuestionAnswerName);
  82. }
  83. /// <summary>
  84. /// 删除
  85. /// </summary>
  86. /// <param name="QuestionAnswerId"></param>
  87. /// <returns></returns>
  88. /// <exception cref="UserFriendlyException"></exception>
  89. public async Task CheckAndDeleteAsync(Guid QuestionAnswerId)
  90. {
  91. var questionRegisterAnswerEnt = await _questionRegisterAnswerRepository.FirstOrDefaultAsync(m => m.QuestionAnswerId == QuestionAnswerId);
  92. if (questionRegisterAnswerEnt != null)
  93. {
  94. throw new UserFriendlyException($"问卷答案已被人员登记使用,不能删除");
  95. }
  96. await _questionAnswerRepository.DeleteAsync(d => d.QuestionAnswerId == QuestionAnswerId);
  97. }
  98. /// <summary>
  99. /// 修改排序 置顶,置底
  100. /// </summary>
  101. /// <param name="id">需要修改的ID</param>
  102. /// <param name="SortType">修改方式:1 置顶 2 置底</param>
  103. /// <returns></returns>
  104. public async Task UpdateSortTopOrBottomAsync(Guid QuestionAnswerId, int SortType)
  105. {
  106. var entity = await _questionAnswerRepository.GetAsync(f => f.QuestionAnswerId == QuestionAnswerId);
  107. await EntityHelper.UpdateSortTopOrBottomAsync(_questionAnswerRepository, entity, SortType);
  108. }
  109. /// <summary>
  110. /// 修改排序 拖拽
  111. /// </summary>
  112. /// <typeparam name="TEntity"></typeparam>
  113. /// <param name="repository"></param>
  114. /// <param name="input"></param>
  115. /// <returns></returns>
  116. public async Task UpdateSortDragAsync(UpdateQuestionAnswerSortDragDto input)
  117. {
  118. var entitylist = await _questionAnswerRepository.GetListAsync(o => input.ItemList.Select(s => s.QuestionAnswerId).Contains(o.QuestionAnswerId));
  119. foreach (var entity in entitylist)
  120. {
  121. foreach (var item in input.ItemList)
  122. {
  123. if (item.QuestionAnswerId == entity.QuestionAnswerId)
  124. entity.DisplayOrder = item.DisplayOrder;
  125. }
  126. }
  127. await _questionAnswerRepository.UpdateManyAsync(entitylist);
  128. }
  129. /// <summary>
  130. /// 自动生成pathcode
  131. /// </summary>
  132. /// <param name="parentId"></param>
  133. /// <returns></returns>
  134. public async Task<string> CreatePathCode(Guid? parentId)
  135. {
  136. string PathCode = "00001";
  137. //一级
  138. if (parentId == null || parentId == Guid.Empty)
  139. {
  140. //最大pathcode
  141. var LastPathCode = (await _questionAnswerRepository.GetListAsync(o => o.ParentId == Guid.Empty || o.ParentId == null))
  142. .OrderByDescending(o =>
  143. {
  144. var sortCode = o.PathCode.Replace(".", "");
  145. return Convert.ToInt32(sortCode);
  146. }).FirstOrDefault();
  147. if (LastPathCode != null)
  148. {
  149. PathCode = (Convert.ToInt32(LastPathCode.PathCode) + 1).ToString().PadLeft(5, '0');
  150. }
  151. else
  152. {
  153. PathCode = "00001";
  154. }
  155. }
  156. else
  157. {
  158. //二级以及以上
  159. //上级pathcode
  160. var ParentPathCode = (await _questionAnswerRepository.GetListAsync(o => o.QuestionAnswerId == parentId)).FirstOrDefault().PathCode;
  161. //最大pathcode
  162. var LastPathCode = (await _questionAnswerRepository.GetListAsync(o => o.ParentId == parentId))
  163. .OrderByDescending(o =>
  164. {
  165. var sortCode = o.PathCode.Replace(".", "");
  166. return Convert.ToInt32(sortCode);
  167. }).Select(s => s.PathCode).FirstOrDefault();
  168. if (!string.IsNullOrEmpty(LastPathCode))
  169. {
  170. var MaxCode = LastPathCode.Split('.').Last();
  171. PathCode = ParentPathCode + "." + (Convert.ToInt32(MaxCode) + 1).ToString().PadLeft(5, '0');
  172. }
  173. else
  174. {
  175. PathCode = ParentPathCode + ".00001";
  176. }
  177. }
  178. return PathCode;
  179. }
  180. }
  181. }