WifiWithholdBackService.cs 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. using System;
  2. using System.Collections.Generic;
  3. using Library;
  4. using LitJson;
  5. using System.Linq;
  6. using System.Data;
  7. using System.Threading;
  8. using MySystem.PxcModels;
  9. namespace MySystem
  10. {
  11. public class WifiWithholdBackService
  12. {
  13. public readonly static WifiWithholdBackService Instance = new WifiWithholdBackService();
  14. private WifiWithholdBackService()
  15. { }
  16. public void Start()
  17. {
  18. Thread th = new Thread(StartDo);
  19. th.IsBackground = true;
  20. th.Start();
  21. }
  22. public void StartDo()
  23. {
  24. while (true)
  25. {
  26. try
  27. {
  28. string content = RedisDbconn.Instance.RPop<string>("WifiWithholdBackQueue");
  29. if(!string.IsNullOrEmpty(content))
  30. {
  31. function.WriteLog("content:" + content, "Wifi预扣款激活处理");
  32. JsonData jsonObj = JsonMapper.ToObject(content);
  33. int UserId = int.Parse(function.CheckInt(jsonObj["UserId"].ToString()));
  34. string SnNo = jsonObj["SnNo"].ToString();
  35. WebCMSEntities db = new WebCMSEntities();
  36. decimal ChargeAmount = 0;
  37. bool doBack = true; //是否减少账户预扣总额
  38. ToChargeBackRecord back = db.ToChargeBackRecord.FirstOrDefault(m => m.UserId == UserId && m.Field1 == SnNo && (m.Remark == "WIFI超时未激活扣款" || m.Remark.Contains(SnNo)));
  39. if(back != null)
  40. {
  41. if(back.LockFlag == 0)
  42. {
  43. function.WriteLog("预扣款数据:" + Newtonsoft.Json.JsonConvert.SerializeObject(back), "Wifi预扣款激活处理");
  44. ChargeAmount = back.ChargeAmount;
  45. function.WriteLog("预扣款是否已扣:" + back.Status, "Wifi预扣款激活处理");
  46. if(back.Status == 1)
  47. {
  48. Utils.Instance.OpAccount(back.UserId, ChargeAmount, 138, false);
  49. doBack = false;
  50. }
  51. db.ToChargeBackRecord.Remove(back);
  52. db.SaveChanges();
  53. }
  54. else
  55. {
  56. Thread.Sleep(5000);
  57. RedisDbconn.Instance.AddList("WifiWithholdBackQueue", content);
  58. }
  59. }
  60. db.Dispose();
  61. //减少账户预扣总额
  62. function.WriteLog("是否减少账户预扣总额:" + doBack, "Wifi预扣款激活处理");
  63. if(doBack)
  64. {
  65. Utils.Instance.ToChargeAmount(UserId, -ChargeAmount);
  66. function.WriteLog("减少账户预扣总额:" + ChargeAmount, "Wifi预扣款激活处理");
  67. }
  68. Thread.Sleep(500);
  69. }
  70. else
  71. {
  72. Thread.Sleep(30000);
  73. }
  74. }
  75. catch (Exception ex)
  76. {
  77. function.WriteLog(DateTime.Now.ToString() + "\r\n" + ex.ToString(), "Wifi预扣款激活处理异常");
  78. Thread.Sleep(300000);
  79. }
  80. }
  81. }
  82. }
  83. }