using APT.Infrastructure.Core;
using APT.BaseData.Domain.ApiModel;
using APT.BaseData.Domain.Entities.FM;
using APT.BaseData.Domain.IServices.FM;
using APT.Utility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Collections.Immutable;
using APT.BaseData.Domain.Entities;
using APT.BaseData.Domain.Enums;
using APT.Infrastructure.Api;
using APT.MS.Domain.Entities.PF;
using APT.BaseData.Domain.Dtos;
using Castle.Core.Internal;

namespace APT.BaseData.Services.Services.FM
{
    public partial class FMSyncLogDetailService : CommonService, IFMSyncLogDetailService
    {
        public FMSyncLogDetailService(IRepository repository)
            : base(repository)
        {
        }

        /// <summary>
        /// 获取日志单
        /// </summary>
        /// <returns></returns>
        public T_FM_SYNC_LOG GetSyncLog(IDictionary<object, object?> Items, Guid? ORG_ID, string API_NAME, string SYNC_PARAM, SyncLogType? SyncLogType, int DATA_COUNT = 0)
        {
            Guid SNC_LOG_ID = (Guid)Items["syncID"];
            T_FM_SYNC_LOG model = new T_FM_SYNC_LOG();
            model.ID = SNC_LOG_ID;
            model.START_TIME = DateTime.Now;
            model.LOGTYPE = SyncLogType;
            model.API_NAME = API_NAME;
            model.DATA_COUNT = DATA_COUNT;
            model.IS_DELETED = false;
            model.ORG_ID = ORG_ID;
            model.SYNC_PARAM = SYNC_PARAM;
            model.CREATE_TIME = DateTime.Now;
            model.MODIFY_TIME = DateTime.Now;
            return model;
        }

        /// <summary>
        /// 获取日志明细
        /// </summary>
        /// <returns></returns>
        public T_FM_SYNC_LOG_DETAIL GetSyncLogDetail(IDictionary<object, object?> Items, string TRACEIDENTIFIER, Guid? ORG_ID, int Count, string REMARK, SyncLogType? SyncLogType = null, Guid? SNC_LOG_ID = null)
        {
            if (SNC_LOG_ID == null && Items != null && Items.ContainsKey("syncID"))
            {
                try
                {
                    SNC_LOG_ID = (Guid)Items["syncID"];
                }
                catch { }
            }
            if (SNC_LOG_ID == null)
            {
                SNC_LOG_ID = Guid.Empty;
            }
            T_FM_SYNC_LOG_DETAIL model = new T_FM_SYNC_LOG_DETAIL();
            model.ID = Guid.NewGuid();
            model.SNC_LOG_ID = SNC_LOG_ID.Value;
            model.LOGTYPE = SyncLogType;
            model.COUNT = Count;
            model.REMARK = REMARK;
            model.IS_DELETED = false;
            model.ORG_ID = ORG_ID;
            model.CREATE_TIME = DateTime.Now;
            model.MODIFY_TIME = DateTime.Now;
            model.TRACEIDENTIFIER = TRACEIDENTIFIER;
            return model;
        }

        /// <summary>
        /// 插入跑批日志
        /// </summary>
        /// <param name="ORG_ID"></param>
        /// <param name="API_NAME"></param>
        /// <param name="SYNC_PARAM"></param>
        /// <param name="DATA_COUNT"></param>
        /// <returns></returns>
        public bool InsertSyncLog(IDictionary<object, object?> Items, Guid? ORG_ID, string API_NAME, string SYNC_PARAM, SyncLogType? SyncLogType, int DATA_COUNT = 0)
        {
            T_FM_SYNC_LOG model = GetSyncLog(Items, ORG_ID, API_NAME, SYNC_PARAM, SyncLogType, DATA_COUNT);
            this.UnifiedCommit(() =>
            {
                UpdateEntityNoCommit(model);
            });
            return true;
        }

