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.

358 lines
16 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 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 Microsoft.AspNetCore.Authorization;
  2. using Microsoft.AspNetCore.Mvc;
  3. using Shentun.WebPeis.DiseaseRiskLevels;
  4. using Shentun.WebPeis.Models;
  5. using Shentun.WebPeis.Questions;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using Volo.Abp.Application.Services;
  12. using Volo.Abp.Domain.Repositories;
  13. namespace Shentun.WebPeis.QuestionAnswers
  14. {
  15. /// <summary>
  16. /// 问卷答案
  17. /// </summary>
  18. [ApiExplorerSettings(GroupName = "Work")]
  19. [Authorize]
  20. public class QuestionAnswerAppService : ApplicationService
  21. {
  22. private readonly IRepository<QuestionAnswer> _questionAnswerRepository;
  23. private readonly IRepository<QuestionAnswerAsbitem> _questionAnswerAsbitemRepository;
  24. private readonly IRepository<Asbitem> _asbitemRepository;
  25. private readonly IRepository<QuestionAnswerRiskLevel> _questionAnswerRiskLevelRepository;
  26. private readonly IRepository<DiseaseRiskLevel> _diseaseRiskLevelRepository;
  27. private readonly QuestionAnswerManager _questionAnswerManager;
  28. private readonly CacheService _cacheService;
  29. public QuestionAnswerAppService(
  30. CacheService cacheService,
  31. IRepository<QuestionAnswer> questionAnswerRepository,
  32. QuestionAnswerManager questionAnswerManager,
  33. IRepository<QuestionAnswerAsbitem> questionAnswerAsbitemRepository,
  34. IRepository<Asbitem> asbitemRepository,
  35. IRepository<QuestionAnswerRiskLevel> questionAnswerRiskLevelRepository,
  36. IRepository<DiseaseRiskLevel> diseaseRiskLevelRepository)
  37. {
  38. _cacheService = cacheService;
  39. _questionAnswerRepository = questionAnswerRepository;
  40. _questionAnswerManager = questionAnswerManager;
  41. _questionAnswerAsbitemRepository = questionAnswerAsbitemRepository;
  42. _asbitemRepository = asbitemRepository;
  43. _questionAnswerRiskLevelRepository = questionAnswerRiskLevelRepository;
  44. _diseaseRiskLevelRepository = diseaseRiskLevelRepository;
  45. }
  46. /// <summary>
  47. /// 获取问卷答案列表 疾病风险级别设置对应答案时用
  48. /// </summary>
  49. /// <returns></returns>
  50. [HttpPost("api/app/QuestionAnswer/GetList")]
  51. public async Task<List<QuestionAnswerDto>> GetListAsync()
  52. {
  53. var entlist = await _questionAnswerRepository.GetQueryableAsync();
  54. var entdto = entlist.Select(s => new QuestionAnswerDto
  55. {
  56. CreationTime = s.CreationTime,
  57. CreatorId = s.CreatorId,
  58. DisplayOrder = s.DisplayOrder,
  59. LastModificationTime = s.LastModificationTime,
  60. LastModifierId = s.LastModifierId,
  61. SimpleCode = s.SimpleCode,
  62. Aliases = s.Aliases,
  63. QuestionAnswerName = s.QuestionAnswerName,
  64. AnswerResultType = s.AnswerResultType,
  65. QuestionAnswerId = s.QuestionAnswerId,
  66. ChildAnswerTitle = s.ChildAnswerTitle,
  67. ChildAnswerType = s.ChildAnswerType,
  68. HealthGuidance = s.HealthGuidance,
  69. IsNone = s.IsNone,
  70. Overview = s.Overview,
  71. Reason = s.Reason,
  72. QuestionId = s.QuestionId,
  73. PathCode = s.PathCode,
  74. ParentId = s.ParentId,
  75. CreatorName = _cacheService.GetSurnameAsync(s.CreatorId).Result,
  76. LastModifierName = _cacheService.GetSurnameAsync(s.LastModifierId).Result
  77. }).OrderBy(o => o.DisplayOrder).ToList();
  78. return entdto;
  79. }
  80. /// <summary>
  81. /// 获取问卷答案树型列表 根据问卷ID
  82. /// </summary>
  83. /// <param name="input"></param>
  84. /// <returns></returns>
  85. [HttpPost("api/app/QuestionAnswer/GetQuestionTreeList")]
  86. public async Task<List<QuestionAnswerTreeListDto>> GetQuestionAnswerTreeListAsync(QuestionIdInputDto input)
  87. {
  88. var dataList = await _questionAnswerRepository.GetListAsync(m => m.QuestionId == input.QuestionId);
  89. var items = from p in dataList.OrderBy(o => o.DisplayOrder)
  90. select new QuestionAnswerTreeListDto()
  91. {
  92. ParentId = p.ParentId,
  93. QuestionId = p.QuestionId,
  94. CreationTime = p.CreationTime,
  95. CreatorId = p.CreatorId,
  96. CreatorName = _cacheService.GetSurnameAsync(p.CreatorId).Result,
  97. LastModificationTime = p.LastModificationTime,
  98. LastModifierId = p.LastModifierId,
  99. LastModifierName = _cacheService.GetSurnameAsync(p.LastModifierId).Result,
  100. PathCode = p.PathCode,
  101. SimpleCode = p.SimpleCode,
  102. DisplayOrder = p.DisplayOrder,
  103. ChildAnswerType = p.ChildAnswerType,
  104. QuestionAnswerId = p.QuestionAnswerId,
  105. Reason = p.Reason,
  106. QuestionAnswerName = p.QuestionAnswerName,
  107. Aliases = p.Aliases,
  108. AnswerResultType = p.AnswerResultType,
  109. ChildAnswerTitle = p.ChildAnswerTitle,
  110. HealthGuidance = p.HealthGuidance,
  111. IsNone = p.IsNone,
  112. Overview = p.Overview
  113. };
  114. return GetTree(items.ToList(), 0, "");
  115. }
  116. /// <summary>
  117. /// 创建
  118. /// </summary>
  119. /// <returns></returns>
  120. [HttpPost("api/app/QuestionAnswer/Create")]
  121. public async Task<QuestionAnswerDto> CreateAsync(CreateQuestionAnswerDto input)
  122. {
  123. var createEntity = ObjectMapper.Map<CreateQuestionAnswerDto, QuestionAnswer>(input);
  124. createEntity.QuestionAnswerId = GuidGenerator.Create();
  125. var entity = await _questionAnswerManager.CreateAsync(createEntity);
  126. entity = await _questionAnswerRepository.InsertAsync(entity);
  127. var dto = ObjectMapper.Map<QuestionAnswer, QuestionAnswerDto>(entity);
  128. dto.CreatorName = await _cacheService.GetSurnameAsync(dto.CreatorId);
  129. dto.LastModifierName = await _cacheService.GetSurnameAsync(dto.LastModifierId);
  130. return dto;
  131. }
  132. /// <summary>
  133. /// 更新
  134. /// </summary>
  135. /// <param name="input"></param>
  136. /// <returns></returns>
  137. [HttpPost("api/app/QuestionAnswer/Update")]
  138. public async Task<QuestionAnswerDto> UpdateAsync(UpdateQuestionAnswerDto input)
  139. {
  140. var entity = await _questionAnswerRepository.GetAsync(f => f.QuestionAnswerId == input.QuestionAnswerId);
  141. var sourceEntity = ObjectMapper.Map<UpdateQuestionAnswerDto, QuestionAnswer>(input);
  142. _questionAnswerManager.UpdateAsync(sourceEntity, entity);
  143. entity = await _questionAnswerRepository.UpdateAsync(entity);
  144. var dto = ObjectMapper.Map<QuestionAnswer, QuestionAnswerDto>(entity);
  145. dto.CreatorName = await _cacheService.GetSurnameAsync(dto.CreatorId);
  146. dto.LastModifierName = await _cacheService.GetSurnameAsync(dto.LastModifierId);
  147. return dto;
  148. }
  149. /// <summary>
  150. /// 删除
  151. /// </summary>
  152. /// <param name="input"></param>
  153. /// <returns></returns>
  154. [HttpPost("api/app/QuestionAnswer/Delete")]
  155. public async Task DeleteAsync(QuestionAnswerIdInputDto input)
  156. {
  157. await _questionAnswerManager.CheckAndDeleteAsync(input.QuestionAnswerId);
  158. }
  159. /// <summary>
  160. /// 修改排序 置顶,置底
  161. /// </summary>
  162. /// <param name="input"></param>
  163. /// <returns></returns>
  164. [HttpPost("api/app/QuestionAnswer/UpdateSortTopOrBottom")]
  165. public async Task UpdateSortTopOrBottomAsync(UpdateQuestionAnswerSortTopOrBottomInputDto input)
  166. {
  167. await _questionAnswerManager.UpdateSortTopOrBottomAsync(input.QuestionAnswerId, input.SortType);
  168. }
  169. /// <summary>
  170. /// 修改排序 拖拽
  171. /// </summary>
  172. /// <param name="input"></param>
  173. /// <returns></returns>
  174. [HttpPost("api/app/QuestionAnswer/UpdateSortDragAsync")]
  175. public async Task UpdateSortDragAsync(UpdateQuestionAnswerSortDragDto input)
  176. {
  177. await _questionAnswerManager.UpdateSortDragAsync(input);
  178. }
  179. /// <summary>
  180. /// 设置答案对应的疾病风险级别
  181. /// </summary>
  182. /// <returns></returns>
  183. [HttpPost("api/app/QuestionAnswer/CreateQuestionAnswerRiskLevel")]
  184. public async Task CreateQuestionAnswerRiskLevelAsync(CreateQuestionAnswerRiskLevelDto input)
  185. {
  186. await _questionAnswerRiskLevelRepository.DeleteAsync(d => d.QuestionAnswerId == input.QuestionAnswerId, true);
  187. if (input.DiseaseRiskLevelIds.Any())
  188. {
  189. foreach (var diseaseRiskLevelId in input.DiseaseRiskLevelIds)
  190. {
  191. var isQuestionAnswer = await _questionAnswerRiskLevelRepository.FirstOrDefaultAsync(f => f.DiseaseRiskLevelId == diseaseRiskLevelId &&
  192. f.QuestionAnswerId == input.QuestionAnswerId);
  193. if (isQuestionAnswer == null)
  194. {
  195. await _questionAnswerRiskLevelRepository.InsertAsync(new QuestionAnswerRiskLevel
  196. {
  197. DiseaseRiskLevelId = diseaseRiskLevelId,
  198. QuestionAnswerId = input.QuestionAnswerId
  199. }, true);
  200. }
  201. }
  202. }
  203. }
  204. /// <summary>
  205. /// 根据答案ID获取疾病风险级别
  206. /// </summary>
  207. /// <param name="input"></param>
  208. /// <returns></returns>
  209. [HttpPost("api/app/QuestionAnswer/GetQuestionAnswerRiskLevelById")]
  210. public async Task<List<QuestionAnswerRiskLevelDto>> GetQuestionAnswerRiskLevelByIdAsync(QuestionAnswerIdInputDto input)
  211. {
  212. var query = from questionAnswerRiskLevel in await _questionAnswerRiskLevelRepository.GetQueryableAsync()
  213. join diseaseRiskLevel in await _diseaseRiskLevelRepository.GetQueryableAsync()
  214. on questionAnswerRiskLevel.DiseaseRiskLevelId equals diseaseRiskLevel.DiseaseRiskLevelId
  215. where questionAnswerRiskLevel.QuestionAnswerId == input.QuestionAnswerId
  216. orderby diseaseRiskLevel.DisplayOrder ascending
  217. select new
  218. {
  219. questionAnswerRiskLevel.QuestionAnswerId,
  220. diseaseRiskLevel.DiseaseRiskLevelId,
  221. diseaseRiskLevel.DiseaseRiskLevelName,
  222. diseaseRiskLevel.DiseaseRiskId
  223. };
  224. var entListDto = query.Select(s => new QuestionAnswerRiskLevelDto
  225. {
  226. DiseaseRiskLevelId = s.DiseaseRiskLevelId,
  227. QuestionAnswerId = s.QuestionAnswerId,
  228. DiseaseRiskLevelName = s.DiseaseRiskLevelName,
  229. DiseaseRiskId = s.DiseaseRiskId
  230. }).ToList();
  231. return entListDto;
  232. }
  233. /// <summary>
  234. /// 设置问卷答案推荐的组合项目
  235. /// </summary>
  236. /// <returns></returns>
  237. [HttpPost("api/app/QuestionAnswer/CreateQuestionAnswerAsbitem")]
  238. public async Task CreateQuestionAnswerAsbitemAsync(CreateQuestionAnswerAsbitemDto input)
  239. {
  240. await _questionAnswerAsbitemRepository.DeleteAsync(d => d.QuestionAnswerId == input.QuestionAnswerId, true);
  241. if (input.Asbitems.Any())
  242. {
  243. foreach (var item in input.Asbitems)
  244. {
  245. var isAsbitem = await _questionAnswerAsbitemRepository.FirstOrDefaultAsync(f => f.AsbitemId == item.AsbitemId
  246. && f.QuestionAnswerId == input.QuestionAnswerId && f.AsbitemRecommendLevelId == item.AsbitemRecommendLevelId);
  247. if (isAsbitem == null)
  248. {
  249. await _questionAnswerAsbitemRepository.InsertAsync(new QuestionAnswerAsbitem
  250. {
  251. QuestionAnswerId = input.QuestionAnswerId,
  252. AsbitemId = item.AsbitemId,
  253. AsbitemRecommendLevelId = item.AsbitemRecommendLevelId
  254. }, true);
  255. }
  256. }
  257. }
  258. }
  259. /// <summary>
  260. /// 根据问卷答案获取推荐的组合项目
  261. /// </summary>
  262. /// <param name="input"></param>
  263. /// <returns></returns>
  264. [HttpPost("api/app/QuestionAnswer/GetQuestionAnswerAsbitemById")]
  265. public async Task<List<QuestionAnswerAsbitemDto>> GetQuestionAnswerAsbitemByIdAsync(QuestionAnswerIdInputDto input)
  266. {
  267. var query = from questionAnswerAsbitem in await _questionAnswerAsbitemRepository.GetQueryableAsync()
  268. join asbitem in await _asbitemRepository.GetQueryableAsync()
  269. on questionAnswerAsbitem.AsbitemId equals asbitem.AsbitemId
  270. where questionAnswerAsbitem.QuestionAnswerId == input.QuestionAnswerId
  271. orderby asbitem.DisplayOrder ascending
  272. select new
  273. {
  274. questionAnswerAsbitem.QuestionAnswerId,
  275. asbitem.AsbitemId,
  276. asbitem.AsbitemName,
  277. questionAnswerAsbitem.AsbitemRecommendLevelId
  278. };
  279. var entListDto = query.Select(s => new QuestionAnswerAsbitemDto
  280. {
  281. QuestionAnswerId = s.QuestionAnswerId,
  282. AsbitemId = s.AsbitemId,
  283. AsbitemName = s.AsbitemName,
  284. AsbitemRecommendLevelId = s.AsbitemRecommendLevelId
  285. }).ToList();
  286. return entListDto;
  287. }
  288. /// <summary>
  289. /// 使用Code进行递归
  290. /// </summary>
  291. /// <param name="items"></param>
  292. /// <param name="deep"></param>
  293. /// <param name="prefix"></param>
  294. /// <returns></returns>
  295. private List<QuestionAnswerTreeListDto> GetTree(List<QuestionAnswerTreeListDto> items, int deep, string prefix)
  296. {
  297. return (from p in items
  298. where p.PathCode.StartsWith(prefix) && p.PathCode.Count(a => a == '.') == deep
  299. orderby p.DisplayOrder ascending
  300. let subs = GetTree(items, deep + 1, p.PathCode)
  301. select new QuestionAnswerTreeListDto()
  302. {
  303. ParentId = p.ParentId,
  304. QuestionId = p.QuestionId,
  305. CreationTime = p.CreationTime,
  306. CreatorId = p.CreatorId,
  307. CreatorName = _cacheService.GetSurnameAsync(p.CreatorId).Result,
  308. LastModificationTime = p.LastModificationTime,
  309. LastModifierId = p.LastModifierId,
  310. LastModifierName = _cacheService.GetSurnameAsync(p.LastModifierId).Result,
  311. PathCode = p.PathCode,
  312. SimpleCode = p.SimpleCode,
  313. DisplayOrder = p.DisplayOrder,
  314. ChildAnswerType = p.ChildAnswerType,
  315. QuestionAnswerId = p.QuestionAnswerId,
  316. Reason = p.Reason,
  317. QuestionAnswerName = p.QuestionAnswerName,
  318. Aliases = p.Aliases,
  319. AnswerResultType = p.AnswerResultType,
  320. ChildAnswerTitle = p.ChildAnswerTitle,
  321. HealthGuidance = p.HealthGuidance,
  322. IsNone = p.IsNone,
  323. Overview = p.Overview,
  324. TreeChildren = subs.ToList()
  325. }
  326. ).ToList();
  327. }
  328. }
  329. }