//--------------------------------------------------------------------
//     此代码由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;
using System.Text.Json.Nodes;
using IRaCIS.Application.Contracts;
using IRaCIS.Core.Infrastructure.Extention;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Information;

namespace IRaCIS.Core.Application.Service
{

    /// <summary>
    /// FrontAuditConfigService
    /// </summary>	
    [ApiExplorerSettings(GroupName = "Reviewer")]
    public class FrontAuditConfigService : BaseService, IFrontAuditConfigService
    {

        private readonly IRepository<FrontAuditConfig> _frontAuditConfigRepository;
        private readonly IRepository<QCChallengeDialog> _qCChallengeDialogRepository;
        private readonly IRepository<DataInspection> _dataInspectionRepository;
        private readonly IRepository<QCChallenge> _qCChallengeRepository;
        private readonly IRepository<Dictionary> _dictionaryRepository;
        private readonly IRepository<Trial> _trialRepository;
        private readonly IRepository<CheckChallengeDialog> _checkChallengeDialogRepository;

        public FrontAuditConfigService(IRepository<FrontAuditConfig> frontAuditConfigRepository,
            IRepository<QCChallengeDialog> qCChallengeDialogRepository,
              IRepository<DataInspection> dataInspectionRepository,
            IRepository<QCChallenge> qCChallengeRepository,
             IRepository<Dictionary> dictionaryRepository,
              IRepository<Trial> trialRepository,
            IRepository<CheckChallengeDialog> checkChallengeDialogRepository
            )
        {
            _frontAuditConfigRepository = frontAuditConfigRepository;
            this._qCChallengeDialogRepository = qCChallengeDialogRepository;
            this._dataInspectionRepository = dataInspectionRepository;
            this._qCChallengeRepository = qCChallengeRepository;
            this._dictionaryRepository = dictionaryRepository;
            this._trialRepository = trialRepository;
            this._checkChallengeDialogRepository = checkChallengeDialogRepository;
        }

        /// <summary>
        /// 获取数据库所有表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<List<TableList>> GetDatabaseTables()
        {
            return await _frontAuditConfigRepository._dbContext.GetTableList().ToListAsync();
        }

        /// <summary>
        /// 获取查询对象
        /// </summary>
        /// <param name="inDto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<List<AccessToDialogueOutDto>> GetDialogList(AccessToDialogueInDto inDto)
        {
            List<AccessToDialogueOutDto> data = new List<AccessToDialogueOutDto>();

            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;
        }

        /// <summary>
        /// 获取表列名
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<List<TableList>> GetTableColumn(string tableName)
        {
            return await _frontAuditConfigRepository._dbContext.GetTableColumn(tableName).ToListAsync();
        }

        /// <summary>
        /// 复制配置项及其子项
        /// </summary>
        /// <param name="input">传入对象</param>
        /// <returns></returns>
        [HttpPost]
        public async Task CopyFrontAuditConfigItem(CopyFrontAuditConfigItemDto input)
        {
            var id = NewId.NextGuid();
            List<FrontAuditConfig> frontAudits = new List<FrontAuditConfig>();

            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();

        }

        /// <summary>
        /// 批量添加字段
        /// </summary>
        /// <param name="data">数据集</param>
        /// <returns></returns>
        public async Task BatchAddFrontAudit(BatchAddFrontAudit data)
        {
            var maxSort = await _frontAuditConfigRepository.Where(x => x.ParentId == data.ParentId).MaxAsync(x => x.Sort);

            List<FrontAuditConfig> fronts = new List<FrontAuditConfig>();
            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);
        }

        /// <summary>
        /// 翻译稽查数据
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<List<string>> SetInspectionEnumValue(SetInspectionEnumValueDto dto)
        {
            return await SetInspectionEnumValueDataList(dto, dto.AuditDataIds.FirstOrDefault());
        }


