//--------------------------------------------------------------------
//     此代码由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;
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
        /// 
        private async Task> SetInspectionEnumValueDataList(SetInspectionEnumValueDto dto)
        {
            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
            }).ToListAsync();
            var listIdentification = auditDatas.Select(x => x.Identification).ToList();
            foreach (var item in auditDatas)
            {
                Dictionary jsonDict = JsonConvert.DeserializeObject>(item.JsonStr);
              
                item.JsonStr = jsonDict["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["Data"] = JsonConvert.DeserializeObject