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

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
{
/// <summary>
/// 问卷答案
/// </summary>
[ApiExplorerSettings(GroupName = "Work")]
[Authorize]
public class QuestionAnswerAppService : ApplicationService
{
private readonly IRepository<QuestionAnswer> _questionAnswerRepository;
private readonly IRepository<QuestionAnswerAsbitem> _questionAnswerAsbitemRepository;
private readonly IRepository<Asbitem> _asbitemRepository;
private readonly IRepository<QuestionAnswerRiskLevel> _questionAnswerRiskLevelRepository;
private readonly IRepository<DiseaseRiskLevel> _diseaseRiskLevelRepository;
private readonly QuestionAnswerManager _questionAnswerManager;
private readonly CacheService _cacheService;
public QuestionAnswerAppService(
CacheService cacheService,
IRepository<QuestionAnswer> questionAnswerRepository,
QuestionAnswerManager questionAnswerManager,
IRepository<QuestionAnswerAsbitem> questionAnswerAsbitemRepository,
IRepository<Asbitem> asbitemRepository,
IRepository<QuestionAnswerRiskLevel> questionAnswerRiskLevelRepository,
IRepository<DiseaseRiskLevel> diseaseRiskLevelRepository)
{
_cacheService = cacheService;
_questionAnswerRepository = questionAnswerRepository;
_questionAnswerManager = questionAnswerManager;
_questionAnswerAsbitemRepository = questionAnswerAsbitemRepository;
_asbitemRepository = asbitemRepository;
_questionAnswerRiskLevelRepository = questionAnswerRiskLevelRepository;
_diseaseRiskLevelRepository = diseaseRiskLevelRepository;
}
/// <summary>
/// 获取问卷答案列表 疾病风险级别设置对应答案时用
/// </summary>
/// <returns></returns>
[HttpPost("api/app/QuestionAnswer/GetList")]
public async Task<List<QuestionAnswerDto>> 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;
}
/// <summary>
/// 获取问卷答案树型列表 根据问卷ID
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost("api/app/QuestionAnswer/GetQuestionTreeList")]
public async Task<List<QuestionAnswerTreeListDto>> 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, "");
}
/// <summary>
/// 创建
/// </summary>
/// <returns></returns>
[HttpPost("api/app/QuestionAnswer/Create")]
public async Task<QuestionAnswerDto> CreateAsync(CreateQuestionAnswerDto input)
{
var createEntity = ObjectMapper.Map<CreateQuestionAnswerDto, QuestionAnswer>(input);
createEntity.QuestionAnswerId = GuidGenerator.Create();
var entity = await _questionAnswerManager.CreateAsync(createEntity);
entity = await _questionAnswerRepository.InsertAsync(entity);
var dto = ObjectMapper.Map<QuestionAnswer, QuestionAnswerDto>(entity);
dto.CreatorName = await _cacheService.GetSurnameAsync(dto.CreatorId);
dto.LastModifierName = await _cacheService.GetSurnameAsync(dto.LastModifierId);
return dto;
}
/// <summary>
/// 更新
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost("api/app/QuestionAnswer/Update")]
public async Task<QuestionAnswerDto> UpdateAsync(UpdateQuestionAnswerDto input)
{
var entity = await _questionAnswerRepository.GetAsync(f => f.QuestionAnswerId == input.QuestionAnswerId);
var sourceEntity = ObjectMapper.Map<UpdateQuestionAnswerDto, QuestionAnswer>(input);
_questionAnswerManager.UpdateAsync(sourceEntity, entity);
entity = await _questionAnswerRepository.UpdateAsync(entity);
var dto = ObjectMapper.Map<QuestionAnswer, QuestionAnswerDto>(entity);
dto.CreatorName = await _cacheService.GetSurnameAsync(dto.CreatorId);
dto.LastModifierName = await _cacheService.GetSurnameAsync(dto.LastModifierId);
return dto;
}
/// <summary>
/// 删除
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost("api/app/QuestionAnswer/Delete")]
public async Task DeleteAsync(QuestionAnswerIdInputDto input)
{
await _questionAnswerManager.CheckAndDeleteAsync(input.QuestionAnswerId);
}
/// <summary>
/// 修改排序 置顶,置底
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost("api/app/QuestionAnswer/UpdateSortTopOrBottom")]
public async Task UpdateSortTopOrBottomAsync(UpdateQuestionAnswerSortTopOrBottomInputDto input)
{
await _questionAnswerManager.UpdateSortTopOrBottomAsync(input.QuestionAnswerId, input.SortType);
}
/// <summary>
/// 修改排序 拖拽
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost("api/app/QuestionAnswer/UpdateSortDragAsync")]
public async Task UpdateSortDragAsync(UpdateQuestionAnswerSortDragDto input)
{
await _questionAnswerManager.UpdateSortDragAsync(input);
}
/// <summary>
/// 设置答案对应的疾病风险级别
/// </summary>
/// <returns></returns>
[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);
}
}
}
}
/// <summary>
/// 根据答案ID获取疾病风险级别
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost("api/app/QuestionAnswer/GetQuestionAnswerRiskLevelById")]
public async Task<List<QuestionAnswerRiskLevelDto>> 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;
}
/// <summary>
/// 设置问卷答案推荐的组合项目
/// </summary>
/// <returns></returns>
[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);
}
}
}
}
/// <summary>
/// 根据问卷答案获取推荐的组合项目
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost("api/app/QuestionAnswer/GetQuestionAnswerAsbitemById")]
public async Task<List<QuestionAnswerAsbitemDto>> 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;
}
/// <summary>
/// 使用Code进行递归
/// </summary>
/// <param name="items"></param>
/// <param name="deep"></param>
/// <param name="prefix"></param>
/// <returns></returns>
private List<QuestionAnswerTreeListDto> GetTree(List<QuestionAnswerTreeListDto> 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();
}
}
}