示例#1
0
        /// <summary>
        /// 将id部门下的人员转到parentid部门下
        /// </summary>
        /// <param name="id"></param>
        /// <param name="parentid"></param>
        private static void DepartmentUserMove(string id, int parentid)
        {
            GetDepartmentList DepartmentList = DepartmentBll.GetList();

            var dddd = DepartmentList.department.Where(e => e.id.Equals(id)).ToList().FirstOrDefault();

            if (dddd != null)
            {
                //钉钉中存在该部门,检查该部门下是否有人,没人的并且重复的部门删除钉钉中该部门,并且删除钉钉关系表和钉钉树关系表
                GetDepartmentForUserList UserList = DepartmentBll.GetByDepartmentIdForUserInfoList(dddd.id);
                Console.Write("\n" + dddd.name + " DD_ID=" + dddd.id + ",其下共有" + UserList.userlist.Count + "个人!\r\n");
                foreach (var Useritem in UserList.userlist)
                {
                    string         EmployeeJson = EmployeeForDingTalkBll.GetEmployee(Useritem.userid);
                    EmployeeEntity Emodel       = JsonHelper.JsonToModel <EmployeeEntity>(EmployeeJson);
                    Emodel.department = new List <int>()
                    {
                        parentid
                    };
                    string param = Emodel.ToJson();

                    Result uResult = EmployeeBll.Update(param);
                    if (uResult.errcode != "0")
                    {
                        Console.Write("\r\n" + uResult.errmsg + "\r\n");
                    }
                }
            }
        }
示例#2
0
        private static void AddEmployee()
        {
            try
            {
                EmployeeEntity model = new EmployeeEntity();
                model.userid     = "wangzezheng";
                model.name       = "王五";
                model.department = new List <int>(new int[] { 123456 });
                model.position   = "软件工程师";
                model.mobile     = "137*****334";
                model.tel        = "";
                model.workPlace  = "";
                model.remark     = "";
                model.email      = "";
                model.jobnumber  = "";
                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);
                    }
                    else
                    {
                        Console.Write(Result.errmsg);
                    }
                }
                else
                {
                    Console.Write("无返回数据");
                }
            }
            catch (Exception ex)
            {
                Console.Write(ex.Message);
            }
        }
示例#3
0
        private static void EmployeeImputDingTalk()
        {
            LogHelper log = LogFactory.GetLogger("EmployeeImputDingTalk");

            try
            {
                List <DepartmentResult> resultList = new List <DepartmentResult>();
                SqlSugarClient          Edb        = new SqlSugarClient(new ConnectionConfig()
                {
                    ConnectionString      = Config.ESBConnectionString,
                    DbType                = DbType.SqlServer,
                    IsAutoCloseConnection = true
                });

                List <V_EmployeeToDingTalk> ESB_EmployeeList = Edb.Queryable <V_EmployeeToDingTalk>().ToList();

                var _DepartmentId = new[] { "1000000360", "1000000575", "1000000010" };

                ESB_EmployeeList = ESB_EmployeeList.Where(it => _DepartmentId.Contains(it.ESB_DepartmentId) && it.Enabled.Equals(1)).ToList();

                SqlSugarClient Ddb = new SqlSugarClient(new ConnectionConfig()
                {
                    ConnectionString      = Config.DingTalkConnectionString,
                    DbType                = DbType.SqlServer,
                    IsAutoCloseConnection = true
                });
                List <DepartmentResult> DTDepartList = Ddb.Queryable <DepartmentResult>().ToList();


                foreach (var item in ESB_EmployeeList)
                {
                    try
                    {
                        EmployeeEntity model = new EmployeeEntity();
                        model.userid     = item.UserId;
                        model.name       = item.Name;
                        model.department = new List <int>(new int[] { Convert.ToInt32(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);
                            }
                            else
                            {
                                Console.Write(Result.errmsg);
                            }
                        }
                        else
                        {
                            Console.Write("无返回数据");
                        }
                    }
                    catch (Exception ex)
                    {
                        log.Error(ex);
                        continue;
                    }
                }
            }
            catch (Exception ex)
            {
                log.Error(ex);
                Console.Write(ex.Message);
            }
        }
