Exemplo n.º 1
0
        public GetTagListResult WX_GetTagList()
        {
            GetTagListResult Ret = new GetTagListResult();

            if (Qyinfo.IsUseWX == "Y")
            {
                Ret = MailListApi.GetTagList(GetToken());
            }
            return(Ret);
        }
Exemplo n.º 2
0
Arquivo: WxFO.cs Projeto: Luyingjin/Qy
        /// <summary>
        /// 更新标签
        /// </summary>
        /// <param name="QyID"></param>
        private void ReFreshTag(string QyID)
        {
            GetTagListResult result = null;

            try
            {
                result = MailListApi.GetTagList(GetAccessToken(QyID));
            }
            catch (Exception ex)
            {
                LogWriter.Error(ex, string.Format("获取QyID为{0}的标签失败", QyID));
                result = MailListApi.GetTagList(GetAccessToken(QyID, true));
            }
            if (result.errcode != ReturnCode_QY.请求成功)
            {
                LogWriter.Info(string.Format("获取QyID为{0}的标签失败,原因:{1}", QyID, result.errmsg));
                throw new Exception(string.Format("获取QyID为{0}的标签失败,原因:{1}", QyID, result.errmsg));
            }
            var oldtags = entities.Set <QyTags>().Where(c => c.QyID == QyID).ToList();
            var users   = entities.Set <QyUser>().Where(c => c.QyID == QyID).ToList();
            var departs = entities.Set <QyDepart>().Where(c => c.QyID == QyID).ToList();

            for (int i = 0; i < users.Count(); i++)
            {
                users[i].TagIDs = "";
            }
            for (int i = 0; i < departs.Count(); i++)
            {
                departs[i].TagIDs = "";
            }
            //新增
            foreach (var add in result.taglist.Where(c => !oldtags.Select(d => d.TagID).Contains(c.tagid)))
            {
                QyTags qt = new QyTags();
                qt.ID    = FormulaHelper.CreateGuid();
                qt.QyID  = QyID;
                qt.TagID = add.tagid;
                qt.Name  = add.tagname;
                entities.Set <QyTags>().Add(qt);

                GetTagMemberResult addresult = null;
                try
                {
                    addresult = MailListApi.GetTagMember(GetAccessToken(QyID), int.Parse(add.tagid));
                }
                catch (Exception ex)
                {
                    LogWriter.Error(ex, string.Format("获取QyID为{0}的标签成员失败", QyID));
                    addresult = MailListApi.GetTagMember(GetAccessToken(QyID, true), int.Parse(add.tagid));
                }
                if (addresult.errcode != ReturnCode_QY.请求成功)
                {
                    LogWriter.Info(string.Format("获取QyID为{0}的标签成员失败,原因:{1}", QyID, result.errmsg));
                    throw new Exception(string.Format("获取QyID为{0}的标签成员失败,原因:{1}", QyID, result.errmsg));
                }
                if (addresult.userlist != null)
                {
                    var tagusers = users.Where(c => addresult.userlist.Select(d => d.userid).Contains(c.UserID)).ToList();
                    for (int i = 0; i < tagusers.Count(); i++)
                    {
                        tagusers[i].TagIDs += tagusers[i].TagIDs == "" ? qt.ID : ("," + qt.ID);
                    }
                }
                if (addresult.partylist != null)
                {
                    var tagdeparts = departs.Where(c => addresult.partylist.Contains(c.DepartID.Value)).ToList();
                    for (int i = 0; i < tagdeparts.Count(); i++)
                    {
                        tagdeparts[i].TagIDs += tagdeparts[i].TagIDs == "" ? qt.ID : ("," + qt.ID);
                    }
                }
            }
            //修改
            foreach (var update in oldtags.Where(c => result.taglist.Select(d => d.tagid).Contains(c.TagID)))
            {
                var item = result.taglist.Where(c => c.tagid == update.TagID).FirstOrDefault();
                update.TagID = item.tagid;
                update.Name  = item.tagname;


                GetTagMemberResult updateresult = null;
                try
                {
                    updateresult = MailListApi.GetTagMember(GetAccessToken(QyID), int.Parse(item.tagid));
                }
                catch (Exception ex)
                {
                    LogWriter.Error(ex, string.Format("获取QyID为{0}的标签成员失败", QyID));
                    updateresult = MailListApi.GetTagMember(GetAccessToken(QyID, true), int.Parse(item.tagid));
                }
                if (updateresult.errcode != ReturnCode_QY.请求成功)
                {
                    LogWriter.Info(string.Format("获取QyID为{0}的标签成员失败,原因:{1}", QyID, result.errmsg));
                    throw new Exception(string.Format("获取QyID为{0}的标签成员失败,原因:{1}", QyID, result.errmsg));
                }
                if (updateresult.userlist != null)
                {
                    var tagusers = users.Where(c => updateresult.userlist.Select(d => d.userid).Contains(c.UserID)).ToList();
                    for (int i = 0; i < tagusers.Count(); i++)
                    {
                        tagusers[i].TagIDs += tagusers[i].TagIDs == "" ? update.ID : ("," + update.ID);
                    }
                }
                if (updateresult.partylist != null)
                {
                    var tagdeparts = departs.Where(c => updateresult.partylist.Contains(c.DepartID.Value)).ToList();
                    for (int i = 0; i < tagdeparts.Count(); i++)
                    {
                        tagdeparts[i].TagIDs += tagdeparts[i].TagIDs == "" ? update.ID : ("," + update.ID);
                    }
                }
            }
            //删除
            foreach (var delete in oldtags.Where(c => !result.taglist.Select(d => d.tagid).Contains(c.TagID)))
            {
                var relateusers = entities.Set <QyUser>().Where(c => c.TagIDs.Contains(delete.ID)).ToList();
                for (int i = 0; i < relateusers.Count(); i++)
                {
                    var oldchildtags = relateusers[i].TagIDs.Split(',').ToList();
                    oldchildtags.Remove(delete.ID);
                    relateusers[i].TagIDs = string.Join(",", oldchildtags);
                }
                entities.Set <QyTags>().Remove(delete);
            }
            entities.SaveChanges();
        }