        /// <summary>
        /// 插入信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool InsertSyncLog(T_FM_SYNC_LOG model)
        {
            model.END_TIME = DateTime.Now;
            this.UnifiedCommit(() =>
            {
                UpdateEntityNoCommit(model);
            });
            return true;
        }
        /// <summary>
        /// 插入跑批日志明细
        /// </summary>
        /// <param name="SNC_LOG_ID"></param>
        /// <param name="ORG_ID"></param>
        /// <param name="Count"></param>
        /// <param name="REMARK"></param>
        /// <returns></returns>
        public bool InsertSyncLogDetail(IDictionary<object, object?> Items, string TRACEIDENTIFIER, Guid? ORG_ID, int Count, string REMARK, SyncLogType? SyncLogType = null, Guid? SNC_LOG_ID = null)
        {
            T_FM_SYNC_LOG_DETAIL model = GetSyncLogDetail(Items, TRACEIDENTIFIER, ORG_ID, Count, REMARK, SyncLogType, SNC_LOG_ID);
            this.UnifiedCommit(() =>
            {
                UpdateEntityNoCommit(model);
            });
            return true;
        }


        /// <summary>
        /// 插入跑批日志信息
        /// </summary>
        /// <param name="API_NAME"></param>
        /// <param name="TRACEIDENTIFIER"></param>
        /// <param name="LogID"></param>
        /// <param name="ORG_ID"></param>
        public T_FM_SYNC_LOG InsertSyncLog(string API_NAME, string TRACEIDENTIFIER, Guid LogID, Guid? ORG_ID, string SYNC_PARAM)
        {
            T_FM_SYNC_LOG log = new T_FM_SYNC_LOG()
            {
                ID = LogID,
                START_TIME = DateTime.Now,
                END_TIME = DateTime.Now,
                API_NAME = API_NAME,
                DATA_COUNT = 0,
                IS_SUCCESS = true,
                IS_DELETED = false,
                ORG_ID = ORG_ID,
                ENTITY_ORG_TPYE = 0,
                FLOW_STATUS = 0,
                FLOW_SEND_STATUS = 0,
                CREATE_TIME = DateTime.Now,
                MODIFY_TIME = DateTime.Now,
                TRACEIDENTIFIER = TRACEIDENTIFIER,
                ISINSERT = false,
                //FLOW_ID = ID,
                //FORM_ID = ID,
                //CREATER_ID = ID,
                //MODIFIER_ID = ID,
                SYNC_PARAM = SYNC_PARAM,
                //LOGTYPE = ID,
            };
            this.UnifiedCommit(() =>
            {
                UpdateEntityNoCommit(log);
            });
            return log;
        }


        /// <summary>
        /// 插入 API 耗时
        /// </summary>
        /// <param name="API_NAME"></param>
        /// <param name="TRACEIDENTIFIER"></param>
        /// <param name="TRACEIDENTIFIERED"></param>
        /// <param name="ORG_ID"></param>
        /// <param name="START_TIME"></param>
        /// <param name="END_TIME"></param>
        /// <returns></returns>
        public T_FM_SYNC_LOG InsertAPISyncLog(string API_NAME, string TRACEIDENTIFIER, string TRACEIDENTIFIERED, Guid? ORG_ID, DateTime START_TIME, DateTime END_TIME)
        {
            T_FM_SYNC_LOG log = new T_FM_SYNC_LOG()
            {
                ID = Guid.NewGuid(),
                START_TIME = START_TIME,
                END_TIME = END_TIME,
                API_NAME = API_NAME,
                DATA_COUNT = 0,
                IS_SUCCESS = true,
                IS_DELETED = false,
                ORG_ID = ORG_ID,
                ENTITY_ORG_TPYE = 0,
                FLOW_STATUS = 0,
                FLOW_SEND_STATUS = 0,
                CREATE_TIME = DateTime.Now,
                //MODIFY_TIME = DateTime.Now,
                TRACEIDENTIFIER = TRACEIDENTIFIER,
                TRACEIDENTIFIERED = TRACEIDENTIFIERED,
                ISINSERT = false,
                LOGTYPE = SyncLogType.APITimeResponsen,
                //FLOW_ID = ID,
                //FORM_ID = ID,
                CREATER_ID = APT.Infrastructure.Api.AppContext.CurrentSession.UserID,
                //MODIFIER_ID = ID,
                //SYNC_PARAM = SYNC_PARAM,
                //LOGTYPE = ID,
                SYNC_PARAM = (END_TIME - START_TIME).TotalSeconds.ToString()
            };

            this.UnifiedCommit(() =>
            {
                UpdateEntityNoCommit(log);
            });
            return log;
        }


    }
}
