TimeOutPosChargeService.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Linq;
  5. using System.Threading;
  6. using MySystem.Models;
  7. using Library;
  8. namespace MySystem
  9. {
  10. /// <summary>
  11. /// 过期机具执行扣费
  12. /// </summary>
  13. public class TimeOutPosChargeService
  14. {
  15. public readonly static TimeOutPosChargeService Instance = new TimeOutPosChargeService();
  16. private TimeOutPosChargeService()
  17. { }
  18. public void Start()
  19. {
  20. Thread th = new Thread(doSomething);
  21. th.IsBackground = true;
  22. th.Start();
  23. }
  24. public void doSomething()
  25. {
  26. while (true)
  27. {
  28. if (DateTime.Now.Hour < 9)
  29. {
  30. try
  31. {
  32. string check = function.ReadInstance("/TimeOutPosCharge/check" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt");
  33. if (string.IsNullOrEmpty(check))
  34. {
  35. function.WritePage("/TimeOutPosCharge/", "check" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt", DateTime.Now.ToString("HH:mm:ss"));
  36. WebCMSEntities db = new WebCMSEntities();
  37. var date = DateTime.Now.AddDays(-60).ToString("yyyy-MM-dd 00:00:00");//过期限制时间
  38. var time = DateTime.Parse(date);//过期限制时间
  39. DataTable dt = CustomerSqlConn.dtable("SELECT BuyUserId FROM PosMachinesTwo WHERE `Status`>-1 AND BuyUserId>0 AND BindingState=0 AND ActivationState=0 AND ScanQrTrade=0 AND RecycEndDate <'" + date + "' GROUP BY BuyUserId", MysqlConn.SqlConnStr);//扣费创客
  40. var query = db.PosMachinesTwo.Where(m => m.Status > -1 && m.BuyUserId > 0 && m.BindingState == 0 && m.ActivationState == 0 && m.ScanQrTrade == 0 && m.RecycEndDate < time).ToList();//循环过期超过15天机具
  41. var brandInfo = db.KqProducts.ToList();
  42. foreach (DataRow item in dt.Rows)
  43. {
  44. int BuyUserId = int.Parse(item["BuyUserId"].ToString());
  45. var userAccount = db.UserAccount.FirstOrDefault(m => m.Id == BuyUserId);
  46. if (userAccount == null)
  47. {
  48. userAccount = db.UserAccount.Add(new UserAccount()
  49. {
  50. Id = BuyUserId,
  51. UserId = BuyUserId,
  52. }).Entity;
  53. db.SaveChanges();
  54. }
  55. var posInfo = query.Where(m => m.BuyUserId == BuyUserId);
  56. var amount = 0;
  57. foreach (var pos in posInfo)
  58. {
  59. var poss = db.PosMachinesTwo.FirstOrDefault(m => m.Id == pos.Id) ?? new PosMachinesTwo();
  60. poss.ScanQrTrade = 999;
  61. var Brand = brandInfo.FirstOrDefault(m => m.Id == pos.BrandId);
  62. if (Brand.Name.Contains("电签"))
  63. {
  64. amount = 200;
  65. }
  66. if (Brand.Name.Contains("大POS"))
  67. {
  68. amount = 300;
  69. }
  70. userAccount.ToChargeAmount += amount;//增加预扣款
  71. var toChargeBackRecord = db.ToChargeBackRecord.Add(new ToChargeBackRecord
  72. {
  73. CreateDate = DateTime.Now,
  74. UserId = BuyUserId,
  75. ChargeAmount = amount,
  76. ChargeType = 124,//过期机具货款扣费
  77. Remark = pos.PosSn,
  78. }).Entity;
  79. }
  80. }
  81. db.SaveChanges();
  82. DoChargeAmount(db);
  83. }
  84. }
  85. catch (Exception ex)
  86. {
  87. function.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "过期机具执行扣费异常");
  88. }
  89. }
  90. Thread.Sleep(1000);
  91. }
  92. }
  93. public void StartDoChargeAmount()
  94. {
  95. Thread th = new Thread(StartDoChargeAmountReady);
  96. th.IsBackground = true;
  97. th.Start();
  98. }
  99. public void StartDoChargeAmountReady()
  100. {
  101. while (true)
  102. {
  103. WebCMSEntities db = new WebCMSEntities();
  104. DoChargeAmount(db);
  105. db.Dispose();
  106. Thread.Sleep(600000);
  107. }
  108. }
  109. public void ListenChargeAmount()
  110. {
  111. Thread th = new Thread(ListenChargeAmountReady);
  112. th.IsBackground = true;
  113. th.Start();
  114. }
  115. public void ListenChargeAmountReady()
  116. {
  117. while (true)
  118. {
  119. string content = RedisDbconn.Instance.RPop<string>("DoChargeAmountQueue");
  120. if (!string.IsNullOrEmpty(content))
  121. {
  122. WebCMSEntities db = new WebCMSEntities();
  123. DoChargeAmount(db, int.Parse(function.CheckInt(content)));
  124. db.Dispose();
  125. }
  126. }
  127. }
  128. public void DoChargeAmount(WebCMSEntities db, int UserId = 0)
  129. {
  130. IQueryable<ToChargeBackRecord> list = db.ToChargeBackRecord.Where(m => m.Status == 0);//过期机具扣费记录
  131. if (UserId > 0)
  132. {
  133. list = list.Where(m => m.UserId == UserId);
  134. }
  135. var info = list.ToList();
  136. foreach (var items in info)
  137. {
  138. function.WriteLog(DateTime.Now.ToString(), "扣款监听日志");
  139. function.WriteLog(items.UserId.ToString(), "扣款监听日志");
  140. var userAccount = db.UserAccount.FirstOrDefault(m => m.Id == items.UserId) ?? new UserAccount();
  141. var record = info.FirstOrDefault(m => m.Id == items.Id);
  142. function.WriteLog("BalanceAmount:" + userAccount.BalanceAmount + ";ChargeAmount:" + record.ChargeAmount + ";", "扣款监听日志");
  143. if (userAccount.BalanceAmount >= record.ChargeAmount)
  144. {
  145. function.WriteLog("in", "扣款监听日志");
  146. var ChangeType = 0;
  147. var Remark = "";
  148. if (record.ChargeType == 124)//过期机具货款扣费
  149. {
  150. ChangeType = 124;
  151. Remark = "扣机具货款";
  152. }
  153. if (record.ChargeType == 1)//普通预扣款
  154. {
  155. ChangeType = 202;
  156. // Remark = "普通扣款";
  157. Remark = items.Remark;
  158. }
  159. if (record.ChargeType == 2)//分期预扣款
  160. {
  161. ChangeType = 201;
  162. // Remark = "分期扣款";
  163. Remark = items.Remark;
  164. }
  165. var userAccountRecord = db.UserAccountRecord.Add(new UserAccountRecord
  166. {
  167. CreateDate = DateTime.Now,
  168. UserId = items.UserId,
  169. BeforeBalanceAmount = userAccount.BalanceAmount,
  170. AfterBalanceAmount = userAccount.BalanceAmount - record.ChargeAmount,
  171. ChangeAmount = record.ChargeAmount,
  172. ChangeType = ChangeType,
  173. Remark = Remark,
  174. }).Entity;
  175. record.Status = 1;
  176. if (record.ChargeType == 2)
  177. {
  178. var toChargeBackRecordSub = db.ToChargeBackRecordSub.FirstOrDefault(m => m.Id == record.Sort) ?? new ToChargeBackRecordSub();
  179. var toChargeByStage = db.ToChargeByStage.FirstOrDefault(m => m.Id == toChargeBackRecordSub.ParentId && m.TimeNumber > m.QueryCount) ?? new ToChargeByStage();
  180. toChargeBackRecordSub.Status = 1;
  181. toChargeByStage.QueryCount += 1;
  182. toChargeByStage.ChargeAmount += toChargeBackRecordSub.ChargeAmount;
  183. if (toChargeByStage.TimeNumber == toChargeByStage.QueryCount)
  184. {
  185. toChargeByStage.Status = 1;
  186. }
  187. }
  188. function.WriteLog("ToChargeAmount:" + userAccount.ToChargeAmount + ";ChargeAmount:" + record.ChargeAmount + ";", "扣款监听日志");
  189. if (userAccount.ToChargeAmount >= record.ChargeAmount)
  190. {
  191. function.WriteLog("ok", "扣款监听日志");
  192. userAccount.ToChargeAmount -= record.ChargeAmount;//分期扣款
  193. }
  194. userAccount.BalanceAmount -= record.ChargeAmount;//扣减余额
  195. ToChargeBackRecord edit = db.ToChargeBackRecord.FirstOrDefault(m => m.Id == items.Id);
  196. if(edit != null)
  197. {
  198. edit.UpdateDate = DateTime.Now;
  199. }
  200. function.WriteLog("\n\n", "扣款监听日志");
  201. }
  202. }
  203. db.SaveChanges();
  204. }
  205. }
  206. }