123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225 |
- using Microsoft.AspNetCore.Http;
- using Microsoft.AspNetCore.Mvc;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using Microsoft.Extensions.Configuration;
- using TEAMModelOS.SDK.DI;
- using TEAMModelOS.Models;
- using DingTalk.Api;
- using DingTalk.Api.Request;
- using DingTalk.Api.Response;
- using TEAMModelOS.SDK.Models.Cosmos.BI;
- using System.Text.Json;
- using Azure.Cosmos;
- namespace TEAMModeBI.Controllers.DingDingStruc
- {
- [Route("branch")]
- [ApiController]
- public class DDDeptController : ControllerBase
- {
- private readonly IConfiguration _configuration;
- //数据容器
- private readonly AzureCosmosFactory _azureCosmos;
- //钉钉提示信息
- private readonly DingDing _dingDing;
- private readonly Option _option;
- public DDDeptController(IConfiguration configuration,AzureCosmosFactory azureCosmos)
- {
- _configuration = configuration;
- _azureCosmos = azureCosmos;
- }
- #region 从钉钉拿数据存CosmosDB中
- /// <summary>
- /// 获取部门机构
- /// </summary>
- /// <returns></returns>
- [ProducesDefaultResponseType]
- [HttpPost("get-depts")]
- public async Task<IActionResult> GetDDDepts()
- {
- try
- {
- string appKey = _configuration["DingDingAuth:appKey"];
- string appSecret = _configuration["DingDingAuth:appSecret"];
- string agentld = _configuration["DingDingAuth:Agentld"];
- //获取access_token
- DefaultDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");
- OapiGettokenRequest request = new OapiGettokenRequest() { Appkey = appKey, Appsecret = appSecret };
- request.SetHttpMethod("Get");
- OapiGettokenResponse response = client.Execute(request);
- if (response.IsError)
- {
- return BadRequest();
- }
- //access_token的有效期为7200秒(2小时),有效期内重复获取会返回相同结果并自动续期,过期后获取会返回新的access_token
- string access_token = response.AccessToken;
- //获取一级部门列表
- IDingTalkClient V2departClient = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/department/listsub");
- OapiV2DepartmentListsubRequest reqPartment1 = new OapiV2DepartmentListsubRequest() { DeptId = 1L, Language = "zh_CN" };
- OapiV2DepartmentListsubResponse rspPartment1 = V2departClient.Execute(reqPartment1, access_token);
- if (rspPartment1.IsError)
- {
- return BadRequest();
- }
- DeptNode deptNodes = new DeptNode();
- deptNodes.id = agentld;
- deptNodes.code = "DDPartment";
- deptNodes.pk = "DDPartment";
- deptNodes.ttl = -1;
- List<DeptInfo> deptlist = new List<DeptInfo>();
- if (rspPartment1.Result != null)
- {
- foreach (var depts in rspPartment1.Result)
- {
- DeptInfo deptInfo = new DeptInfo();
- deptInfo.id = depts.DeptId;
- deptInfo.pid = depts.ParentId;
- deptInfo.name = depts.Name;
- //获取一级部门用户列表
- IDingTalkClient userListClient1 = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/user/listid");
- OapiUserListidRequest reqUsers1 = new OapiUserListidRequest() { DeptId = depts.DeptId };
- OapiUserListidResponse rspUsers1 = userListClient1.Execute(reqUsers1, access_token);
- if (rspUsers1.Result != null)
- {
- deptInfo.users = rspUsers1.Result.UseridList;
- }
- //获取二级部门列表
- OapiV2DepartmentListsubRequest reqPartment2 = new OapiV2DepartmentListsubRequest() { DeptId = depts.DeptId, Language = "zh_CN" };
- OapiV2DepartmentListsubResponse rspPartment2 = V2departClient.Execute(reqPartment2, access_token);
- if (rspPartment2.Result != null)
- {
- foreach (var depts2 in rspPartment2.Result)
- {
- DeptInfo deptInfo2 = new DeptInfo();
- deptInfo2.id = depts2.DeptId;
- deptInfo2.pid = depts2.ParentId;
- deptInfo2.name = depts2.Name;
- //获取三级部门用户列表
- OapiUserListidRequest reqUsers2 = new OapiUserListidRequest() { DeptId = depts2.DeptId };
- OapiUserListidResponse rspUsers2 = userListClient1.Execute(reqUsers2, access_token);
- if (rspUsers2.Result != null)
- {
- //添加三级部门用户
- deptInfo2.users = rspUsers2.Result.UseridList;
- }
- //获取三级部门列表
- OapiV2DepartmentListsubRequest reqPartment3 = new OapiV2DepartmentListsubRequest() { DeptId = depts2.DeptId, Language = "zh_CN" };
- OapiV2DepartmentListsubResponse rspPartment3 = V2departClient.Execute(reqPartment3, access_token);
- if (rspPartment3.Result != null)
- {
- foreach (var depts3 in rspPartment3.Result)
- {
- DeptInfo deptInfo3 = new DeptInfo();
- deptInfo3.id = depts3.DeptId;
- deptInfo3.pid = depts3.ParentId;
- deptInfo3.name = depts3.Name;
- //获取三级部门用户列表
- OapiUserListidRequest reqUsers3 = new OapiUserListidRequest() { DeptId = depts3.DeptId };
- OapiUserListidResponse rspUsers3 = userListClient1.Execute(reqUsers3, access_token);
- if (rspUsers3.Result != null)
- {
- //添加三级部门用户
- deptInfo3.users = rspUsers3.Result.UseridList;
- }
- //获取四级部门列表
- OapiV2DepartmentListsubRequest reqPartment4 = new OapiV2DepartmentListsubRequest() { DeptId = depts3.DeptId, Language = "zh_CN" };
- OapiV2DepartmentListsubResponse rspPartment4 = V2departClient.Execute(reqPartment4, access_token);
- if (rspPartment4.Result != null)
- {
- foreach (var depts4 in rspPartment4.Result)
- {
- DeptInfo deptInfo4 = new DeptInfo();
- deptInfo4.id = depts4.DeptId;
- deptInfo4.pid = depts4.ParentId;
- deptInfo4.name = depts4.Name;
- //获取四级部门用户列表
- OapiUserListidRequest reqUsers4 = new OapiUserListidRequest() { DeptId = depts4.DeptId };
- OapiUserListidResponse rspUsers4 = userListClient1.Execute(reqUsers4, access_token);
- if (rspUsers4.Result != null)
- {
- //添加四级部门用户
- deptInfo4.users = rspUsers4.Result.UseridList;
- }
- deptlist.Add(deptInfo4);
- }
- }
- deptlist.Add(deptInfo3);
- }
- }
- deptlist.Add(deptInfo2);
- }
- }
- deptlist.Add(deptInfo);
- }
- }
- deptNodes.depts = deptlist;
- await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync<DeptNode>(deptNodes, new Azure.Cosmos.PartitionKey($"DDPartment"));
- return Ok(new { state = 200, message = "钉钉的组织架构添加到数据库成功", deptNodes = deptNodes });
- }
- catch (Exception ex)
- {
- return Ok(new { state = 1, message = $"访问失败!状态:{ex.StackTrace} 错误:{ex.Message}" });
- }
- }
- #endregion
- #region 从数据库获取(设置、修改、)钉钉组织架构信息
- /// <summary>
- /// 依据当前部门编号获取下级部门信息
- /// </summary>
- /// <param name="jsonElement"></param>
- /// <returns></returns>
- [ProducesDefaultResponseType]
- [HttpPost("get-partment")]
- public async Task<IActionResult> GetPartment(JsonElement jsonElement)
- {
- try
- {
- if (!jsonElement.TryGetProperty("pid", out JsonElement pid)) return BadRequest();
- var client = _azureCosmos.GetCosmosClient();
- List<DeptInfo> deptInfos = new List<DeptInfo>();
- string sqltxt = $"select a1.id,a1.pid,a1.name,a1.users from c join a1 IN c.depts where a1.pid={pid}";
- await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Normal").GetItemQueryIterator<DeptInfo>(queryText: sqltxt, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("DDPartment") }))
- {
- deptInfos.Add(item);
- }
- return Ok(new { state = 200, deptInfos });
- }
- catch (Exception ex)
- {
- return Ok(new { state = 1, message = $"访问失败! 状态:{ex.StackTrace} 错误:{ex.Message}" });
- }
- }
- #endregion
- }
- }
|