DepositReturnStatService.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 DepositReturnStatService
  12. {
  13. public readonly static DepositReturnStatService Instance = new DepositReturnStatService();
  14. private DepositReturnStatService()
  15. { }
  16. public void Start()
  17. {
  18. Thread th = new Thread(StartEverDay);
  19. th.IsBackground = true;
  20. th.Start();
  21. }
  22. public void StartEverDay()
  23. {
  24. while(true)
  25. {
  26. if(DateTime.Now.Day <= 15 && DateTime.Now.Hour > 1 && DateTime.Now.Hour < 13)
  27. {
  28. try
  29. {
  30. StartEverDayDo();
  31. }
  32. catch(Exception ex)
  33. {
  34. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "每月统计押金达标数据异常");
  35. }
  36. }
  37. Thread.Sleep(60000);
  38. }
  39. }
  40. public void StartEverDayDo()
  41. {
  42. string Month = DateTime.Now.ToString("yyyyMM");
  43. string chk = function.ReadInstance("/DepositReturn/" + Month + ".txt");
  44. if(!string.IsNullOrEmpty(chk))
  45. {
  46. return;
  47. }
  48. function.WritePage("/DepositReturn/", Month + ".txt", DateTime.Now.ToString());
  49. WebCMSEntities db = new WebCMSEntities();
  50. int startid = int.Parse(function.CheckInt(function.ReadInstance("/DepositReturn/" + Month + "MerchantId.txt")));
  51. bool op = true;
  52. while(op)
  53. {
  54. List<PosMerchantInfo> merchants = db.PosMerchantInfo.Where(m => m.Id > startid && m.ActiveStatus == 1 && m.StandardStatus == 0).OrderBy(m => m.Id).Take(50).ToList();
  55. if(merchants.Count > 0)
  56. {
  57. foreach(PosMerchantInfo merchant in merchants)
  58. {
  59. DoSomething(db, merchant);
  60. startid = merchant.Id;
  61. }
  62. }
  63. else
  64. {
  65. op = false;
  66. }
  67. function.WritePage("/DepositReturn/", Month + "MerchantId.txt", startid.ToString());
  68. }
  69. db.Dispose();
  70. }
  71. public void StartEverTime()
  72. {
  73. Thread th = new Thread(StartEverTimeDo);
  74. th.IsBackground = true;
  75. th.Start();
  76. }
  77. public void StartEverTimeDo()
  78. {
  79. while(true)
  80. {
  81. try
  82. {
  83. string content = RedisDbconn.Instance.RPop<string>("DepositReturnStatQueue");
  84. if(!string.IsNullOrEmpty(content))
  85. {
  86. int MerchantId = int.Parse(function.CheckInt(content));
  87. WebCMSEntities db = new WebCMSEntities();
  88. PosMerchantInfo merchant = db.PosMerchantInfo.FirstOrDefault(m => m.Id == MerchantId);
  89. if(merchant != null)
  90. {
  91. DoSomething(db, merchant);
  92. }
  93. db.Dispose();
  94. }
  95. else
  96. {
  97. Thread.Sleep(60000);
  98. }
  99. }
  100. catch(Exception ex)
  101. {
  102. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "统计单个商户押金达标数据异常");
  103. }
  104. }
  105. }
  106. public void DoSomething(WebCMSEntities db, PosMerchantInfo merchant)
  107. {
  108. function.WriteLog("商户ID:" + merchant.Id, "每月统计押金达标情况");
  109. // string start = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
  110. int StandardMonths = merchant.StandardMonths;
  111. int StandardStatus = merchant.StandardStatus;
  112. function.WriteLog("StandardMonths:" + StandardMonths, "每月统计押金达标情况");
  113. function.WriteLog("StandardStatus:" + StandardStatus, "每月统计押金达标情况");
  114. DateTime ActDate = merchant.CreateDate.Value;
  115. if(merchant.MerStandardDate == null)
  116. {
  117. PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.PosSn == merchant.KqSnNo && m.ActivationState == 1 && m.ActivationTime != null);
  118. if(pos != null)
  119. {
  120. ActDate = pos.ActivationTime.Value;
  121. }
  122. }
  123. else
  124. {
  125. ActDate = merchant.MerStandardDate.Value;
  126. }
  127. int MonthNum = StandardMonths;
  128. for (int i = StandardMonths + 1; i <= 10; i++)
  129. {
  130. string TradeMonth = ActDate.AddMonths(i).ToString("yyyyMM");
  131. if(int.Parse(TradeMonth) < int.Parse(DateTime.Now.ToString("yyyyMM")))
  132. {
  133. decimal TradeAmount = 0;
  134. bool CheckAmount = db.PosMerchantTradeSummay.Any(m => m.MerchantId == merchant.Id && m.TradeMonth == TradeMonth);
  135. if(CheckAmount)
  136. {
  137. TradeAmount = db.PosMerchantTradeSummay.Where(m => m.MerchantId == merchant.Id && m.TradeMonth == TradeMonth).Sum(m => m.TradeAmount);
  138. }
  139. if(TradeAmount >= 10000)
  140. {
  141. MonthNum = i;
  142. }
  143. else
  144. {
  145. StandardStatus = -1;
  146. break;
  147. }
  148. }
  149. }
  150. function.WriteLog("MonthNum:" + MonthNum, "每月统计押金达标情况");
  151. if(MonthNum >= 10)
  152. {
  153. StandardStatus = 4;
  154. }
  155. function.WriteLog("StandardStatus:" + StandardStatus + "\n\n", "每月统计押金达标情况");
  156. PosMerchantInfo edit = db.PosMerchantInfo.FirstOrDefault(m => m.Id == merchant.Id);
  157. if(edit != null)
  158. {
  159. edit.StandardMonths = MonthNum;
  160. edit.StandardStatus = StandardStatus;
  161. db.SaveChanges();
  162. }
  163. }
  164. }
  165. }