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 { /// /// 问卷 /// [ApiExplorerSettings(GroupName = "Work")] [Authorize] public class QuestionAppService : ApplicationService { private readonly IRepository _questionRepository; private readonly QuestionManager _questionManager; private readonly CacheService _cacheService; public QuestionAppService( IRepository questionRepository, QuestionManager questionManager, CacheService cacheService) { _questionRepository = questionRepository; _questionManager = questionManager; _cacheService = cacheService; } ///// ///// 获取列表 ///// ///// //[HttpPost("api/app/Question/GetList")] //public async Task> 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; //} /// /// 获取问卷树型列表 /// /// [HttpPost("api/app/Question/GetQuestionTreeList")] public async Task> 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, ""); } /// /// 创建 /// /// [HttpPost("api/app/Question/Create")] public async Task CreateAsync(CreateQuestionDto input) { var createEntity = ObjectMapper.Map(input); var entity = await _questionManager.CreateAsync(createEntity); entity = await _questionRepository.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/Question/Update")] public async Task UpdateAsync(UpdateQuestionDto input) { var entity = await _questionRepository.GetAsync(f => f.QuestionTypeId == input.QuestionTypeId); var sourceEntity = ObjectMapper.Map(input); _questionManager.UpdateAsync(sourceEntity, entity); entity = await _questionRepository.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/Question/Delete")] public async Task DeleteAsync(QuestionIdInputDto input) { await _questionManager.CheckAndDeleteAsync(input.QuestionId); } /// /// 修改排序 置顶,置底 /// /// /// [HttpPost("api/app/Question/UpdateSortTopOrBottom")] public async Task UpdateSortTopOrBottomAsync(UpdateQuestionSortTopOrBottomInputDto input) { await _questionManager.UpdateSortTopOrBottomAsync(input.QuestionId, input.SortType); } /// /// 修改排序 拖拽 /// /// /// [HttpPost("api/app/Question/UpdateSortDragAsync")] public async Task UpdateSortDragAsync(UpdateQuestionSortDragDto input) { await _questionManager.UpdateSortDragAsync(input); } /// /// 使用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 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(); } } }