using ASHermed.SinoInterface.Api.ContractModel.Framework.Log;
using ASHermed.SinoInterface.Api.ContractModel.Shared.Log;
using ASHermed.SinoInterface.Api.IRepository.Log;
using ASHermed.SinoInterface.ContractModel;
using ASHermed.SinoInterface.Enum;
using ASHermed.SinoInterface.Exception;
using ASHermed.SinoInterface.IFramework.Log;
using PoJun.Util;
using PoJun.Util.Maps;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ASHermed.SinoInterface.LogFramework
{
    /// <summary>
    /// 接口日志服务
    /// </summary>
    public class APILogService : IAPILogService
    {
        /// <summary>
        /// 日志接口仓储
        /// </summary>
        public readonly IAPILogsRepository apiLogsRepository;

        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="_apiLogsRepository"></param>
        public APILogService(IAPILogsRepository _apiLogsRepository)
        {
            apiLogsRepository = _apiLogsRepository;
        }

        /// <summary>
        /// 新增请求日志
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public Task<AddRequestLogModel> AddRequestLogAsync(AddRequestLogParam param)
        {
            return apiLogsRepository.AddRequestLogAsync(param);
        }

        /// <summary>
        /// 新增[响应/错误]日志
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public Task AddResponseLogAsync(AddResponseLogParam param)
        {
            return apiLogsRepository.AddResponseLogAsync(param);
        }

        /// <summary>
        /// 分页查询接口日志数据
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public async Task<BaseResponse<GetApiLogsPageListModel>> GetApiLogsPageList(GetApiLogsPageListParam param)
        {
            var result = await apiLogsRepository.GetApiLogsPageList(param);

            if (result == null || result.Count == 0)
                throw new DataIsNullException();

            //获取
            var ids = result.ApiLogs.Select(x => x.ID).ToList();
            //查询二级接口日志数据
            var subset = await apiLogsRepository.GetListAsync(x => ids.Contains(x.ParentTraceID));
            if (subset == null)
                return new BaseResponse<GetApiLogsPageListModel>() { Data = result };

            foreach (var item in result.ApiLogs)
            {
                var subsetList = subset.Where(x => x.ParentTraceID == item.ID).ToList();
                if (subsetList == null || subsetList.Count == 0)
                    continue;

                item.Subset = new List<ApiLogsEntity>();
                item.Subset.AddRange(subsetList.MapToList<ApiLogsEntity>());
            }

            return new BaseResponse<GetApiLogsPageListModel>() { Data = result };
        }

        #region 获取所有接口日志系统信息

        /// <summary>
        /// 获取所有接口日志系统信息
        /// </summary>
        /// <returns></returns>
        public async Task<BaseResponse<GetAllSystemInfoModel>> GetAllApiLogsSystemInfo()
        {
            var result = await apiLogsRepository.GetAllApiLogsSystemInfo();
            if (result == null || result.ApiLogsSystemInfos == null || result.ApiLogsSystemInfos.Count == 0)
                throw new DataIsNullException();

            return new BaseResponse<GetAllSystemInfoModel>() { Data = result };
        } 

        #endregion


    }
}
