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.

208 lines
7.7 KiB

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