irc-netcore-api/IRaCIS.Core.Application/Service/QC/QCQuestionService.cs

123 lines
4.7 KiB
C#

//--------------------------------------------------------------------
// 此代码由T4模板自动生成 byzhouhang 20210918
// 生成时间 2021-11-11 11:04:54
// 对此文件的更改可能会导致不正确的行为,并且如果重新生成代码,这些更改将会丢失。
//--------------------------------------------------------------------
using IRaCIS.Core.Infrastructure;
using Microsoft.AspNetCore.Mvc;
namespace IRaCIS.Core.Application.Contracts
{
/// <summary>
/// 系统QC 问题管理
/// </summary>
[ApiExplorerSettings(GroupName = "Image")]
public class QCQuestionConfigureService : BaseService, IQCQuestionService
{
private readonly IRepository<QCQuestion> _qcQuestionRepository;
public QCQuestionConfigureService(IRepository<QCQuestion> qcQuestionRepository)
{
_qcQuestionRepository = qcQuestionRepository;
}
/// <summary>
/// 父问题 下拉框选项 需要排除自己 、把自己设置为父亲 (互为父亲) 、是自己孙辈的(明明是自己子孙,却设置为自己父亲)
/// </summary>
/// <param name="trialQCQuestionFilterSelect"></param>
/// <returns></returns>
[HttpPost]
public async Task<List<TrialQCQuestionSelect>> GetQCQuestionSelectList(QCQuestionFilterSelect trialQCQuestionFilterSelect)
{
//设置父亲的时候,不允许设置为自己的孙子 这种会形成环
var initList = await _qcQuestionRepository
.WhereIf(trialQCQuestionFilterSelect.TypeArray.Count() > 0, t => trialQCQuestionFilterSelect.TypeArray.Contains(t.Type))
.OrderBy(t => t.ShowOrder).Select(x=>new TrialQCQuestionSelect() {
ShowOrder=x.ShowOrder,
Id=x.Id,
ParentId=x.ParentId,
QuestionName=x.QuestionName,
TypeValue=x.TypeValue,
}).ToListAsync();
//父亲的序号肯定要比自己小
if (trialQCQuestionFilterSelect.Id != null)
{
var selectItem = initList.FirstOrDefault(t => t.Id == trialQCQuestionFilterSelect.Id);
initList = initList.WhereIf(selectItem != null, t => t.Id != selectItem!.Id && t.ShowOrder < selectItem.ShowOrder).ToList();
}
var exceptList = GetChildId(trialQCQuestionFilterSelect.Id ?? Guid.Empty, initList);
return initList.Where(t => !exceptList.Contains(t.Id)).ToList();
}
private List<Guid> GetChildId(Guid parentId, List<TrialQCQuestionSelect> list)
{
var ids = new List<Guid>();
var childIds = list.Where(t => t.ParentId == parentId).Select(t => t.Id).ToList();
foreach (var childId in childIds)
{
ids.AddRange(childId);
var childs = GetChildId(childId, list);
ids.AddRange(childs);
}
return ids;
}
[HttpPost]
public async Task<List<QCQuestionConfigureView>> GetQCQuestionConfigureList(QCQuestionQuery queryQCQuestionConfigure)
{
var QCQuestionQueryable = _qcQuestionRepository
.WhereIf(!string.IsNullOrWhiteSpace(queryQCQuestionConfigure.QuestionName), t => t.QuestionName.Contains(queryQCQuestionConfigure.QuestionName))
.WhereIf(!string.IsNullOrWhiteSpace(queryQCQuestionConfigure.Type), t => t.Type.Contains(queryQCQuestionConfigure.Type))
.WhereIf(queryQCQuestionConfigure.IsEnable!=null, t => t.IsEnable==queryQCQuestionConfigure.IsEnable)
.ProjectTo<QCQuestionConfigureView>(_mapper.ConfigurationProvider);
return await QCQuestionQueryable.ToListAsync();
}
public async Task<IResponseOutput> AddOrUpdateQCQuestionConfigure(QCQuestionAddOrEdit addOrEditQCQuestionConfigure)
{
var entity = await _qcQuestionRepository.InsertOrUpdateAsync(addOrEditQCQuestionConfigure, true);
return ResponseOutput.Ok(entity.Id.ToString());
}
[HttpDelete("{qCQuestionConfigureId:guid}")]
public async Task<IResponseOutput> DeleteQCQuestionConfigure(Guid qCQuestionConfigureId)
{
if (await _qcQuestionRepository.AnyAsync(x => x.ParentId == qCQuestionConfigureId))
{
throw new BusinessValidationFailedException("当前任务存在子问题,删除失败");
}
var success = await _qcQuestionRepository.BatchDeleteNoTrackingAsync(t => t.Id == qCQuestionConfigureId);
return ResponseOutput.Result(success);
}
}
}