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

using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Shentun.WebPeis.Models;
using Shentun.WebPeis.QuestionTypes;
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.Questions
{
/// <summary>
/// 问卷
/// </summary>
[ApiExplorerSettings(GroupName = "Work")]
[Authorize]
public class QuestionAppService : ApplicationService
{
private readonly IRepository<Question> _questionRepository;
private readonly QuestionManager _questionManager;
private readonly CacheService _cacheService;
public QuestionAppService(
IRepository<Question> questionRepository,
QuestionManager questionManager,
CacheService cacheService)
{
_questionRepository = questionRepository;
_questionManager = questionManager;
_cacheService = cacheService;
}
///// <summary>
///// 获取列表
///// </summary>
///// <returns></returns>
//[HttpPost("api/app/Question/GetList")]
//public async Task<List<QuestionDto>> GetListAsync()
//{
// var entlist = await _questionRepository.GetQueryableAsync();
// var entdto = entlist.Select(s => new QuestionDto
// {
// CreationTime = s.CreationTime,
// CreatorId = s.CreatorId,
// DisplayOrder = s.DisplayOrder,
// LastModificationTime = s.LastModificationTime,
// LastModifierId = s.LastModifierId,
// SimpleCode = s.SimpleCode,
// QuestionTypeId = s.QuestionTypeId,
// QuestionSubjectTypeId = s.QuestionSubjectTypeId,
// QuestionName = s.QuestionName,
// QuestionId = s.QuestionId,
// PathCode = s.PathCode,
// ParentId = s.ParentId,
// IsActive = s.IsActive,
// AnswerType = s.AnswerType,
// CreatorName = _cacheService.GetSurnameAsync(s.CreatorId).Result,
// LastModifierName = _cacheService.GetSurnameAsync(s.LastModifierId).Result
// }).OrderBy(o => o.DisplayOrder).ToList();
// return entdto;
//}
/// <summary>
/// 获取问卷树型列表
/// </summary>
/// <returns></returns>
[HttpPost("api/app/Question/GetQuestionTreeList")]
public async Task<List<QuestionTreeListDto>> GetQuestionTreeListAsync()
{
var dataList = await _questionRepository.GetListAsync();
var items = from p in dataList.OrderBy(o => o.DisplayOrder)
select new QuestionTreeListDto()
{
ParentId = p.ParentId,
AnswerType = p.AnswerType,
QuestionId = p.QuestionId,
CreationTime = p.CreationTime,
CreatorId = p.CreatorId,
CreatorName = _cacheService.GetSurnameAsync(p.CreatorId).Result,
IsActive = p.IsActive,
LastModificationTime = p.LastModificationTime,
LastModifierId = p.LastModifierId,
LastModifierName = _cacheService.GetSurnameAsync(p.LastModifierId).Result,
PathCode = p.PathCode,
QuestionName = p.QuestionName,
QuestionSubjectTypeId = p.QuestionSubjectTypeId,
QuestionTypeId = p.QuestionTypeId,
SimpleCode = p.SimpleCode,
DisplayOrder = p.DisplayOrder
};
return GetTree(items.ToList(), 0, "");
}
/// <summary>
/// 创建
/// </summary>
/// <returns></returns>
[HttpPost("api/app/Question/Create")]
public async Task<QuestionDto> CreateAsync(CreateQuestionDto input)
{
var createEntity = ObjectMapper.Map<CreateQuestionDto, Question>(input);
var entity = await _questionManager.CreateAsync(createEntity);
entity = await _questionRepository.InsertAsync(entity);
var dto = ObjectMapper.Map<Question, QuestionDto>(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/Question/Update")]
public async Task<QuestionDto> UpdateAsync(UpdateQuestionDto input)
{
var entity = await _questionRepository.GetAsync(f => f.QuestionTypeId == input.QuestionTypeId);
var sourceEntity = ObjectMapper.Map<UpdateQuestionDto, Question>(input);
_questionManager.UpdateAsync(sourceEntity, entity);
entity = await _questionRepository.UpdateAsync(entity);
var dto = ObjectMapper.Map<Question, QuestionDto>(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/Question/Delete")]
public async Task DeleteAsync(QuestionIdInputDto input)
{
await _questionManager.CheckAndDeleteAsync(input.QuestionId);
}
/// <summary>
/// 修改排序 置顶,置底
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost("api/app/Question/UpdateSortTopOrBottom")]
public async Task UpdateSortTopOrBottomAsync(UpdateQuestionSortTopOrBottomInputDto input)
{
await _questionManager.UpdateSortTopOrBottomAsync(input.QuestionId, input.SortType);
}
/// <summary>
/// 修改排序 拖拽
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost("api/app/Question/UpdateSortDragAsync")]
public async Task UpdateSortDragAsync(UpdateQuestionSortDragDto input)
{
await _questionManager.UpdateSortDragAsync(input);
}
/// <summary>
/// 使用Code进行递归
/// </summary>
/// <param name="items"></param>
/// <param name="deep"></param>
/// <param name="prefix"></param>
/// <returns></returns>
private List<QuestionTreeListDto> GetTree(List<QuestionTreeListDto> 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 QuestionTreeListDto()
{
ParentId = p.ParentId,
AnswerType = p.AnswerType,
QuestionId = p.QuestionId,
CreationTime = p.CreationTime,
CreatorId = p.CreatorId,
CreatorName = _cacheService.GetSurnameAsync(p.CreatorId).Result,
IsActive = p.IsActive,
LastModificationTime = p.LastModificationTime,
LastModifierId = p.LastModifierId,
LastModifierName = _cacheService.GetSurnameAsync(p.LastModifierId).Result,
PathCode = p.PathCode,
QuestionName = p.QuestionName,
QuestionSubjectTypeId = p.QuestionSubjectTypeId,
QuestionTypeId = p.QuestionTypeId,
SimpleCode = p.SimpleCode,
DisplayOrder = p.DisplayOrder,
TreeChildren = subs.ToList()
}
).ToList();
}
}
}