using Hncore.Infrastructure.Service;
using Hncore.Pass.Vpn.Domain;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Hncore.Pass.Vpn.Service
{
    public partial class ProductAccountChargeService : ServiceBase<ProductAccountChargeEntity>, IFindService
    {
        CourseContext m_DbContext;            
        public ProductAccountChargeService(CourseContext dbContext  
            , IHttpContextAccessor httpContextAccessor) : base(dbContext, httpContextAccessor)
        {
            m_DbContext = dbContext;          
           
        }

        /// <summary>
        /// 记录新开操作
        /// </summary>
        /// <param name="package"></param>
        /// <param name="orderId"></param>
        /// <param name="gid"></param>
        /// <param name="account"></param>
        /// <param name="pwd"></param>
        /// <param name="accountType"></param>
        /// <param name="connCount"></param>
        /// <param name="chargeStatus"></param>
        /// <returns></returns>
        public async Task RecordNew(ProductPackageEntity package, int orderId, string gid, string account, string pwd, int accountType, int connCount, ChargeStatus chargeStatus = ChargeStatus.Ok)
        {
            var chargeEntity = new ProductAccountChargeEntity()
            {
                Account = account,
                AccountType = accountType,
                ConnectCount = connCount,
                OperationType = ChargeOperationType.New,
                OrderId = orderId,
                PackageId = package.Id,
                PackageOriginKey = package.OriginKey,
                ProductGroup = gid,
                ProductId = package.ProductId,
                Pwd = pwd,
                Status = chargeStatus,
                TryTimes = 1,
            };          
            await this.Add(chargeEntity);
        }

        /// <summary>
        /// 记录续费操作
        /// </summary>
        /// <param name="package"></param>
        /// <param name="orderId"></param>
        /// <param name="gid"></param>
        /// <param name="account"></param>
        /// <param name="pwd"></param>
        /// <param name="accountType"></param>
        /// <param name="connCount"></param>
        /// <param name="chargeStatus"></param>
        /// <returns></returns>
        public async Task RecordReNew(ProductPackageEntity package, int orderId, string gid, string account, int connCount, ChargeStatus chargeStatus = ChargeStatus.Ok)
        {
            var chargeEntity = new ProductAccountChargeEntity()
            {
                Account = account,
                AccountType =0,
                ConnectCount = connCount,
                OperationType = ChargeOperationType.ReNew,
                OrderId = orderId,
                PackageId = package.Id,
                PackageOriginKey = package.OriginKey,
                ProductGroup = gid,
                ProductId = package.ProductId,
                Pwd = "",
                Status = chargeStatus,
                TryTimes = 1,
            };
            await this.Add(chargeEntity);
        }

        public async Task<List<ProductAccountChargeEntity>> GetFaildRecord(int maxTryTimes = 3, int maxTop = 100)
        {
            var query = this.Query(m => m.Status == ChargeStatus.Faild
                                   && m.TryTimes <= maxTryTimes
                                   && (DateTime.Now - m.CreateTime).Minutes <= 10);
            return await query.OrderBy(m => m.Id)
                    .Take(maxTop)
                    .ToListAsync();
        }
    }
}
