PosWithholdService.cs 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 PosWithholdService
  12. {
  13. public readonly static PosWithholdService Instance = new PosWithholdService();
  14. private PosWithholdService()
  15. { }
  16. // ·若扣款已扣,则返还创客余额
  17. // ·若扣款未扣,则删除该条扣款
  18. public void Start()
  19. {
  20. Thread th = new Thread(StartDo);
  21. th.IsBackground = true;
  22. th.Start();
  23. }
  24. public void StartDo()
  25. {
  26. while (true)
  27. {
  28. try
  29. {
  30. string content = RedisDbconn.Instance.RPop<string>("PosWithholdQueue");
  31. if(!string.IsNullOrEmpty(content))
  32. {
  33. function.WriteLog("content:" + content, "过期机具扣款机具申请");
  34. JsonData jsonObj = JsonMapper.ToObject(content);
  35. int UserId = int.Parse(function.CheckInt(jsonObj["UserId"].ToString()));
  36. string SnNo = jsonObj["SnNo"].ToString();
  37. WebCMSEntities db = new WebCMSEntities();
  38. decimal ChargeAmount = 0;
  39. bool doBack = true; //是否减少账户预扣总额
  40. ToChargeBackRecord back = db.ToChargeBackRecord.FirstOrDefault(m => m.UserId == UserId && m.Field1 == SnNo && m.ChargeType == 124 && m.Remark.StartsWith("循环过期"));
  41. if(back != null)
  42. {
  43. function.WriteLog("扣款数据:" + Newtonsoft.Json.JsonConvert.SerializeObject(back), "过期机具扣款机具申请");
  44. ChargeAmount = back.ChargeAmount;
  45. function.WriteLog("扣款是否已扣:" + back.Status, "过期机具扣款机具申请");
  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. function.WriteLog("是否减少账户预扣总额:" + doBack, "过期机具扣款机具申请");
  55. if(doBack)
  56. {
  57. Utils.Instance.ToChargeAmount(UserId, -ChargeAmount);
  58. function.WriteLog("减少账户预扣总额:" + ChargeAmount, "过期机具扣款机具申请");
  59. }
  60. }
  61. db.Dispose();
  62. Thread.Sleep(500);
  63. }
  64. else
  65. {
  66. Thread.Sleep(30000);
  67. }
  68. }
  69. catch (Exception ex)
  70. {
  71. function.WriteLog(DateTime.Now.ToString() + "\r\n" + ex.ToString(), "过期机具扣款机具申请异常");
  72. Thread.Sleep(300000);
  73. }
  74. }
  75. }
  76. }
  77. }