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.

197 lines
6.8 KiB

  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. ParentId = entity.ParentId,
  38. PathCode = await CreatePathCode(entity.ParentId),
  39. QuestionId = entity.QuestionId,
  40. QuestionName = entity.QuestionName,
  41. QuestionSubjectTypeId = entity.QuestionSubjectTypeId,
  42. SimpleCode = LanguageConverter.GetPYSimpleCode(entity.QuestionName),
  43. DisplayOrder = await EntityHelper.CreateMaxDisplayOrder<Question>(_questionRepository),
  44. QuestionTypeId = entity.QuestionTypeId
  45. };
  46. }
  47. /// <summary>
  48. /// 更新
  49. /// </summary>
  50. /// <param name="sourceEntity"></param>
  51. /// <param name="targetEntity"></param>
  52. /// <returns></returns>
  53. public void UpdateAsync(
  54. Question sourceEntity,
  55. Question targetEntity
  56. )
  57. {
  58. DataHelper.CheckEntityIsNull(sourceEntity);
  59. DataHelper.CheckEntityIsNull(targetEntity);
  60. //DataHelper.CheckStringIsNull(sourceEntity.QuestionTypeName, "名称");
  61. targetEntity.AnswerType = sourceEntity.AnswerType;
  62. targetEntity.IsActive = sourceEntity.IsActive;
  63. targetEntity.ParentId = sourceEntity.ParentId;
  64. targetEntity.QuestionName = sourceEntity.QuestionName;
  65. targetEntity.QuestionSubjectTypeId = sourceEntity.QuestionSubjectTypeId;
  66. targetEntity.SimpleCode = LanguageConverter.GetPYSimpleCode(sourceEntity.QuestionName);
  67. targetEntity.QuestionTypeId = sourceEntity.QuestionTypeId;
  68. }
  69. /// <summary>
  70. /// 删除
  71. /// </summary>
  72. /// <param name="QuestionId"></param>
  73. /// <returns></returns>
  74. /// <exception cref="UserFriendlyException"></exception>
  75. public async Task CheckAndDeleteAsync(Guid QuestionId)
  76. {
  77. //var questionEnt = await _questionRepository.FirstOrDefaultAsync(m => m.QuestionTypeId == QuestionTypeId);
  78. //if (questionEnt != null)
  79. //{
  80. // throw new UserFriendlyException($"问卷类别已被使用,不能删除");
  81. //}
  82. await _questionRepository.DeleteAsync(d => d.QuestionId == QuestionId);
  83. }
  84. /// <summary>
  85. /// 修改排序 置顶,置底
  86. /// </summary>
  87. /// <param name="id">需要修改的ID</param>
  88. /// <param name="SortType">修改方式:1 置顶 2 置底</param>
  89. /// <returns></returns>
  90. public async Task UpdateSortTopOrBottomAsync(Guid QuestionId, int SortType)
  91. {
  92. var entity = await _questionRepository.GetAsync(f => f.QuestionId == QuestionId);
  93. await EntityHelper.UpdateSortTopOrBottomAsync(_questionRepository, entity, SortType);
  94. }
  95. /// <summary>
  96. /// 修改排序 拖拽
  97. /// </summary>
  98. /// <typeparam name="TEntity"></typeparam>
  99. /// <param name="repository"></param>
  100. /// <param name="input"></param>
  101. /// <returns></returns>
  102. public async Task UpdateSortDragAsync(UpdateQuestionSortDragDto input)
  103. {
  104. var entitylist = await _questionRepository.GetListAsync(o => input.ItemList.Select(s => s.QuestionId).Contains(o.QuestionId));
  105. foreach (var entity in entitylist)
  106. {
  107. foreach (var item in input.ItemList)
  108. {
  109. if (item.QuestionId == entity.QuestionId)
  110. entity.DisplayOrder = item.DisplayOrder;
  111. }
  112. }
  113. await _questionRepository.UpdateManyAsync(entitylist);
  114. }
  115. /// <summary>
  116. /// 自动生成pathcode
  117. /// </summary>
  118. /// <param name="parentId"></param>
  119. /// <returns></returns>
  120. public async Task<string> CreatePathCode(Guid? parentId)
  121. {
  122. string PathCode = "00001";
  123. //一级
  124. if (parentId == null || parentId == Guid.Empty)
  125. {
  126. //最大pathcode
  127. var LastPathCode = (await _questionRepository.GetListAsync(o => o.ParentId == Guid.Empty || o.ParentId == null))
  128. .OrderByDescending(o =>
  129. {
  130. var sortCode = o.PathCode.Replace(".", "");
  131. return Convert.ToInt32(sortCode);
  132. }).FirstOrDefault();
  133. if (LastPathCode != null)
  134. {
  135. PathCode = (Convert.ToInt32(LastPathCode.PathCode) + 1).ToString().PadLeft(5, '0');
  136. }
  137. else
  138. {
  139. PathCode = "00001";
  140. }
  141. }
  142. else
  143. {
  144. //二级以及以上
  145. //上级pathcode
  146. var ParentPathCode = (await _questionRepository.GetListAsync(o => o.QuestionId == parentId)).FirstOrDefault().PathCode;
  147. //最大pathcode
  148. var LastPathCode = (await _questionRepository.GetListAsync(o => o.ParentId == parentId))
  149. .OrderByDescending(o =>
  150. {
  151. var sortCode = o.PathCode.Replace(".", "");
  152. return Convert.ToInt32(sortCode);
  153. }).Select(s => s.PathCode).FirstOrDefault();
  154. if (!string.IsNullOrEmpty(LastPathCode))
  155. {
  156. var MaxCode = LastPathCode.Split('.').Last();
  157. PathCode = ParentPathCode + "." + (Convert.ToInt32(MaxCode) + 1).ToString().PadLeft(5, '0');
  158. }
  159. else
  160. {
  161. PathCode = ParentPathCode + ".00001";
  162. }
  163. }
  164. return PathCode;
  165. }
  166. }
  167. }