public static string FirstInitialize(SqlSugarClient Ddb) { Ddb.CodeFirst.InitTables(typeof(Tbiz_ExistData)); Ddb.DbMaintenance.TruncateTable("Tbiz_ExistData"); //创建一个临时部门,用于存放管理员 DepartmentEntity Deptmodel = new DepartmentEntity(); Deptmodel.name = "TempDepartment"; Deptmodel.parentid = "1"; Deptmodel.sourceIdentifier = ""; DepartmentResult DepResult = DepartmentBll.Create(Deptmodel); string TempDDDeptID = DepResult.id; //查出所有部门id GetDepartmentList DepartmentList = DepartmentBll.GetList(); if (string.IsNullOrWhiteSpace(TempDDDeptID)) { TempDDDeptID = DepartmentList.department.Where(e => e.name.Equals("TempDepartment")).ToList().FirstOrDefault().id; } var list = DepartmentList.department.Where(e => !e.id.Equals("1") && !e.id.Equals(TempDDDeptID)).ToList(); foreach (var item in list) { //遍历每个id下的所有人员 GetDepartmentForUserList UserList = DepartmentBll.GetByDepartmentIdForUserInfoList(item.id); foreach (var Useritem in UserList.userlist) { try { //删除所有人员 Result Result = EmployeeBll.Delete(Useritem.userid); //当删除失败时,将该人员转移到临时部门中 if (Result.errcode != "0") { Tbiz_ExistData model = new Tbiz_ExistData(); model.ObjectId = Useritem.userid; model.Type = 1; model.CreateDate = DateTime.Now; Ddb.Insertable <Tbiz_ExistData>(model).ExecuteCommand(); string EmployeeJson = EmployeeForDingTalkBll.GetEmployee(Useritem.userid); EmployeeEntity Emodel = Newtonsoft.Json.JsonConvert.DeserializeObject <EmployeeEntity>(EmployeeJson); for (int i = 0; i < Emodel.department.Count; i++) { if (!Emodel.department[i].ToString().Equals("1")) { Emodel.department.Remove(Emodel.department[i]); } } Emodel.department.Add(Convert.ToInt32(TempDDDeptID)); string param = Emodel.ToJson(); Result uResult = EmployeeBll.Update(param); if (uResult.errcode != "0") { Console.Write("\r\n" + uResult.errmsg + "\r\n"); } } } catch (Exception e) { Console.Write("\r\n InitializeDingTalk-FirstInitialize() " + e + "\r\n"); log.Error(e); } } //最后删除公司下的所有部门 Result r = DepartmentBll.Delete(item.id); if (r.errcode != "0") { Tbiz_ExistData model = new Tbiz_ExistData(); model.ObjectId = item.id; model.Type = 0; model.CreateDate = DateTime.Now; Ddb.Insertable <Tbiz_ExistData>(model).ExecuteCommand(); Console.Write("\r\n" + r.errmsg + "\r\n"); } Console.Write("\r\n 当前进度" + Math.Round(Convert.ToDecimal((Convert.ToDecimal(list.IndexOf(item)) / list.Count())), 2, MidpointRounding.AwayFromZero) * 100 + "%"); } return(TempDDDeptID); }
private static void DeleteEmp() { SqlSugarClient Ddb = new SqlSugarClient(new ConnectionConfig() { ConnectionString = Config.DingTalkConnectionString, DbType = DbType.SqlServer, IsAutoCloseConnection = true, InitKeyType = InitKeyType.Attribute }); string TempDDDeptID = ""; //查出所有部门id GetDepartmentList DepartmentList = DepartmentBll.GetList(); if (string.IsNullOrWhiteSpace(TempDDDeptID)) { TempDDDeptID = DepartmentList.department.Where(e => e.name.Equals("TempDepartment")).ToList().FirstOrDefault().id; } var list = DepartmentList.department.Where(e => !e.id.Equals("1") && !e.id.Equals(TempDDDeptID)).ToList(); foreach (var item in list) { //遍历每个id下的所有人员 GetDepartmentForUserList UserList = DepartmentBll.GetByDepartmentIdForUserInfoList(item.id); foreach (var Useritem in UserList.userlist) { try { //删除所有人员 Result Result = EmployeeBll.Delete(Useritem.userid); //当删除失败时,将该人员转移到临时部门中 if (Result.errcode != "0") { Tbiz_ExistData model = new Tbiz_ExistData(); model.ObjectId = Useritem.userid; model.Type = 1; model.CreateDate = DateTime.Now; Ddb.Insertable <Tbiz_ExistData>(model).ExecuteCommand(); string EmployeeJson = EmployeeForDingTalkBll.GetEmployee(Useritem.userid); EmployeeEntity Emodel = Newtonsoft.Json.JsonConvert.DeserializeObject <EmployeeEntity>(EmployeeJson); for (int i = 0; i < Emodel.department.Count; i++) { if (!Emodel.department[i].ToString().Equals("1")) { Emodel.department.Remove(Emodel.department[i]); } } Emodel.department.Add(Convert.ToInt32(TempDDDeptID)); string param = Emodel.ToJson(); Result uResult = EmployeeBll.Update(param); if (uResult.errcode != "0") { Console.Write("\r\n" + uResult.errmsg + "\r\n"); } } } catch (Exception e) { Console.Write("\r\n" + e + "\r\n"); log.Error("\r\n InitializeDingTalk-DeleteEmp() " + e); } } Console.Write("\r\n 当前进度" + Math.Round(Convert.ToDecimal((Convert.ToDecimal(list.IndexOf(item)) / list.Count())), 2, MidpointRounding.AwayFromZero) * 100 + "%"); } }