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.

200 lines
6.9 KiB

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