using System; using System.Collections.Generic; using System.Linq; using MySystem.PxcModels; using Library; using LitJson; using System.Threading; namespace MySystem { public class SycnJavaUsersService { public readonly static SycnJavaUsersService Instance = new SycnJavaUsersService(); private SycnJavaUsersService() { } public bool Add(string content) { try { JsonData jsonObj = JsonMapper.ToObject(content); WebCMSEntities db = new WebCMSEntities(); int id = int.Parse(jsonObj["id"].ToString()); string pidPath = jsonObj["pidPath"].ToString(); pidPath = "," + pidPath.Substring(1).Replace(",",",,").Trim(',') + ","; int pid = int.Parse(jsonObj["pid"].ToString()); DateTime CreateDate = DateTime.Parse(jsonObj["createTime"].ToString()); string MakerCode = jsonObj["userCode"].ToString(); string Mobile = jsonObj["phone"].ToString(); string RealName = ""; string HeadPhoto = ""; if(content.Contains("\"username\"")) { RealName = jsonObj["username"].ToString(); } if(content.Contains("\"avatar\"")) { HeadPhoto = jsonObj["avatar"].ToString(); } Users edit = db.Users.FirstOrDefault(m => m.Mobile == Mobile); if(edit != null) { db.Users.Remove(edit); } UserForMobile forMobile = db.UserForMobile.FirstOrDefault(m => m.Mobile == Mobile); if(forMobile != null) { db.UserForMobile.Remove(forMobile); } UserForMakerCode forMakerCode = db.UserForMakerCode.FirstOrDefault(m => m.MakerCode == MakerCode); if(forMakerCode != null) { db.UserForMakerCode.Remove(forMakerCode); } db.SaveChanges(); Users add = db.Users.Add(new Users() { Id = id, ParentUserId = pid, ParentNav = pidPath, CreateDate = CreateDate, MakerCode = MakerCode, Mobile = Mobile, RealName = RealName, HeadPhoto = HeadPhoto, }).Entity; db.SaveChanges(); db.UserForMobile.Add(new UserForMobile() { Mobile = Mobile, UserId = add.Id }); db.UserForMakerCode.Add(new UserForMakerCode() { MakerCode = MakerCode, UserId = add.Id }); db.SaveChanges(); db.Dispose(); return true; } catch (Exception ex) { Utils.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "同步创客信息异常"); return false; } } } }