Exemplo n.º 3
0
        /// <summary>
        /// Get the wechat users and save to database.
        /// </summary>
        /// <returns></returns>
        public static bool GetWechatUserToDB()
        {
            string        token   = AccessTokenContainer.TryGetToken(corpID, corpSecret);
            WechatContext context = new WechatContext();

            //todo: 清空现有用户数据

            //获取微信企业号内的用户架构信息
            Dictionary <string, List <Tag> > _userTags  = new Dictionary <string, List <Tag> >();
            Dictionary <int, List <Tag> >    _partyTags = new Dictionary <int, List <Tag> >();

            Dictionary <string, User> Users = new Dictionary <string, User>();

            //查找所有Tag并插入数据库
            GetTagListResult tagList = MailListApi.GetTagList(token);

            if (tagList != null && tagList.taglist != null && tagList.taglist.Count > 0)
            {
                foreach (var tag in tagList.taglist)
                {
                    int tagId = -1;
                    if (Int32.TryParse(tag.tagid, out tagId))
                    {
                        Tag tempTag = new Tag()
                        {
                            TagId = tagId, TagName = tag.tagname
                        };

                        GetTagMemberResult tagMemberResult = MailListApi.GetTagMember(token, tagId);
                        if (tagMemberResult != null && tagMemberResult.partylist != null && tagMemberResult.partylist.Length > 0)
                        {
                            foreach (int party in tagMemberResult.partylist)
                            {
                                if (!_partyTags.ContainsKey(party))
                                {
                                    _partyTags[party] = new List <Tag>();
                                }

                                _partyTags[party].Add(tempTag);
                            }
                        }

                        if (tagMemberResult != null && tagMemberResult.userlist != null && tagMemberResult.userlist.Count > 0)
                        {
                            foreach (var tagUser in tagMemberResult.userlist)
                            {
                                if (!_userTags.ContainsKey(tagUser.userid))
                                {
                                    _userTags[tagUser.userid] = new List <Tag>();
                                }

                                _userTags[tagUser.userid].Add(tempTag);
                            }
                        }

                        context.Tags.Add(tempTag);
                    }
                }
                context.SaveChanges();
            }

            //查找所有部门并插入数据库
            GetDepartmentListResult departmentList = MailListApi.GetDepartmentList(token);

            if (departmentList != null && departmentList.department != null)
            {
                foreach (var party in departmentList.department)
                {
                    var tempParty = new Party()
                    {
                        PartyId = party.id, Name = party.name, Order = party.order, ParentPartyId = party.parentid
                    };

                    //此处需要查询所有的Tag保存到库中
                    if (_partyTags.ContainsKey(party.id))
                    {
                        tempParty.PartyTags = _partyTags[party.id].Select(f => new PartyTag()
                        {
                            PartyId = tempParty.PartyId, TagId = f.TagId
                        }).ToList();
                    }

                    //根据部门查找所有用户并存入缓存
                    GetDepartmentMemberInfoResult memberInfos = MailListApi.GetDepartmentMemberInfo(token, party.id, 1, 0);
                    if (memberInfos != null && memberInfos.userlist != null && memberInfos.userlist.Count > 0)
                    {
                        foreach (var member in memberInfos.userlist)
                        {
                            if (!Users.ContainsKey(member.userid))
                            {
                                Users[member.userid] = new User()
                                {
                                    Avatar   = member.avatar,
                                    Email    = member.email,
                                    Gender   = member.gender,
                                    Mobile   = member.mobile,
                                    Name     = member.name,
                                    Position = member.position,
                                    Status   = member.status,
                                    UserId   = member.userid,
                                    Weixinid = member.weixinid,
                                    UserTags = (_userTags.ContainsKey(member.userid) && _userTags[member.userid].Count > 0) ? _userTags[member.userid].Select(f => new UserTag()
                                    {
                                        UserId = member.userid, TagId = f.TagId
                                    }).ToList() : null
                                };
                            }
                            Users[member.userid].UserPartys.Add(new UserParty()
                            {
                                PartyId = tempParty.PartyId, UserId = member.userid
                            });
                        }
                    }

                    context.Partys.Add(tempParty);
                }
                context.SaveChanges();
            }

            if (Users != null && Users.Count > 0)
            {
                foreach (var user in  Users.Values)
                {
                    context.Users.Add(user);
                }

                context.SaveChanges();
            }

            return(true);
        }