using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Threading;
using MySystem.Models;
using Library;
using LitJson;

namespace MySystem
{
    public class BalancePayBackService
    {
        public readonly static BalancePayBackService Instance = new BalancePayBackService();
        private BalancePayBackService()
        { }

        public void Start()
        {
            Thread th = new Thread(dosomething);
            th.IsBackground = true;
            th.Start();
        }
        
        private void dosomething()
        {
            while (true)
            {
                try
                {
                    string content = RedisDbconn.Instance.RPop<string>("BalancePayQueue");
                    if (!string.IsNullOrEmpty(content))
                    {
                        sloveAlipayCallBack(content);
                    }
                    else
                    {
                        Thread.Sleep(2000);
                    }
                }
                catch (Exception ex)
                {
                    LogHelper.Instance.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "商城订单余额支付异常");
                    Thread.Sleep(2000);
                }
            }
        }

        public void sloveAlipayCallBack(string content)
        {
            int OrderId = int.Parse(function.CheckInt(content));
            WebCMSEntities db = new WebCMSEntities();
            Orders order = db.Orders.FirstOrDefault(m => m.Id == OrderId && m.PayMode == 3 && m.PayStatus == 0);
            if (order != null)
            {
                decimal TotalPrice = order.TotalPrice;
                if (order.UserId == 1)
                {
                    TotalPrice = 0.01M;
                }
                string ProductName = "";
                List<OrderProduct> orderPros = db.OrderProduct.Where(m => m.OrderId == OrderId).ToList();
                foreach(OrderProduct orderPro in orderPros)
                {
                    ProductName += orderPro.ProductName + ",";
                }
                if(ProductName.Length > 64)
                {
                    ProductName = ProductName.Substring(0, 64);
                }
                UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == order.UserId);
                if (account != null)
                {
                    if(account.BalanceAmount >= TotalPrice)
                    {
                        decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
                        decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
                        decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
                        account.BalanceAmount -= TotalPrice;
                        decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
                        decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
                        decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
                        db.SaveChanges();
                        UserAccountRecord accountRecord = db.UserAccountRecord.Add(new UserAccountRecord()
                        {
                            CreateDate = DateTime.Now,
                            UpdateDate = DateTime.Now,
                            UserId = order.UserId, //创客
                            ChangeType = 20, //变动类型
                            ChangeAmount = TotalPrice, //变更金额
                            BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
                            AfterTotalAmount = AfterTotalAmount, //变更后总金额
                            BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
                            AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
                            BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
                            AfterBalanceAmount = AfterBalanceAmount, //变更后余额
                            TransRecordNo = order.OrderNo, //交易流水编号
                            Remark = "购买" + ProductName,
                        }).Entity;
                        db.SaveChanges();
                        // AlipayPayBack2Service.Instance.DoOrderV2(db, OrderId);
                    }
                }
            }
            db.Dispose();
        }
    }
}