OperateService.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Linq;
  5. using System.Threading;
  6. using MySystem.PxcModels;
  7. using Library;
  8. namespace MySystem
  9. {
  10. /// <summary>
  11. /// 运营中心定时程序
  12. /// </summary>
  13. public class OperateService
  14. {
  15. public readonly static OperateService Instance = new OperateService();
  16. private OperateService()
  17. { }
  18. #region 每天统计一次数据
  19. public void Start()
  20. {
  21. Thread th = new Thread(doSomething);
  22. th.IsBackground = true;
  23. th.Start();
  24. }
  25. public void doSomething()
  26. {
  27. while (true)
  28. {
  29. if (DateTime.Now.Hour > 0 && DateTime.Now.Hour < 4)
  30. {
  31. try
  32. {
  33. string Month = DateTime.Now.ToString("yyyyMM");
  34. string Date = DateTime.Now.ToString("yyyyMMdd");
  35. string check = function.ReadInstance("/Operate/" + Month + ".txt");
  36. if (string.IsNullOrEmpty(check))
  37. {
  38. function.WritePage("/Operate/", "" + Month + ".txt", DateTime.Now.ToString("HH:mm:ss"));
  39. WebCMSEntities db = new WebCMSEntities();
  40. OpModels.WebCMSEntities opdb = new OpModels.WebCMSEntities();
  41. //统计发货量
  42. Dictionary<int, int> OpData = new Dictionary<int, int>();
  43. if(DateTime.Now.Day == 1)
  44. {
  45. var list = opdb.SendMonthSummary.Select(m => new { m.TradeMonth, m.OpId, m.SendCount }).Where(m => m.TradeMonth == Month).ToList();
  46. foreach(var sub in list)
  47. {
  48. if(OpData.ContainsKey(sub.OpId))
  49. {
  50. OpData[sub.OpId] += sub.SendCount;
  51. }
  52. else
  53. {
  54. OpData.Add(sub.OpId, sub.SendCount);
  55. }
  56. }
  57. foreach(int OpId in OpData.Keys)
  58. {
  59. Users user = db.Users.FirstOrDefault(m => m.Id == OpId);
  60. if(user != null)
  61. {
  62. user.ThisMonthSend = OpData[OpId];
  63. }
  64. }
  65. db.SaveChanges();
  66. }
  67. OpData.Clear();
  68. var datelist = opdb.SendDaySummary.Select(m => new { m.TradeDate, m.OpId, m.SendCount }).Where(m => m.TradeDate == Date).ToList();
  69. foreach(var sub in datelist)
  70. {
  71. if(OpData.ContainsKey(sub.OpId))
  72. {
  73. OpData[sub.OpId] += sub.SendCount;
  74. }
  75. else
  76. {
  77. OpData.Add(sub.OpId, sub.SendCount);
  78. }
  79. }
  80. foreach(int OpId in OpData.Keys)
  81. {
  82. Users user = db.Users.FirstOrDefault(m => m.Id == OpId);
  83. if(user != null)
  84. {
  85. user.ThisMonthSend += OpData[OpId];
  86. }
  87. }
  88. db.SaveChanges();
  89. //统计库存
  90. Dictionary<int, int> StoreList = db.StoreHouse.Select(m => new { m.UserId, m.LaveNum }).Where(m => m.LaveNum > 0).GroupBy(m => m.UserId).Select(m => new { UserId = m.Key, Count = m.Count() }).ToDictionary(m => m.UserId, m => m.Count);
  91. foreach(int UserId in StoreList.Keys)
  92. {
  93. Users user = db.Users.FirstOrDefault(m => m.Id == UserId);
  94. if(user != null)
  95. {
  96. user.StoreStock += StoreList[UserId];
  97. }
  98. }
  99. db.SaveChanges();
  100. opdb.Dispose();
  101. db.Dispose();
  102. }
  103. }
  104. catch (Exception ex)
  105. {
  106. function.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "运营中心重置本月发货量异常");
  107. }
  108. }
  109. Thread.Sleep(800000);
  110. }
  111. }
  112. #endregion
  113. }
  114. }