using NPOI.POIFS.Properties; using Shentun.Utilities; using Shentun.WebPeis.Models; using Shentun.WebPeis.Questions; using System; using System.Collections.Generic; using System.Linq; using System.Runtime.CompilerServices; 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.QuestionAnswers { public class QuestionAnswerManager : DomainService { private readonly IRepository _questionAnswerRepository; private readonly IRepository _questionRegisterAnswerRepository; public QuestionAnswerManager( IRepository questionAnswerRepository, IRepository questionRegisterAnswerRepository) { _questionAnswerRepository = questionAnswerRepository; _questionRegisterAnswerRepository = questionRegisterAnswerRepository; } /// /// 创建 /// /// public async Task CreateAsync(QuestionAnswer entity) { DataHelper.CheckEntityIsNull(entity); //DataHelper.CheckStringIsNull(entity.QuestionTypeName, "名称"); return new QuestionAnswer { HealthGuidance = entity.HealthGuidance, Aliases = entity.Aliases, IsNone = entity.IsNone, Overview = entity.Overview, ChildAnswerTitle = entity.ChildAnswerTitle, AnswerResultType = entity.AnswerResultType, ChildAnswerType = entity.ChildAnswerType, QuestionAnswerId = entity.QuestionAnswerId, QuestionAnswerName = entity.QuestionAnswerName, QuestionRegisterAnswers = entity.QuestionRegisterAnswers, Reason = entity.Reason, ParentId = entity.ParentId, PathCode = await CreatePathCode(entity.ParentId), QuestionId = entity.QuestionId, SimpleCode = LanguageConverter.GetPYSimpleCode(entity.QuestionAnswerName), DisplayOrder = await EntityHelper.CreateMaxDisplayOrder(_questionAnswerRepository) }; } /// /// 更新 /// /// /// /// public void UpdateAsync( QuestionAnswer sourceEntity, QuestionAnswer targetEntity ) { DataHelper.CheckEntityIsNull(sourceEntity); DataHelper.CheckEntityIsNull(targetEntity); //DataHelper.CheckStringIsNull(sourceEntity.QuestionTypeName, "名称"); targetEntity.HealthGuidance = sourceEntity.HealthGuidance; targetEntity.Aliases = sourceEntity.Aliases; targetEntity.IsNone = sourceEntity.IsNone; targetEntity.Overview = sourceEntity.Overview; targetEntity.ChildAnswerTitle = sourceEntity.ChildAnswerTitle; targetEntity.AnswerResultType = sourceEntity.AnswerResultType; targetEntity.ChildAnswerType = sourceEntity.ChildAnswerType; targetEntity.QuestionAnswerName = sourceEntity.QuestionAnswerName; targetEntity.QuestionRegisterAnswers = sourceEntity.QuestionRegisterAnswers; targetEntity.Reason = sourceEntity.Reason; targetEntity.QuestionId = sourceEntity.QuestionId; targetEntity.SimpleCode = LanguageConverter.GetPYSimpleCode(sourceEntity.QuestionAnswerName); } /// /// 删除 /// /// /// /// public async Task CheckAndDeleteAsync(Guid QuestionAnswerId) { var questionRegisterAnswerEnt = await _questionRegisterAnswerRepository.FirstOrDefaultAsync(m => m.QuestionAnswerId == QuestionAnswerId); if (questionRegisterAnswerEnt != null) { throw new UserFriendlyException($"问卷答案已被人员登记使用,不能删除"); } await _questionAnswerRepository.DeleteAsync(d => d.QuestionAnswerId == QuestionAnswerId); } /// /// 修改排序 置顶,置底 /// /// 需要修改的ID /// 修改方式:1 置顶 2 置底 /// public async Task UpdateSortTopOrBottomAsync(Guid QuestionAnswerId, int SortType) { var entity = await _questionAnswerRepository.GetAsync(f => f.QuestionAnswerId == QuestionAnswerId); await EntityHelper.UpdateSortTopOrBottomAsync(_questionAnswerRepository, entity, SortType); } /// /// 修改排序 拖拽 /// /// /// /// /// public async Task UpdateSortDragAsync(UpdateQuestionAnswerSortDragDto input) { var entitylist = await _questionAnswerRepository.GetListAsync(o => input.ItemList.Select(s => s.QuestionAnswerId).Contains(o.QuestionAnswerId)); foreach (var entity in entitylist) { foreach (var item in input.ItemList) { if (item.QuestionAnswerId == entity.QuestionAnswerId) entity.DisplayOrder = item.DisplayOrder; } } await _questionAnswerRepository.UpdateManyAsync(entitylist); } /// /// 自动生成pathcode /// /// /// public async Task CreatePathCode(Guid? parentId) { string PathCode = "00001"; //一级 if (parentId == null || parentId == Guid.Empty) { //最大pathcode var LastPathCode = (await _questionAnswerRepository.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 _questionAnswerRepository.GetListAsync(o => o.QuestionAnswerId == parentId)).FirstOrDefault().PathCode; //最大pathcode var LastPathCode = (await _questionAnswerRepository.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; } } }