LogoutUserHelper.cs 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. DateTime start = DateTime.Parse(DateTime.Now.AddMonths(-1).ToString("yyyy-MM") + "-01 00:00:00");
  36. var users = db.Users.Select(m => new { m.Id, m.ParentUserId, m.ParentNav, m.Status, m.LogOutDate }).Where(m => m.Status == -1 && m.LogOutDate >= start).ToList();
  37. foreach(var user in users)
  38. {
  39. function.WriteLog("创客Id:" + user.Id + ",上级:" + user.ParentNav, "每月归档注销创客日志");
  40. int ParentUserId = user.ParentUserId;
  41. bool op = true;
  42. while(op)
  43. {
  44. function.WriteLog("上级创客Id:" + user.ParentUserId, "每月归档注销创客日志");
  45. Users puser = db.Users.FirstOrDefault(m => m.Id == ParentUserId);
  46. if(puser != null)
  47. {
  48. if(puser.Status > -1)
  49. {
  50. op = false;
  51. RelationForUserSub(db, user.Id, puser);
  52. db.SaveChanges();
  53. CustomerSqlConn.op("update PosMachinesTwo set BuyUserId=" + puser.Id + " where BuyUserId=" + user.Id, MysqlConn.SqlConnStr);
  54. CustomerSqlConn.op("update PosMachinesTwo set UserId=" + puser.Id + " where UserId=" + user.Id + " and BuyUserId=UserId", MysqlConn.SqlConnStr);
  55. CustomerSqlConn.op("update PosMerchantInfo set UserId=" + puser.Id + " where UserId=" + user.Id + " and MerUserType=0", MysqlConn.SqlConnStr);
  56. }
  57. else
  58. {
  59. ParentUserId = puser.ParentUserId;
  60. }
  61. }
  62. else
  63. {
  64. op = false;
  65. }
  66. }
  67. }
  68. }
  69. }
  70. catch (Exception ex)
  71. {
  72. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "每月归档注销创客异常");
  73. }
  74. db.Dispose();
  75. function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "\n\n", "每月归档注销创客日志");
  76. }
  77. private void RelationForUserSub(WebCMSEntities db, int ParentUserId, Users puser)
  78. {
  79. var subusers = db.Users.Select(m => new { m.Id, m.ParentUserId, m.ParentNav }).Where(m => m.ParentUserId == ParentUserId).ToList();
  80. foreach (var subuser in subusers)
  81. {
  82. Users user = db.Users.FirstOrDefault(m => m.Id == subuser.Id);
  83. if (user != null)
  84. {
  85. user.ParentUserId = puser.Id;
  86. user.ParentNav = puser.ParentNav + "," + puser.Id + ",";
  87. RelationForUserSub(db, user.Id, user);
  88. }
  89. }
  90. }
  91. }