示例#4
0
        private static void OperationRepeatData3()
        {
            GetDepartmentList DepartmentList = DepartmentBll.GetList();

            string sql  = @"
                            with ta as(
                            select *   from DepartmentResult where ESB_DepartmentID in(
                            select ESB_DepartmentID from DepartmentResult group by ESB_DepartmentID having count(1)>1
                            )
                            ),tb as(
                            select a.* from [PM.WebApi].dbo.V_Effective_Tbiz_DepartmentInfo a inner join [PM.WebApi].dbo.V_Effective_Tbiz_DepartmentTreeas b
                            on a.DepartmentId=b.TreeNode
                            ),tc as(
                            select tb.fullname a,ta.* from ta left join tb 
                            on ta.ESB_DepartmentName=tb.FullName
                            where tb.fullname is null
                            ),td as(
                            select a.* from [PM.WebApi].dbo.V_Effective_Tbiz_DepartmentInfo a inner join tc
                            on tc.ESB_DepartmentId=a.DepartmentId
                            )
                            select distinct td.fullname aa,tc.* from tc right join  td
                            on tc.ESB_DepartmentId=td.DepartmentId
                            order by td.fullname
                        ";
            string sql2 = @"
                            with ta as(
                            select *   from DepartmentResult where ESB_DepartmentID in(
                            select ESB_DepartmentID from DepartmentResult group by ESB_DepartmentID having count(1)>1
                            )
                            ),tb as(
                            select a.* from [PM.WebApi].dbo.V_Effective_Tbiz_DepartmentInfo a inner join [PM.WebApi].dbo.V_Effective_Tbiz_DepartmentTreeas b
                            on a.DepartmentId=b.TreeNode
                            ),tc as(
                            select tb.fullname a,ta.* from ta left join tb 
                            on ta.ESB_DepartmentName=tb.FullName
                            where tb.fullname is null
                            ),td as(
                            select a.* from [PM.WebApi].dbo.V_Effective_Tbiz_DepartmentInfo a inner join tc
                            on tc.ESB_DepartmentId=a.DepartmentId
                            ),te as(
                            select a.* from td inner join DepartmentResult a
                            on td.DepartmentID=a.ESB_DepartmentID and td.FullName=a.ESB_DepartmentName
                            )select '' aa,'' a  ,* from te
                            order by ESB_DepartmentID
                        ";

            SqlSugarClient Ddb = new SqlSugarClient(new ConnectionConfig()
            {
                ConnectionString      = Config.DingTalkConnectionString,
                DbType                = DbType.SqlServer,
                IsAutoCloseConnection = true,
                InitKeyType           = InitKeyType.Attribute
            });

            List <DepartmentResult> list = Ddb.Ado.SqlQuery <DepartmentResult>(sql);

            List <DepartmentResult> list2 = Ddb.Ado.SqlQuery <DepartmentResult>(sql2);

            foreach (var item in list)
            {
                var dddd = DepartmentList.department.Where(e => e.id.Equals(item.id)).ToList().FirstOrDefault();
                if (dddd != null)
                {
                    //钉钉中存在该部门,检查该部门下是否有人,没人的并且重复的部门删除钉钉中该部门,并且删除钉钉关系表和钉钉树关系表
                    GetDepartmentForUserList UserList = DepartmentBll.GetByDepartmentIdForUserInfoList(dddd.id);
                    Console.Write("\n" + dddd.name + " DD_ID=" + dddd.id + ",其下共有" + UserList.userlist.Count + "个人!\r\n");
                    string id = list2.Where(it => it.ESB_DepartmentID.Equals(item.ESB_DepartmentID)).FirstOrDefault().id;
                    if (UserList.userlist.Count != 0)
                    {
                        //将该部门下的人迁移到正确的部门下
                        foreach (var Useritem in UserList.userlist)
                        {
                            string         EmployeeJson = EmployeeForDingTalkBll.GetEmployee(Useritem.userid);
                            EmployeeEntity Emodel       = JsonHelper.JsonToModel <EmployeeEntity>(EmployeeJson);
                            Emodel.department = new List <int>()
                            {
                                Convert.ToInt32(id)
                            };
                            string param = Emodel.ToJson();

                            Result uResult = EmployeeBll.Update(param);
                            if (uResult.errcode != "0")
                            {
                                Console.Write("\r\n" + uResult.errmsg + "\r\n");
                            }
                        }


                        //删除钉钉中的部门
                        var a = DepartmentBll.Delete(dddd.id);
                        //删除钉钉关系表和钉钉树关系表
                        int i = Ddb.Deleteable <DepartmentResult>().Where(it => it.id.Equals(item.id)).ExecuteCommand();
                        i = Ddb.Deleteable <DepartmentTrees>().Where(it => it.DD_Id.Equals(item.id)).ExecuteCommand();
                    }
                }
            }
            Console.Write("\r\n处理完毕");
        }
        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 UpdateEmployeePhoneByUserId(string userid, string MobilePhone)
        {
            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));

            LogHelper log = LogFactory.GetLogger("UpdateEmployeePhoneByUserId");

            try
            {
                GetEmployee    emp       = EmployeeBll.Get(userid);
                EmployeeEntity model     = JsonHelper.JsonToModel <EmployeeEntity>(emp.ToJson());
                string         oldMobile = model.mobile;

                model.mobile = MobilePhone;

                string param = model.ToJson();

                Result Result = EmployeeBll.Update(param);
                if (Result != null)
                {
                    if (Result.errcode == "0")
                    {
                        //Console.Write("更新成功\r\n");
                    }
                    else if (Result.errcode == "40022" || Result.errcode == "40021")
                    {
                        //40021	更换的号码已注册过钉钉,无法使用该号码
                        //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 UpdateEmployeePhoneByUserId - 行号666 更新钉钉人员信息时,成功删除员工信息,但是创建员工信息时报错,错误编码如下:" + Createcode);
                            }
                        }

                        if (Deletecode != "0")
                        {
                            InsertErroUpdateEmployee(Ddb, model.userid, oldMobile, MobilePhone, "删除失败,错误编号:" + Deletecode);
                        }
                        if (Createcode != "0")
                        {
                            InsertErroUpdateEmployee(Ddb, model.userid, oldMobile, MobilePhone, "执行删除后创建失败,错误编号:" + Createcode);
                        }

                        //Console.Write("更新成功,Userid=" + m.userid + "\r\n");
                    }
                    else
                    {
                        log.Error("\r\n EmployeeForDingTalkBll-UpdateEmployeePhoneByUserId() " + Result.errmsg);

                        Task.Factory.StartNew(() =>
                        {
                            InsertErroUpdateEmployee(Ddb, model.userid, oldMobile, MobilePhone, "更新用户时失败,错误编号:" + Result.errcode);
                        });
                    }
                }
                else
                {
                    //Console.Write("无返回数据");
                }
            }
            catch (Exception ex)
            {
                log.Error("\r\n EmployeeForDingTalkBll-UpdateEmployeePhoneByUserId() " + ex);
                //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 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 + "%");
            }
        }
        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);
            }
        }