|
|
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<QuestionAnswer> _questionAnswerRepository; private readonly IRepository<QuestionRegisterAnswer> _questionRegisterAnswerRepository; public QuestionAnswerManager( IRepository<QuestionAnswer> questionAnswerRepository, IRepository<QuestionRegisterAnswer> questionRegisterAnswerRepository) { _questionAnswerRepository = questionAnswerRepository; _questionRegisterAnswerRepository = questionRegisterAnswerRepository; }
/// <summary>
/// 创建
/// </summary>
/// <returns></returns>
public async Task<QuestionAnswer> 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<QuestionAnswer>(_questionAnswerRepository) }; }
/// <summary>
/// 更新
/// </summary>
/// <param name="sourceEntity"></param>
/// <param name="targetEntity"></param>
/// <returns></returns>
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);
}
/// <summary>
/// 删除
/// </summary>
/// <param name="QuestionAnswerId"></param>
/// <returns></returns>
/// <exception cref="UserFriendlyException"></exception>
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);
}
/// <summary>
/// 修改排序 置顶,置底
/// </summary>
/// <param name="id">需要修改的ID</param>
/// <param name="SortType">修改方式:1 置顶 2 置底</param>
/// <returns></returns>
public async Task UpdateSortTopOrBottomAsync(Guid QuestionAnswerId, int SortType) { var entity = await _questionAnswerRepository.GetAsync(f => f.QuestionAnswerId == QuestionAnswerId); await EntityHelper.UpdateSortTopOrBottomAsync(_questionAnswerRepository, entity, SortType); }
/// <summary>
/// 修改排序 拖拽
/// </summary>
/// <typeparam name="TEntity"></typeparam>
/// <param name="repository"></param>
/// <param name="input"></param>
/// <returns></returns>
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); }
/// <summary>
/// 自动生成pathcode
/// </summary>
/// <param name="parentId"></param>
/// <returns></returns>
public async Task<string> 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; } }}
|