using Hncore.Infrastructure.Service;
using Hncore.Infrastructure.WebApi;
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 ProductOrderItemService : ServiceBase<ProductOrderItemEntity>, IFindService
    {
        CourseContext m_DbContext;
        public ProductOrderItemService(CourseContext dbContext
            , IHttpContextAccessor httpContextAccessor) : base(dbContext, httpContextAccessor)
        {
            m_DbContext = dbContext;
        }
        public async Task<List<ProductOrderItemEntity>> CreateOrderItems(ProductOrderEntity orderEntity,string pwd)
        {
            List<ProductOrderItemEntity> items = new List<ProductOrderItemEntity>();
            var accounts = orderEntity.Accounts.Split(',').ToList();
            accounts.ForEach(m =>
            {
                var item = new ProductOrderItemEntity()
                {                   
                    ConnectCount = orderEntity.ConnectCount,
                    DayPrice = orderEntity.DayPrice.Value,
                    DayCount=orderEntity.DayCount,
                    OrderId = orderEntity.Id,
                    OrderNo = orderEntity.OrderNo,
                    OrderState = orderEntity.OrderState,
                    OrderType = orderEntity.OrderType,
                    PackageId = orderEntity.PackageId,
                    PackageName = orderEntity.PackageName,
                    PayState = orderEntity.PayState,
                    PayType = orderEntity.PayType,
                    Account = m,
                    ProductId = orderEntity.ProductId,
                    ProductName = orderEntity.ProductName,
                    TenantId = orderEntity.TenantId,
                    UserId = orderEntity.UserId,
                    UserName = orderEntity.UserName,
                    AccountPwd = pwd
                };
                items.Add(item);
            });
            await this.Adds(items);
            return items;
        }


        public async Task<ProductOrderItemEntity> GetLastByAccount(string account)
        {
            var orderItem = await this.Query(m => m.Account == account && m.OrderState == OrderStatus.Complete)
                  .OrderByDescending(m => m.Id)
                  .FirstOrDefaultAsync();
            return orderItem;
        }
    }
}
