using System;
using System.Collections.Generic;
using System.Linq;
using MySystem.PxcModels;
using Library;
using LitJson;

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

        public void Start()
        {
            try
            {
                dosomething();
            }
            catch (Exception ex)
            {
                function.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "支付回调异常");
            }
        }

        private void dosomething()
        { 
            bool op = true;
            while (op)
            {
                string content = RedisDbconn.Instance.RPop<string>("PayCallBack");
                if (!string.IsNullOrEmpty(content))
                {
                    bool pass = true;
                    string[] datalist = content.Split(new string[] { "#cut#" }, StringSplitOptions.None);
                    JsonData jsonObj = JsonMapper.ToObject(datalist[1]);
                    string OrderNo = "";
                    if(datalist[0] == "wechat")
                    {
                        if(jsonObj["event_type"].ToString() != "TRANSACTION.SUCCESS")
                        {
                            pass = false;
                        }
                        string ciphertext = jsonObj["resource"]["ciphertext"].ToString();
                        
                        OrderNo = "";
                    }
                    if(pass)
                    {
                        WebCMSEntities db = new WebCMSEntities();
                        ConsumerOrderForNo forNo = db.ConsumerOrderForNo.FirstOrDefault(m => m.OrderNo == OrderNo);
                        if (forNo != null)
                        {
                            ConsumerOrders order = db.ConsumerOrders.FirstOrDefault(m => m.Id == forNo.OrderIds && m.Status == 0);
                            if (order != null)
                            {
                                order.Status = 1;
                                order.UpdateDate = DateTime.Now;
                                order.PayMoney = order.PayMoney;
                                order.MaxDivi = order.MaxDivi;
                                db.SaveChanges();
                                RedisDbconn.Instance.AddList("ConsumerOrdersStat", order.Id);
                                RedisDbconn.Instance.AddList("ConsumerOrders:Divi:List", order.Id.ToString());
                                RedisDbconn.Instance.AddRightList("ConsumerOrders:Divi:" + order.MerchantId, order);
                                // ConsumerOrdersStatService.Instance.Stat(order);
                            }
                        }
                        db.Dispose();
                    }
                }
                else
                {
                    op = false;
                }
            }
        }
    }
}