MerchantStandardService.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Data;
  5. using System.Threading;
  6. using MySystem.Models;
  7. using Library;
  8. using LitJson;
  9. namespace MySystem
  10. {
  11. public class MerchantStandardService
  12. {
  13. public readonly static MerchantStandardService Instance = new MerchantStandardService();
  14. private MerchantStandardService()
  15. { }
  16. public void Start()
  17. {
  18. Thread th = new Thread(Listen);
  19. th.IsBackground = true;
  20. th.Start();
  21. }
  22. private void Listen()
  23. {
  24. while (true)
  25. {
  26. try
  27. {
  28. DoSomething();
  29. }
  30. catch(Exception ex)
  31. {
  32. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "商户达标奖异常");
  33. }
  34. Thread.Sleep(600000);
  35. }
  36. }
  37. public void DoSomething()
  38. {
  39. WebCMSEntities db = new WebCMSEntities();
  40. DateTime start = DateTime.Parse(DateTime.Now.AddMonths(-1).ToString("yyyy-MM") + "-01 00:00:00");
  41. DateTime end = DateTime.Parse(DateTime.Now.ToString("yyyy-MM") + "-01 00:00:00");
  42. string month = DateTime.Now.ToString("yyyyMM");
  43. function.WriteLog(DateTime.Now.ToString() + "-start", "达标奖励日志");
  44. var orders = db.MerchantDepositOrder.Where(m => m.Status > 0 && m.Version == 0 && m.UpdateDate >= start && m.UpdateDate < end).ToList();
  45. foreach(var order in orders)
  46. {
  47. function.WriteLog("MerchantId:" + order.MerchantId, "达标奖励日志");
  48. decimal TradeMoney = 0;
  49. bool has = db.MerchantAmountSummay.Any(m => m.MerchantId == order.MerchantId && m.IsAct == 1 && m.TradeMonth == month);
  50. if(has)
  51. {
  52. TradeMoney = db.MerchantAmountSummay.Where(m => m.MerchantId == order.MerchantId && m.IsAct == 1 && m.TradeMonth == month).Sum(m => m.TradeAmount);
  53. }
  54. function.WriteLog("TradeMoney:" + TradeMoney, "达标奖励日志");
  55. if(TradeMoney >= 10000)
  56. {
  57. RedisDbconn.Instance.AddList("StandardProfitQueue", "{\"MerchantId\":\"" + order.MerchantId + "\",\"Prize\":\"50\"}");
  58. MerchantDepositOrder edit = db.MerchantDepositOrder.FirstOrDefault(m => m.Id == order.Id);
  59. if(edit != null)
  60. {
  61. edit.Version = 1;
  62. }
  63. }
  64. db.SaveChanges();
  65. }
  66. function.WriteLog(DateTime.Now.ToString() + "-end", "达标奖励日志");
  67. db.Dispose();
  68. }
  69. public void StartThree()
  70. {
  71. Thread th = new Thread(ListenThree);
  72. th.IsBackground = true;
  73. th.Start();
  74. }
  75. private void ListenThree()
  76. {
  77. while (true)
  78. {
  79. try
  80. {
  81. DoSomethingThree();
  82. }
  83. catch(Exception ex)
  84. {
  85. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "商户连续3月达标奖异常");
  86. }
  87. Thread.Sleep(605000);
  88. }
  89. }
  90. public void DoSomethingThree()
  91. {
  92. WebCMSEntities db = new WebCMSEntities();
  93. DateTime start = DateTime.Parse(DateTime.Now.AddMonths(-3).ToString("yyyy-MM") + "-01 00:00:00");
  94. DateTime end = DateTime.Parse(DateTime.Now.AddMonths(-2).ToString("yyyy-MM") + "-01 00:00:00");
  95. string month = DateTime.Now.AddMonths(-2).ToString("yyyyMM");
  96. string month2 = DateTime.Now.AddMonths(-1).ToString("yyyyMM");
  97. string month3 = DateTime.Now.ToString("yyyyMM");
  98. var orders = db.MerchantDepositOrder.Where(m => m.Status > 0 && m.Version == 1 && m.UpdateDate >= start && m.UpdateDate < end).ToList();
  99. foreach(var order in orders)
  100. {
  101. decimal TradeMoney = 0, TradeMoney2 = 0, TradeMoney3 = 0;
  102. bool has = db.MerchantAmountSummay.Any(m => m.MerchantId == order.MerchantId && m.IsAct == 1 && m.TradeMonth == month);
  103. if(has)
  104. {
  105. TradeMoney = db.MerchantAmountSummay.Where(m => m.MerchantId == order.MerchantId && m.IsAct == 1 && m.TradeMonth == month).Sum(m => m.TradeAmount);
  106. }
  107. has = db.MerchantAmountSummay.Any(m => m.MerchantId == order.MerchantId && m.IsAct == 1 && m.TradeMonth == month2);
  108. if(has)
  109. {
  110. TradeMoney2 = db.MerchantAmountSummay.Where(m => m.MerchantId == order.MerchantId && m.IsAct == 1 && m.TradeMonth == month2).Sum(m => m.TradeAmount);
  111. }
  112. has = db.MerchantAmountSummay.Any(m => m.MerchantId == order.MerchantId && m.IsAct == 1 && m.TradeMonth == month3);
  113. if(has)
  114. {
  115. TradeMoney3 = db.MerchantAmountSummay.Where(m => m.MerchantId == order.MerchantId && m.IsAct == 1 && m.TradeMonth == month3).Sum(m => m.TradeAmount);
  116. }
  117. if(TradeMoney >= 10000 && TradeMoney2 >= 10000 && TradeMoney3 >= 10000)
  118. {
  119. RedisDbconn.Instance.AddList("StandardProfitQueue", "{\"MerchantId\":\"" + order.MerchantId + "\",\"Prize\":\"100\"}");
  120. MerchantDepositOrder edit = db.MerchantDepositOrder.FirstOrDefault(m => m.Id == order.Id);
  121. if(edit != null)
  122. {
  123. edit.Version = 2;
  124. }
  125. }
  126. db.SaveChanges();
  127. }
  128. db.Dispose();
  129. }
  130. #region 达标奖励队列
  131. public void StartActive()
  132. {
  133. Thread th = new Thread(StartListenActive);
  134. th.IsBackground = true;
  135. th.Start();
  136. }
  137. public void StartListenActive()
  138. {
  139. while (true)
  140. {
  141. string content = RedisDbconn.Instance.RPop<string>("StandardProfitQueue");
  142. if (!string.IsNullOrEmpty(content))
  143. {
  144. StartListenActiveDo(content);
  145. }
  146. else
  147. {
  148. Thread.Sleep(60000);
  149. }
  150. }
  151. }
  152. public void StartListenActiveDo(string content)
  153. {
  154. try
  155. {
  156. JsonData jsonObj = JsonMapper.ToObject(content);
  157. int MerchantId = int.Parse(jsonObj["MerchantId"].ToString());
  158. decimal Prize = decimal.Parse(jsonObj["Prize"].ToString());
  159. KxsMainModels.WebCMSEntities kxsdb = new KxsMainModels.WebCMSEntities();
  160. WebCMSEntities db = new WebCMSEntities();
  161. MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == MerchantId) ?? new MerchantInfo();
  162. string IdBrand = merchant.UserId + "_0";
  163. UserMachineData userData = db.UserMachineData.FirstOrDefault(m => m.IdBrand == IdBrand);
  164. if (userData == null)
  165. {
  166. userData = db.UserMachineData.Add(new UserMachineData()
  167. {
  168. IdBrand = IdBrand,
  169. }).Entity;
  170. db.SaveChanges();
  171. }
  172. userData.OtherProfit += Prize;
  173. db.SaveChanges();
  174. int GetUserId = merchant.UserId;
  175. KxsMainModels.UserAccount account = kxsdb.UserAccount.FirstOrDefault(m => m.Id == GetUserId);
  176. if (account == null)
  177. {
  178. account = kxsdb.UserAccount.Add(new KxsMainModels.UserAccount()
  179. {
  180. Id = GetUserId,
  181. UserId = GetUserId,
  182. }).Entity;
  183. kxsdb.SaveChanges();
  184. }
  185. decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
  186. decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
  187. decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
  188. account.BalanceAmount += Prize;
  189. account.TotalAmount += Prize;
  190. decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
  191. decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
  192. decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
  193. KxsMainModels.UserAccountRecord userAccountRecord = kxsdb.UserAccountRecord.Add(new KxsMainModels.UserAccountRecord()
  194. {
  195. CreateDate = DateTime.Now,
  196. UpdateDate = DateTime.Now,
  197. UserId = GetUserId, //创客
  198. ChangeType = 315, //变动类型
  199. ChangeAmount = Prize, //变更金额
  200. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  201. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  202. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  203. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  204. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  205. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  206. }).Entity;
  207. kxsdb.SaveChanges();
  208. kxsdb.Dispose();
  209. db.Dispose();
  210. }
  211. catch (Exception ex)
  212. {
  213. LogHelper.Instance.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + ":" + ex.ToString(), "达标奖励服务异常");
  214. }
  215. }
  216. #endregion
  217. }
  218. }