OperateService.cs 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 < 9)
  30. {
  31. try
  32. {
  33. string Month = DateTime.Now.ToString("yyyyMM");
  34. string Date = DateTime.Now.ToString("yyyyMMdd");
  35. string check = function.ReadInstance("/Operate/" + Date + ".txt");
  36. if (string.IsNullOrEmpty(check))
  37. {
  38. function.WritePage("/Operate/", "" + Date + ".txt", DateTime.Now.ToString("HH:mm:ss"));
  39. WebCMSEntities db = new WebCMSEntities();
  40. //统计发货量
  41. DataTable dt = CustomerSqlConn.dtable("select CONCAT('update Users set ThisMonthSend=(select count(Id) from StoreStockChange where CreateDate>=\\'" + DateTime.Now.ToString("yyyy-MM") + "-01 00:00:00\\' and TransType in (10,11) and OpId=',Id,') where Id=',Id,';') from Users where UserType=1", MysqlConn.SqlConnStr);
  42. foreach(DataRow dr in dt.Rows)
  43. {
  44. CustomerSqlConn.op(dr[0].ToString(), MysqlConn.SqlConnStr);
  45. }
  46. dt = CustomerSqlConn.dtable("select distinct CONCAT('update Users set ThisMonthSend=(select count(Id) from StoreStockChange where CreateDate>=\\'" + DateTime.Now.ToString("yyyy-MM") + "-01 00:00:00\\' and TransType in (10,11) and StoreId in (select Id from StoreHouse where UserId=',QueryCount,') and OpId=',OpId,') where Id=', QueryCount,';') from StoreForOperate where Sort=0 and Status>-1", MysqlConn.OpSqlConnStr);
  47. foreach(DataRow dr in dt.Rows)
  48. {
  49. CustomerSqlConn.op(dr[0].ToString(), MysqlConn.SqlConnStr);
  50. }
  51. //统计库存
  52. 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);
  53. foreach(int UserId in StoreList.Keys)
  54. {
  55. Users user = db.Users.FirstOrDefault(m => m.Id == UserId);
  56. if(user != null)
  57. {
  58. user.StoreStock += StoreList[UserId];
  59. }
  60. }
  61. db.SaveChanges();
  62. db.Dispose();
  63. }
  64. }
  65. catch (Exception ex)
  66. {
  67. function.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "运营中心重置本月发货量异常");
  68. }
  69. }
  70. Thread.Sleep(800000);
  71. }
  72. }
  73. #endregion
  74. }
  75. }