//--------------------------------------------------------------------
// 此代码由T4模板自动生成 byzhouhang 20210918
// 生成时间 2022-03-28 16:46:23
// 对此文件的更改可能会导致不正确的行为,并且如果重新生成代码,这些更改将会丢失。
//--------------------------------------------------------------------
using Microsoft.AspNetCore.Mvc;
using IRaCIS.Core.Application.Interfaces;
using IRaCIS.Core.Application.ViewModel;
using Castle.Core.Internal;
using MassTransit;
using IRaCIS.Core.Infra.EFCore.Common.Dto;
using Microsoft.Data.SqlClient;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using IRaCIS.Core.Domain.Share;
using IRaCIS.Core.Domain.Share.Management;
namespace IRaCIS.Core.Application.Service
{
///
/// FrontAuditConfigService
///
[ApiExplorerSettings(GroupName = "Reviewer")]
public class FrontAuditConfigService : BaseService, IFrontAuditConfigService
{
private readonly IRepository _frontAuditConfigRepository;
private readonly IRepository _qCChallengeDialogRepository;
private readonly IRepository _dataInspectionRepository;
private readonly IRepository _qCChallengeRepository;
private readonly IRepository _dictionaryRepository;
private readonly IRepository _trialRepository;
private readonly IRepository _checkChallengeDialogRepository;
public FrontAuditConfigService(IRepository frontAuditConfigRepository,
IRepository qCChallengeDialogRepository,
IRepository dataInspectionRepository,
IRepository qCChallengeRepository,
IRepository dictionaryRepository,
IRepository trialRepository,
IRepository checkChallengeDialogRepository
)
{
_frontAuditConfigRepository = frontAuditConfigRepository;
this._qCChallengeDialogRepository = qCChallengeDialogRepository;
this._dataInspectionRepository = dataInspectionRepository;
this._qCChallengeRepository = qCChallengeRepository;
this._dictionaryRepository = dictionaryRepository;
this._trialRepository = trialRepository;
this._checkChallengeDialogRepository = checkChallengeDialogRepository;
}
///
/// 获取数据库所有表
///
///
[HttpPost]
public async Task> GetDatabaseTables()
{
return await _frontAuditConfigRepository._dbContext.GetTableList().ToListAsync();
}
///
/// 获取查询对象
///
///
///
[HttpPost]
public async Task> GetDialogList(AccessToDialogueInDto inDto)
{
List data = new List();
switch (inDto.Type)
{
case AccessToDialogueEnum.Question:
AccessToDialogueOutDto title = (await _qCChallengeRepository.Where(x => x.Id == inDto.Id).Include(x => x.CreateUser).Select(x => new AccessToDialogueOutDto()
{
CreateTime = x.CreateTime,
CreateUserName = x.CreateUser.UserName,
TalkContent = x.Content,
IsTitle = true,
}).FirstOrDefaultAsync()) ?? new AccessToDialogueOutDto();
data = await _qCChallengeDialogRepository.Where(x => x.QCChallengeId == inDto.Id && x.CreateTime <= inDto.Createtime).Include(x => x.CreateUser).Select(
x => new AccessToDialogueOutDto()
{
CreateTime = x.CreateTime,
CreateUserName = x.CreateUser.UserName,
TalkContent = x.TalkContent
}
).OrderBy(x => x.CreateTime).ToListAsync();
data.Insert(0, title);
break;
case AccessToDialogueEnum.Consistency:
data = await _checkChallengeDialogRepository.Where(x => x.SubjectVisitId == inDto.Id && x.CreateTime <= inDto.Createtime).Include(x => x.CreateUser).Select(
x => new AccessToDialogueOutDto()
{
CreateTime = x.CreateTime,
CreateUserName = x.CreateUser.UserName,
TalkContent = x.TalkContent
}
).OrderBy(x => x.CreateTime).ToListAsync();
break;
}
return data;
}
///
/// 获取表列名
///
///
[HttpPost]
public async Task> GetTableColumn(string tableName)
{
return await _frontAuditConfigRepository._dbContext.GetTableColumn(tableName).ToListAsync();
}
///
/// 复制配置项及其子项
///
/// 传入对象
///
[HttpPost]
public async Task CopyFrontAuditConfigItem(CopyFrontAuditConfigItemDto input)
{
var id = NewId.NextGuid();
List frontAudits = new List();
var frontAuditConfig = (await _frontAuditConfigRepository.FirstOrDefaultAsync(x => x.Id == input.ChildId)).Clone();
var fronts = await _frontAuditConfigRepository.Where(x => x.ParentId == frontAuditConfig.Id).ToListAsync();
fronts.ForEach(x =>
{
x.Id = NewId.NextGuid();
x.ParentId = id;
});
frontAuditConfig.ParentId = input.ParentId;
frontAuditConfig.Id = id;
frontAudits.Add(frontAuditConfig);
frontAudits.AddRange(fronts);
await _frontAuditConfigRepository.AddRangeAsync(frontAudits);
await _frontAuditConfigRepository.SaveChangesAsync();
}
///
/// 批量添加字段
///
/// 数据集
///
public async Task BatchAddFrontAudit(BatchAddFrontAudit data)
{
var maxSort = await _frontAuditConfigRepository.Where(x => x.ParentId == data.ParentId).MaxAsync(x => x.Sort);
List fronts = new List();
foreach (var item in data.Columns)
{
maxSort++;
fronts.Add(new FrontAuditConfig()
{
Sort = maxSort,
Code = item.Name,
ValueCN = item.Remake,
IsEnable = true,
ParentId = data.ParentId
});
}
await _frontAuditConfigRepository.AddRangeAsync(fronts);
}
///
/// 翻译稽查数据
///
///
///
[HttpPost]
public async Task> SetInspectionEnumValue(SetInspectionEnumValueDto dto)
{
return await SetInspectionEnumValueDataList(dto, dto.AuditDataIds.FirstOrDefault());
}
public async Task> GetInspectionJsonDataList(Guid trialId, Guid id)
{
//找到上一条Id
var currentInspection = await _dataInspectionRepository.Where(t => t.Id == id).Select(t => new { t.GeneralId, t.ObjectRelationParentId, t.CreateTime }).FirstOrDefaultAsync();
var beforeId = await _dataInspectionRepository.Where(x => x.GeneralId == currentInspection.GeneralId && x.ObjectRelationParentId == currentInspection.ObjectRelationParentId && x.CreateTime <= currentInspection.CreateTime).OrderByDescending(x => x.CreateTime).Select(t => t.Id)
.FirstOrDefaultAsync();
List searchGuidList = new List() { id };
if (beforeId != Guid.Empty)
{
searchGuidList.Add(beforeId);
}
return await SetInspectionEnumValueDataList(new SetInspectionEnumValueDto() { TrialId = trialId, AuditDataIds = searchGuidList }, id);
}
///
/// 翻译稽查数据
///
/// 传入Dto
///
private async Task> SetInspectionEnumValueDataList(SetInspectionEnumValueDto dto, Guid currentInspectionId)
{
var auditDatas = await _dataInspectionRepository.AsQueryable().Where(x => dto.AuditDataIds.Contains(x.Id)).Select(x => new SetInspectionEnumDataDto()
{
Id = x.Id,
JsonStr = x.JsonDetail,
Identification = x.Identification,
ObjectRelationParentId = x.ObjectRelationParentId,
ObjectRelationParentId2 = x.ObjectRelationParentId2,
CreateTime = x.CreateTime,
BatchId = x.BatchId,
}).ToListAsync();
var listIdentification = auditDatas.Select(x => x.Identification).ToList();
foreach (var item in auditDatas)
{
Dictionary jsonDict = JsonConvert.DeserializeObject>(item.JsonStr);
//查询关联父层级数据
if (item.Id == currentInspectionId)
{
//把父层级的数据的 CommonData 数据合并(每一个层级把下面层级需要的数据放在CommonData 里面) 麻烦点是每个层级都需要记录一些信息,而且名称不能重复
var commonDataObjList = await GetRelationParentData(item.ObjectRelationParentId, item.ObjectRelationParentId2, item.CreateTime, item.BatchId);
var currentDic = JsonConvert.DeserializeObject>(jsonDict[nameof(InspectionJsonDetail.CommonData)].ToJsonStr());
foreach (var commonDataObj in commonDataObjList)
{
var otherDic = JsonConvert.DeserializeObject>(commonDataObj.ToJsonStr());
foreach (var valuePair in otherDic)
{
//关联层级的数据
if (valuePair.Key.Contains("_"))
{
var entityProperName = valuePair.Key.Split("_").ToList().Last();
if (!currentDic.ContainsKey(entityProperName))
{
currentDic.Add(entityProperName, valuePair.Value);
}
else
{
if (!currentDic.ContainsKey(valuePair.Key))
{
currentDic.Add(valuePair.Key, valuePair.Value);
}
}
}
else
{
if (!currentDic.ContainsKey(valuePair.Key))
{
currentDic.Add(valuePair.Key, valuePair.Value);
}
}
}
}
jsonDict[nameof(InspectionJsonDetail.CommonData)] = currentDic;
}
item.JsonStr = jsonDict[nameof(InspectionJsonDetail.Data)].ToString();
if (item.Identification == string.Empty || item.JsonStr == string.Empty)
{
continue;
}
item.JsonStr = await GetInspectionEnumValue(listIdentification, item.JsonStr);
item.JsonStr = await SetEnum(dto.TrialId, listIdentification, item.JsonStr);
item.JsonStr = await SetDataInspectionDateType(listIdentification, item.JsonStr);
jsonDict[nameof(InspectionJsonDetail.Data)] = JsonConvert.DeserializeObject