public GetDepartmentListResult GetDepartmentList(GetDepartmentListDto dto) { //dto.AccessToken = GetAccessToken(QyConfig.CorpID, QyConfig.CorpSecret); var result = MailListApi.GetDepartmentList(dto.AccessToken, dto.ID); return(result); }
public JsonResult GetSubDepartment(string id, string departlist) { string accessToken = WeChatCommonService.GetWeiXinToken(AppId); //修改Department数据源 先根据AppId获取应用信息 allowPartys Pending var config = WeChatCommonService.GetWeChatConfigByID(AppId); var app = AppApi.GetAppInfo(accessToken, int.Parse(config.WeixinAppId)); GetAppInfo_AllowPartys allowPartys = app.allow_partys; // TODO: async/await执行较长等待的task var subdepartList = MailListApi.GetDepartmentList(accessToken, Int32.Parse(id)).department;//.Where(x => allowPartys.partyid.Contains(x.id)).ToList() var listReturn = EasyUITreeData.GetTreeData(subdepartList, "id", "name", "parentid"); listReturn.ForEach(a => { a.state = "closed"; }); if (!string.IsNullOrEmpty(departlist)) { var departids = departlist.Split('|'); EasyUITreeData.SetChecked <string>(departids.ToList(), listReturn); } return(Json(listReturn, JsonRequestBehavior.AllowGet)); }
private List <PersonGroup> GetListByGroupId(string groupId, List <SysAddressBookMember> userInfos) { var allDeptList = MailListApi.GetDepartmentList(GetToken()).department; var subDepts = allDeptList.Where(a => a.parentid == Int32.Parse(groupId)); List <PersonGroup> list = new List <PersonGroup>(); foreach (var dept in subDepts) { PersonGroup person = new PersonGroup(); person.WeixinId = dept.id.ToString(); person.WeixinName = dept.name; person.Type = "Group"; list.Add(person); } var members = userInfos.Where(a => a.Department.Replace("[", ",").Replace("]", ",").Contains("," + groupId + ",")).ToList(); foreach (var member in members) { PersonGroup person = new PersonGroup(); person.WeixinId = member.UserId; person.WeixinName = member.UserName; person.Avatar = member.Avatar; person.Type = "Person"; list.Add(person); } return(list); }
public IActionResult OAuth(string code, string state) { var tokenResult = CommonApi.GetToken(WeixinOptions.CorpId, WeixinOptions.Secret); var openInfo = new Senparc.Weixin.QY.AdvancedAPIs.OAuth2.GetUserInfoResult(); try { openInfo = OAuth2Api.GetUserId(tokenResult.access_token, code); } catch (ErrorJsonResultException e) when(e.JsonResult.errcode == Senparc.Weixin.ReturnCode.合法的oauth_code) { ViewData["Message"] = Newtonsoft.Json.JsonConvert.SerializeObject(e.JsonResult, Newtonsoft.Json.Formatting.Indented); return(View()); } //catch (ArgumentNullException e) //{ // ViewData["Message"] = e.Message; // return View(); //} if (string.IsNullOrEmpty(openInfo.UserId)) { ViewData["Message"] = "非企业人员,考勤功能不可用!"; } else { //ViewData["Member"] = MailListApi.GetMember(tokenResult.access_token, openInfo.UserId); ViewData["DepartmentList"] = MailListApi.GetDepartmentList(tokenResult.access_token); } return(View()); }
public string GetDisplayName(string appId, int AccountManageID, string userIds = "", string groupIds = "") { var appInfo = lstSysWeChatConfig.SingleOrDefault(a => a.AccountManageId == AccountManageID); var token = CommonApi.GetToken(appInfo.WeixinCorpId, appInfo.WeixinCorpSecret); StringBuilder sb = new StringBuilder(); if (!string.IsNullOrEmpty(userIds)) { var users = userIds.Split(','); foreach (var user in users) { sb.Append(MailListApi.GetMember(token.access_token, user).name).Append(","); } } if (!string.IsNullOrEmpty(groupIds)) { sb.Append("|"); var groups = groupIds.Split(','); foreach (var group in groups) { sb.Append(MailListApi.GetDepartmentList(token.access_token, Int32.Parse(group)).department[0].name).Append(","); } } return(sb.ToString().TrimEnd(',')); }
public TreeNode <DepartmentList> GetTree() { var result = MailListApi.GetDepartmentList(APP_KEY); var tree = InitTree(result.department); return(tree); }
public ActionResult GetDepartmentList() { var accessToken = Senparc.Weixin.QY.Containers.AccessTokenContainer.TryGetToken(CorpId, CorpSecrets); GetDepartmentListResult result = MailListApi.GetDepartmentList(accessToken); return(Json(result.department, JsonRequestBehavior.AllowGet)); }
public WorkJsonResult WX_GetDepartmentList() { WorkJsonResult Ret = new WorkJsonResult(); if (Qyinfo.IsUseWX == "Y") { Ret = MailListApi.GetDepartmentList(GetToken()); } return(Ret); }
//[TestMethod] public void GetDepartmentListTest() { var accessToken = AccessTokenContainer.GetToken(_corpId, base._corpSecret); var result = MailListApi.GetDepartmentList(accessToken, 4); var result1 = MailListApi.GetDepartmentList(accessToken); Assert.IsNotNull(result); Assert.IsNotNull(result1); Assert.IsTrue(result.errcode == ReturnCode_Work.请求成功); }
public List <Department> GetDepartments(string accessToken) { var result = MailListApi.GetDepartmentList(accessToken); if (result.errcode != ReturnCode_QY.请求成功) { throw new Exception(result.errcode.ToString()); } return(result.department.Select(x => new Department(x.id, x.name, x.parentid, x.order)).ToList()); }
public static bool DeleteWxDepartment(int departmentId) { if (MailListApi.GetDepartmentList(GetToken(), 0).department.Any(m => m.id == departmentId)) { var result = MailListApi.DeleteDepartment(GetToken(), departmentId.ToString()); return(result.errcode == ReturnCode_QY.请求成功); } else { return(true);; } }
public GetDepartmentListResult WX_GetBranchList(string strDeptCode) { GetDepartmentListResult Ret = new GetDepartmentListResult(); int?id = null; if (!string.IsNullOrEmpty(strDeptCode)) { id = Int32.Parse(strDeptCode); } if (Qyinfo.IsUseWX == "Y") { Ret = MailListApi.GetDepartmentList(GetToken(), id); } return(Ret); }
/// <summary> /// 当前用户是否在指定部门 /// </summary> /// <param name="deptName">部门名称,可以是子部门</param> /// <returns>true|false</returns> public bool IsInDept(string deptName, WorkOption option) { if(string.IsNullOrEmpty(option.AccessToken)) option.AccessToken = AccessTokenContainer.TryGetToken(option.CorpId, option.Secret); GetDepartmentMemberInfoResult membersinfo = MailListApi.GetDepartmentMemberInfo(option.AccessToken, 1, 1); GetMemberResult user = membersinfo.userlist.FirstOrDefault(m => m.name == CurrentUser); GetDepartmentListResult depts = MailListApi.GetDepartmentList(option.AccessToken); long landDeptId = depts.department.First(d => d.name == deptName).id; foreach (var deptId in user.department) { if (deptId == landDeptId) return true; } return false; }
public override ActionResult Get(string sID) { string strID = Request["Id"]; if (!string.IsNullOrEmpty(strID)) { int Id = int.Parse(strID); var strToken = GetToken(); var result = MailListApi.GetDepartmentList(strToken); return(Json(result.department.Find(a => a.id == Id), JsonRequestBehavior.AllowGet)); } return(Json(null, JsonRequestBehavior.AllowGet)); }
private List <DepartmentList> Init() { var token = AccessTokenContainer.TryGetToken(WeixinWorkOptions.CorpId, WeixinWorkOptions.Secret); var result = MailListApi.GetDepartmentList(token); if (result.errcode != Senparc.Weixin.ReturnCode_Work.请求成功) { throw new Exception("部门列表接口获取失败"); } else { //_cache.Set("WeixinDepartments", Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(result.department))); _cacheHelper.Set("WeixinDepartments", result.department); return(result.department); } }
/// <summary> /// 获取部门列表 /// </summary> /// <param name="_id"></param> /// <returns></returns> public static List <DepartmentList> GetDepartmentList(int _id) { AccessTokenResult access_token_result = new AccessTokenResult(); QYConfigInfo config = GetQYConfig(); access_token_result = CommonApi.GetToken(config.QYCorpId, config.QYSecret); //调用接口 var result = MailListApi.GetDepartmentList(access_token_result.access_token, _id); if (result.errmsg == "ok") { return(result.department); } else { return(null); } }
public List <PersonGroup> GetAllVisibleByGroup(string appId, string groupId, int accountManageId) { var id = int.Parse(appId); SysWechatConfig appInfo = null; if (id == 0) { appInfo = lstSysWeChatConfig.FirstOrDefault(a => a.AccountManageId == accountManageId); } else { appInfo = lstSysWeChatConfig.SingleOrDefault(a => a.Id == id && a.AccountManageId == accountManageId); } string token = (appInfo.IsCorp != null && !appInfo.IsCorp.Value) ? Innocellence.Weixin.MP.CommonAPIs.AccessTokenContainer.GetToken(appInfo.WeixinCorpId, appInfo.WeixinCorpSecret) : Innocellence.Weixin.QY.CommonAPIs.AccessTokenContainer.TryGetToken(appInfo.WeixinCorpId, appInfo.WeixinCorpSecret); var allDeptList = MailListApi.GetDepartmentList(token).department; var subDepts = allDeptList.Where(a => a.parentid == Int32.Parse(groupId)); List <PersonGroup> list = new List <PersonGroup>(); foreach (var dept in subDepts) { PersonGroup person = new PersonGroup(); person.WeixinId = dept.id.ToString(); person.WeixinName = dept.name; person.Type = "Group"; list.Add(person); } var members = MailListApi.GetDepartmentMemberInfo(token, Int32.Parse(groupId), 0, 1); foreach (var member in members.userlist) { PersonGroup person = new PersonGroup(); person.WeixinId = member.userid; person.WeixinName = member.name; person.Avatar = member.avatar; person.Type = "Person"; list.Add(person); } return(list); }
/// <summary> /// 获取组织 /// </summary> /// <param name="QyID">企业ID</param> /// <param name="id">部门ID。获取指定部门ID下的子部门</param> /// <returns>指定部门及其下的子部门</returns> public GetDepartmentListResult GetDepartmentList(string QyID, int?id = null) { GetDepartmentListResult result = null; try { result = MailListApi.GetDepartmentList(GetAccessToken(QyID), id); } catch (Exception ex) { LogWriter.Error(ex, string.Format("获取QyID为{0}的部门失败", QyID)); result = MailListApi.GetDepartmentList(GetAccessToken(QyID, true), id); } if (result.errcode != ReturnCode_QY.请求成功) { LogWriter.Info(string.Format("获取QyID为{0}的部门失败,原因:{1}", QyID, result.errmsg)); } return(result); }
public static void DownloadDatafromWX() { QYConfig.RegistWX(); var accessToken = AccessTokenContainer.GetToken(QYConfig.CorpId); var result = MailListApi.GetDepartmentList(accessToken); for (int i = 0; i < result.department.Count; i++) { DepartmentDA.AddDepartment(result.department[i].id.ToString(), result.department[i].parentid.ToString(), result.department[i].name, "1", "1"); } QYDepartmentBL d = GetAllDepartments(); var l = MailListApi.GetDepartmentMemberInfo(accessToken, 1, 1, 0); for (int i = 0; i < l.userlist.Count; i++) { QYMemberDA mda = new QYMemberDA(); QYDepartmentBL p = QYDepartmentBL.GetByWXID(l.userlist[i].department[0].ToString()); mda.AddMember(l.userlist[i].userid, l.userlist[i].name, l.userlist[i].position, l.userlist[i].mobile, l.userlist[i].email, l.userlist[i].weixinid, l.userlist[i].avatar, l.userlist[i].status.ToString(), "1", p.ID.ToString()); } }
public static List <DepartmentList> lstDepartment(int AccountManageId) { // Logger.Debug("Getting lstDepartment"); var lst = cacheManager.Get <List <DepartmentList> >("DepartmentList", () => { Logger.Debug("cache is empty, creating cache...."); var Config = WeChatCommonService.lstSysWeChatConfig.FirstOrDefault(a => a.AccountManageId == AccountManageId); var strToken = AccessTokenContainer.TryGetToken(Config.WeixinCorpId, Config.WeixinCorpSecret); var result = MailListApi.GetDepartmentList(strToken); var departments = result.department; //var departmentID = int.Parse(CommonService.GetSysConfig("DimissionDepartment", "")); //var dismissionDepartment = departments.Where(d => d.id == departmentID).FirstOrDefault(); //if (dismissionDepartment != null) //{ // departments.Remove(dismissionDepartment); //} return(departments); }); // Logger.Debug("returning lstDepartment"); return(lst); }
public static List <DepartmentList> GetAllDepartment() { var result = MailListApi.GetDepartmentList(GetToken()); return(result.department); }
public GetDepartmentListResult Get() { var result = MailListApi.GetDepartmentList(option.AccessToken); return(result); }
public GetDepartmentListResult Get() { var result = MailListApi.GetDepartmentList(APP_KEY); return(result); }
/// <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); }
//public SysWechatConfig GetAppSysWechatConfig(string appId) { // return lstSysWeChatConfig.Find(a => a.WeixinAppId == appId); //} public List <PersonGroup> GetAllPersonAndGroup(string appId, int accountManageId) { var id = int.Parse(appId); SysWechatConfig appInfo = null; if (id == 0) { appInfo = lstSysWeChatConfig.FirstOrDefault(a => a.AccountManageId == accountManageId); } else { appInfo = lstSysWeChatConfig.SingleOrDefault(a => a.Id == id && a.AccountManageId == accountManageId); } string token = (appInfo.IsCorp != null && !appInfo.IsCorp.Value) ? Innocellence.Weixin.MP.CommonAPIs.AccessTokenContainer.GetToken(appInfo.WeixinCorpId, appInfo.WeixinCorpSecret) : Innocellence.Weixin.QY.CommonAPIs.AccessTokenContainer.TryGetToken(appInfo.WeixinCorpId, appInfo.WeixinCorpSecret); var results = AppApi.GetAppInfo(token, int.Parse(appInfo.WeixinAppId)); List <PersonGroup> list = new List <PersonGroup>(); if (results.allow_userinfos != null) { foreach (var person in results.allow_userinfos.user) { PersonGroup personGroup = new PersonGroup(); personGroup.Type = "Person"; var p = MailListApi.GetMember(token, person.userid); personGroup.WeixinName = p.name; personGroup.WeixinId = p.userid; personGroup.Avatar = p.avatar; list.Add(personGroup); } } if (results.allow_partys != null) { foreach (var groupId in results.allow_partys.partyid) { PersonGroup personGroup = new PersonGroup(); personGroup.Type = "Group"; var d = MailListApi.GetDepartmentList(token, groupId).department.FirstOrDefault(); if (d != null) { personGroup.WeixinName = d.name; personGroup.WeixinId = d.id.ToString(); list.Add(personGroup); } } } if (results.allow_tags != null) { var allTagList = MailListApi.GetTagList(token).taglist; foreach (var groupId in results.allow_tags.tagid) //foreach (var tag in allTagList) { PersonGroup personGroup = new PersonGroup(); personGroup.Type = "Tag"; var tag = allTagList.Find(a => a.tagid == groupId.ToString()); if (tag != null) { personGroup.WeixinName = tag.tagname; personGroup.WeixinId = tag.tagid; list.Add(personGroup); } } } return(list); }
public ActionResult GetUserInfo() { string accessToken = GetToken(); var tags = MailListApi.GetTagList(accessToken).taglist; var empDetails = MailListApi.GetDepartmentMemberInfo(accessToken, 1, 1, 0); // 更新这些人的tag为N foreach (var e in empDetails.userlist) { e.tags = new Dictionary <string, string>(); foreach (var t in tags) { e.tags[t.tagid] = "N"; } } // 获取部门列表 var departments = MailListApi.GetDepartmentList(accessToken); // 处理部门关系,暂定只有5层 foreach (var dept in departments.department) { int level = 1; int parentId = dept.parentid; while (parentId != 0) { var parentDept = departments.department.Where(t => t.id == parentId).FirstOrDefault(); if (parentDept != null) { level++; parentId = parentDept.parentid; } } dept.level = level; } // 更新员工信息,把部门的名称填入 foreach (var emp in empDetails.userlist) { // 如果有多个部门的话,只列出其中一个部门。 if (emp.department.Count() >= 1) { var dept = departments.department.Where(t => t.id == emp.department[0]).FirstOrDefault(); if (dept != null) { int level = dept.level; while (level > 1) { emp.deptLvs[level] = dept.name; dept = departments.department.Where(t => t.id == dept.parentid).FirstOrDefault(); level--; } } if (emp.department.Count() > 1) { emp.name += "*"; } } } // 获取每个标签里的人 foreach (var tagitem in tags) { var tagInfo = MailListApi.GetTagMember(accessToken, int.Parse(tagitem.tagid)); var tagUsers = tagInfo.userlist; //tagInfo.partylist; var departList = WeChatCommonService.GetSubDepartments(tagInfo.partylist).ToList(); foreach (var u in tagUsers) { var employee = empDetails.userlist.FirstOrDefault(a => a.userid == u.userid); if (employee != null) { employee.tags[tagitem.tagid] = "Y"; } } // var u1 = u; var usersUnderCurrentDepartment = WeChatCommonService.lstUserWithDeptTag.AsParallel() .Where(x => x.department.Any(y => departList.Any(id => id.id == y))).Select(x => x.userid).ToList(); var tagitem1 = tagitem; //var user = empDetails.userlist.AsParallel() // .FirstOrDefault(x => x.tags[tagitem1.tagid] == "N" && usersUnderCurrentDepartment.Any(y => y == x.userid)); //if (user != null) //{ // user.tags[tagitem.tagid] = "Y"; //} Parallel.ForEach(empDetails.userlist, userItem => { if (userItem.tags[tagitem1.tagid] == "N" && usersUnderCurrentDepartment.Any(y => y == userItem.userid)) { userItem.tags[tagitem1.tagid] = "Y"; } }); //if (userWithDept != null) //{ // if (userWithDept.department.Any(x => departList.Any(y => x == y.id))) // { // // employee.tags[tagitem.tagid] = "Y"; // } //} } return(ExportToCSV(empDetails.userlist, tags)); }
public ActionResult UploadUser() { try { //同步部门 GetDepartmentListResult result = MailListApi.GetDepartmentList(WeixinWorkService.GetToken()); var departments = dbContext.SysDepartment.OrderByDescending(c => c.DepartmentName).ToList(); foreach (var item in departments) { if (item.DepartmentId == Guid.Parse("2379788E-45F0-417B-A103-0B6440A9D55D")) { continue; } var parentId = Convert.ToInt64(departments.Where(c => c.DepartmentId == item.ParentId).FirstOrDefault().WeixinWorkId); DepartmentList qyDep = result.department.Where(c => c.id == item.WeixinWorkId).FirstOrDefault(); if (qyDep == null) { var createResult = MailListApi.CreateDepartment(WeixinWorkService.GetToken(), item.DepartmentName, parentId == 0 ? 1 : parentId, 1000 - (item.OrderNo.HasValue ? item.OrderNo.Value : 0)); item.WeixinWorkId = createResult.id; } else { MailListApi.UpdateDepartment(WeixinWorkService.GetToken(), qyDep.id, item.DepartmentName, parentId == 0 ? 1 : parentId, 1000 - (item.OrderNo.HasValue ? item.OrderNo.Value : 0)); } dbContext.SaveChanges(); } //同步用户 var users = dbContext.SysUser.Where(c => c.UserCode != "admin").ToList(); foreach (var userItem in users) { long[] longArr = new long[1]; longArr[0] = Convert.ToInt64(dbContext.SysDepartment.Where(c => c.DepartmentId == userItem.DepartmentId).FirstOrDefault().WeixinWorkId); try { var memberResult = MailListApi.GetMember(WeixinWorkService.GetToken(), userItem.UserCode); if (memberResult.errcode == Senparc.Weixin.ReturnCode_Work.UserID不存在) { MemberCreateRequest request = new MemberCreateRequest(); request.email = userItem.Email; request.department = longArr; request.enable = 1; request.mobile = userItem.Mobile; request.name = userItem.UserName; request.userid = userItem.UserCode; MailListApi.CreateMember(WeixinWorkService.GetToken(), request); } else { MemberUpdateRequest updateRequest = new MemberUpdateRequest(); updateRequest.department = longArr; updateRequest.email = userItem.Email; updateRequest.enable = 1; updateRequest.mobile = userItem.Mobile; updateRequest.name = userItem.UserName; updateRequest.userid = userItem.UserCode; MailListApi.UpdateMember(WeixinWorkService.GetToken(), updateRequest); } } catch { } } return(Json(new { Code = 0, Msg = "同步成功!" })); } catch (Exception ex) { LogHelper.SaveLog(ex); return(Json(new { Code = 1, Msg = "服务器异常,请联系管理员!" })); } }
/// <summary> /// 更新部门 /// </summary> /// <param name="QyID"></param> private void ReFreshDepart(string QyID) { GetDepartmentListResult result = null; try { result = MailListApi.GetDepartmentList(GetAccessToken(QyID)); } catch (Exception ex) { LogWriter.Error(ex, string.Format("获取QyID为{0}的部门失败", QyID)); result = MailListApi.GetDepartmentList(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 olddepts = entities.Set <QyDepart>().Where(c => c.QyID == QyID).ToList(); List <QyDepart> addlist = new List <QyDepart>(), alllist = new List <QyDepart>(); //新增 foreach (var add in result.department.Where(c => !olddepts.Select(d => d.DepartID).Contains(c.id))) { QyDepart qd = new QyDepart(); qd.ID = FormulaHelper.CreateGuid(); qd.QyID = QyID; qd.DepartName = add.name; qd.ParentDepartID = add.parentid; qd.DepartID = add.id; qd.DepartOrder = add.order; addlist.Add(qd); alllist.Add(qd); } //修改 foreach (var update in olddepts.Where(c => result.department.Select(d => d.id).Contains(c.DepartID.Value))) { var item = result.department.Where(c => c.id == update.DepartID).FirstOrDefault(); update.DepartName = item.name; update.ParentDepartID = item.parentid; update.DepartOrder = item.order; alllist.Add(update); } //删除 foreach (var delete in olddepts.Where(c => !result.department.Select(d => d.id).Contains(c.DepartID.Value))) { var relateusers = entities.Set <QyUser>().Where(c => c.DepartIDs.Contains(delete.ID)).ToList(); for (int i = 0; i < relateusers.Count(); i++) { var olddeparts = relateusers[i].DepartIDs.Split(',').ToList(); olddeparts.Remove(delete.ID); relateusers[i].DepartIDs = string.Join(",", olddeparts); } entities.Set <QyDepart>().Remove(delete); } //更新树形字段 var root = alllist.Where(c => !alllist.Select(d => d.DepartID).Contains(c.ParentDepartID)).FirstOrDefault(); if (root != null) { root.ParentID = ""; root.Length = 1; root.FullPath = root.ID; root.ChildCount = alllist.Where(c => c.ParentDepartID == root.DepartID).Count(); var ids = new int?[] { root.DepartID }; var parentlength = 1; while (ids.Count() > 0) { var children = alllist.Where(c => ids.Contains(c.ParentDepartID)).ToList(); for (int i = 0; i < children.Count(); i++) { var child = children[i]; var parent = alllist.Where(c => c.DepartID == child.ParentDepartID).FirstOrDefault(); if (parent == null) { continue; } child.ParentID = parent.ID; child.Length = parentlength + 1; child.FullPath = parent.FullPath + "." + child.ID; child.ChildCount = alllist.Where(c => c.ParentDepartID == child.DepartID).Count(); } ids = children.Select(c => c.DepartID).ToArray(); parentlength++; } } foreach (var item in addlist) { entities.Set <QyDepart>().Add(item); } entities.SaveChanges(); }
private GetDepartmentListResult GetDepartmentList() { var tokenResult = CommonApi.GetToken(WeixinOptions.CorpId, WeixinOptions.Secret); return(MailListApi.GetDepartmentList(tokenResult.access_token)); }
private async Task <GetDepartmentListResult> getDepartments(string token) { return(MailListApi.GetDepartmentList(token)); }