PrePosWithholdService.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 PrePosWithholdService
  12. {
  13. public readonly static PrePosWithholdService Instance = new PrePosWithholdService();
  14. private PrePosWithholdService()
  15. { }
  16. public void Start()
  17. {
  18. Thread th = new Thread(dosomething);
  19. th.IsBackground = true;
  20. th.Start();
  21. }
  22. public void dosomething()
  23. {
  24. while (true)
  25. {
  26. try
  27. {
  28. if(DateTime.Now.Hour > 3 && DateTime.Now.Hour < 20 && DateTime.Now >= DateTime.Parse("2023-09-20 11:00:00"))
  29. {
  30. string chk = function.ReadInstance("/PrePosWithhold/" + DateTime.Now.ToString("yyyyMMdd") + ".txt");
  31. if(string.IsNullOrEmpty(chk))
  32. {
  33. function.WritePage("/PrePosWithhold/", "" + DateTime.Now.ToString("yyyyMMdd") + ".txt", DateTime.Now.ToString());
  34. PrePosWithhold();
  35. }
  36. }
  37. }
  38. catch (Exception ex)
  39. {
  40. function.WriteLog(DateTime.Now.ToString() + "\r\n" + ex.ToString(), "预发机提醒异常");
  41. }
  42. Thread.Sleep(300000);
  43. }
  44. }
  45. //预发机30天未完成申请时向预发创客增加一条预扣款
  46. private void PrePosWithhold()
  47. {
  48. WebCMSEntities db = new WebCMSEntities();
  49. DateTime end = DateTime.Parse(DateTime.Now.AddDays(-29).ToString("yyyy-MM-dd") + " 00:00:00");
  50. int Id = 0;
  51. bool op = true;
  52. while(op)
  53. {
  54. var PrePosList = db.PreSendStockDetail.Where(m => m.Id > Id && m.ApplyFlag == 0 && m.CreateDate < end && m.Status >= 0 && m.Status <= 1 && m.WithholdFlag == 0).OrderBy(m => m.Id).Take(20).ToList();
  55. if(PrePosList.Count > 0)
  56. {
  57. foreach(var Pos in PrePosList)
  58. {
  59. PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == Pos.SnId && m.BuyUserId == 0 && m.PreUserId > 0);
  60. if(pos != null)
  61. {
  62. KqProducts brand = db.KqProducts.FirstOrDefault(m => m.Id == Pos.BrandId) ?? new KqProducts();
  63. decimal ChargeAmount = brand.Kind == 2 ? 300 : 200;
  64. db.ToChargeBackRecord.Add(new ToChargeBackRecord
  65. {
  66. CreateDate = DateTime.Now,
  67. SeoTitle = "系统",
  68. SeoDescription = "预发机超时未申请扣款",
  69. Remark = "预发机超时未申请扣款",
  70. ChargeType = 1,
  71. ChargeAmount = ChargeAmount,
  72. UserId = Pos.ToUserId,
  73. Field1 = Pos.SnNo,
  74. });
  75. pos.WithholdFlag = 1;
  76. PreSendStockDetail pre = db.PreSendStockDetail.FirstOrDefault(m => m.Id == Pos.Id);
  77. if(pre != null)
  78. {
  79. pre.WithholdFlag = 1;
  80. }
  81. //增加账户预扣总额
  82. Utils.Instance.ToChargeAmount(Pos.ToUserId, ChargeAmount);
  83. }
  84. Id = Pos.Id;
  85. }
  86. db.SaveChanges();
  87. }
  88. else
  89. {
  90. op = false;
  91. }
  92. }
  93. db.Dispose();
  94. }
  95. // 含有【预】字标签的机具完成申请时
  96. // ·若预扣款已扣,则返还创客余额
  97. // ·若预扣款未扣,则删除该条预扣款
  98. public void StartPre()
  99. {
  100. Thread th = new Thread(StartPreDo);
  101. th.IsBackground = true;
  102. th.Start();
  103. }
  104. public void StartPreDo()
  105. {
  106. while (true)
  107. {
  108. try
  109. {
  110. string content = RedisDbconn.Instance.RPop<string>("PreWithholdQueue");
  111. if(!string.IsNullOrEmpty(content))
  112. {
  113. JsonData jsonObj = JsonMapper.ToObject(content);
  114. int UserId = int.Parse(function.CheckInt(jsonObj["UserId"].ToString()));
  115. string SnNo = jsonObj["SnNo"].ToString();
  116. WebCMSEntities db = new WebCMSEntities();
  117. decimal ChargeAmount = 0;
  118. bool doBack = true; //是否减少账户预扣总额
  119. ToChargeBackRecord back = db.ToChargeBackRecord.FirstOrDefault(m => m.UserId == UserId && m.Field1 == SnNo && m.Remark == "预发机超时未申请扣款");
  120. if(back != null)
  121. {
  122. ChargeAmount = back.ChargeAmount;
  123. if(back.Status == 1)
  124. {
  125. Utils.Instance.OpAccount(back.UserId, ChargeAmount, 134, false);
  126. doBack = false;
  127. }
  128. db.ToChargeBackRecord.Remove(back);
  129. db.SaveChanges();
  130. }
  131. db.Dispose();
  132. //减少账户预扣总额
  133. if(doBack)
  134. {
  135. Utils.Instance.ToChargeAmount(UserId, -ChargeAmount);
  136. }
  137. Thread.Sleep(500);
  138. }
  139. else
  140. {
  141. Thread.Sleep(30000);
  142. }
  143. }
  144. catch (Exception ex)
  145. {
  146. function.WriteLog(DateTime.Now.ToString() + "\r\n" + ex.ToString(), "预发扣款机具申请异常");
  147. Thread.Sleep(300000);
  148. }
  149. }
  150. }
  151. }
  152. }