using NPOI.POIFS.Properties; using Shentun.Utilities; 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; using Volo.Abp.Domain.Entities; using Volo.Abp.Domain.Repositories; using Volo.Abp.Domain.Services; namespace Shentun.WebPeis.Questions { public class QuestionManager : DomainService { private readonly IRepository _questionRepository; public QuestionManager( IRepository questionRepository ) { _questionRepository = questionRepository; } /// /// 创建 /// /// public async Task CreateAsync(Question entity) { DataHelper.CheckEntityIsNull(entity); //DataHelper.CheckStringIsNull(entity.QuestionTypeName, "名称"); return new Question { AnswerType = entity.AnswerType, IsActive = entity.IsActive, ParentId = entity.ParentId, PathCode = await CreatePathCode(entity.ParentId), QuestionId = entity.QuestionId, QuestionName = entity.QuestionName, QuestionSubjectTypeId = entity.QuestionSubjectTypeId, SimpleCode = LanguageConverter.GetPYSimpleCode(entity.QuestionName), DisplayOrder = await EntityHelper.CreateMaxDisplayOrder(_questionRepository), QuestionTypeId = entity.QuestionTypeId }; } /// /// 更新 /// /// /// /// public void UpdateAsync( Question sourceEntity, Question targetEntity ) { DataHelper.CheckEntityIsNull(sourceEntity); DataHelper.CheckEntityIsNull(targetEntity); //DataHelper.CheckStringIsNull(sourceEntity.QuestionTypeName, "名称"); targetEntity.AnswerType = sourceEntity.AnswerType; targetEntity.IsActive = sourceEntity.IsActive; targetEntity.ParentId = sourceEntity.ParentId; targetEntity.QuestionName = sourceEntity.QuestionName; targetEntity.QuestionSubjectTypeId = sourceEntity.QuestionSubjectTypeId; targetEntity.SimpleCode = LanguageConverter.GetPYSimpleCode(sourceEntity.QuestionName); targetEntity.QuestionTypeId = sourceEntity.QuestionTypeId; } /// /// 删除 /// /// /// /// public async Task CheckAndDeleteAsync(Guid QuestionId) { //var questionEnt = await _questionRepository.FirstOrDefaultAsync(m => m.QuestionTypeId == QuestionTypeId); //if (questionEnt != null) //{ // throw new UserFriendlyException($"问卷类别已被使用,不能删除"); //} await _questionRepository.DeleteAsync(d => d.QuestionId == QuestionId); } /// /// 修改排序 置顶,置底 /// /// 需要修改的ID /// 修改方式:1 置顶 2 置底 /// public async Task UpdateSortTopOrBottomAsync(Guid QuestionId, int SortType) { var entity = await _questionRepository.GetAsync(f => f.QuestionId == QuestionId); await EntityHelper.UpdateSortTopOrBottomAsync(_questionRepository, entity, SortType); } /// /// 修改排序 拖拽 /// /// /// /// /// public async Task UpdateSortDragAsync(UpdateQuestionSortDragDto input) { var entitylist = await _questionRepository.GetListAsync(o => input.ItemList.Select(s => s.QuestionId).Contains(o.QuestionId)); foreach (var entity in entitylist) { foreach (var item in input.ItemList) { if (item.QuestionId == entity.QuestionId) entity.DisplayOrder = item.DisplayOrder; } } await _questionRepository.UpdateManyAsync(entitylist); } /// /// 自动生成pathcode /// /// /// public async Task CreatePathCode(Guid? parentId) { string PathCode = "00001"; //一级 if (parentId == null || parentId == Guid.Empty) { //最大pathcode var LastPathCode = (await _questionRepository.GetListAsync(o => o.ParentId == Guid.Empty || o.ParentId == null)) .OrderByDescending(o => { var sortCode = o.PathCode.Replace(".", ""); return Convert.ToInt32(sortCode); }).FirstOrDefault(); if (LastPathCode != null) { PathCode = (Convert.ToInt32(LastPathCode.PathCode) + 1).ToString().PadLeft(5, '0'); } else { PathCode = "00001"; } } else { //二级以及以上 //上级pathcode var ParentPathCode = (await _questionRepository.GetListAsync(o => o.QuestionId == parentId)).FirstOrDefault().PathCode; //最大pathcode var LastPathCode = (await _questionRepository.GetListAsync(o => o.ParentId == parentId)) .OrderByDescending(o => { var sortCode = o.PathCode.Replace(".", ""); return Convert.ToInt32(sortCode); }).Select(s => s.PathCode).FirstOrDefault(); if (!string.IsNullOrEmpty(LastPathCode)) { var MaxCode = LastPathCode.Split('.').Last(); PathCode = ParentPathCode + "." + (Convert.ToInt32(MaxCode) + 1).ToString().PadLeft(5, '0'); } else { PathCode = ParentPathCode + ".00001"; } } return PathCode; } } }