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("WifiWithholdBackQueue"); if(!string.IsNullOrEmpty(content)) { function.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.UserId == UserId && m.Field1 == SnNo && (m.Remark == "WIFI超时未激活扣款" || m.Remark.Contains(SnNo)) && m.LockFlag == 0); if(back != null) { back.LockFlag = 1; db.SaveChanges(); function.WriteLog("预扣款数据:" + Newtonsoft.Json.JsonConvert.SerializeObject(back), "Wifi预扣款激活处理"); ChargeAmount = back.ChargeAmount; function.WriteLog("预扣款是否已扣:" + back.Status, "Wifi预扣款激活处理"); if(back.Status == 1) { Utils.Instance.OpAccount(back.UserId, ChargeAmount, 134, false); doBack = false; } db.ToChargeBackRecord.Remove(back); back.LockFlag = 0; db.SaveChanges(); } db.Dispose(); //减少账户预扣总额 function.WriteLog("是否减少账户预扣总额:" + doBack, "Wifi预扣款激活处理"); if(doBack) { Utils.Instance.ToChargeAmount(UserId, -ChargeAmount); function.WriteLog("减少账户预扣总额:" + ChargeAmount, "Wifi预扣款激活处理"); } Thread.Sleep(500); } else { Thread.Sleep(30000); } } catch (Exception ex) { function.WriteLog(DateTime.Now.ToString() + "\r\n" + ex.ToString(), "Wifi预扣款激活处理异常"); Thread.Sleep(300000); } } } } }