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.
|
|
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<Question> _questionRepository;
public QuestionManager( IRepository<Question> questionRepository ) { _questionRepository = questionRepository; }
/// <summary>
/// 创建
/// </summary>
/// <returns></returns>
public async Task<Question> 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<Question>(_questionRepository), QuestionTypeId = entity.QuestionTypeId }; }
/// <summary>
/// 更新
/// </summary>
/// <param name="sourceEntity"></param>
/// <param name="targetEntity"></param>
/// <returns></returns>
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;
}
/// <summary>
/// 删除
/// </summary>
/// <param name="QuestionId"></param>
/// <returns></returns>
/// <exception cref="UserFriendlyException"></exception>
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);
}
/// <summary>
/// 修改排序 置顶,置底
/// </summary>
/// <param name="id">需要修改的ID</param>
/// <param name="SortType">修改方式:1 置顶 2 置底</param>
/// <returns></returns>
public async Task UpdateSortTopOrBottomAsync(Guid QuestionId, int SortType) { var entity = await _questionRepository.GetAsync(f => f.QuestionId == QuestionId); await EntityHelper.UpdateSortTopOrBottomAsync(_questionRepository, entity, SortType); }
/// <summary>
/// 修改排序 拖拽
/// </summary>
/// <typeparam name="TEntity"></typeparam>
/// <param name="repository"></param>
/// <param name="input"></param>
/// <returns></returns>
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); }
/// <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 _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; } }}
|