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
8.4 KiB

  1. using Microsoft.AspNetCore.Authorization;
  2. using Microsoft.AspNetCore.Mvc;
  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.Application.Services;
  11. using Volo.Abp.Domain.Repositories;
  12. namespace Shentun.WebPeis.Questions
  13. {
  14. /// <summary>
  15. /// 问卷
  16. /// </summary>
  17. [ApiExplorerSettings(GroupName = "Work")]
  18. [Authorize]
  19. public class QuestionAppService : ApplicationService
  20. {
  21. private readonly IRepository<Question> _questionRepository;
  22. private readonly QuestionManager _questionManager;
  23. private readonly CacheService _cacheService;
  24. public QuestionAppService(
  25. IRepository<Question> questionRepository,
  26. QuestionManager questionManager,
  27. CacheService cacheService)
  28. {
  29. _questionRepository = questionRepository;
  30. _questionManager = questionManager;
  31. _cacheService = cacheService;
  32. }
  33. ///// <summary>
  34. ///// 获取列表
  35. ///// </summary>
  36. ///// <returns></returns>
  37. //[HttpPost("api/app/Question/GetList")]
  38. //public async Task<List<QuestionDto>> GetListAsync()
  39. //{
  40. // var entlist = await _questionRepository.GetQueryableAsync();
  41. // var entdto = entlist.Select(s => new QuestionDto
  42. // {
  43. // CreationTime = s.CreationTime,
  44. // CreatorId = s.CreatorId,
  45. // DisplayOrder = s.DisplayOrder,
  46. // LastModificationTime = s.LastModificationTime,
  47. // LastModifierId = s.LastModifierId,
  48. // SimpleCode = s.SimpleCode,
  49. // QuestionTypeId = s.QuestionTypeId,
  50. // QuestionSubjectTypeId = s.QuestionSubjectTypeId,
  51. // QuestionName = s.QuestionName,
  52. // QuestionId = s.QuestionId,
  53. // PathCode = s.PathCode,
  54. // ParentId = s.ParentId,
  55. // IsActive = s.IsActive,
  56. // AnswerType = s.AnswerType,
  57. // CreatorName = _cacheService.GetSurnameAsync(s.CreatorId).Result,
  58. // LastModifierName = _cacheService.GetSurnameAsync(s.LastModifierId).Result
  59. // }).OrderBy(o => o.DisplayOrder).ToList();
  60. // return entdto;
  61. //}
  62. /// <summary>
  63. /// 获取问卷树型列表
  64. /// </summary>
  65. /// <returns></returns>
  66. [HttpPost("api/app/Question/GetQuestionTreeList")]
  67. public async Task<List<QuestionTreeListDto>> GetQuestionTreeListAsync()
  68. {
  69. var dataList = await _questionRepository.GetListAsync();
  70. var items = from p in dataList.OrderBy(o => o.DisplayOrder)
  71. select new QuestionTreeListDto()
  72. {
  73. ParentId = p.ParentId,
  74. AnswerType = p.AnswerType,
  75. QuestionId = p.QuestionId,
  76. CreationTime = p.CreationTime,
  77. CreatorId = p.CreatorId,
  78. CreatorName = _cacheService.GetSurnameAsync(p.CreatorId).Result,
  79. IsActive = p.IsActive,
  80. LastModificationTime = p.LastModificationTime,
  81. LastModifierId = p.LastModifierId,
  82. LastModifierName = _cacheService.GetSurnameAsync(p.LastModifierId).Result,
  83. PathCode = p.PathCode,
  84. QuestionName = p.QuestionName,
  85. QuestionSubjectTypeId = p.QuestionSubjectTypeId,
  86. QuestionTypeId = p.QuestionTypeId,
  87. SimpleCode = p.SimpleCode,
  88. DisplayOrder = p.DisplayOrder
  89. };
  90. return GetTree(items.ToList(), 0, "");
  91. }
  92. /// <summary>
  93. /// 创建
  94. /// </summary>
  95. /// <returns></returns>
  96. [HttpPost("api/app/Question/Create")]
  97. public async Task<QuestionDto> CreateAsync(CreateQuestionDto input)
  98. {
  99. var createEntity = ObjectMapper.Map<CreateQuestionDto, Question>(input);
  100. var entity = await _questionManager.CreateAsync(createEntity);
  101. entity = await _questionRepository.InsertAsync(entity);
  102. var dto = ObjectMapper.Map<Question, QuestionDto>(entity);
  103. dto.CreatorName = await _cacheService.GetSurnameAsync(dto.CreatorId);
  104. dto.LastModifierName = await _cacheService.GetSurnameAsync(dto.LastModifierId);
  105. return dto;
  106. }
  107. /// <summary>
  108. /// 更新
  109. /// </summary>
  110. /// <param name="input"></param>
  111. /// <returns></returns>
  112. [HttpPost("api/app/Question/Update")]
  113. public async Task<QuestionDto> UpdateAsync(UpdateQuestionDto input)
  114. {
  115. var entity = await _questionRepository.GetAsync(f => f.QuestionTypeId == input.QuestionTypeId);
  116. var sourceEntity = ObjectMapper.Map<UpdateQuestionDto, Question>(input);
  117. _questionManager.UpdateAsync(sourceEntity, entity);
  118. entity = await _questionRepository.UpdateAsync(entity);
  119. var dto = ObjectMapper.Map<Question, QuestionDto>(entity);
  120. dto.CreatorName = await _cacheService.GetSurnameAsync(dto.CreatorId);
  121. dto.LastModifierName = await _cacheService.GetSurnameAsync(dto.LastModifierId);
  122. return dto;
  123. }
  124. /// <summary>
  125. /// 删除
  126. /// </summary>
  127. /// <param name="input"></param>
  128. /// <returns></returns>
  129. [HttpPost("api/app/Question/Delete")]
  130. public async Task DeleteAsync(QuestionIdInputDto input)
  131. {
  132. await _questionManager.CheckAndDeleteAsync(input.QuestionId);
  133. }
  134. /// <summary>
  135. /// 修改排序 置顶,置底
  136. /// </summary>
  137. /// <param name="input"></param>
  138. /// <returns></returns>
  139. [HttpPost("api/app/Question/UpdateSortTopOrBottom")]
  140. public async Task UpdateSortTopOrBottomAsync(UpdateQuestionSortTopOrBottomInputDto input)
  141. {
  142. await _questionManager.UpdateSortTopOrBottomAsync(input.QuestionId, input.SortType);
  143. }
  144. /// <summary>
  145. /// 修改排序 拖拽
  146. /// </summary>
  147. /// <param name="input"></param>
  148. /// <returns></returns>
  149. [HttpPost("api/app/Question/UpdateSortDragAsync")]
  150. public async Task UpdateSortDragAsync(UpdateQuestionSortDragDto input)
  151. {
  152. await _questionManager.UpdateSortDragAsync(input);
  153. }
  154. /// <summary>
  155. /// 使用Code进行递归
  156. /// </summary>
  157. /// <param name="items"></param>
  158. /// <param name="deep"></param>
  159. /// <param name="prefix"></param>
  160. /// <returns></returns>
  161. private List<QuestionTreeListDto> GetTree(List<QuestionTreeListDto> items, int deep, string prefix)
  162. {
  163. return (from p in items
  164. where p.PathCode.StartsWith(prefix) && p.PathCode.Count(a => a == '.') == deep
  165. orderby p.DisplayOrder ascending
  166. let subs = GetTree(items, deep + 1, p.PathCode)
  167. select new QuestionTreeListDto()
  168. {
  169. ParentId = p.ParentId,
  170. AnswerType = p.AnswerType,
  171. QuestionId = p.QuestionId,
  172. CreationTime = p.CreationTime,
  173. CreatorId = p.CreatorId,
  174. CreatorName = _cacheService.GetSurnameAsync(p.CreatorId).Result,
  175. IsActive = p.IsActive,
  176. LastModificationTime = p.LastModificationTime,
  177. LastModifierId = p.LastModifierId,
  178. LastModifierName = _cacheService.GetSurnameAsync(p.LastModifierId).Result,
  179. PathCode = p.PathCode,
  180. QuestionName = p.QuestionName,
  181. QuestionSubjectTypeId = p.QuestionSubjectTypeId,
  182. QuestionTypeId = p.QuestionTypeId,
  183. SimpleCode = p.SimpleCode,
  184. DisplayOrder = p.DisplayOrder,
  185. TreeChildren = subs.ToList()
  186. }
  187. ).ToList();
  188. }
  189. }
  190. }