using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using TouchSocket.Core;
using WCS.BLL.DbModels;
using WCS.BLL.Services.IService;
using WCS.DAL;
using WCS.DAL.Db;
using WCS.Model;
using WCS.Model.ApiModel;
using WCS.Model.ApiModel.User;

namespace WCS.BLL.Services.Service
{
    public class InterfaceRecordService : IInterfaceRecordService
    {
        public InterfaceRecordService() { }

        public async Task<PageQueryResponse<SystemApiLogRecord>> getInterfaceRecord(GetInterfaceRecordsRequest request)
        {
            try
            {
                var recordsQueryable = DbHelp.dbLog.Queryable<SystemApiLogRecord>()
                                          .WhereIF(!string.IsNullOrEmpty(request.RequestType), t => t.RequestType == request.RequestType)
                                          .WhereIF(!string.IsNullOrEmpty(request.RequestUrl), t => t.RequestUrl.Contains(request.RequestUrl))
                                          .WhereIF(!string.IsNullOrEmpty(request.RequestBody), t => t.RequestBody.Contains(request.RequestBody));
                if (request.TimeType == TimeType.请求时间)
                {
                    recordsQueryable = recordsQueryable.WhereIF(request.StartTime != null, t => request.StartTime <= t.RequestTime);
                    recordsQueryable = recordsQueryable.WhereIF(request.StartTime != null, t => request.EndTime >= t.RequestTime);
                }
                else if (request.TimeType == TimeType.响应时间)
                {
                    recordsQueryable = recordsQueryable.WhereIF(request.StartTime != null, t => request.StartTime <= t.ResponseTime);
                    recordsQueryable = recordsQueryable.WhereIF(request.StartTime != null, t => request.EndTime >= t.ResponseTime);
                }
                var totalCount = await recordsQueryable.CountAsync();
                var records = await recordsQueryable
                                    //.OrderByDescending(t => t.Id)
                                    .Skip((request.PageNumber - 1) * request.PageSize).Take(request.PageSize)
                                    .ToListAsync();
                //生成序号
                for (int i = 0; i < records.Count; i++)
                {
                    records[i].RowNumber = (request.PageNumber - 1) * request.PageSize + i + 1;
                }

                return new PageQueryResponse<SystemApiLogRecord>()
                {
                    Code = 200,
                    Message = $"success",
                    Data = new PageQueryResponseData<SystemApiLogRecord>()
                    {
                        TotalCount = totalCount,
                        MaxPage = request.PageSize == 0 ? 0 : (int)Math.Ceiling((decimal)totalCount / request.PageSize),
                        Count = records.Count,
                        Lists = records.ToList()
                    }
                };

            }
            catch (Exception ex)
            {
                return new PageQueryResponse<SystemApiLogRecord>()
                {
                    Code = 300,
                    Message = $"操作失败:{ex.Message}",
                };
            }
        }

        public async Task<PageQueryResponse<SystemApiLogRecord>> exportInterfaceRecord(GetInterfaceRecordsRequest request)
        {
            try
            {
                var recordsQueryable = DbHelp.dbLog.Queryable<SystemApiLogRecord>()
                                          .WhereIF(!string.IsNullOrEmpty(request.RequestType), t => t.RequestType == request.RequestType)
                                          .WhereIF(!string.IsNullOrEmpty(request.RequestUrl), t => t.RequestUrl.Contains(request.RequestUrl))
                                          .WhereIF(!string.IsNullOrEmpty(request.RequestBody), t => t.RequestBody.Contains(request.RequestBody));
                if (request.TimeType == TimeType.请求时间)
                {
                    recordsQueryable = recordsQueryable.WhereIF(request.StartTime != null, t => request.StartTime <= t.RequestTime);
                    recordsQueryable = recordsQueryable.WhereIF(request.StartTime != null, t => request.EndTime >= t.RequestTime);
                }
                else if (request.TimeType == TimeType.响应时间)
                {
                    recordsQueryable = recordsQueryable.WhereIF(request.StartTime != null, t => request.StartTime <= t.ResponseTime);
                    recordsQueryable = recordsQueryable.WhereIF(request.StartTime != null, t => request.EndTime >= t.ResponseTime);
                }
                var records = await recordsQueryable.OrderByDescending(t => t.Id).ToListAsync();
                //生成序号
                var index = 1;
                records.ForEach(r =>
                {
                    r.RowNumber = index++;
                });
                return new PageQueryResponse<SystemApiLogRecord>()
                {
                    Code = 200,
                    Message = $"success",
                    Data = new PageQueryResponseData<SystemApiLogRecord>()
                    {
                        Lists = records
                    }
                };



            }
            catch (Exception ex)
            {
                return new PageQueryResponse<SystemApiLogRecord>()
                {
                    Code = 300,
                    Message = $"操作失败:{ex.Message}",
                };
            }
        }
    }
}
