ToBigHelper.cs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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).OrderBy(m => m.RecycEndDate).ToList();
  57. foreach(PosMachinesTwo sub in list)
  58. {
  59. index += 1;
  60. if(index % 2 == 0)
  61. {
  62. PosMachinesTwo edit = db.PosMachinesTwo.FirstOrDefault(m => m.Id == sub.Id);
  63. if(edit != null)
  64. {
  65. edit.OpId = 2;
  66. ToCodes += sub.PosSn + ",";
  67. }
  68. }
  69. }
  70. db.SaveChanges();
  71. int Count = Codes.Count / 2;
  72. db.PosCouponExchangeRecord.Add(new PosCouponExchangeRecord()
  73. {
  74. CreateDate = DateTime.Now,
  75. Note = "通兑资格",
  76. ToCount = Count,
  77. SourceCount = Codes.Count,
  78. ToCoupons = ToCodes.TrimEnd(','),
  79. SourceCoupons = SnNos,
  80. UserId = UserId,
  81. QueryCount = 1,
  82. });
  83. db.SaveChanges();
  84. }
  85. }