using CloudGaming.Code.Account;
using CloudGaming.Code.DataAccess;
using CloudGaming.DtoModel.Account.User;
using CloudGaming.DtoModel.Payment;

using Newtonsoft.Json;

using StackExchange.Redis;

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

namespace CloudGaming.Code.Payment;

public static class OrderExtend
{

    /// <summary>
    /// 不带锁，出现异常需要自己处理
    /// </summary>
    /// <param name="product"></param>
    /// <param name="user"></param>
    /// <param name="pay"></param>
    /// <param name="orderId"></param>
    /// <param name="price"></param>
    /// <param name="intentDate"></param>
    /// <param name="dao"></param>
    /// <returns></returns>
    public static async Task OrderRewardsNoWorkAsync(this T_Products product, T_User user, string pay, string orderId, decimal price,string channel, DateTime intentDate, DAO dao, T_User_OrderItems orderItems = null)
    {
        var userId = user.Id;
        var chargeMoneyCount = dao.DaoUser.Context.T_User_Order.Count(it => it.UserId == userId && it.ProductId == product.ProductId);
        var productReward = await dao.DaoPhone.Context.T_Products_Reward.Where(it => it.ProductId == product.ProductId).ToListAsync();

        if (productReward != null && productReward.Count > 0)
        {

            List<string> tips = new List<string>();
            //List<T_User_Currency> user_Currencies = new List<T_User_Currency>();
            foreach (var reward in productReward)
            {
                var money = reward.Money;
                var currency = (UserCurrencyType)reward.CurrencyType;
                var userCurrency = new T_User_Currency();
                await user.ConsumeMoneyNoWork(currency, money, dao, userCurrency, orderId, $"购买{product.ProductName}");
                tips.Add($"获得{currency}*{money}");
                if (product.IsFirstCharge && chargeMoneyCount == 0 && reward.FirstChargeMoney > 0)
                {
                    await user.ConsumeMoneyNoWork(currency, reward.FirstChargeMoney ?? 0, dao, userCurrency, orderId, $"首充赠送{currency}{reward.FirstChargeMoney}");
                    tips.Add($"首充赠送{currency}*{money}");
                }
            }
            var rewardTips = string.Join(',', tips.ToArray());
            T_User_Order order = new T_User_Order()
            {
                OrderId = orderId,
                CreatedAt = DateTime.Now,
                OrderDate = intentDate,
                PaymentDate = DateTime.Now,
                PaymentDay = DateOnly.FromDateTime(DateTime.Now),
                PaymentMethod = pay,
                ProductId = product.ProductId,
                Status = (int)OrderState.已完成,
                TenantId = product.TenantId,
                TotalPrice = price,
                UpdatedAt = DateTime.Now,
                UserId = userId,
                Channel= channel
            };
            if (orderItems == null)
            {
                orderItems = new T_User_OrderItems();
            }
            orderItems.OrderId = orderId;
            orderItems.RewardTips = rewardTips;
            orderItems.Product = product.Id;
            orderItems.TenantId = product.TenantId;
            orderItems.ProductId = product.ProductId;
            orderItems.RewardInfo = JsonConvert.SerializeObject(productReward);
            orderItems.RewardTips = rewardTips;
            dao.DaoUser.Context.T_User_OrderItems.Add(orderItems);
            dao.DaoUser.Context.T_User_Order.Add(order);
            await dao.DaoUser.Context.SaveChangesAsync();
        }
    }

}
