SIMActCountService.cs 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Linq;
  5. using System.Threading;
  6. using MySystem.Models;
  7. using Library;
  8. using LitJson;
  9. namespace MySystem
  10. {
  11. public class SIMActCountService
  12. {
  13. public readonly static SIMActCountService Instance = new SIMActCountService();
  14. private SIMActCountService()
  15. { }
  16. public void Start()
  17. {
  18. Thread th = new Thread(doSomething);
  19. th.IsBackground = true;
  20. th.Start();
  21. }
  22. public void doSomething()
  23. {
  24. while (true)
  25. {
  26. string content = RedisDbconn.Instance.RPop<string>("SIMActCountQueue");
  27. if (!string.IsNullOrEmpty(content))
  28. {
  29. try
  30. {
  31. JsonData jsonObj = JsonMapper.ToObject(content);
  32. int UserId = int.Parse(jsonObj["UserId"].ToString());
  33. int ActCount = int.Parse(jsonObj["ActCount"].ToString());
  34. WebCMSEntities db = new WebCMSEntities();
  35. Users user = db.Users.FirstOrDefault(m => m.Id == UserId) ?? new Users();
  36. string ParentNav = user.ParentNav;
  37. UserSimActSummary selfStat = db.UserSimActSummary.FirstOrDefault(m => m.UserId == UserId && m.Kind == 0);
  38. if (selfStat == null)
  39. {
  40. selfStat = db.UserSimActSummary.Add(new UserSimActSummary()
  41. {
  42. UserId = UserId
  43. }).Entity;
  44. db.SaveChanges();
  45. }
  46. selfStat.ActCount += ActCount;
  47. ParentNav += "," + UserId + ",";
  48. if (!string.IsNullOrEmpty(ParentNav))
  49. {
  50. string[] ParentNavList = ParentNav.Trim(',').Replace(",,", ",").Split(',');
  51. foreach (string NavUserIdString in ParentNavList)
  52. {
  53. int NavUserId = int.Parse(NavUserIdString);
  54. UserSimActSummary teamStat = db.UserSimActSummary.FirstOrDefault(m => m.UserId == NavUserId && m.Kind == 1);
  55. if (teamStat == null)
  56. {
  57. teamStat = db.UserSimActSummary.Add(new UserSimActSummary()
  58. {
  59. UserId = NavUserId,
  60. Kind = 1,
  61. }).Entity;
  62. db.SaveChanges();
  63. }
  64. teamStat.ActCount += ActCount;
  65. }
  66. }
  67. db.SaveChanges();
  68. db.Dispose();
  69. }
  70. catch (Exception ex)
  71. {
  72. function.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "广电卡激活数变更异常");
  73. }
  74. }
  75. else
  76. {
  77. Thread.Sleep(60000);
  78. }
  79. }
  80. }
  81. }
  82. }