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(JobMqMsg jobInfo)
        {
            string content = "";
            try
            {
                dosomething();
                // string Msg = "success";
                // jobInfo.Status = Msg == "success" ? 1 : 0;
                // jobInfo.Msg = Msg == "success" ? "执行完成" : Msg;
                // RabbitMQClient.Instance.SendMsg(Newtonsoft.Json.JsonConvert.SerializeObject(jobInfo), "PublicBack");
            }
            catch (Exception ex)
            {
                if (!string.IsNullOrEmpty(content))
                {
                    Dictionary<string, string> data = new Dictionary<string, string>();
                    data.Add("ErrTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                    data.Add("ErrMsg", ex.ToString());
                    Utils.WriteLog(Newtonsoft.Json.JsonConvert.SerializeObject(data), "public_err");
                }
                else
                {
                    Utils.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "public_service");
                }
            }
        }

        private void dosomething()
        { 
            bool op = true;
            while (op)
            {
                string content = RedisDbconn.Instance.RPop<string>("PayCallBack");
                if (!string.IsNullOrEmpty(content))
                {
                    JsonData jsonObj = JsonMapper.ToObject(content);
                    string OrderNo = jsonObj["out_trade_no"].ToString();
                    string TradeNo = jsonObj["transaction_id"].ToString();
                    decimal TotalFee = decimal.Parse(function.CheckNum(jsonObj["total_fee"].ToString()));
                    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;
                }
            }
        }
    }
}