        public async Task<List<string>> 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.CreateTime <= currentInspection.CreateTime && x.Id != id).OrderByDescending(x => x.CreateTime).Select(t => t.Id)
                .FirstOrDefaultAsync();

            List<Guid> searchGuidList = new List<Guid>() { id };

            if (beforeId != Guid.Empty)
            {
                searchGuidList.Add(beforeId);
            }

            return await SetInspectionEnumValueDataList(new SetInspectionEnumValueDto() { TrialId = trialId, AuditDataIds = searchGuidList }, id);
        }




        /// <summary>
        /// 翻译稽查数据
        /// </summary>
        /// <param name="dto">传入Dto</param>
        /// <returns></returns>
        private async Task<List<string>> 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,
                ObjectRelationParentId3 = x.ObjectRelationParentId3,
                CreateTime = x.CreateTime,
                BatchId = x.BatchId,

            }).ToListAsync();




            var listIdentification = auditDatas.Select(x => x.Identification).Distinct().ToList();
            foreach (var item in auditDatas)
            {
                Dictionary<string, object> jsonDict = JsonConvert.DeserializeObject<Dictionary<string, object>>(item.JsonStr);

                if (!jsonDict.ContainsKey(nameof(InspectionJsonDetail.CommonData)))
                {
                    jsonDict.Add(nameof(InspectionJsonDetail.CommonData), new { });
                }

                //查询关联父层级数据
                if (item.Id == currentInspectionId)
                {
                    //把父层级的数据的 CommonData 数据合并(每一个层级把下面层级需要的数据放在CommonData 里面)  麻烦点是每个层级都需要记录一些信息，而且名称不能重复

                    var objectLsit = new List<object>();

                    var relationParentDataObjList = await GetRelationParentData(item.Id, item.ObjectRelationParentId, item.ObjectRelationParentId2, item.ObjectRelationParentId3, item.CreateTime, item.BatchId, objectLsit);



                    var currentCommonDataDic = JsonConvert.DeserializeObject<IDictionary<string, object>>(jsonDict[nameof(InspectionJsonDetail.CommonData)].ToJsonStr());

                    foreach (var relationParentDataObj in relationParentDataObjList)
                    {
                        var otherDic = JsonConvert.DeserializeObject<IDictionary<string, object>>(relationParentDataObj.ToJsonStr());

                        foreach (var valuePair in otherDic)
                        {
                            //关联层级的数据
                            if (valuePair.Key.Contains("_"))
                            {
                                var entityProperName = valuePair.Key.Split("_").ToList().Last();

                                if (!currentCommonDataDic.ContainsKey(entityProperName))
                                {
                                    currentCommonDataDic.Add(entityProperName, valuePair.Value);
                                }
                                else
                                {
                                    if (!currentCommonDataDic.ContainsKey(valuePair.Key))
                                    {
                                        currentCommonDataDic.Add(valuePair.Key, valuePair.Value);

                                    }
                                }
                            }
                            else
                            {
                                if (!currentCommonDataDic.ContainsKey(valuePair.Key))
                                {
                                    currentCommonDataDic.Add(valuePair.Key, valuePair.Value);
                                }
                            }
                        }
                    }

                    jsonDict[nameof(InspectionJsonDetail.CommonData)] = currentCommonDataDic;
                }


                #region Old
                //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<object>(item.JsonStr);
                #endregion

                #region New

                var str = jsonDict[nameof(InspectionJsonDetail.Data)].ToString();
                if (item.Identification == string.Empty || str == string.Empty)
                {
                    continue;
                }
                str = await GetInspectionEnumValue(listIdentification, str);
                str = await SetEnum(dto.TrialId, listIdentification, str);
                str = await SetDataInspectionDateType(listIdentification, str);

                jsonDict[nameof(InspectionJsonDetail.Data)] = JsonConvert.DeserializeObject<object>(str);


                var str2 = jsonDict[nameof(InspectionJsonDetail.CommonData)].ToJsonStr();
                if (item.Identification == string.Empty || str2 == string.Empty)
                {
                    continue;
                }
                str2 = await GetInspectionEnumValue(listIdentification, str2);
                str2 = await SetEnum(dto.TrialId, listIdentification, str2);
                str2 = await SetDataInspectionDateType(listIdentification, str2);

                jsonDict[nameof(InspectionJsonDetail.CommonData)] = JsonConvert.DeserializeObject<object>(str2);
                #endregion


                #region 后续移除   避免前端看到的不统一  因为采用了新的关联方式，之前数据在Data里面取   现在配置在CommonData 里取 
                //var dataDic = JsonConvert.DeserializeObject<IDictionary<string, object>>(str);
                //var commonDic = JsonConvert.DeserializeObject<IDictionary<string, object>>(str2);
                //foreach (var valuePair in dataDic)
                //{
                //    if (!commonDic.ContainsKey(valuePair.Key))
                //    {
                //        commonDic.Add(valuePair.Key, valuePair.Value);
                //    }
                //}

                //jsonDict[nameof(InspectionJsonDetail.CommonData)] = JsonConvert.DeserializeObject<object>(commonDic.ToJsonStr());
                #endregion


                item.JsonStr = JsonConvert.SerializeObject(jsonDict);
            }

            var resultJsonStrList = new List<string>();

            dto.AuditDataIds.ForEach(x =>
            {
                var auditData = auditDatas.FirstOrDefault(y => y.Id == x);

                resultJsonStrList.Add(auditData?.JsonStr);
            });

            if (resultJsonStrList.Count() < 2)
            {
                resultJsonStrList.Add(String.Empty);
            }
            return resultJsonStrList;
        }



        private async Task AddJsonObjectToDic(Guid id, Guid? objectRelationParentId, DateTime createTime, Guid batchId, List<object> objectLsit)
        {
            if (objectRelationParentId != null)
            {
                //父子层级的数据可能在同一个批次 进行更新  但是后插入的是父层级的数据  找父层级的稽查应该优先同一批次的
                var relationParentInspection = await _dataInspectionRepository.Where(t => t.GeneralId == objectRelationParentId && (t.CreateTime <= createTime || t.BatchId == batchId) && t.Id != id).OrderByDescending(x => x.CreateTime).Select(t => new { t.ObjectRelationParentId, t.CreateTime, t.JsonDetail, t.BatchId, t.ObjectRelationParentId2, t.ObjectRelationParentId3, t.EntityName, t.Id }).FirstOrDefaultAsync();


                if (relationParentInspection != null)
                {

                    Dictionary<string, object> jsonDic = JsonConvert.DeserializeObject<Dictionary<string, object>>(relationParentInspection.JsonDetail);


                    //避免对象信息记录 把  Data里面的信息也取过去 但是加上稽查对象的前缀
                    var dataDicObj = jsonDic[nameof(InspectionJsonDetail.Data)];

                    if (dataDicObj != null)
                    {
                        var entityName = relationParentInspection.EntityName;

                        IDictionary<string, object> newNamepDic = new Dictionary<string, object>();

                        var tempDic = JsonConvert.DeserializeObject<Dictionary<string, object>>(dataDicObj.ToJsonStr());

                        foreach (var valuePair in tempDic)
                        {
                            newNamepDic.Add(entityName + "_" + valuePair.Key, valuePair.Value);
                        }


                        objectLsit.Add(newNamepDic);
                    }



                    await AddJsonObjectToDic(relationParentInspection.Id, relationParentInspection.ObjectRelationParentId, relationParentInspection.CreateTime, relationParentInspection.BatchId, objectLsit);
                    await AddJsonObjectToDic(relationParentInspection.Id, relationParentInspection.ObjectRelationParentId2, relationParentInspection.CreateTime, relationParentInspection.BatchId, objectLsit);
                    await AddJsonObjectToDic(relationParentInspection.Id, relationParentInspection.ObjectRelationParentId3, relationParentInspection.CreateTime, relationParentInspection.BatchId, objectLsit);
                }

                else
                {
                    //用户的数据稽查没有  临时处理


                    var userObj = await _repository.Where<User>(t => t.Id == objectRelationParentId).Select(t => new { UserRealName = t.FullName, t.Phone, t.UserName, UserType = t.UserTypeRole.UserTypeShortName, t.EMail, t.OrganizationName }).FirstOrDefaultAsync();

                    if (userObj != null)
                    {
                        objectLsit.Add(userObj);

                    }
                }

            }
        }



        private async Task<List<object>> GetRelationParentData(Guid id, Guid? objectRelationParentId, Guid? objectRelationParentId2, Guid? objectRelationParentId3, DateTime createTime, Guid batchId, List<object> objectLsit)
        {
            await AddJsonObjectToDic(id, objectRelationParentId, createTime, batchId, objectLsit);
            await AddJsonObjectToDic(id, objectRelationParentId2, createTime, batchId, objectLsit);

            await AddJsonObjectToDic(id, objectRelationParentId3, createTime, batchId, objectLsit);

            return objectLsit;
        }






        /// <summary>
        /// 格式化日期和时间
        /// </summary>
        /// <param name="identificationList"></param>
        /// <param name="jsonStr"></param>
        /// <returns></returns>
        private async Task<string> SetDataInspectionDateType(List<string> identificationList, string jsonStr)
        {
            var list = await (from parent in _frontAuditConfigRepository.AsQueryable().Where(x => identificationList.Contains(x.Identification))
                              join child in _frontAuditConfigRepository.AsQueryable().Where(x => x.EnumType == "Date" && x.IsEnable) on parent.Id equals child.ParentId
                              select new DateDto()
                              {
                                  Code = child.Code,
                                  DateType = child.DateType,
                              }).ToListAsync();

            list = list.GroupBy(x => new { x.Code }, (key, lst) => new DateDto()
            {
                Code = key.Code,
                DateType = lst.Max(x => x.DateType),
            }).ToList();

            var jsonDataDic = JsonConvert.DeserializeObject<IDictionary<string, object>>(jsonStr);

            if (jsonDataDic == null)
            {
                return jsonStr;
            }

            foreach (var item in jsonDataDic.Keys)
            {
                var datefirst = list.FirstOrDefault(x => x.Code.ToLower() == item.ToLower());
                if (datefirst != null && !IsNullOrEmpty(jsonDataDic[item]))
                {
                    try
                    {
                        if (datefirst.DateType == FrontAuditDateType.Date.GetDescription())
                        {
                            jsonDataDic[item] = DateTime.Parse(jsonDataDic[item].ToString()).ToString("yyyy-MM-dd");
                        }

                        if (datefirst.DateType == FrontAuditDateType.DateTime.GetDescription())
                        {
                            jsonDataDic[item] = DateTime.Parse(jsonDataDic[item].ToString()).ToString("yyyy-MM-dd HH:mm:ss");
                        }
                    }
                    catch (Exception)
                    {
                        continue;
                    }
                }

            }

            return JsonConvert.SerializeObject(jsonDataDic);


        }

        /// <summary>
        /// 获取外键表数据
        /// </summary>
        /// <param name="identificationList"></param>
        /// <param name="jsonStr"></param>
        /// <returns></returns>
        ///// <param name="Table">表名称</param>
        ///// <param name="ForeignKeyValue">外键value</param>
        ///// <param name="ForeignKeyText">要查询的外键值</param>
        ///// <param name="value">传入的纸</param>
        private async Task<string> GetInspectionEnumValue(List<string> identificationList, string jsonStr)
        {
            var list = await (from u in _frontAuditConfigRepository.Where(x => identificationList.Contains(x.Identification))
                              join p in _frontAuditConfigRepository.Where(x => x.EnumType == "Foreign" && x.IsEnable) on u.Id equals p.ParentId
                              select new
                              {
                                  Key = p.Code,
                                  ForeignKeyValue = p.ForeignKeyValue,
                                  ForeignKeyText = p.ForeignKeyText,
                                  ForeignKeyTable = p.ForeignKeyTableName
                              }).ToListAsync();
            list = list.GroupBy(x => new { x.Key }, (key, lst) => new
            {
                Key = key.Key,
                ForeignKeyValue = lst.Max(x => x.ForeignKeyValue),
                ForeignKeyText = lst.Max(x => x.ForeignKeyText),
                ForeignKeyTable = lst.Max(x => x.ForeignKeyTable),

            }).ToList();

            var jsonDataValueDic = JsonConvert.DeserializeObject<IDictionary<string, object>>(jsonStr);
            foreach (var item in list)
            {
                if (!jsonDataValueDic.ContainsKey(item.Key))
                {
                    continue;
                }
                string Table = item.ForeignKeyTable;
                string ForeignKeyValue = item.ForeignKeyValue;
                string ForeignKeyText = item.ForeignKeyText;
                if (jsonDataValueDic[item.Key] != null)
                {
                    string value = jsonDataValueDic[item.Key].ToString();
                    string para = string.Empty;
                    string sql = string.Empty;
                    var JsonData = JsonConvert.DeserializeObject<IDictionary<string, object>>(JsonConvert.SerializeObject(new { item = value }));
                    if (JsonData["item"].GetType() == typeof(JArray))
                    {
                        foreach (var v in JsonData["item"] as JArray)
                        {
                            para += para == string.Empty ? $"'{v.ToString()}'" : $",'{v.ToString()}'";
                        }
                        sql = $"select {ForeignKeyText} Text from [{Table}] where {ForeignKeyValue} in (@para)";
                    }
                    else
                    {
                        para = $"{JsonData["item"].ToString()}";
                        sql = $"select {ForeignKeyText}  Text from [{Table}] where {ForeignKeyValue} = @para";
                    }
                    SqlParameter[] paravalue = new SqlParameter[] {
                   new SqlParameter("@para",para)
                                             };
                    jsonDataValueDic[item.Key] = string.Join(",", _frontAuditConfigRepository._dbContext.Database.SqlQuery<ForeignKey>(sql, paravalue).Select(x => x.Text).ToList());
                }
            }
            return JsonConvert.SerializeObject(jsonDataValueDic);
        }


        /// <summary>
        /// 获取枚举
        /// </summary>
        /// <param name="trialId">标识</param>
        /// <param name="identificationList">标识</param>
        /// <param name="jsonStr">Json对象</param>
        /// <returns></returns>
        private async Task<string> SetEnum(Guid trialId, List<string> identificationList, string jsonStr)
        {
            if (jsonStr == null || jsonStr == "null")
            {
                return null;
            }


            //DictionaryCode='' and EnumType='Dictionary'  是审核状态
            var list = await (from u in _frontAuditConfigRepository.Where(x => identificationList.Contains(x.Identification))
                              join p in _frontAuditConfigRepository.Where(x => (x.DictionaryCode != string.Empty && x.EnumType == "Dictionary") || (x.DataType == "Table") && x.IsEnable) on u.Id equals p.ParentId
                              select new
                              {
                                  //前端展示类型
                                  DataType = p.DataType,
                                  TableConfigJsonStr = p.TableConfigJsonStr,
                                  Key = p.Code,
                                  Code = p.DictionaryCode,
                                  Type = p.DictionaryType
                              }).ToListAsync();

            //两条不同的标识  但是里面配置有相同的翻译字典
            list = list.Distinct().ToList();

            // 添加单双审
            var trialtype = await _trialRepository.AsQueryable().Where(x => x.Id == trialId).Select(x => x.QCProcessEnum).FirstOrDefaultAsync();

            if (!list.Any(x => x.Key == "AuditState"))
            {
                list.Add(new
                {
                    DataType = string.Empty,
                    TableConfigJsonStr = string.Empty,
                    Key = "AuditState",
                    Code = trialtype == TrialQCProcess.SingleAudit ? "AuditStatePE" : "AuditStateRC",
                    Type = "Code",
                });
            }

            var jsonDataDic = JsonConvert.DeserializeObject<IDictionary<string, object>>(jsonStr);
            foreach (var item in list)
            {
                try
                {

                    if (!jsonDataDic.ContainsKey(item.Key) || jsonDataDic[item.Key] == null)
                    {
                        continue;
                    }
                    var value = jsonDataDic[item.Key];

                    //翻译的是数组
                    if (value.GetType() == typeof(JArray))
                    {
                        JArray arrays = (JArray)value;

                        //动态配置表翻译
                        if (item.DataType == "Table")
                        {
                            var tableConfigList = JsonConvert.DeserializeObject<List<TableConfig>>(item.TableConfigJsonStr) ?? new List<TableConfig>();


                            //处理静态翻译
                            var translateInfoList = tableConfigList.Where(t => t.IsNeedTransalate).Select(t => new { t.ColumnValue, t.TranslateDictionaryName }).Distinct().ToList();
                            var dictionaryNameList = translateInfoList.Select(t => t.TranslateDictionaryName).Distinct().ToList();



                            //处理动态翻译   会在数组中提供 一个属性  “DictionaryCode”  这个是默认约束，做稽查的时候记得注意，免得配置麻烦
                            var dynamicTranslateInfoList = tableConfigList.Where(t => t.IsDynamicTranslate && t.IsList).Select(t => new { t.ListName, t.ColumnValue }).Distinct().ToList();

                            var dynamicDictionaryNameList = new List<string>();

                            foreach (var dynamicTranslateInfo in dynamicTranslateInfoList)
                            {
                                var tempNameList = arrays[0][dynamicTranslateInfo.ListName].Select(t => t["DictionaryCode"].ToString()).Where(t => !string.IsNullOrEmpty(t)).ToList();

                                dynamicDictionaryNameList.AddRange(tempNameList);

                                dynamicDictionaryNameList = dynamicDictionaryNameList.Distinct().ToList();
                            }

                            var specialDynamicColumnValueList = new List<string>();
                            if (tableConfigList.Any(t => t.IsDynamicTranslate && t.IsList == false))
                            {
                                // 兼容之前数组的那种方式  实际配置只会配置一条
                                 specialDynamicColumnValueList = tableConfigList.Where(t => t.IsDynamicTranslate && t.IsList == false)
                                        .Select(t =>  t.ColumnValue ).Distinct().ToList();

                               var dicNames= arrays.Where(t=>!string.IsNullOrEmpty( t["DictionaryCode"].ToString()) ).Select(t => t["DictionaryCode"].ToString()).Distinct().ToList();

                                dictionaryNameList.AddRange(dicNames);
                            }
                         


                            var allDictionaryNameList = dictionaryNameList.Union(dynamicDictionaryNameList).Distinct();


                            var searchList = await _dictionaryRepository.Where(t => allDictionaryNameList.Contains(t.Parent.Code) && t.ParentId != null && t.IsEnable).ProjectTo<BasicDicSelect>(_mapper.ConfigurationProvider).ToListAsync();

                            //翻译的字典数据
                            var translateDataList = searchList.GroupBy(t => t.ParentCode).ToDictionary(g => g.Key, g => g.OrderBy(t => t.ShowOrder).ToList());

                            List<JObject> jsonList = new List<JObject>();
                            foreach (JToken arraysItem in arrays)
                            {
                                var jsonObject = JObject.Parse(arraysItem.ToString());

                                //处理静态翻译
                                foreach (var translateInfo in translateInfoList)
                                {
                                    //Json  解析后 true  变为了True
                                    jsonObject[translateInfo.ColumnValue] = translateDataList[translateInfo.TranslateDictionaryName].Where(t => t.Code.ToLower() == jsonObject[translateInfo.ColumnValue]?.ToString().ToLower()).Select(t => _userInfo.IsEn_Us?t.Value: t.ValueCN).FirstOrDefault();
                                }

                                foreach (var specialDynamicColumnValue in specialDynamicColumnValueList)
                                {
                                    jsonObject[specialDynamicColumnValue] =
                                        translateDataList[jsonObject["DictionaryCode"].ToString()].Where(t => t.Code.ToLower() == jsonObject[specialDynamicColumnValue]?.ToString().ToLower()).Select(t => _userInfo.IsEn_Us ? t.Value : t.ValueCN).FirstOrDefault();
                                }

                                //处理动态翻译

                                foreach (var dynamicTranslateInfo in dynamicTranslateInfoList)
                                {

                                    var innerArrays = (JArray)jsonObject[dynamicTranslateInfo.ListName];


                                    List<JObject> innberJsonList = new List<JObject>();

                                    foreach (var innerItem in innerArrays)
                                    {
                                        var innerObject = JObject.Parse(innerItem.ToString());


                                        var dicName = innerObject["DictionaryCode"]?.ToString();

                                        if (dicName != null && !string.IsNullOrEmpty(dicName))
                                        {
                                            innerObject[dynamicTranslateInfo.ColumnValue] = translateDataList[dicName].Where(t => t.Code.ToLower() == innerObject[dynamicTranslateInfo.ColumnValue].ToString().ToLower()).Select(t => _userInfo.IsEn_Us ? t.Value : t.ValueCN).FirstOrDefault();

                                        }
                                        innberJsonList.Add(innerObject);

                                    }

                                    jsonObject[dynamicTranslateInfo.ListName] = JToken.FromObject(innberJsonList);

                                }

                                jsonList.Add(jsonObject);

                            }

                            jsonDataDic[item.Key] = JToken.FromObject(jsonList);

                            continue;

                        }


                        if (item.Type.ToLower() == FrontAudit.Id.GetDescription().ToLower())
                        {
                            List<Guid> guids = new List<Guid>();
                            arrays.ForEach(x =>
                            {
                                guids.Add(Guid.Parse(x.ToString()));
                            });
                            jsonDataDic[item.Key] = string.Join(',', await _dictionaryRepository.Where(x => guids.Contains(x.Id)).Select(x => _userInfo.IsEn_Us ? x.Value : x.ValueCN).ToListAsync());
                        }
                        else if (item.Type.ToLower() == FrontAudit.ChildGroup.GetDescription().ToLower())
                        {
                            List<string> guids = new List<string>();
                            arrays.ForEach(x =>
                            {
                                guids.Add(x.ToString());
                            });
                            jsonDataDic[item.Key] = string.Join(',', await
                               _dictionaryRepository.Where(x => x.Code == item.Code).GroupJoin(
                                    _dictionaryRepository.Where(x => guids.Contains(x.ChildGroup)), a => a.Id, b => b.ParentId, (a, b) => new
                                    {
                                        parent = b
                                    }).SelectMany(a => a.parent, (m, n) => new
                                    {
                                        value = _userInfo.IsEn_Us ? n.Value : n.ValueCN 
                                    }).Select(x => x.value).ToListAsync()
                                    );
                        }

                        //稽查  后端查询记录出表格数据，但是表格数据需要翻译，显示给出了翻译字典名，翻译的配置
                        else if (item.Type.ToLower() == FrontAudit.DictionaryType.GetDescription().ToLower())
                        {

                            List<JObject> jsonList = new List<JObject>();

                            foreach (JToken arraysItem in arrays)
                            {
                                var jsonObject = JObject.Parse(arraysItem.ToString());
                                try
                                {
                                    if (jsonObject["DictionaryCode"] != null && jsonObject["DictionaryCode"].ToString() != string.Empty)
                                    {

                                        jsonObject[item.Code] = await _dictionaryRepository.Where(x => x.Code == jsonObject["DictionaryCode"].ToString()).Join(_dictionaryRepository.Where(x => x.Code == jsonObject[item.Code].ToString()), a => a.Id, b => b.ParentId, (a, b) => new
                                        {
                                            value = _userInfo.IsEn_Us ? b.Value : b.ValueCN 
                                        }).Select(x => x.value).FirstOrDefaultAsync();

                                    }
                                    jsonList.Add(jsonObject);


                                }
                                catch (Exception)
                                {
                                    jsonList.Add(jsonObject);

                                }
                            }


                            jsonDataDic[item.Key] = JToken.FromObject(jsonList);
                        }
                        else
                        {
                            List<string> guids = new List<string>();
                            arrays.ForEach(x =>
                            {
                                guids.Add(x.ToString());
                            });
                            jsonDataDic[item.Key] = string.Join(',', await
                                _dictionaryRepository.Where(x => x.Code == item.Code).GroupJoin(
                                     _dictionaryRepository.Where(x => guids.Contains(x.Code)), a => a.Id, b => b.ParentId, (a, b) => new
                                     {
                                         parent = b
                                     }).SelectMany(a => a.parent, (m, n) => new
                                     {
                                         value = n.ValueCN
                                     }).Select(x => x.value).ToListAsync()
                                    );
                        }
                    }

                    //翻译的是单个字段
                    else
                    {
                        //通过字典项的Guid 翻译
                        if (item.Type.ToLower() == FrontAudit.Id.GetDescription().ToLower())
                        {
                            Guid guid = Guid.Parse(value.ToString());
                            jsonDataDic[item.Key] = await _dictionaryRepository.Where(x => x.Id == guid).Select(x => _userInfo.IsEn_Us ? x.Value : x.ValueCN).FirstOrDefaultAsync();
                        }
                        else if (item.Type.ToLower() == FrontAudit.ChildGroup.GetDescription().ToLower())
                        {
                            jsonDataDic[item.Key] = await _dictionaryRepository.Where(x => x.Code == item.Code).Join(_dictionaryRepository.Where(x => x.ChildGroup == value.ToString()), a => a.Id, b => b.ParentId, (a, b) => new
                            {
                                value = _userInfo.IsEn_Us ? b.Value : b.ValueCN
                            }).Select(x => x.value).FirstOrDefaultAsync();
                        }
                        //通过字典项的code 翻译  枚举或者 bool  
                        else
                        {
                            jsonDataDic[item.Key] = await _dictionaryRepository.Where(x => x.Code == item.Code).Join(_dictionaryRepository.Where(x => x.Code == value.ToString()), a => a.Id, b => b.ParentId, (a, b) => new
                            {
                                value = _userInfo.IsEn_Us ? b.Value : b.ValueCN
                            }).Select(x => x.value).FirstOrDefaultAsync();
                        }
                    }
                }
                catch (Exception)
                {
                }
            }
            return JsonConvert.SerializeObject(jsonDataDic);
        }




        /// <summary>
        /// 格式化日期和时间
        /// </summary>
        /// <param name="Data">稽查数据</param>
        /// <returns></returns>
        private async Task<DataInspection> SetDataInspectionDateType(DataInspection Data)
        {
            var list = await (from parent in _frontAuditConfigRepository.AsQueryable().Where(x => x.Identification == Data.Identification)
                              join child in _frontAuditConfigRepository.AsQueryable().Where(x => x.EnumType == "Date") on parent.Id equals child.ParentId
                              select new DateDto()
                              {
                                  Code = child.Code,
                                  DateType = child.DateType,
                              }).ToListAsync();

            var JsonData = JsonConvert.DeserializeObject<IDictionary<string, object>>(Data.JsonDetail);

            foreach (var item in JsonData.Keys)
            {
                var datefirst = list.FirstOrDefault(x => x.Code.ToLower() == item.ToLower());
                if (datefirst != null && !IsNullOrEmpty(JsonData[item]))
                {
                    try
                    {
                        if (datefirst.DateType == FrontAuditDateType.Date.GetDescription())
                        {
                            JsonData[item] = DateTime.Parse(JsonData[item].ToString()).ToString("yyyy-MM-dd");
                        }

                        if (datefirst.DateType == FrontAuditDateType.DateTime.GetDescription())
                        {
                            JsonData[item] = DateTime.Parse(JsonData[item].ToString()).ToString("yyyy-MM-dd HH:mm:ss");
                        }
                    }
                    catch (Exception)
                    {
                        continue;
                    }
                }

            }
            Data.JsonDetail = JsonConvert.SerializeObject(JsonData);

            return Data;
        }

        /// <summary>
        /// IsNullOrEmpty
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        private bool IsNullOrEmpty(object value)
        {
            if (value == null || value.ToString() == string.Empty)
            {
                return true;
            }
            else
            {
                return false;
            }
        }



        /// <summary>
        /// 获取子数据
        /// </summary>
        /// <param name="frontAuditConfigId"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<List<FrontAuditConfigDTO>> GetAuditConfigChildList(Guid frontAuditConfigId)
        {
            //var list = await (from data in _repository.GetQueryable<FrontAuditConfig>().Where(x => x.Id == frontAuditConfigId)
            //                  join childrenType in _repository.GetQueryable<FrontAuditConfig>() on data.Id equals childrenType.ParentId
            //                  select childrenType).OrderBy(x => x.Sort).ToListAsync();
            //return list;

            var list = await _frontAuditConfigRepository.Where(t => t.ParentId == frontAuditConfigId).OrderBy(x => x.Sort).ProjectTo<FrontAuditConfigDTO>(_mapper.ConfigurationProvider).ToListAsync();

            foreach (var item in list)
            {
                item.TableConfigList = JsonConvert.DeserializeObject<List<TableConfig>>(item.TableConfigJsonStr) ?? new List<TableConfig>();
                item.UrlConfig = JsonConvert.DeserializeObject<UrlConfig>(item.UrlConfigJsonStr) ?? new UrlConfig();
            }

            return list;
        }

        /// <summary>
        /// 完全复制其他子项到当前项
        /// </summary>
        /// <param name="fully"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IResponseOutput> FullyReplicated(FullyReplicated fully)
        {
            await _frontAuditConfigRepository.DeleteFromQueryAsync(x => x.ParentId == fully.ToItemId);
            var list = await _frontAuditConfigRepository.Where(x => x.ParentId == fully.FromItemId).ToListAsync();
            list.ForEach(x =>
            {
                x.Id = NewId.NextGuid();
                x.ParentId = fully.ToItemId;
            });
            await _frontAuditConfigRepository.AddRangeAsync(list);
            await _frontAuditConfigRepository.SaveChangesAsync();
            return ResponseOutput.Ok();
        }


        /// <summary>
        /// Cope子项数据
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IResponseOutput> CopyOtherToThisItem(CopyOtherToThisItem item)
        {

            var lists = _frontAuditConfigRepository.Where(x => x.ParentId == item.AddItemGuid).ToList();

            var additem = await _frontAuditConfigRepository.FirstOrDefaultAsync(x => x.Id == item.AddItemGuid);

            var alllist = _frontAuditConfigRepository.Where(x => item.DataSourceGuids.Contains(x.ParentId)).ToList().GroupBy(x => new { x.ValueCN }, (key, lst) => new FrontAuditConfig
            {
                Sort = lst.Select(x => x.Sort).FirstOrDefault(),
                TableConfigJsonStr = lst.Select(x => x.TableConfigJsonStr).FirstOrDefault(),
                UrlConfigJsonStr = lst.Select(x => x.UrlConfigJsonStr).FirstOrDefault(),
                IsShowByTrialConfig = lst.Select(x => x.IsShowByTrialConfig).FirstOrDefault(),
                TrialConfigRelyFieldName = lst.Select(x => x.TrialConfigRelyFieldName).FirstOrDefault(),
                Code = lst.Max(x => x.Code),
                ConfigType = lst.Select(x => x.ConfigType).FirstOrDefault(),
                CreateTime = DateTime.Now,
                Description = lst.Select(x => x.Description).FirstOrDefault(),
                //EnumList = lst.Select(x => x.EnumList).FirstOrDefault(),
                //IsConfig = lst.Select(x => x.IsConfig).FirstOrDefault(),
                IsShowParent = lst.Select(x => x.IsShowParent).FirstOrDefault(),
                ParentId = item.AddItemGuid,
                CreateUserId = _userInfo.Id,
                IsEnable = lst.Select(x => x.IsEnable).FirstOrDefault(),
                DictionaryKey = lst.Select(x => x.DictionaryKey).FirstOrDefault(),
                EnumType = lst.Select(x => x.EnumType).FirstOrDefault(),
                UpdateTime = DateTime.Now,
                ValueCN = lst.Select(x => x.ValueCN).FirstOrDefault(),
                Value = lst.Max(x => x.Value),
                UpdateUserId = _userInfo.Id,
                ChildrenTypeId = additem?.ChildrenTypeId,
                ModuleTypeId = additem?.ModuleTypeId,
                ObjectTypeId = additem?.ObjectTypeId,
                OptTypeId = additem?.OptTypeId,
                DictionaryCode = lst.Max(x => x.DictionaryCode),
                DictionaryType = lst.Max(x => x.DictionaryType),
                DateType = lst.Select(x => x.DateType).FirstOrDefault(),
                ForeignKeyValue = lst.Select(x => x.ForeignKeyValue).FirstOrDefault(),
                ForeignKeyText = lst.Select(x => x.ForeignKeyText).FirstOrDefault(),
                ForeignKeyTableName = lst.Select(x => x.ForeignKeyTableName).FirstOrDefault(),
                DataType = lst.Select(x => x.DataType).FirstOrDefault(),
                Id = NewId.NextGuid()//新id,
            }).ToList();

            // 获取已存在的所有名称
            var names = lists.Select(x => x.ValueCN).ToList();

            // 获取不存在的数据
            var list = alllist.Where(x => !names.Contains(x.ValueCN)).ToList();

            // 获取要添加的name
            var addvaluecns = list.Select(x => x.ValueCN);

            // 获取要修改的数据
            var neewupdate = lists.Where(x => !addvaluecns.Contains(x.ValueCN));

            neewupdate.ForEach(x =>
            {
                var item = alllist.FirstOrDefault(y => y.ValueCN == x.ValueCN);
                if (item != null)
                {
                    x.Code = item.Code;
                    x.Value = !item.Code.IsNullOrEmpty() ? item.Value : x.Value;
                    x.DictionaryType = !item.DictionaryType.IsNullOrEmpty() ? item.DictionaryType : x.DictionaryType;
                    x.DictionaryCode = !item.DictionaryCode.IsNullOrEmpty() ? item.DictionaryCode : x.DictionaryCode;
                    x.DataType = !item.DataType.IsNullOrEmpty() ? item.DataType : x.DataType;
                    x.DateType = !item.DateType.IsNullOrEmpty() ? item.DateType : x.DateType;
                    x.DictionaryKey = !item.DictionaryKey.IsNullOrEmpty() ? item.DictionaryKey : x.DictionaryKey;
                    x.IsShowParent = /*!item.IsShowParent == null ?*/ item.IsShowParent /*: x.IsShowParent*/;
                    x.ForeignKeyTableName = !item.ForeignKeyTableName.IsNullOrEmpty() ? item.ForeignKeyTableName : x.ForeignKeyTableName;
                    x.ForeignKeyText = !item.ForeignKeyText.IsNullOrEmpty() ? item.ForeignKeyText : x.ForeignKeyText;
                    x.ForeignKeyValue = !item.ForeignKeyValue.IsNullOrEmpty() ? item.ForeignKeyValue : x.ForeignKeyValue;
                    x.EnumType = !item.EnumType.IsNullOrEmpty() ? item.EnumType : x.EnumType;
                }

            });


            await _repository.UpdateRange(neewupdate);
            await _repository.AddRangeAsync(list);
            await _repository.SaveChangesAsync();

            return ResponseOutput.Ok();
        }


        /// <summary>
        /// 获取Description
        /// </summary>
        /// <param name="moduleTypeId"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<List<string>> GetModuleTypeDescriptionList(Guid moduleTypeId)
        {
            var result = await _frontAuditConfigRepository.Where(x => x.ModuleTypeId == moduleTypeId && x.ObjectTypeId != null && x.OptTypeId != null && x.Description.Length > 0).Select(x => new { x.Description, x.DescriptionCN, x.Sort }).OrderBy(t => t.Sort).Select(t =>  _userInfo.IsEn_Us? t.Description:t.DescriptionCN).Distinct().ToListAsync();
            return result;
        }

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="iq"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<List<FrontAuditConfigView>> GetFrontAuditConfigList(FrontAuditConfigQuery iq)
        {
            var query = from data in _repository.GetQueryable<FrontAuditConfig>()
                        join childrenType in _repository.GetQueryable<Dictionary>() on data.ChildrenTypeId equals childrenType.Id into childrenTypetemp
                        from leftchildrenType in childrenTypetemp.DefaultIfEmpty()
                        join ModuleType in _repository.GetQueryable<Dictionary>() on data.ModuleTypeId equals ModuleType.Id into ModuleTypetemp
                        from leftModuleType in ModuleTypetemp.DefaultIfEmpty()
                        join OptTypeId in _repository.GetQueryable<Dictionary>() on data.OptTypeId equals OptTypeId.Id into OptTypeIdtemp
                        from leftOptTypeId in OptTypeIdtemp.DefaultIfEmpty()
                        join ObjectTypeId in _repository.GetQueryable<Dictionary>() on data.ObjectTypeId equals ObjectTypeId.Id into ObjectTypeIdtemp
                        from leftObjectTypeIdtemp in ObjectTypeIdtemp.DefaultIfEmpty()
                        select new FrontAuditConfigView()
                        {
                            IsShowParent = data.IsShowParent,
                            ChildrenTypeId = data.ChildrenTypeId,
                            Code = data.Code,
                            ConfigType = data.ConfigType,
                            CreateTime = data.CreateTime,
                            CreateUserId = data.CreateUserId,
                            Description = data.Description,
                            DescriptionCN=data.DescriptionCN,
                            IsConfig = data.IsConfig,
                            IsEnable = data.IsEnable,
                            ModuleTypeId = data.ModuleTypeId,
                            Id = data.Id,
                            ParentId = data.ParentId,
                            UpdateTime = data.UpdateTime,
                            Value = data.Value,
                            ChildrenTypeValueCN = leftchildrenType.ValueCN,
                            ModuleTypeValue = leftModuleType.Value,
                            ModuleTypeValueCN = leftModuleType.ValueCN,
                            OptTypeId = data.OptTypeId,
                            OptTypeValue = leftOptTypeId.Value,
                            OptTypeValueCN = leftOptTypeId.ValueCN,
                            UpdateUserId = data.UpdateUserId,
                            Sort = data.Sort,
                            ValueCN = data.ValueCN,
                            ChildrenTypeValue = leftchildrenType.Value,

                            DictionaryKey = data.DictionaryKey,
                            EnumType = data.EnumType,
                            ObjectTypeId = data.ObjectTypeId,
                            ObjectTypeValue = leftObjectTypeIdtemp.Value,
                            ObjectTypeValueCN = leftObjectTypeIdtemp.ValueCN,
                            IsShowByTrialConfig = data.IsShowByTrialConfig,
                            TrialConfigRelyFieldName = data.TrialConfigRelyFieldName,

                            Identification = data.Identification,
                            IsHaveReason = data.IsHaveReason,
                            IsHaveSign = data.IsHaveSign,
                            IsFinish = data.IsFinish,
                            IsJoinPlan = data.IsJoinPlan,
                            DataType = data.DataType,
                            ChildDataLabel = data.ChildDataLabel,
                            ChildDataValue = data.ChildDataValue,
                            IsSpecialType = data.IsSpecialType,
                            DateType = data.DataType,
                            DictionaryCode = data.DictionaryCode,
                            DictionaryType = data.DictionaryType,
                            InterfaceName = data.InterfaceName,

                            UrlConfigJsonStr = data.UrlConfigJsonStr,
                            TableConfigJsonStr = data.TableConfigJsonStr,
                        };

            query = query
                .WhereIf(!iq.Value.IsNullOrEmpty(), x => x.Value == iq.Value)
                .WhereIf(!iq.ValueCN.IsNullOrEmpty(), x => x.ValueCN == iq.ValueCN)
                .WhereIf(!iq.Description.IsNullOrEmpty(), x => x.Description == iq.Description)
                .WhereIf(iq.OptTypeId != null, x => x.OptTypeId == iq.OptTypeId)
                .WhereIf(!iq.Code.IsNullOrEmpty(), x => x.Code == iq.Code)
                .WhereIf(iq.ChildrenTypeId != null, x => x.ChildrenTypeId == iq.ChildrenTypeId)
                .WhereIf(iq.ModuleTypeId != null, x => x.ModuleTypeId == iq.ModuleTypeId)
                .WhereIf(iq.ObjectTypeId != null, x => x.ObjectTypeId == iq.ObjectTypeId)
                .WhereIf(!iq.ConfigType.IsNullOrEmpty(), x => x.ConfigType == iq.ConfigType);

            return await query.OrderBy(x => x.Sort).ToListAsync();
        }


        /// <summary>
        /// 修改排序
        /// </summary>
        /// <param name="sortDataList"></param>
        /// <returns></returns>
        public async Task<IResponseOutput> ChangeFrontAuditSort(List<FrontAuditSort> sortDataList)
        {
            foreach (var item in sortDataList)
            {
                await _frontAuditConfigRepository.BatchUpdateNoTrackingAsync(x => x.Id == item.Id, x => new FrontAuditConfig
                {
                    Sort = item.Sort
                });
            }
            await _frontAuditConfigRepository.SaveChangesAsync();
            return ResponseOutput.Ok();
        }

        /// <summary>
        /// 新增或者修改
        /// </summary>
        /// <param name="addOrEditFrontAuditConfig"></param>
        /// <returns></returns>
        public async Task<IResponseOutput> AddOrUpdateFrontAuditConfig(FrontAuditConfigAddOrEdit addOrEditFrontAuditConfig)
        {

            if (await _frontAuditConfigRepository.AnyAsync(x => x.Identification != string.Empty && x.Identification == addOrEditFrontAuditConfig.Identification && x.Id != addOrEditFrontAuditConfig.Id && x.ConfigType == "M" && addOrEditFrontAuditConfig.ConfigType == "M"))
            {
                //---标识重复
                return ResponseOutput.NotOk(_localizer["FrontAudit_IdDup"]);
            }

            if (await _frontAuditConfigRepository.AnyAsync(x => x.Description == addOrEditFrontAuditConfig.Description && x.Id != addOrEditFrontAuditConfig.Id && x.ConfigType == "M" && addOrEditFrontAuditConfig.ConfigType == "M"))
            {
                //---名称重复
                return ResponseOutput.NotOk(_localizer["FrontAudit_NameDup"]);
            }

            if (addOrEditFrontAuditConfig.ConfigType == "C")
            {
                addOrEditFrontAuditConfig.Description = "";
            }

            addOrEditFrontAuditConfig.TableConfigJsonStr = JsonConvert.SerializeObject(addOrEditFrontAuditConfig.TableConfigList);
            addOrEditFrontAuditConfig.UrlConfigJsonStr = JsonConvert.SerializeObject(addOrEditFrontAuditConfig.UrlConfig);

            var entity = await _frontAuditConfigRepository.InsertOrUpdateAsync(addOrEditFrontAuditConfig, true);

            return ResponseOutput.Ok(entity.Id.ToString());

        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="frontAuditConfigId"></param>
        /// <returns></returns>
        [HttpDelete("{frontAuditConfigId:guid}")]
        public async Task<IResponseOutput> DeleteFrontAuditConfig(Guid frontAuditConfigId)
        {
            if (await _frontAuditConfigRepository.AnyAsync(x => x.ParentId == frontAuditConfigId))
            {
                //---存在子类 不能删除
                return ResponseOutput.NotOk(_localizer["FrontAudit_CannotDelSub"]);
            }
            var success = await _repository.BatchDeleteAsync<FrontAuditConfig>(t => t.Id == frontAuditConfigId);
            return ResponseOutput.Result(success);
        }


    }
}
