public async Task <WX_OrganizationUnitDto> UpdateOrganizationUnit(WX_UpdateOrganizationUnitInput input)
        {
            var wxOrganizationUnit = await _weixinOrganizationUnitRepository.GetAsync(input.Id);

            wxOrganizationUnit.DisplayName    = input.DisplayName;
            wxOrganizationUnit.ParentId       = input.ParentId;
            wxOrganizationUnit.WeiXinDepId    = input.WeiXinDepId;
            wxOrganizationUnit.WeiXinParentId = input.WeiXinParentId;
            wxOrganizationUnit.WeiXinCorpId   = input.WeiXinCorpId;

            wxOrganizationUnit.Code = await _weixinOrganizationUnitManager.GetNextChildCodeAsync(wxOrganizationUnit.ParentId);

            await _weixinOrganizationUnitManager.ValidateOrganizationUnitAsync(wxOrganizationUnit);

            await _weixinOrganizationUnitRepository.UpdateAsync(wxOrganizationUnit);

            await CurrentUnitOfWork.SaveChangesAsync();

            return(wxOrganizationUnit.MapTo <WX_OrganizationUnitDto>());
        }
Beispiel #2
0
        /// <summary>
        /// 下载通讯录,递归
        /// </summary>
        /// <param name="api"></param>
        /// <param name="joDeps"></param>
        /// <param name="parentId"></param>
        private async void DownTXL(WeiXinApi api,
                                   string corpId,
                                   List <JToken> joDeps,
                                   string parentId)
        {
            var deps = joDeps.Where(d => d["parentid"].ToString() == parentId).ToList();

            for (int i = 0; i < deps.Count(); i++)
            {
                //用户对应OU的对象
                var userToOrg = new UserToOrganizationUnitInput();

                var departName = deps[i]["name"].ToString();
                var parentid   = deps[i]["parentid"].ToString();
                var id         = deps[i]["id"].ToString();

                var org = await _wx_OrganizationUnitAppService.GetOrganizationUnitsByWXID(corpId, id);

                var parentOrg = await _wx_OrganizationUnitAppService.GetOrganizationUnitsByWXPID(corpId, parentid);

                //var org = orgList.SingleOrDefault(o => o.WeiXinDepId == id);
                if (org == null)
                {//系统没有同步ID就创建
                    //因为同级别 不能用相同名字的部门,这里先查找是否有符合条件的、有就更新
                    org = await _wx_OrganizationUnitAppService.GetOrganizationUnitsByName(departName, parentOrg?.Id);

                    if (org == null)
                    {
                        var input = new WX_CreateOrganizationUnitInput();
                        input.DisplayName    = departName;
                        input.ParentId       = parentOrg?.Id;
                        input.WeiXinDepId    = id;
                        input.WeiXinParentId = parentId;
                        input.WeiXinCorpId   = corpId;
                        var o = await _wx_OrganizationUnitAppService.CreateOrganizationUnit(input);

                        userToOrg.OrganizationUnitId = o.Id;
                    }
                    else
                    {//系统有就更新
                        var input = new WX_UpdateOrganizationUnitInput();
                        input.Id             = org.Id;
                        input.DisplayName    = departName;
                        input.ParentId       = parentOrg?.Id;
                        input.WeiXinDepId    = id;
                        input.WeiXinParentId = parentId;
                        input.WeiXinCorpId   = corpId;
                        var o = await _wx_OrganizationUnitAppService.UpdateOrganizationUnit(input);

                        userToOrg.OrganizationUnitId = o.Id;
                    }
                }
                else
                {//系统有就更新
                    var input = new WX_UpdateOrganizationUnitInput();
                    input.Id             = org.Id;
                    input.DisplayName    = departName;
                    input.ParentId       = parentOrg?.Id;
                    input.WeiXinDepId    = id;
                    input.WeiXinParentId = parentId;
                    input.WeiXinCorpId   = corpId;
                    var o = await _wx_OrganizationUnitAppService.UpdateOrganizationUnit(input);

                    userToOrg.OrganizationUnitId = o.Id;
                }

                JObject joUsers = api.GetUserInfoList(id);
                for (int u = 0; u < joUsers["userlist"].Count(); u++)
                {
                    //对应用户账号
                    var userid = joUsers["userlist"][u]["userid"].ToString();
                    //用户姓名
                    var username = joUsers["userlist"][u]["name"].ToString();
                    var email    = joUsers["userlist"][u]["email"].ToString();
                    var mobile   = joUsers["userlist"][u]["mobile"].ToString();

                    var user = await _wx_UserAppService.GetUsersByWeiXinUID(userid);

                    if (user == null)
                    {//没有找到对应的微信账号
                        user = await _wx_UserAppService.GetUsersByWeiUserName(userid);

                        if (user == null)//找对应的用户账号
                        {
                            userToOrg.UserId = await CreateUser(username, email, mobile, userid, "123456");
                        }
                        else
                        {
                            userToOrg.UserId = await UpdateUser(user, username, email, mobile, userid, "123456");
                        }
                    }
                    else
                    {//有就更新账号
                        userToOrg.UserId = await UpdateUser(user, username, email, mobile, userid, "123456");
                    }

                    //更新用户对应的OU
                    await _wx_OrganizationUnitAppService.AddUserToOrganizationUnit(userToOrg);
                }
                DownTXL(api, corpId, joDeps, id);
            }
        }