StoreApplyHelper.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading;
  4. using System.Linq;
  5. using System.Data;
  6. using MySystem;
  7. using MySystem.PxcModels;
  8. using Library;
  9. using LitJson;
  10. public class StoreApplyHelper
  11. {
  12. public readonly static StoreApplyHelper Instance = new StoreApplyHelper();
  13. private StoreApplyHelper()
  14. {
  15. }
  16. public void Start()
  17. {
  18. Thread th = new Thread(DoWorks);
  19. th.IsBackground = true;
  20. th.Start();
  21. }
  22. private void DoWorks()
  23. {
  24. while (true)
  25. {
  26. WebCMSEntities db = new WebCMSEntities();
  27. try
  28. {
  29. if(DateTime.Now.Day == 1 && DateTime.Now.Hour > 0 && DateTime.Now.Hour < 3)
  30. {
  31. string check = function.ReadInstance("/StoreApply/" + DateTime.Now.ToString("yyyyMM") + ".txt");
  32. if(string.IsNullOrEmpty(check))
  33. {
  34. function.WritePage("/StoreApply/", DateTime.Now.ToString("yyyyMM") + ".txt", DateTime.Now.ToString());
  35. // OtherMySqlConn.connstr =
  36. db.SaveChanges();
  37. }
  38. }
  39. }
  40. catch (Exception ex)
  41. {
  42. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "计算分仓申请机具额度异常");
  43. }
  44. db.Dispose();
  45. function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "\n\n", "计算分仓申请机具额度日志");
  46. Thread.Sleep(60000);
  47. }
  48. }
  49. public void StartEverTime()
  50. {
  51. Thread th = new Thread(StartEverTimeDo);
  52. th.IsBackground = true;
  53. th.Start();
  54. }
  55. private void StartEverTimeDo()
  56. {
  57. while (true)
  58. {
  59. WebCMSEntities db = new WebCMSEntities();
  60. try
  61. {
  62. string data = RedisDbconn.Instance.RPop<string>("StoreApplyQueue");
  63. if(!string.IsNullOrEmpty(data))
  64. {
  65. function.WriteLog("data:" + data, "分仓向总仓申请机具日志");
  66. JsonData jsonObj = JsonMapper.ToObject(data);
  67. if(jsonObj["Kind"].ToString() == "1")
  68. {
  69. function.WriteLog("1", "分仓向总仓申请机具日志");
  70. int OrderId = int.Parse(jsonObj["Data"]["OrderId"].ToString());
  71. Orders order = db.Orders.FirstOrDefault(m => m.Id == OrderId);
  72. if(order != null)
  73. {
  74. function.WriteLog("2", "分仓向总仓申请机具日志");
  75. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == order.UserId);
  76. if (account == null)
  77. {
  78. account = db.UserAccount.Add(new UserAccount()
  79. {
  80. Id = order.UserId,
  81. UserId = order.UserId,
  82. }).Entity;
  83. db.SaveChanges();
  84. }
  85. decimal TotalPrice = order.TotalPrice * 2;
  86. decimal BeforeTotalAmount = account.TempAmount; //变更前总金额
  87. account.TempAmount += TotalPrice;
  88. account.ValidAmount += TotalPrice;
  89. decimal AfterTotalAmount = account.TempAmount; //变更后总金额
  90. StoreHouseAmountRecord record = db.StoreHouseAmountRecord.Add(new StoreHouseAmountRecord()
  91. {
  92. CreateDate = DateTime.Now,
  93. UpdateDate = DateTime.Now,
  94. OperateType = 1,
  95. AmountType = 1,
  96. AfterAmount = AfterTotalAmount,
  97. BeforeAmount = BeforeTotalAmount,
  98. UseAmount = TotalPrice,
  99. UserId = order.UserId,
  100. QueryCount = order.Id,
  101. }).Entity;
  102. db.SaveChanges();
  103. }
  104. }
  105. db.SaveChanges();
  106. }
  107. }
  108. catch (Exception ex)
  109. {
  110. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "分仓向总仓申请机具线程异常");
  111. }
  112. db.Dispose();
  113. }
  114. }
  115. }