示例#1
0
        /// <summary>
        /// 同步钉钉通讯录到CCGPM
        /// </summary>
        /// <returns></returns>
        public bool AnsyOrgToCCGPM()
        {
            string access_token = getAccessToken();
            string url          = "https://oapi.dingtalk.com/department/list?access_token=" + access_token;

            try
            {
                string          str            = new HttpWebResponseUtility().HttpResponseGet(url);
                DepartMent_List departMentList = FormatToJson.ParseFromJson <DepartMent_List>(str);
                //部门集合
                if (departMentList != null && departMentList.department != null && departMentList.department.Count > 0)
                {
                    //删除旧数据
                    ClearOrg_Old();
                    //获取根部门
                    DepartMentDetailInfo rootDepartMent = new DepartMentDetailInfo();
                    foreach (DepartMentDetailInfo deptMenInfo in departMentList.department)
                    {
                        if (deptMenInfo.id == "1")
                        {
                            rootDepartMent = deptMenInfo;
                            break;
                        }
                    }
                    //增加跟部门
                    int  deptIdx  = 0;
                    Dept rootDept = new Dept();
                    rootDept.No       = rootDepartMent.id;
                    rootDept.Name     = rootDepartMent.name;
                    rootDept.ParentNo = "0";
                    rootDept.DirectInsert();


                    //部门信息
                    foreach (DepartMentDetailInfo deptMentInfo in departMentList.department)
                    {
                        //增加部门,排除根目录
                        if (deptMentInfo.id != "1")
                        {
                            Dept dept = new Dept();
                            dept.No       = deptMentInfo.id;
                            dept.Name     = deptMentInfo.name;
                            dept.ParentNo = deptMentInfo.parentid;
                            dept.DirectInsert();
                        }

                        //部门人员
                        DepartMentUser_List userList = GenerDeptUser_List(access_token, deptMentInfo.id);
                        if (userList != null)
                        {
                            foreach (DepartMentUserInfo userInfo in userList.userlist)
                            {
                                Emp     emp     = new Emp();
                                DeptEmp deptEmp = new DeptEmp();
                                //如果账户存在则人员信息不添加,添加关联表
                                if (emp.IsExit(EmpAttr.No, userInfo.userid) == true)
                                {
                                    deptEmp.MyPK    = deptMentInfo.id + "_" + emp.No;
                                    deptEmp.FK_Dept = deptMentInfo.id;
                                    deptEmp.FK_Emp  = emp.No;
                                    deptEmp.DirectInsert();
                                    continue;
                                }

                                //增加人员
                                emp.No      = userInfo.userid;
                                emp.Name    = userInfo.name;
                                emp.FK_Dept = deptMentInfo.id;
                                emp.Tel     = userInfo.mobile;
                                emp.Email   = userInfo.email;
                                //emp.Idx = DataType.IsNullOrEmpty(userInfo.order) == true ? 0 : Int32.Parse(userInfo.order);
                                emp.DirectInsert();

                                //增加人员与部门对应表
                                deptEmp.MyPK    = deptMentInfo.id + "_" + emp.No;
                                deptEmp.FK_Dept = deptMentInfo.id;
                                deptEmp.FK_Emp  = emp.No;
                                deptEmp.DirectInsert();
                            }
                        }
                    }

                    #region 处理部门名称全程
                    //OrgInit_NameOfPath nameOfPath = new OrgInit_NameOfPath();
                    //if (nameOfPath.IsCanDo)
                    //    nameOfPath.Do();
                    #endregion

                    return(true);
                }
            }
            catch (Exception ex)
            {
                BP.DA.Log.DefaultLogWriteLineError(ex.Message);
            }
            return(false);
        }