LogoutUserHelper.cs 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 LogoutUserHelper
  11. {
  12. public readonly static LogoutUserHelper Instance = new LogoutUserHelper();
  13. private LogoutUserHelper()
  14. {
  15. }
  16. public void Start()
  17. {
  18. Thread th = new Thread(DoWorks);
  19. th.IsBackground = true;
  20. th.Start();
  21. }
  22. // 判断已注销的创客在每月1号,分离创客表之后,伞下自动归到注销创客的上级
  23. public void DoWorks()
  24. {
  25. function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), "每月归档注销创客日志");
  26. WebCMSEntities db = new WebCMSEntities();
  27. OtherMySqlConn.connstr = Library.ConfigurationManager.AppSettings["Pxc1SqlConnStr"].ToString();
  28. try
  29. {
  30. string Month = DateTime.Now.ToString("yyyyMM");
  31. string check = function.ReadInstance("/LogoutUser/" + Month + ".txt");
  32. if(string.IsNullOrEmpty(check))
  33. {
  34. function.WritePage("/LogoutUser/", Month + ".txt", DateTime.Now.ToString());
  35. var users = db.Users.Select(m => new { m.Id, m.ParentUserId, m.ParentNav, m.Status }).Where(m => m.Status == -1).ToList();
  36. foreach(var user in users)
  37. {
  38. function.WriteLog("创客Id:" + user.Id + ",上级:" + user.ParentNav, "每月归档注销创客日志");
  39. int ParentUserId = user.ParentUserId;
  40. bool op = true;
  41. while(op)
  42. {
  43. function.WriteLog("上级创客Id:" + user.ParentUserId, "每月归档注销创客日志");
  44. Users puser = db.Users.FirstOrDefault(m => m.Id == ParentUserId);
  45. if(puser != null)
  46. {
  47. if(puser.Status > -1)
  48. {
  49. op = false;
  50. RelationForUserSub(db, user.Id, puser);
  51. db.SaveChanges();
  52. }
  53. else
  54. {
  55. ParentUserId = puser.ParentUserId;
  56. }
  57. }
  58. else
  59. {
  60. op = false;
  61. }
  62. }
  63. }
  64. }
  65. }
  66. catch (Exception ex)
  67. {
  68. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "每月归档注销创客异常");
  69. }
  70. db.Dispose();
  71. function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "\n\n", "每月归档注销创客日志");
  72. }
  73. private void RelationForUserSub(WebCMSEntities db, int ParentUserId, Users puser)
  74. {
  75. var subusers = db.Users.Select(m => new { m.Id, m.ParentUserId, m.ParentNav }).Where(m => m.ParentUserId == ParentUserId).ToList();
  76. foreach (var subuser in subusers)
  77. {
  78. Users user = db.Users.FirstOrDefault(m => m.Id == subuser.Id);
  79. if (user != null)
  80. {
  81. user.ParentUserId = puser.Id;
  82. user.ParentNav = puser.ParentNav + "," + puser.Id + ",";
  83. RelationForUserSub(db, user.Id, user);
  84. }
  85. }
  86. }
  87. }