SycnJavaUsersService.cs 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using MySystem.PxcModels;
  5. using Library;
  6. using LitJson;
  7. using System.Threading;
  8. namespace MySystem
  9. {
  10. public class SycnJavaUsersService
  11. {
  12. public readonly static SycnJavaUsersService Instance = new SycnJavaUsersService();
  13. private SycnJavaUsersService()
  14. { }
  15. public bool Add(string content)
  16. {
  17. try
  18. {
  19. JsonData jsonObj = JsonMapper.ToObject(content);
  20. WebCMSEntities db = new WebCMSEntities();
  21. int id = int.Parse(jsonObj["id"].ToString());
  22. string pidPath = jsonObj["pidPath"].ToString();
  23. pidPath = "," + pidPath.Substring(1).Replace(",",",,").Trim(',') + ",";
  24. int pid = int.Parse(jsonObj["pid"].ToString());
  25. DateTime CreateDate = DateTime.Parse(jsonObj["createTime"].ToString());
  26. string MakerCode = jsonObj["userCode"].ToString();
  27. string Mobile = jsonObj["phone"].ToString();
  28. string RealName = "";
  29. string HeadPhoto = "";
  30. if(content.Contains("\"username\""))
  31. {
  32. RealName = jsonObj["username"].ToString();
  33. }
  34. if(content.Contains("\"avatar\""))
  35. {
  36. HeadPhoto = jsonObj["avatar"].ToString();
  37. }
  38. Users edit = db.Users.FirstOrDefault(m => m.Mobile == Mobile);
  39. if(edit != null)
  40. {
  41. db.Users.Remove(edit);
  42. }
  43. UserForMobile forMobile = db.UserForMobile.FirstOrDefault(m => m.Mobile == Mobile);
  44. if(forMobile != null)
  45. {
  46. db.UserForMobile.Remove(forMobile);
  47. }
  48. UserForMakerCode forMakerCode = db.UserForMakerCode.FirstOrDefault(m => m.MakerCode == MakerCode);
  49. if(forMakerCode != null)
  50. {
  51. db.UserForMakerCode.Remove(forMakerCode);
  52. }
  53. db.SaveChanges();
  54. Users add = db.Users.Add(new Users()
  55. {
  56. Id = id,
  57. ParentUserId = pid,
  58. ParentNav = pidPath,
  59. CreateDate = CreateDate,
  60. MakerCode = MakerCode,
  61. Mobile = Mobile,
  62. RealName = RealName,
  63. HeadPhoto = HeadPhoto,
  64. }).Entity;
  65. db.SaveChanges();
  66. db.UserForMobile.Add(new UserForMobile()
  67. {
  68. Mobile = Mobile,
  69. UserId = add.Id
  70. });
  71. db.UserForMakerCode.Add(new UserForMakerCode()
  72. {
  73. MakerCode = MakerCode,
  74. UserId = add.Id
  75. });
  76. db.SaveChanges();
  77. db.Dispose();
  78. return true;
  79. }
  80. catch (Exception ex)
  81. {
  82. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "同步创客信息异常");
  83. return false;
  84. }
  85. }
  86. }
  87. }