/// <summary> /// 同步单个部门 /// </summary> /// <param name="dingId"></param> /// <returns></returns> private static GuYuanDepartment SyncDepartment(long dingId) { var db = new Context(); var corpClient = GetCorpClient(); var dep = corpClient.GetDepartment(dingId); var dbData = db.GuYuanDepartment.FirstOrDefault(n => n.DingId == dep.id); if (dbData == null) { dbData = new GuYuanDepartment { createdate = DateTime.Now, DingId = dep.id, DingParentId = dep.parentid, DepartmentName = dep.name, OptId = 0, OptName = "钉钉同步" }; db.GuYuanDepartment.Add(dbData); } else { dbData.DepartmentName = dep.name; dbData.OptName = "钉钉同步"; } db.SaveChanges(); return(dbData); }
/// <summary> /// 设置model,如果不为空就设置,如果为空则和之前一样 /// </summary> /// <param name="target"></param> /// <param name="source"></param> /// <returns></returns> public GuYuanDepartment SetGuYuanDepartment(GuYuanDepartment model, GuYuanDepartmentReq data) { if (!string.IsNullOrEmpty(data.DepartmentName)) { model.DepartmentName = data.DepartmentName; } if (data.projectid != null) { model.ProjectId = data.projectid.Value; } if (!string.IsNullOrEmpty(data.ProjectName)) { model.ProjectName = data.ProjectName; } if (!string.IsNullOrEmpty(data.State)) { model.State = data.State; } if (!string.IsNullOrEmpty(data.OptName)) { model.OptName = data.OptName; } if (data.OptId != null) { model.OptId = data.OptId.Value; } if (data.createdate != null && data.createdate != DateTime.MinValue && data.createdate != SqlDateTime.MinValue.Value) { model.createdate = data.createdate.Value; } return(model); }
/// <summary> /// 同步部门信息 /// </summary> /// <returns></returns> public int SyncDepartment() { var corpClient = GetCorpClient(); var allDep = corpClient.GetAllDepartment(); foreach (var dep in allDep) { var data = db.GuYuanDepartment.FirstOrDefault(n => n.DingId == dep.id); if (data == null) { data = new GuYuanDepartment { createdate = DateTime.Now, DingId = dep.id, DingParentId = dep.parentid, DepartmentName = dep.name, OptId = 0, OptName = "钉钉同步" }; db.GuYuanDepartment.Add(data); } else { data.DepartmentName = dep.name; data.OptName = "钉钉同步"; } } return(db.SaveChanges()); }
public GuYuanDepartment UpdateSingle(int id, GuYuanDepartmentReq data) { GuYuanDepartment model = db.GuYuanDepartment.Find(id); SetGuYuanDepartment(model, data); db.SaveChanges(); return(model); }
private static GuYuanUser SetLocalUser(User dingUser, Context db) { GuYuanUser dbGyUser = null; //--钉钉员工表-- var dbDingUser = db.DingTalkUser.FirstOrDefault(n => n.userid == dingUser.userid); if (dbDingUser == null) { dbDingUser = new DingTalkUser { Guid = Guid.NewGuid() }; dbDingUser = db.DingTalkUser.Add(dbDingUser); dbGyUser = db.GuYuanUser.FirstOrDefault(n => n.Phone == dingUser.mobile); } else { dbGyUser = db.GuYuanUser.FirstOrDefault(n => n.Guid == dbDingUser.Guid); } db.Entry(dbDingUser).CurrentValues.SetValues(dingUser); //--雇员所在部门-- GuYuanDepartment gyDep = null; if (dingUser.department?.Any() == true) { var depId = dingUser.department.First(); gyDep = db.GuYuanDepartment.FirstOrDefault(n => n.DingId == depId) ?? SyncDepartment(depId); } //--雇员信息表-- if (dbGyUser == null) { dbGyUser = new GuYuanUser { DepartmentName = gyDep?.DepartmentName, OptName = "钉钉同步", OptId = 0, Name = dingUser.name, createdate = DateTime.Now, PositionName = dingUser.position, projectid = gyDep?.ProjectId, ProjectName = gyDep?.ProjectName, Guid = dbDingUser.Guid, Phone = dingUser.mobile }; db.GuYuanUser.Add(dbGyUser); } else { dbGyUser.DepartmentName = gyDep?.DepartmentName; dbGyUser.OptName = "钉钉同步"; dbGyUser.Name = dingUser.name; dbGyUser.PositionName = dingUser.position; dbGyUser.projectid = gyDep?.ProjectId; dbGyUser.ProjectName = gyDep?.ProjectName; dbGyUser.Phone = dingUser.mobile; } return(dbGyUser); }