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

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


        public void Start()
        {
            Thread th = new Thread(StartDo);
            th.IsBackground = true;
            th.Start();
        }

        public void StartDo()
        {
            while (true)
            {
                try
                {
                    string content = RedisDbconn.Instance.RPop<string>("WifiWithholdBackQueue");
                    if(!string.IsNullOrEmpty(content))
                    {
                        Utils.WriteLog("content:" + content, "Wifi预扣款激活处理");
                        JsonData jsonObj = JsonMapper.ToObject(content);
                        // int UserId = int.Parse(function.CheckInt(jsonObj["UserId"].ToString()));
                        string SnNo = jsonObj["SnNo"].ToString();
                        WebCMSEntities db = new WebCMSEntities();
                        decimal ChargeAmount = 0;
                        bool doBack = true; //是否减少账户预扣总额
                        ToChargeBackRecord back = db.ToChargeBackRecord.FirstOrDefault(m => m.Field1 == SnNo && (m.Remark == "WIFI超时未激活扣款" || m.Remark.Contains(SnNo)));
                        if(back != null)
                        {
                            if(back.LockFlag == 0)
                            {
                                Utils.WriteLog("预扣款数据:" + Newtonsoft.Json.JsonConvert.SerializeObject(back), "Wifi预扣款激活处理");
                                ChargeAmount = back.ChargeAmount;
                                Utils.WriteLog("预扣款是否已扣:" + back.Status, "Wifi预扣款激活处理");
                                if(back.Status == 1)
                                {
                                    Utils.Instance.OpAccount(back.UserId, ChargeAmount, 138, false);
                                    doBack = false;
                                }
                                db.ToChargeBackRecord.Remove(back);
                                db.SaveChanges();
                            }
                            else
                            {
                                Thread.Sleep(5000);
                                RedisDbconn.Instance.AddList("WifiWithholdBackQueue", content);
                            }
                        }
                        db.Dispose();

                        //减少账户预扣总额
                        Utils.WriteLog("是否减少账户预扣总额:" + doBack, "Wifi预扣款激活处理");
                        if(doBack)
                        {
                            Utils.Instance.ToChargeAmount(back.UserId, -ChargeAmount);
                            Utils.WriteLog("减少账户预扣总额:" + ChargeAmount, "Wifi预扣款激活处理");
                        }

                        Thread.Sleep(500);
                    }
                    else
                    {
                        Thread.Sleep(30000);
                    }
                }
                catch (Exception ex)
                {
                    Utils.WriteLog(DateTime.Now.ToString() + "\r\n" + ex.ToString(), "Wifi预扣款激活处理异常");
                    Thread.Sleep(300000);
                }
            }
        }
    }
}