TxjHelper.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Threading;
  6. using Library;
  7. using LitJson;
  8. using MySystem;
  9. public class TxjHelper
  10. {
  11. public readonly static TxjHelper Instance = new TxjHelper();
  12. private TxjHelper()
  13. { }
  14. public void Start()
  15. {
  16. Thread th = new Thread(DoWorks);
  17. th.IsBackground = true;
  18. th.Start();
  19. }
  20. public void DoWorks()
  21. {
  22. while (true)
  23. {
  24. string month = RedisDbconn.Instance.RPop<string>("TxjQueue");
  25. if (!string.IsNullOrEmpty(month))
  26. {
  27. try
  28. {
  29. DoQueue(month);
  30. }
  31. catch (Exception ex)
  32. {
  33. Utils.WriteLog(DateTime.Now.ToString() + "\n" + month + "\n" + ex, "同行奖励异常");
  34. }
  35. }
  36. else
  37. {
  38. Thread.Sleep(5000);
  39. }
  40. }
  41. }
  42. public void DoQueue(string month)
  43. {
  44. DateTime start = DateTime.Parse(month + "-01 00:00:00");
  45. DateTime end = start.AddMonths(1);
  46. Dictionary<string, string> txjList = TxjList();
  47. MySystem.JavaProductModels.WebCMSEntities dbpro = new MySystem.JavaProductModels.WebCMSEntities();
  48. MySystem.JavaUserModels.WebCMSEntities dbuser = new MySystem.JavaUserModels.WebCMSEntities();
  49. IQueryable<MySystem.JavaProductModels.KxsMachineAward> dellist = dbpro.KxsMachineAward.Where(m => m.VariationType == 30 && m.CreateTime >= start && m.CreateTime < end);
  50. foreach(MySystem.JavaProductModels.KxsMachineAward sub in dellist)
  51. {
  52. dbpro.KxsMachineAward.Remove(sub);
  53. }
  54. dbpro.SaveChanges();
  55. List<MySystem.JavaProductModels.KxsMachineAward> list = dbpro.KxsMachineAward.Where(m => m.VariationType == 118 && m.CreateTime >= start && m.CreateTime < end).ToList();
  56. foreach(MySystem.JavaProductModels.KxsMachineAward sub in list)
  57. {
  58. MySystem.JavaProductModels.KxsShopOrder order = dbpro.KxsShopOrder.FirstOrDefault(m => m.OrderSn == sub.PosSn && m.ReturnStatus == 0);
  59. if(order != null)
  60. {
  61. int UserId = order.UserId;
  62. MySystem.JavaUserModels.KxsUser user = dbuser.KxsUser.FirstOrDefault(m => m.Id == UserId);
  63. if(user != null)
  64. {
  65. string ParentNav = user.PidPath;
  66. string[] ParentList = ParentNav.TrimEnd(',').Split(',');
  67. Array.Reverse(ParentList);
  68. foreach(string ParentId in ParentList)
  69. {
  70. if(txjList.ContainsKey(ParentId))
  71. {
  72. MySystem.JavaProductModels.KxsShopOrderInfo orderInfo = dbpro.KxsShopOrderInfo.FirstOrDefault(m => m.OrderId == order.Id) ?? new MySystem.JavaProductModels.KxsShopOrderInfo();
  73. string[] userData = txjList[ParentId].Split('|');
  74. dbpro.KxsMachineAward.Add(new MySystem.JavaProductModels.KxsMachineAward()
  75. {
  76. Amount = orderInfo.Number * 60,
  77. VariationType = 30,
  78. Username = userData[1],
  79. UserCode = userData[0],
  80. UserId = int.Parse(ParentId),
  81. MachineId = sub.MachineId,
  82. PosSn = sub.PosSn,
  83. CreateTime = DateTime.Parse(month + "-01 00:00:00"),
  84. AwardType = 1,
  85. });
  86. dbpro.SaveChanges();
  87. break;
  88. }
  89. }
  90. }
  91. }
  92. }
  93. dbpro.Dispose();
  94. dbuser.Dispose();
  95. }
  96. private Dictionary<string, string> TxjList()
  97. {
  98. Dictionary<string, string> dic = new Dictionary<string, string>();
  99. string[] list = function.ReadInstance("/TxjList.txt").Split('\n');
  100. foreach(string sub in list)
  101. {
  102. string[] data = sub.Split('|');
  103. dic.Add(data[0], data[1] + "|" + data[2]);
  104. }
  105. return dic;
  106. }
  107. }