public static void Initialize() { SqlSugarClient Ddb = new SqlSugarClient(new ConnectionConfig() { ConnectionString = Config.DingTalkConnectionString, DbType = DbType.SqlServer, IsAutoCloseConnection = true, InitKeyType = InitKeyType.Attribute }); SqlSugarClient Edb = new SqlSugarClient(new ConnectionConfig() { ConnectionString = Config.ESBConnectionString, DbType = DbType.SqlServer, IsAutoCloseConnection = true }); #region 数据初始化 string TempDeptID = FirstInitialize(Ddb); #endregion ImputEsbDepartment(Edb, Ddb); ImputEsbDepartmentTree(Edb, Ddb); UpdateEsbDepartmentParentID(Ddb); DepartmentImputDingTalk(Ddb); EmployeeImputDingTalk(Edb, Ddb); //删除临时部门 DepartmentForDingTalkBll.DeleteTempDDDept(Edb, TempDeptID); }
public static void Synchronous() { LogHelper log = LogFactory.GetLogger("Synchronous"); try { Stopwatch watch = CommonHelper.TimerStart(); #region SqlSugarClient初始化 SqlSugarClient Edb = new SqlSugarClient(new ConnectionConfig() { ConnectionString = Config.ESBConnectionString, DbType = DbType.SqlServer, IsAutoCloseConnection = true }); SqlSugarClient Ddb = new SqlSugarClient(new ConnectionConfig() { ConnectionString = Config.DingTalkConnectionString, DbType = DbType.SqlServer, IsAutoCloseConnection = true, InitKeyType = InitKeyType.Attribute }); Ddb.CodeFirst.InitTables(typeof(DepartmentResult)); #endregion #region 查出目前新增的部门,导入到钉钉组织框架 DepartmentForDingTalkBll.InsertDepartmentForDingTalk(Edb, Ddb); #endregion #region 查出目前更新的部门,更新钉钉组织框架 DepartmentForDingTalkBll.UpdateDepartmentForDingTalk(Edb, Ddb); #endregion //部门List List <V_EmployeeToDingTalk> ESB_EmployeeList = Edb.Queryable <V_EmployeeToDingTalk>().ToList(); #region 查出目前新增的人员,导入到钉钉组织框架 EmployeeForDingTalkBll.EmployeeInsertForDingTalk(Edb, Ddb, ESB_EmployeeList); #endregion #region 查出目前更新的人员,更新钉钉中的人员信息 EmployeeForDingTalkBll.EmployeeUpdateForDingTalk(Edb, Ddb, ESB_EmployeeList); #endregion log.Info("\n钉钉接口同步对接成功,共耗时:" + CommonHelper.TimerEnd(watch) + "毫秒\n"); //Console.Write("\n钉钉接口同步对接成功,共耗时:" + CommonHelper.TimerEnd(watch) + "毫秒\n"); } catch (Exception ex) { log.Error("\r\n SynchronousDingTalk-Synchronous()" + ex); //Console.Write(ex.Message); } }
public static void DepartmentImputDingTalk(SqlSugarClient Ddb) { LogHelper log = LogFactory.GetLogger("ImputEsbDepartment"); Ddb.CodeFirst.InitTables(typeof(DepartmentTrees)); int treeCount = Ddb.Queryable <DepartmentTrees>().Count(); int maxlevel = Ddb.Queryable <DepartmentTrees>().Max(it => it.level); int countRow = 0; log.Info("\r\n------------------------------------------------钉钉组织框架根据ESB部门树数据调整------------------------------------------------\r\n"); for (int i = 0; i <= maxlevel; i++) { List <DepartmentTrees> list = new List <DepartmentTrees>(); list = Ddb.Queryable <DepartmentTrees>().Where(it => it.level == i).ToList(); foreach (var item in list) { countRow++; try { string DepartmentJson = DepartmentForDingTalkBll.GetDepartment(item.DD_Id); DepartmentEntity model = new DepartmentEntity(); model = Newtonsoft.Json.JsonConvert.DeserializeObject <DepartmentEntity>(DepartmentJson); model.parentid = item.DD_ParentId; model.order = list.IndexOf(item).ToString(); model.sourceIdentifier = item.DepartmentId; string param = model.ToJson(); Result result = DepartmentBll.Update(param); if (result != null) { if (result.errcode == "0") { Console.Write("第" + i + "级数据更新成功," + result.errmsg + ",已更新:" + countRow + "条,当前进度:" + (Math.Round(Convert.ToDecimal(Convert.ToDouble(countRow) / Convert.ToDouble(treeCount)), 2, MidpointRounding.AwayFromZero) * 100) + "%\n\n"); } else { Console.Write(result.errmsg); } } else { Console.Write("\r\n无返回数据\r\n"); } } catch (Exception ex) { log.Error("\r\n InitializeDingTalk-DepartmentImputDingTalk() " + ex.Message + "\r\n"); Console.Write("\r\n" + ex.Message + "\r\n"); } } } Console.Write("更新成功,共更新 " + countRow + " 条数据"); }
public static void AddEmployee(SqlSugarClient Edb, SqlSugarClient Ddb, V_EmployeeToDingTalk item) { LogHelper log = LogFactory.GetLogger("AddEmployee"); try { EmployeeEntity model = new EmployeeEntity(); string DD_DepartmentId = "1"; if (!item.ESB_DepartmentId.Equals("1000000001")) { try { DepartmentResult DD_DepModel = Ddb.Queryable <DepartmentResult>().With(SqlWith.NoLock).Where(it => it.ESB_DepartmentID.Equals(item.ESB_DepartmentId)).First(); if (DD_DepModel == null) { DD_DepartmentId = DepartmentForDingTalkBll.DD_DepartmentIsNullForDingTalk(Edb, Ddb, item.ESB_DepartmentId); } else { DD_DepartmentId = DD_DepModel.id; } } catch { DD_DepartmentId = "1"; } } model.userid = item.UserId; model.name = item.Name; model.department = new List <int>(new int[] { Convert.ToInt32(DD_DepartmentId) }); model.position = item.PositionName; model.mobile = item.Mobile; model.tel = item.Telephone; model.workPlace = ""; model.remark = ""; model.email = item.Email; model.jobnumber = item.UserId; model.isSenior = false; string param = model.ToJson(); EmployeeResult Result = EmployeeBll.Create(param); if (Result != null) { if (Result.errcode == "0") { //Console.Write("创建成功,UserId=" + Result.userid); } //该外部联系人已存在 || 手机号码在公司中已存在 else if (Result.errcode == "40026" || Result.errcode == "60104") { string res = EmployeeBll.Delete(model.userid).errcode; if (res != "0") { log.Error("\r\n EmployeeForDingTalkBll-AddEmployee() 手机号码在公司中已存在删除时报错,错误编号:" + res); } EmployeeResult Result2 = EmployeeBll.Create(model.ToJson()); if (Result2.errcode != "0") { if (Result2.errcode == "40022") { string Deletecode = EmployeeBll.Delete(model.userid).errcode; string Createcode = EmployeeBll.Create(model.ToJson()).errcode; if (Createcode != "0") { Createcode = EmployeeBll.Create(model.ToJson()).errcode; if (Createcode != "0" && Createcode != "40021") { log.Error("\r\n AddEmployee - 行号507 成功删除员工信息,但是创建员工信息时报错,错误编码如下:" + Createcode); } } //手机号码在公司中已存在 if (Result2.errcode == "60104") { log.Debug("\r\n EmployeeForDingTalkBll-AddEmployee() 手机号码在公司中已存在 时报错,信息如下" + Result2.errmsg + ",错误编码为:" + Result2.errcode + ", Json参数为:" + param); } else { log.Error("\r\n EmployeeForDingTalkBll-AddEmployee() 该外部联系人已存在 || 手机号码在公司中已存在 时报错,信息如下"+ Result2.errmsg + ",错误编码为:" + Result2.errcode + ", Json参数为:" + param); } Task.Factory.StartNew(() => { InsertErroUpdateEmployee(Ddb, model.userid, "", item.Mobile, Result.errcode); if (Deletecode != "0") { InsertErroUpdateEmployee(Ddb, model.userid, "", item.Mobile, "新增用户时,删除失败,错误编号:" + Deletecode); } if (Createcode != "0") { InsertErroUpdateEmployee(Ddb, model.userid, "", item.Mobile, "新增用户时,执行删除后创建失败,错误编号:" + Createcode); } }); } } } else { if (Result.errcode == "60103") { log.Debug("\r\n EmployeeForDingTalkBll-AddEmployee() " + Result.errmsg + ",错误编码为:" + Result.errcode + ",手机号为" + item.Mobile + ",用户id为" + item.UserId + " Json参数为:" + param); } else { log.Error("\r\n EmployeeForDingTalkBll-AddEmployee() " + Result.errmsg + ",错误编码为:" + Result.errcode); } Task.Factory.StartNew(() => { InsertErroUpdateEmployee(Ddb, model.userid, "", item.Mobile, "新增用户时创建失败,错误编号:" + Result.errcode); }); } } else { //Console.Write("无返回数据"); } } catch (Exception ex) { log.Error("\r\n EmployeeForDingTalkBll-AddEmployee() " + ex + "\r\n"); //Console.Write(ex.Message); } }
public static void EmployeeUpdateForDingTalk(SqlSugarClient Edb, SqlSugarClient Ddb, List <V_EmployeeToDingTalk> ESB_EmployeeList) { LogHelper log = LogFactory.GetLogger("EmployeeUpdateForDingTalk"); List <V_EmployeeToDingTalk> EmpList = new List <V_EmployeeToDingTalk>(); //获取ESB中目前已经更新的数据 List <V_OperationObject> OperationList = Tbiz_OperationTempBll.GetOperationList(Edb, 1, 0); //根据ESB操作记录中更新的ID,查出目前的用户信息 List <V_EmployeeToDingTalk> EmployeeList = ESB_EmployeeList.Where(p => OperationList.Exists(q => q.UserId == p.UserId)).ToList(); log.Info("\r\n------------------------------------------------根据ESB更新钉钉中的人员信息------------------------------------------------\r\n"); #region 循环更新到钉钉 foreach (var item in EmployeeList) { if (item.Enabled == 1) { string EmployeeJson = GetEmployee(item.UserId); //用户ID在钉钉中不存在,即:离职用户重新录用时,应将该用户重新添加进钉钉 if (EmployeeJson.Equals("-1")) { AddEmployee(Edb, Ddb, item); EmpList.Add(item); continue; } else { EmployeeEntity model = Newtonsoft.Json.JsonConvert.DeserializeObject <EmployeeEntity>(EmployeeJson); string oldMobile = model.mobile; string DD_DepartmentId = "1"; DepartmentResult DD_DepModel = Ddb.Queryable <DepartmentResult>().With(SqlWith.NoLock).Where(it => it.ESB_DepartmentID.Equals(item.ESB_DepartmentId)).First(); if (DD_DepModel == null) { try { DD_DepartmentId = DepartmentForDingTalkBll.DD_DepartmentIsNullForDingTalk(Edb, Ddb, item.ESB_DepartmentId); } catch (Exception ex) { DD_DepartmentId = DepartmentForDingTalkBll.DD_DepartmentIsNullForDingTalk(Edb, Ddb, item.ESB_DepartmentId); } } else { DD_DepartmentId = DD_DepModel.id; } model.userid = item.UserId; model.name = item.Name; model.department = new List <int>(new int[] { Convert.ToInt32(DD_DepartmentId) }); model.position = item.PositionName; model.mobile = item.Mobile; model.tel = item.Telephone; model.email = item.Email; model.jobnumber = item.UserId; string param = model.ToJson(); Result Result = EmployeeBll.Update(param); if (Result != null) { if (Result.errcode == "0") { EmpList.Add(item); //Console.Write("更新成功," + Result.errmsg); } else { //UserID不存在 if (Result.errcode == "60111") { AddEmployee(Edb, Ddb, item); EmpList.Add(item); continue; } //更新手机号出错时 else if (Result.errcode == "40022" || Result.errcode == "40021" || Result.errcode == "60104" || Result.errcode == "60121") { //40021 更换的号码已注册过钉钉,无法使用该号码 //40022 企业中的手机号码和登陆钉钉的手机号码不一致,暂时不支持修改用户信息,可以删除后重新添加 //60104 手机号码在公司中已存在 //60121 找不到该用户 string Deletecode = EmployeeBll.Delete(model.userid).errcode; string Createcode = EmployeeBll.Create(model.ToJson()).errcode; if (Createcode != "0") { Createcode = EmployeeBll.Create(model.ToJson()).errcode; if (Createcode != "0" && Createcode != "40021") { log.Error("\r\n EmployeeUpdateForDingTalk - 行号135 更新钉钉人员信息时,成功删除员工信息,但是创建员工信息时报错,错误编码如下:" + Createcode + ", 员工编号为:" + model.userid); } } EmpList.Add(item); Task.Factory.StartNew(() => { InsertErroUpdateEmployee(Ddb, model.userid, oldMobile, item.Mobile, Result.errcode); if (Deletecode != "0") { InsertErroUpdateEmployee(Ddb, model.userid, oldMobile, item.Mobile, "删除失败,错误编号:" + Deletecode); } if (Createcode != "0") { InsertErroUpdateEmployee(Ddb, model.userid, oldMobile, item.Mobile, "执行删除后创建失败,错误编号:" + Createcode); } }); //Console.Write("更新成功\r\n"); } else { //手机号码不合法 if (Result.errcode == "60103") { model.mobile = oldMobile; Result = EmployeeBll.Update(model.ToJson()); if (Result.errcode == "0") { EmpList.Add(item); } else { log.Error("\r\n EmployeeForDingTalkBll-EmployeeUpdateForDingTalk() 失败后不更新手机号,还是失败,具体信息: " + Result.errmsg + "; UserId=" + item.UserId); } } //部门在钉钉中不存在的时候 else if (Result.errcode == "60003") { model.department = new List <int>(new int[] { 1 }); param = model.ToJson(); Result r = EmployeeBll.Update(param); if (r.errcode == "0") { EmpList.Add(item); } else if (r.errcode == "40022" || r.errcode == "40021" || r.errcode == "60104" || r.errcode == "60121") { //40021 更换的号码已注册过钉钉,无法使用该号码 //40022 企业中的手机号码和登陆钉钉的手机号码不一致,暂时不支持修改用户信息,可以删除后重新添加 //60104 手机号码在公司中已存在 //60121 找不到该用户 string Deletecode = EmployeeBll.Delete(model.userid).errcode; string Createcode = EmployeeBll.Create(model.ToJson()).errcode; if (Createcode != "0") { Createcode = EmployeeBll.Create(model.ToJson()).errcode; if (Createcode != "0" && Createcode != "40021") { log.Error("\r\n EmployeeUpdateForDingTalk - 行号199 更新钉钉人员信息时,成功删除员工信息,但是创建员工信息时报错,错误编码如下:" + Createcode); } } EmpList.Add(item); Task.Factory.StartNew(() => { InsertErroUpdateEmployee(Ddb, model.userid, oldMobile, item.Mobile, r.errcode); if (Deletecode != "0") { InsertErroUpdateEmployee(Ddb, model.userid, oldMobile, item.Mobile, "删除失败,错误编号:" + Deletecode); } if (Createcode != "0") { InsertErroUpdateEmployee(Ddb, model.userid, oldMobile, item.Mobile, "执行删除后创建失败,错误编号:" + Createcode); } }); //Console.Write("更新成功\r\n"); } else { log.Debug("\r\n EmployeeForDingTalkBll-EmployeeUpdateForDingTalk() 钉钉中部,部门id=" + DD_DepartmentId + ",已将该人员挂在公司下," + Result.errmsg + "; UserId=" + item.UserId); Task.Factory.StartNew(() => { InsertErroUpdateEmployee(Ddb, model.userid, oldMobile, item.Mobile, "更新用户时失败,错误编号:" + Result.errcode); }); } } else { log.Error("\r\n EmployeeForDingTalkBll-EmployeeUpdateForDingTalk() " + Result.errmsg + "; UserId=" + item.UserId); Task.Factory.StartNew(() => { InsertErroUpdateEmployee(Ddb, model.userid, oldMobile, item.Mobile, "更新用户时失败,错误编号:" + Result.errcode); }); } } } } else { //Console.Write("无返回数据"); } } } else { Result Result = EmployeeBll.Delete(item.UserId); if (Result != null) { //找不到该用户 if (Result.errcode == "0" || Result.errcode == "60121") { EmpList.Add(item); //Console.Write("删除成功," + Result.errmsg + "\r\n"); } else { log.Error("\r\n EmployeeForDingTalkBll-EmployeeUpdateForDingTalk() " + Result.errmsg + "; UserId=" + item.UserId); //Console.Write("\r\n" + Result.errmsg + "; UserId=" + item.UserId); } } else { //Console.Write("无返回数据"); } } } int d = 0; foreach (var item in EmpList) { d = Edb.Deleteable <Tbiz_OperationTemp>().Where(it => it.ObjectId.Equals(item.UserId)).ExecuteCommand(); } #endregion }
public static void InsertForDingTalk(SqlSugarClient Edb, SqlSugarClient Ddb, List <V_EmployeeToDingTalk> EmpList, V_EmployeeToDingTalk item) { LogHelper log = LogFactory.GetLogger("InsertEmployeeForDingTalk"); string DD_DepartmentId = "1"; if (!item.ESB_DepartmentId.Equals("1000000001")) { DepartmentResult DD_DepModel = Ddb.Queryable <DepartmentResult>().With(SqlWith.NoLock).Where(it => it.ESB_DepartmentID.Equals(item.ESB_DepartmentId)).First(); if (DD_DepModel == null) { DD_DepartmentId = DepartmentForDingTalkBll.DD_DepartmentIsNullForDingTalk(Edb, Ddb, item.ESB_DepartmentId); } else { DD_DepartmentId = DD_DepModel.id; } try { DD_DepartmentId = Ddb.Queryable <DepartmentResult>().With(SqlWith.NoLock).Where(it => it.ESB_DepartmentID.Equals(item.ESB_DepartmentId)).First().id; } catch (Exception ex) { Console.Write("根据ESB_DepartmentId获取对应的钉钉id时报错,该ID为" + item.ESB_DepartmentId + ",错误信息为:" + ex + "\r\n"); } } EmployeeEntity model = new EmployeeEntity(); model.userid = item.UserId; model.name = item.Name; model.department = new List <int>(new int[] { Convert.ToInt32(DD_DepartmentId) }); model.position = item.PositionName; model.mobile = item.Mobile; model.tel = item.Telephone; model.workPlace = ""; model.remark = ""; model.email = item.Email; model.jobnumber = item.UserId; model.isSenior = false; string param = model.ToJson(); EmployeeResult Result = EmployeeBll.Create(param); if (Result != null) { if (Result.errcode == "0") { EmpList.Add(item); //Console.Write("创建成功,UserId=" + Result.userid + "\r\n"); } else { //UserID在公司中已存在 if (Result.errcode == "60102") { string EmployeeJson = GetEmployee(item.UserId); model = Newtonsoft.Json.JsonConvert.DeserializeObject <EmployeeEntity>(EmployeeJson); model.userid = item.UserId; model.name = item.Name; model.department = new List <int>(new int[] { Convert.ToInt32(DD_DepartmentId) }); model.position = item.PositionName; model.mobile = item.Mobile; model.tel = item.Telephone; model.email = item.Email; model.jobnumber = item.UserId; param = model.ToJson(); Result res = EmployeeBll.Update(param); if (res != null) { if (res.errcode == "0") { EmpList.Add(item); //Console.Write("更新成功," + Result.errmsg); } else { log.Error("\r\n EmployeeForDingTalkBll-InsertForDingTalk() " + Result.errmsg + "; UserId=" + item.UserId); //Console.Write("\r\n" + Result.errmsg + "; UserId=" + item.UserId); } } else { //Console.Write("无返回数据"); } } else { if (Result.errcode == "60104") { log.Error("\r\n EmployeeForDingTalkBll-InsertForDingTalk() " + Result.errmsg + "; UserId=" + item.UserId); } else if (Result.errcode == "40026") { //该外部联系人已存在 AddEmployee(Edb, Ddb, item); EmpList.Add(item); } else { log.Error("\r\n EmployeeForDingTalkBll-InsertForDingTalk() " + Result.errmsg + "; UserId=" + item.UserId); } //Console.Write("\r\n" + Result.errmsg + "; UserId=" + item.UserId); } } } else { //Console.Write("无返回数据"); } }
public static void EmployeeImputDingTalk(SqlSugarClient Edb, SqlSugarClient Ddb) { LogHelper log = LogFactory.GetLogger("EmployeeImputDingTalk"); try { List <DepartmentResult> resultList = new List <DepartmentResult>(); List <V_EmployeeToDingTalk> ESB_EmployeeList = Edb.Queryable <V_EmployeeToDingTalk>().ToList(); List <DepartmentResult> DTDepartList = Ddb.Queryable <DepartmentResult>().ToList(); foreach (var item in ESB_EmployeeList) { try { if (item.Enabled == 1) { EmployeeEntity model = new EmployeeEntity(); model.userid = item.UserId; model.name = item.Name; model.department = new List <int>(new int[] { Convert.ToInt32(DepartmentForDingTalkBll.GetDingTalkDepartmentId(DTDepartList, item.ESB_DepartmentId)) }); model.position = item.PositionName; model.mobile = item.Mobile; model.tel = item.Telephone; model.workPlace = ""; model.remark = ""; model.email = item.Email; model.jobnumber = item.UserId; model.isHide = false; model.isSenior = false; string param = model.ToJson(); EmployeeResult Result = EmployeeBll.Create(param); if (Result != null) { if (Result.errcode == "0") { Console.Write("创建成功,UserId=" + Result.userid + "\r\n"); } else { Console.Write(Result.errmsg + "\r\n"); } } else { Console.Write("无返回数据"); } } else { Result Result = EmployeeBll.Delete(item.UserId); if (Result != null) { if (Result.errcode == "0") { Console.Write("删除成功," + Result.errmsg + "\r\n"); } else { Console.Write(Result.errmsg + "\r\n"); } } else { Console.Write("无返回数据"); } } } catch (Exception ex) { log.Error("\r\n InitializeDingTalk-EmployeeImputDingTalk() " + ex); continue; } } } catch (Exception ex) { log.Error("\r\n InitializeDingTalk-EmployeeImputDingTalk() " + ex); Console.Write(ex.Message); } }