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 PosWithholdService
    {
        public readonly static PosWithholdService Instance = new PosWithholdService();
        private PosWithholdService()
        { }

        
        // ·若扣款已扣，则返还创客余额
        // ·若扣款未扣，则删除该条扣款
        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>("PosWithholdQueue");
                    if(!string.IsNullOrEmpty(content))
                    {
                        Utils.WriteLog("content:" + content, "过期机具扣款机具申请");
                        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.UserId == UserId && m.Field1 == SnNo && m.ChargeType == 124 && m.Remark.StartsWith("循环过期"));
                        if(back != null)
                        {
                            Utils.WriteLog("扣款数据:" + Newtonsoft.Json.JsonConvert.SerializeObject(back), "过期机具扣款机具申请");
                            ChargeAmount = back.ChargeAmount;
                            Utils.WriteLog("扣款是否已扣:" + back.Status, "过期机具扣款机具申请");
                            if(back.Status == 1)
                            {
                                Utils.Instance.OpAccount(back.UserId, ChargeAmount, 138, false);
                                doBack = false;
                            }
                            db.ToChargeBackRecord.Remove(back);
                            db.SaveChanges();

                            //减少账户预扣总额
                            Utils.WriteLog("是否减少账户预扣总额:" + doBack, "过期机具扣款机具申请");
                            if(doBack)
                            {
                                Utils.Instance.ToChargeAmount(UserId, -ChargeAmount);
                                Utils.WriteLog("减少账户预扣总额:" + ChargeAmount, "过期机具扣款机具申请");
                            }
                        }
                        db.Dispose();

                        Thread.Sleep(500);
                    }
                    else
                    {
                        Thread.Sleep(30000);
                    }
                }
                catch (Exception ex)
                {
                    Utils.WriteLog(DateTime.Now.ToString() + "\r\n" + ex.ToString(), "过期机具扣款机具申请异常");
                    Thread.Sleep(300000);
                }
            }
        }
    }
}