///// <summary>
        ///// 插入组织架构
        ///// </summary>
        ///// <param name="input"></param>
        ///// <returns></returns>
        private async Task <Organization> CreateSyncOrganizationAsync(OrganizationListDto input)
        {
            var entity = ObjectMapper.Map <Organization>(input);

            entity = await _entityRepository.InsertAsync(entity);

            return(entity.MapTo <Organization>());
        }
Esempio n. 2
0
        /// <summary>
        /// 同步组织架构&内部员工
        /// </summary>
        public async Task <APIResultDto> SynchronousOrganizationAsync()
        {
            //var arr = GetAreaCodeArray();  取消区县更新 改为区县配置
            string accessToken = _dingDingAppService.GetAccessTokenByApp(DingDingAppEnum.会议申请); //GetAccessToken();
            //IDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/department/list");
            //OapiDepartmentListRequest request = new OapiDepartmentListRequest();
            //request.SetHttpMethod("GET");
            //OapiDepartmentListResponse response = client.Execute(request, accessToken);
            var depts      = Get.GetJson <DingDepartmentDto>(string.Format("https://oapi.dingtalk.com/department/list?access_token={0}", accessToken));
            var entityByDD = depts.department.Select(o => new OrganizationListDto()
            {
                Id             = o.id,
                DepartmentName = o.name,
                ParentId       = o.parentid,
                CreationTime   = DateTime.Now
            }).ToList();

            var originEntity = await _organizationRepository.GetAll().ToListAsync();

            foreach (var item in entityByDD)
            {
                var o = originEntity.Where(r => r.Id == item.Id).FirstOrDefault();
                if (o != null)
                {
                    o.DepartmentName = item.DepartmentName;
                    o.ParentId       = item.ParentId;
                    o.CreationTime   = DateTime.Now;
                    if (o.Id != 1)
                    {
                        await SynchronousEmployeeAsync(o.Id, accessToken);
                    }
                }
                else
                {
                    var organization = new OrganizationListDto();
                    organization.Id             = item.Id;
                    organization.DepartmentName = item.DepartmentName;
                    organization.ParentId       = item.ParentId;
                    organization.CreationTime   = DateTime.Now;
                    await CreateSyncOrganizationAsync(organization);

                    if (organization.Id != 1)
                    {
                        await SynchronousEmployeeAsync(organization.Id, accessToken);
                    }
                }
            }
            await CurrentUnitOfWork.SaveChangesAsync();

            return(new APIResultDto()
            {
                Code = 0, Msg = "同步组织架构成功"
            });
        }
        public async Task <APIResultDto> SynchronousOrganizationAsync()
        {
            string accessToken = await _dingTalkManager.GetAccessTokenByAppAsync(DingDingAppEnum.智能办公); //GetAccessToken();

            var depts      = Get.GetJson <DingDepartmentDto>(string.Format("https://oapi.dingtalk.com/department/list?access_token={0}", accessToken));
            var entityByDD = depts.department.Select(o => new OrganizationListDto()
            {
                Id             = o.id,
                DepartmentName = o.name,
                ParentId       = o.parentid,
                CreationTime   = DateTime.Now
            }).ToList();

            var originEntity = await _entityRepository.GetAll().ToListAsync();

            foreach (var item in entityByDD)
            {
                var o = originEntity.Where(r => r.Id == item.Id).FirstOrDefault();
                if (o != null)
                {
                    o.DepartmentName = item.DepartmentName;
                    o.ParentId       = item.ParentId;
                    o.CreationTime   = DateTime.Now;
                    if (o.Id != 1)
                    {
                        await SynchronousEmployeeAsync(o.Id, accessToken);
                    }
                }
                else
                {
                    var organization = new OrganizationListDto();
                    organization.Id             = item.Id;
                    organization.DepartmentName = item.DepartmentName;
                    organization.ParentId       = item.ParentId;
                    organization.CreationTime   = DateTime.Now;
                    await CreateSyncOrganizationAsync(organization);

                    if (organization.Id != 1)
                    {
                        await SynchronousEmployeeAsync(organization.Id, accessToken);
                    }
                }
            }
            await CurrentUnitOfWork.SaveChangesAsync();

            return(new APIResultDto()
            {
                Code = 0, Msg = "同步组织架构成功"
            });
        }