コード例 #1
0
        /// <summary>
        /// 更新组织父节点和排序
        /// </summary>
        /// <param name="org"></param>
        /// <returns></returns>
        private bool UpdateWeChatOrg(WeChatOrg_New org)
        {
            string res = post(string.Format(URL_UpdateOrg, AccessToken), JsonConvert.SerializeObject(org));

            this.LogWriter.Write("更新微信组织返回:Result=" + res + ",参数=" + JsonConvert.SerializeObject(org));
            WXResult wxRes = JsonConvert.DeserializeObject <WXResult>(res);

            if (wxRes.errcode == 0)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
コード例 #2
0
        /// <summary>
        /// 同步微信的组织
        /// </summary>
        /// <param name="SourceUnits"></param>
        /// <param name="ParentID"></param>
        /// <param name="WeChatParent"></param>
        /// <returns></returns>
        private bool SyncOrg(DateTime LastSyncTime, string ParentID, int WeChatParent, DataTable dtUserRelation, ref SyncResult syncResult)
        {
            LoadOrgToken();

            var childList = Organization.GetChildUnits(ParentID, UnitType.OrganizationUnit, false, State.Active);
            var ChildList = childList.OrderBy(a => a.SortKey);

            #region  用户同步
            // 同步用户
            OThinker.Organization.Unit[] users = Organization.GetChildUnits(ParentID, UnitType.User, false, State.Unspecified).OrderBy(a => a.SortKey).ToArray();
            if (users != null)
            {
                foreach (OThinker.Organization.User user in users)
                {
                    if (string.IsNullOrEmpty(user.WeChatAccount) &&
                        string.IsNullOrEmpty(user.Email) &&
                        string.IsNullOrEmpty(user.Mobile))
                    {
                        continue;
                    }
                    //1.增量同步且用户的修改时间少于上次的同步时间,不做增量同步
                    if (LastSyncTime != DateTime.MinValue && user.ModifiedTime < LastSyncTime)
                    {
                        continue;
                    }
                    this.LogWriter.Write("微信用户同步,Code=" + user.Code + ",Name=" + user.Name + ",Mobile=" + user.Mobile + ",WeChatParent=" + WeChatParent);
                    if (NotSyncToWechatUserCodes.Replace("'", "").Split(',').Contains(user.Code))
                    {
                        this.LogWriter.Write("微信用户同步跳过,存在于不同步配置中,Code=" + user.Code + ",Name=" + user.Name + ",Mobile=" + user.Mobile + ",WeChatParent=" + WeChatParent);
                        continue;
                    }
                    var wxuser = GetWeChatUser(user.Code);
                    if (wxuser.errcode != "0")//0表示正常返回
                    {
                        //H3禁用或者离职:不用同步到微信
                        if (user.State == State.Inactive || user.ServiceState == UserServiceState.Dismissed)
                        {
                        }
                        else
                        {
                            if (user.Appellation != null && user.Appellation.Contains("金融专员"))
                            {
                                this.LogWriter.Write("微信用户同步跳过:金融专员不做处理");
                                //金融专员不做处理:
                                //金融专员有CAP账号,又有263邮箱,会导致手机号码重复;
                                //所以不同步
                            }
                            else
                            {
                                // 不存在,
                                if (CreateWeChatUser(user, WeChatParent))
                                {
                                    DataRow[] rows = dtUserRelation.Select("CODE='" + user.Code + "'");
                                    if (rows.Length == 0)
                                    {
                                        InsertToWechatUserRelation(user.Code, user.Name, user.Mobile);
                                    }
                                    syncResult.create_User_Success_Num++;
                                }
                                else
                                {
                                    syncResult.create_User_Fail_Num++;
                                }
                            }
                        }
                    }
                    else//微信存在此用户
                    {
                        //微信中有这个用户,但是H3中是禁用或者离职状态的,删除企业微信
                        //如果是金融专员,删除:保证一个人有一个号码;
                        if (user.State == State.Inactive || user.ServiceState == UserServiceState.Dismissed ||
                            (user.Appellation != null && user.Appellation.Contains("金融专员")))
                        {
                            this.LogWriter.Write("微信用户删除:禁用/离职/金融专员");
                            if (DeleteWeChatUser(user.Code))
                            {
                                syncResult.delete_User_Success_Num++;
                            }
                            else
                            {
                                syncResult.delete_User_Fail_Num++;
                            }
                        }
                        else
                        {
                            #region 手机号码被修改了
                            //手机号码被修改了
                            if (user.Mobile != wxuser.mobile)
                            {
                                this.LogWriter.Write($"微信用户手机号码不一致,微信{wxuser.mobile},H3{user.Mobile}");
                                //这块删除逻辑有问题,先注释;
                                //DataTable dtTel = OThinker.H3.Controllers.AppUtility.Engine.EngineConfig.CommandFactory.CreateCommand().ExecuteDataTable("SELECT * FROM C_H3_Wechat_OrgSync WHERE tel='" + user.Mobile + "'");
                                //if (dtTel != null && dtTel.Rows.Count > 0)//此修改的手机号码有用户在使用,先删除此用户;
                                //{
                                //    foreach (DataRow row in dtTel.Rows)
                                //    {
                                //        var wxuserCF = GetWeChatUser(row["code"] + string.Empty);
                                //        if (DeleteWeChatUser(wxuserCF.userid))
                                //        {
                                //            syncResult.delete_User_Success_Num++;
                                //        }
                                //    }
                                //}

                                //微信认证后,默认会绑定微信对应的手机号码,会修改掉原系统中的手机号,导致2个系统的手机号不一致
                                //此时下面的删除逻辑会有问题,故注释;

                                //手机号码被修改了,但是此被激活了,需要先删除此用户,然后再同步
                                //if (wxuser.status == 1)
                                //{
                                //    this.LogWriter.Write($"微信用户删除:手机号码修改且已激活,原{wxuser.mobile},现{user.Mobile}");
                                //    if (DeleteWeChatUser(wxuser.userid))
                                //    {
                                //        syncResult.delete_User_Success_Num++;
                                //    }
                                //    this.LogWriter.Write($"微信用户删除后创建:手机号码修改且已激活,原{wxuser.mobile},现{user.Mobile}");
                                //    if (CreateWeChatUser(user, WeChatParent))
                                //    {
                                //        syncResult.create_User_Success_Num++;
                                //        InsertToWechatUserRelation(user.Code, user.Name, user.Mobile);
                                //    }
                                //    else
                                //    {
                                //        syncResult.create_User_Fail_Num++;
                                //    }
                                //    continue;
                                //}
                            }
                            #endregion

                            #region 手机号码没有被修改
                            //手机号码没有被修改
                            if (UpdateWeChatUser(user, WeChatParent))
                            {
                                //判断中间表是否有数据,有数据就更新,没有数据就插入
                                DataRow[] rows = dtUserRelation.Select("CODE='" + user.Code + "'");
                                if (rows.Length == 0)
                                {
                                    InsertToWechatUserRelation(user.Code, user.Name, user.Mobile);
                                }
                                else
                                {
                                    if (user.Mobile != wxuser.mobile)
                                    {
                                        //手机号码修改了才去Update中间表;
                                        string sqlUpdate = "UPDATE C_H3_Wechat_OrgSync SET name='" + user.Name + "',tel='" + user.Mobile + "' WHERE code='" + user.Code + "'";
                                        int    n         = OThinker.H3.Controllers.AppUtility.Engine.EngineConfig.CommandFactory.CreateCommand().ExecuteNonQuery(sqlUpdate);
                                        this.LogWriter.Write("Update C_H3_Wechat_OrgSync表,Code:" + user.Code + ",Name:" + user.Name + ",Tel:" + user.Mobile + ",受影响行数:" + n);
                                    }
                                }
                                syncResult.update_User_Success_Num++;
                            }
                            else
                            {
                                syncResult.update_User_Fail_Num++;
                            }
                            #endregion
                        }
                    }
                }
            }
            #endregion

            #region OU同步
            foreach (OThinker.Organization.OrganizationUnit sourceUnit in ChildList)
            {
                this.LogWriter.Write("微信OU同步,ID=" + sourceUnit.ObjectID + ",Name=" + sourceUnit.Name + ",WeChatParent=" + WeChatParent + ",WeChatID=" + sourceUnit.WeChatID);

                // 同步当前组织
                WeChatOrg_New org = this.GetWeChatOrgByParentId(WeChatParent, sourceUnit.WeChatID);
                if (org != null)
                {
                    // 已存在
                    if (sourceUnit.WeChatID != org.id)
                    {
                        sourceUnit.WeChatID = org.id;
                        Organization.UpdateUnit(OThinker.Organization.User.AdministratorID, sourceUnit);
                    }

                    //更新组织顺序
                    if (sourceUnit.SortKey != 5000 - sourceUnit.SortKey)
                    {
                        org.order    = 5000 - sourceUnit.SortKey;
                        org.parentid = WeChatParent;
                        org.name     = sourceUnit.Name;
                        if (UpdateWeChatOrg(org))
                        {
                            syncResult.update_OU_Success_Num++;
                        }
                        else
                        {
                            syncResult.update_OU_Fail_Num++;
                        }
                    }
                }
                else
                {// 不存在
                    org = CreateWeChatOrg(sourceUnit.WeChatID, sourceUnit.Name, WeChatParent, 5000 - sourceUnit.SortKey);
                    if (org.id != 0)
                    {
                        sourceUnit.WeChatID = org.id;
                        Organization.UpdateUnit(OThinker.Organization.User.AdministratorID, sourceUnit);
                    }
                    if (org.errcode == "0")
                    {
                        syncResult.create_OU_Success_Num++;
                    }
                    else
                    {
                        syncResult.create_OU_Fail_Num++;
                    }
                }

                // 递归同步子结构
                SyncOrg(LastSyncTime, sourceUnit.ObjectID, sourceUnit.WeChatID, dtUserRelation, ref syncResult);
            }
            #endregion

            return(true);
        }