ToBigHelper.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 ToBigHelper
  11. {
  12. public readonly static ToBigHelper Instance = new ToBigHelper();
  13. private ToBigHelper()
  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>("ToBigQueue");
  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 SnNos = data["SnNos"].ToString(); //券列表
  51. Dictionary<string, object> Obj = new Dictionary<string, object>();
  52. List<string> Codes = SnNos.Split(',').ToList();
  53. List<int> LeaderUserIds = new List<int>();
  54. int index = 0;
  55. string ToCodes = "";
  56. List<PosMachinesTwo> list = db.PosMachinesTwo.Where(m => Codes.Contains(m.PosSn) && m.BuyUserId == UserId && m.IsPurchase == 0 && (m.ActivationState == 1 || (!string.IsNullOrEmpty(m.SeoKeyword) && m.SeoKeyword != "0"))).OrderBy(m => m.RecycEndDate).ToList();
  57. foreach(PosMachinesTwo sub in list)
  58. {
  59. index += 1;
  60. PosMachinesTwo edit = db.PosMachinesTwo.FirstOrDefault(m => m.Id == sub.Id);
  61. if(edit != null)
  62. {
  63. if(index % 2 == 0)
  64. {
  65. edit.OpId = 2;
  66. ToCodes += sub.PosSn + ",";
  67. }
  68. else
  69. {
  70. edit.IsPurchase = 1;
  71. }
  72. }
  73. }
  74. db.SaveChanges();
  75. int Count = Codes.Count / 2;
  76. db.PosCouponExchangeRecord.Add(new PosCouponExchangeRecord()
  77. {
  78. CreateDate = DateTime.Now,
  79. Note = "通兑资格",
  80. ToCount = Count,
  81. SourceCount = Codes.Count,
  82. ToCoupons = ToCodes.TrimEnd(','),
  83. SourceCoupons = SnNos,
  84. UserId = UserId,
  85. QueryCount = 1,
  86. });
  87. db.SaveChanges();
  88. }
  89. }