using CloudGaming.Code.Account;
using CloudGaming.DtoModel.Mall;

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CloudGaming.Code.Mall;

/// <summary>
/// 产品逻辑
/// </summary>
public class ProductBLL : CloudGamingBase
{
    public ProductBLL(IServiceProvider serviceProvider) : base(serviceProvider)
    {
    }
    /// <summary>
    /// 获取商城数据
    /// </summary>
    /// <returns></returns>
    public async Task<List<DiamondMallDto>> GetDiamondMall()
    {
        int productType = (int)ProductType.钻石商城;

        var diamondMallDtos = await GetProductList(productType);
        return diamondMallDtos;
    }


    /// <summary>
    /// 获取包夜卡数据
    /// </summary>
    /// <returns></returns>
    public async Task<List<DiamondMallDto>> GetNightCard()
    {
        var diamondMallDtos = await GetProductList((int)ProductType.包夜卡);
        return diamondMallDtos;
    }
    /// <summary>
    /// 获取商品数据
    /// </summary>
    /// <param name="productType"></param>
    /// <returns></returns>

    private async Task<List<DiamondMallDto>> GetProductList(int productType)
    {
        List<DiamondMallDto> diamondMallDtos = new List<DiamondMallDto>();
        //获取首充
        Dictionary<string, int> orderDictionary = new Dictionary<string, int>();
        if (_UserId > 0)
        {
            orderDictionary = await UserInfo.GetBuyProductCount(Dao, RedisCache);
        }
        //获取钻石商城产品列表
        var list = Cache.ProductCacheList.Where(it => it.ProductType == productType).ToList();
        list.ForEach(it =>
        {
            DiamondMallDto diamondMallDto = new DiamondMallDto()
            {
                Price = it.Price.ToString("0.##"),
                ProductDesc = it.ProductDesc ?? "",
                ProductId = it.ProductId,
                ProductImage = it.ImageId,
                ProductName = it.ProductName,
                ProductSelectImage = it.SelectImageId ?? 0,
                ProductType = it.ProductType,
            };
            if (it.IsFirstCharge && (!orderDictionary.ContainsKey(it.ProductId)))
            {
                diamondMallDto.Price = (it.FirstChargePrice ?? it.Price).ToString("0.##");
                diamondMallDto.ProductImage = it.FirstChargeImageId ?? it.ImageId;
                diamondMallDto.ProductSelectImage = it.SelectFirstChargeImageId ?? it.SelectImageId ?? 0;
            }
            diamondMallDtos.Add(diamondMallDto);
        });
        return diamondMallDtos;
    }
    

}
