SycnUserMachineCountHelper.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using System;
  2. using System.Linq;
  3. using System.Threading;
  4. using MySystem.Models;
  5. using LitJson;
  6. namespace MySystem
  7. {
  8. public class SycnUserMachineCountHelper
  9. {
  10. public readonly static SycnUserMachineCountHelper Instance = new SycnUserMachineCountHelper();
  11. private SycnUserMachineCountHelper()
  12. {
  13. }
  14. public void Start()//启动
  15. {
  16. Thread thread = new Thread(StartDo);
  17. thread.IsBackground = true;
  18. thread.Start();
  19. }
  20. public void StartDo()
  21. {
  22. while (true)
  23. {
  24. string data = RedisDbconn.Instance.RPop<string>("SycnUserMachineCountQueue");
  25. if (!string.IsNullOrEmpty(data))
  26. {
  27. JsonData json = JsonMapper.ToObject(data);
  28. int UserId = int.Parse(json["UserId"].ToString());
  29. int BrandId = int.Parse(json["BrandId"].ToString());
  30. WebCMSEntities dbnew = new WebCMSEntities();
  31. string IdBrand = UserId + "_" + BrandId;
  32. UserMachineData machineData = dbnew.UserMachineData.FirstOrDefault(m => m.IdBrand == IdBrand);
  33. if (machineData == null)
  34. {
  35. machineData = dbnew.UserMachineData.Add(new UserMachineData()
  36. {
  37. IdBrand = IdBrand
  38. }).Entity;
  39. dbnew.SaveChanges();
  40. }
  41. machineData.BindCount = dbnew.PosMachinesTwo.Count(m => m.Status > -1 && m.BuyUserId == UserId && m.BrandId == BrandId && m.BindingState == 1);
  42. machineData.UnBindCount = dbnew.PosMachinesTwo.Count(m => m.Status > -1 && m.BuyUserId == UserId && m.BrandId == BrandId && m.BindingState == 0);
  43. machineData.TotalMachineCount = machineData.BindCount + machineData.UnBindCount;
  44. dbnew.SaveChanges();
  45. dbnew.Dispose();
  46. }
  47. else
  48. {
  49. Thread.Sleep(10000);
  50. }
  51. }
  52. }
  53. }
  54. }