PosCouponExchangeHelper.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 PosCouponExchangeHelper
  11. {
  12. public readonly static PosCouponExchangeHelper Instance = new PosCouponExchangeHelper();
  13. private PosCouponExchangeHelper()
  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. try
  27. {
  28. string content = RedisDbconn.Instance.RPop<string>("PosCouponExchangeQueue");
  29. if(!string.IsNullOrEmpty(content))
  30. {
  31. WebCMSEntities db = new WebCMSEntities();
  32. DoSomething(db, content);
  33. db.Dispose();
  34. }
  35. else
  36. {
  37. Thread.Sleep(2000);
  38. }
  39. }
  40. catch (Exception ex)
  41. {
  42. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "电签券对大机券异常");
  43. }
  44. }
  45. }
  46. public void DoSomething(WebCMSEntities db, string content)
  47. {
  48. JsonData data = JsonMapper.ToObject(content);
  49. int UserId = int.Parse(function.CheckInt(data["UserId"].ToString())); //创客Id
  50. string CouponCodes = data["CouponCodes"].ToString(); //券列表
  51. Dictionary<string, object> Obj = new Dictionary<string, object>();
  52. List<string> Codes = CouponCodes.Split(',').ToList();
  53. List<int> LeaderUserIds = new List<int>();
  54. int BeforeUserId = 0;
  55. int AfterUserId = 0;
  56. int index = 0;
  57. IQueryable<PosCoupons> list = db.PosCoupons.Where(m => Codes.Contains(m.ExchangeCode) && m.UserId == UserId && m.IsLock == 0 && m.IsUse == 0).OrderBy(m => m.CreateDate);
  58. foreach(PosCoupons sub in list)
  59. {
  60. index += 1;
  61. if(index % 2 == 1) BeforeUserId = sub.LeaderUserId;
  62. if(index % 2 == 0)
  63. {
  64. AfterUserId = sub.LeaderUserId;
  65. if(BeforeUserId > 0 && AfterUserId > 0) LeaderUserIds.Add(AfterUserId);
  66. if(BeforeUserId == 0 && AfterUserId > 0) LeaderUserIds.Add(AfterUserId);
  67. if(BeforeUserId > 0 && AfterUserId == 0) LeaderUserIds.Add(BeforeUserId);
  68. if(BeforeUserId == 0 && AfterUserId == 0) LeaderUserIds.Add(0);
  69. BeforeUserId = 0;
  70. AfterUserId = 0;
  71. }
  72. sub.UserId = 0;
  73. }
  74. db.SaveChanges();
  75. int Count = Codes.Count / 2;
  76. index = 0;
  77. string ToCodes = "";
  78. IQueryable<PosCoupons> chklist = db.PosCoupons.Where(m => m.QueryCount == 2 && m.UserId == 0 && m.IsLock == 0 && m.IsUse == 0).Take(Count);
  79. foreach(PosCoupons sub in chklist)
  80. {
  81. sub.UserId = UserId;
  82. sub.LeaderUserId = LeaderUserIds[index];
  83. ToCodes += sub.ExchangeCode + ",";
  84. index += 1;
  85. }
  86. db.PosCouponExchangeRecord.Add(new PosCouponExchangeRecord()
  87. {
  88. CreateDate = DateTime.Now,
  89. Note = "兑换大机",
  90. ToCount = Count,
  91. SourceCount = Codes.Count,
  92. ToCoupons = ToCodes.TrimEnd(','),
  93. SourceCoupons = CouponCodes,
  94. UserId = UserId,
  95. });
  96. db.SaveChanges();
  97. }
  98. }