using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Shentun.WebPeis.DiseaseRiskLevels; using Shentun.WebPeis.Models; using Shentun.WebPeis.Questions; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Volo.Abp.Application.Services; using Volo.Abp.Domain.Repositories; namespace Shentun.WebPeis.QuestionAnswers { /// /// 问卷答案 /// [ApiExplorerSettings(GroupName = "Work")] [Authorize] public class QuestionAnswerAppService : ApplicationService { private readonly IRepository _questionAnswerRepository; private readonly IRepository _questionAnswerAsbitemRepository; private readonly IRepository _asbitemRepository; private readonly IRepository _questionAnswerRiskLevelRepository; private readonly IRepository _diseaseRiskLevelRepository; private readonly QuestionAnswerManager _questionAnswerManager; private readonly CacheService _cacheService; public QuestionAnswerAppService( CacheService cacheService, IRepository questionAnswerRepository, QuestionAnswerManager questionAnswerManager, IRepository questionAnswerAsbitemRepository, IRepository asbitemRepository, IRepository questionAnswerRiskLevelRepository, IRepository diseaseRiskLevelRepository) { _cacheService = cacheService; _questionAnswerRepository = questionAnswerRepository; _questionAnswerManager = questionAnswerManager; _questionAnswerAsbitemRepository = questionAnswerAsbitemRepository; _asbitemRepository = asbitemRepository; _questionAnswerRiskLevelRepository = questionAnswerRiskLevelRepository; _diseaseRiskLevelRepository = diseaseRiskLevelRepository; } /// /// 获取问卷答案列表 疾病风险级别设置对应答案时用 /// /// [HttpPost("api/app/QuestionAnswer/GetList")] public async Task> GetListAsync() { var entlist = await _questionAnswerRepository.GetQueryableAsync(); var entdto = entlist.Select(s => new QuestionAnswerDto { CreationTime = s.CreationTime, CreatorId = s.CreatorId, DisplayOrder = s.DisplayOrder, LastModificationTime = s.LastModificationTime, LastModifierId = s.LastModifierId, SimpleCode = s.SimpleCode, Aliases = s.Aliases, QuestionAnswerName = s.QuestionAnswerName, AnswerResultType = s.AnswerResultType, QuestionAnswerId = s.QuestionAnswerId, ChildAnswerTitle = s.ChildAnswerTitle, ChildAnswerType = s.ChildAnswerType, HealthGuidance = s.HealthGuidance, IsNone = s.IsNone, Overview = s.Overview, Reason = s.Reason, QuestionId = s.QuestionId, PathCode = s.PathCode, ParentId = s.ParentId, CreatorName = _cacheService.GetSurnameAsync(s.CreatorId).Result, LastModifierName = _cacheService.GetSurnameAsync(s.LastModifierId).Result }).OrderBy(o => o.DisplayOrder).ToList(); return entdto; } /// /// 获取问卷答案树型列表 根据问卷ID /// /// /// [HttpPost("api/app/QuestionAnswer/GetQuestionTreeList")] public async Task> GetQuestionAnswerTreeListAsync(QuestionIdInputDto input) { var dataList = await _questionAnswerRepository.GetListAsync(m => m.QuestionId == input.QuestionId); var items = from p in dataList.OrderBy(o => o.DisplayOrder) select new QuestionAnswerTreeListDto() { ParentId = p.ParentId, QuestionId = p.QuestionId, CreationTime = p.CreationTime, CreatorId = p.CreatorId, CreatorName = _cacheService.GetSurnameAsync(p.CreatorId).Result, LastModificationTime = p.LastModificationTime, LastModifierId = p.LastModifierId, LastModifierName = _cacheService.GetSurnameAsync(p.LastModifierId).Result, PathCode = p.PathCode, SimpleCode = p.SimpleCode, DisplayOrder = p.DisplayOrder, ChildAnswerType = p.ChildAnswerType, QuestionAnswerId = p.QuestionAnswerId, Reason = p.Reason, QuestionAnswerName = p.QuestionAnswerName, Aliases = p.Aliases, AnswerResultType = p.AnswerResultType, ChildAnswerTitle = p.ChildAnswerTitle, HealthGuidance = p.HealthGuidance, IsNone = p.IsNone, Overview = p.Overview }; return GetTree(items.ToList(), 0, ""); } /// /// 创建 /// /// [HttpPost("api/app/QuestionAnswer/Create")] public async Task CreateAsync(CreateQuestionAnswerDto input) { var createEntity = ObjectMapper.Map(input); createEntity.QuestionAnswerId = GuidGenerator.Create(); var entity = await _questionAnswerManager.CreateAsync(createEntity); entity = await _questionAnswerRepository.InsertAsync(entity); var dto = ObjectMapper.Map(entity); dto.CreatorName = await _cacheService.GetSurnameAsync(dto.CreatorId); dto.LastModifierName = await _cacheService.GetSurnameAsync(dto.LastModifierId); return dto; } /// /// 更新 /// /// /// [HttpPost("api/app/QuestionAnswer/Update")] public async Task UpdateAsync(UpdateQuestionAnswerDto input) { var entity = await _questionAnswerRepository.GetAsync(f => f.QuestionAnswerId == input.QuestionAnswerId); var sourceEntity = ObjectMapper.Map(input); _questionAnswerManager.UpdateAsync(sourceEntity, entity); entity = await _questionAnswerRepository.UpdateAsync(entity); var dto = ObjectMapper.Map(entity); dto.CreatorName = await _cacheService.GetSurnameAsync(dto.CreatorId); dto.LastModifierName = await _cacheService.GetSurnameAsync(dto.LastModifierId); return dto; } /// /// 删除 /// /// /// [HttpPost("api/app/QuestionAnswer/Delete")] public async Task DeleteAsync(QuestionAnswerIdInputDto input) { await _questionAnswerManager.CheckAndDeleteAsync(input.QuestionAnswerId); } /// /// 修改排序 置顶,置底 /// /// /// [HttpPost("api/app/QuestionAnswer/UpdateSortTopOrBottom")] public async Task UpdateSortTopOrBottomAsync(UpdateQuestionAnswerSortTopOrBottomInputDto input) { await _questionAnswerManager.UpdateSortTopOrBottomAsync(input.QuestionAnswerId, input.SortType); } /// /// 修改排序 拖拽 /// /// /// [HttpPost("api/app/QuestionAnswer/UpdateSortDragAsync")] public async Task UpdateSortDragAsync(UpdateQuestionAnswerSortDragDto input) { await _questionAnswerManager.UpdateSortDragAsync(input); } /// /// 设置答案对应的疾病风险级别 /// /// [HttpPost("api/app/QuestionAnswer/CreateQuestionAnswerRiskLevel")] public async Task CreateQuestionAnswerRiskLevelAsync(CreateQuestionAnswerRiskLevelDto input) { await _questionAnswerRiskLevelRepository.DeleteAsync(d => d.QuestionAnswerId == input.QuestionAnswerId, true); if (input.DiseaseRiskLevelIds.Any()) { foreach (var diseaseRiskLevelId in input.DiseaseRiskLevelIds) { var isQuestionAnswer = await _questionAnswerRiskLevelRepository.FirstOrDefaultAsync(f => f.DiseaseRiskLevelId == diseaseRiskLevelId && f.QuestionAnswerId == input.QuestionAnswerId); if (isQuestionAnswer == null) { await _questionAnswerRiskLevelRepository.InsertAsync(new QuestionAnswerRiskLevel { DiseaseRiskLevelId = diseaseRiskLevelId, QuestionAnswerId = input.QuestionAnswerId }, true); } } } } /// /// 根据答案ID获取疾病风险级别 /// /// /// [HttpPost("api/app/QuestionAnswer/GetQuestionAnswerRiskLevelById")] public async Task> GetQuestionAnswerRiskLevelByIdAsync(QuestionAnswerIdInputDto input) { var query = from questionAnswerRiskLevel in await _questionAnswerRiskLevelRepository.GetQueryableAsync() join diseaseRiskLevel in await _diseaseRiskLevelRepository.GetQueryableAsync() on questionAnswerRiskLevel.DiseaseRiskLevelId equals diseaseRiskLevel.DiseaseRiskLevelId where questionAnswerRiskLevel.QuestionAnswerId == input.QuestionAnswerId orderby diseaseRiskLevel.DisplayOrder ascending select new { questionAnswerRiskLevel.QuestionAnswerId, diseaseRiskLevel.DiseaseRiskLevelId, diseaseRiskLevel.DiseaseRiskLevelName, diseaseRiskLevel.DiseaseRiskId }; var entListDto = query.Select(s => new QuestionAnswerRiskLevelDto { DiseaseRiskLevelId = s.DiseaseRiskLevelId, QuestionAnswerId = s.QuestionAnswerId, DiseaseRiskLevelName = s.DiseaseRiskLevelName, DiseaseRiskId = s.DiseaseRiskId }).ToList(); return entListDto; } /// /// 设置问卷答案推荐的组合项目 /// /// [HttpPost("api/app/QuestionAnswer/CreateQuestionAnswerAsbitem")] public async Task CreateQuestionAnswerAsbitemAsync(CreateQuestionAnswerAsbitemDto input) { await _questionAnswerAsbitemRepository.DeleteAsync(d => d.QuestionAnswerId == input.QuestionAnswerId, true); if (input.Asbitems.Any()) { foreach (var item in input.Asbitems) { var isAsbitem = await _questionAnswerAsbitemRepository.FirstOrDefaultAsync(f => f.AsbitemId == item.AsbitemId && f.QuestionAnswerId == input.QuestionAnswerId && f.AsbitemRecommendLevelId == item.AsbitemRecommendLevelId); if (isAsbitem == null) { await _questionAnswerAsbitemRepository.InsertAsync(new QuestionAnswerAsbitem { QuestionAnswerId = input.QuestionAnswerId, AsbitemId = item.AsbitemId, AsbitemRecommendLevelId = item.AsbitemRecommendLevelId }, true); } } } } /// /// 根据问卷答案获取推荐的组合项目 /// /// /// [HttpPost("api/app/QuestionAnswer/GetQuestionAnswerAsbitemById")] public async Task> GetQuestionAnswerAsbitemByIdAsync(QuestionAnswerIdInputDto input) { var query = from questionAnswerAsbitem in await _questionAnswerAsbitemRepository.GetQueryableAsync() join asbitem in await _asbitemRepository.GetQueryableAsync() on questionAnswerAsbitem.AsbitemId equals asbitem.AsbitemId where questionAnswerAsbitem.QuestionAnswerId == input.QuestionAnswerId orderby asbitem.DisplayOrder ascending select new { questionAnswerAsbitem.QuestionAnswerId, asbitem.AsbitemId, asbitem.AsbitemName, questionAnswerAsbitem.AsbitemRecommendLevelId }; var entListDto = query.Select(s => new QuestionAnswerAsbitemDto { QuestionAnswerId = s.QuestionAnswerId, AsbitemId = s.AsbitemId, AsbitemName = s.AsbitemName, AsbitemRecommendLevelId = s.AsbitemRecommendLevelId }).ToList(); return entListDto; } /// /// 使用Code进行递归 /// /// /// /// /// private List GetTree(List items, int deep, string prefix) { return (from p in items where p.PathCode.StartsWith(prefix) && p.PathCode.Count(a => a == '.') == deep orderby p.DisplayOrder ascending let subs = GetTree(items, deep + 1, p.PathCode) select new QuestionAnswerTreeListDto() { ParentId = p.ParentId, QuestionId = p.QuestionId, CreationTime = p.CreationTime, CreatorId = p.CreatorId, CreatorName = _cacheService.GetSurnameAsync(p.CreatorId).Result, LastModificationTime = p.LastModificationTime, LastModifierId = p.LastModifierId, LastModifierName = _cacheService.GetSurnameAsync(p.LastModifierId).Result, PathCode = p.PathCode, SimpleCode = p.SimpleCode, DisplayOrder = p.DisplayOrder, ChildAnswerType = p.ChildAnswerType, QuestionAnswerId = p.QuestionAnswerId, Reason = p.Reason, QuestionAnswerName = p.QuestionAnswerName, Aliases = p.Aliases, AnswerResultType = p.AnswerResultType, ChildAnswerTitle = p.ChildAnswerTitle, HealthGuidance = p.HealthGuidance, IsNone = p.IsNone, Overview = p.Overview, TreeChildren = subs.ToList() } ).ToList(); } } }