瀏覽代碼

钉钉组织架构的数据结构的更改,并存储到数据库中,添加接口:依据当前部门编号获取下级部门信息

Li 3 年之前
父節點
當前提交
2635f600ba

+ 83 - 0
TEAMModeBI/Controllers/BIHome/HomeStatisController.cs

@@ -0,0 +1,83 @@
+using Microsoft.AspNetCore.Http;
+using Microsoft.AspNetCore.Mvc;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Threading.Tasks;
+using System.Text.Json;
+using TEAMModelOS.SDK.DI;
+using TEAMModelOS.Models;
+using Azure.Cosmos;
+using Microsoft.Extensions.Options;
+
+namespace TEAMModeBI.Controllers.BIHome
+{
+    [Route("homestatis")]
+    [ApiController]
+    public class HomeStatisController : ControllerBase
+    {
+        private readonly AzureCosmosFactory _azureCosmos;
+        private readonly DingDing _dingDing;
+        private readonly Option _option;
+
+        public HomeStatisController(AzureCosmosFactory azureCosmos, DingDing dingDing, IOptionsSnapshot<Option> option) 
+        {
+            _azureCosmos = azureCosmos;
+            _dingDing = dingDing;
+            _option = option?.Value;
+        }
+
+
+        /// <summary>
+        /// 获取人数
+        /// </summary>
+        /// <param name="jsonElement"></param>
+        /// <returns></returns>
+        [ProducesDefaultResponseType]
+        [HttpPost("numberpeople")]
+        public async Task<IActionResult> NumberPeople(JsonElement jsonElement)  
+        {
+            if (!jsonElement.TryGetProperty("schooolId", out JsonElement schoolId)) return BadRequest();
+
+            var client = _azureCosmos.GetCosmosClient();
+
+            //依据学校查询教师人数
+            List<string> teacherCount_list = new();
+            await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryStreamIterator(queryText: $"select c.id from c join S1 in c.schools where S1.schoolId='{schoolId}'", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base") })) 
+            {
+                using var json = await JsonDocument.ParseAsync(item.ContentStream);
+                if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0) 
+                {
+                    var accounts = json.RootElement.GetProperty("Documents").EnumerateArray();
+                    while (accounts.MoveNext()) 
+                    {
+                        JsonElement account = accounts.Current;
+                        teacherCount_list.Add(account.GetProperty("id").GetString());
+                    }
+                }
+            }
+
+            //
+            List<string> studentCount_List = new();
+            await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Student").GetItemQueryStreamIterator(queryText: $"select c.id from c", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base-{schoolId}") })) 
+            {
+                using var json = await JsonDocument.ParseAsync(item.ContentStream);
+                if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
+                {
+                    var accounts = json.RootElement.GetProperty("Documents").EnumerateArray();
+                    while (accounts.MoveNext()) 
+                    {
+                        JsonElement account = accounts.Current;
+                        studentCount_List.Add(account.GetProperty("id").GetString());
+                    }
+                }
+            }
+
+
+
+            return Ok(new { SchoolTeacherCount = teacherCount_list.Count });
+
+
+        }
+    }
+}

+ 225 - 0
TEAMModeBI/Controllers/DingDingStruc/DDDeptController.cs

@@ -0,0 +1,225 @@
+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
+    }
+}

+ 17 - 0
TEAMModeBI/Controllers/DingDingStruc/DDPowerController.cs

@@ -0,0 +1,17 @@
+using Microsoft.AspNetCore.Http;
+using Microsoft.AspNetCore.Mvc;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Threading.Tasks;
+
+namespace TEAMModeBI.Controllers.DingDingStruc
+{
+    [Route("power")]
+    [ApiController]
+    public class DDPowerController : ControllerBase
+    {
+        //public 
+
+    }
+}

+ 530 - 0
TEAMModeBI/Controllers/DingDingStruc/DDStructController.cs

@@ -0,0 +1,530 @@
+using DingTalk.Api;
+using DingTalk.Api.Request;
+using DingTalk.Api.Response;
+using Microsoft.AspNetCore.Http;
+using Microsoft.AspNetCore.Mvc;
+using Microsoft.Extensions.Configuration;
+using Microsoft.Extensions.Options;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text.Json;
+using System.Threading.Tasks;
+using TEAMModelOS.Models;
+using TEAMModelOS.SDK.DI;
+using TEAMModelOS.SDK.Models.Service;
+
+namespace TEAMModeBI.Controllers.DingDingStruc
+{
+    [Route("dd")]
+    [ApiController]
+    public class DDStructController : ControllerBase
+    {
+        private readonly IConfiguration _configuration;
+        //数据容器
+        private readonly AzureCosmosFactory _azureCosmos;
+        //文件容器
+        private readonly AzureStorageFactory _azureStorage;
+        //钉钉提示信息
+        private readonly DingDing _dingDing;
+        private readonly Option _option;
+
+        public DDStructController(IConfiguration configuration, AzureCosmosFactory azureCosmos, AzureStorageFactory azureStorage, DingDing dingDing, IOptionsSnapshot<Option> option, CoreAPIHttpService aoreAPIHttpService)
+        {
+            _configuration = configuration;
+            _azureCosmos = azureCosmos;
+            _azureStorage = azureStorage;
+            _dingDing = dingDing;
+            _option = option?.Value;
+        }
+
+        /// <summary>
+        /// 获取组织架构列表
+        /// </summary>
+        /// <returns></returns>
+        [ProducesDefaultResponseType]
+        [HttpPost("ddminstruc")]
+        public async Task<IActionResult> DDMainStruc()
+        {
+            string str_appKey = _configuration["DingDingAuth:appKey"];
+            string str_appSecret = _configuration["DingDingAuth:appSecret"];
+
+            //获取企业内部应用的accessToken
+            IDingTalkClient Iclient = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");
+            OapiGettokenRequest request = new OapiGettokenRequest();
+            request.Appkey = str_appKey;
+            request.Appsecret = str_appSecret;
+            request.SetHttpMethod("GET");
+            OapiGettokenResponse tokenResponse = Iclient.Execute(request);
+            if (tokenResponse.IsError)
+            {
+                return Ok(new { status = 0, message = "请检查配置" });
+            }
+            string access_token1 = tokenResponse.AccessToken;
+            IDingTalkClient dingTalkClient = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/org/union/trunk/get");
+            OapiOrgUnionTrunkGetRequest oapiOrgUnionTrunkGetRequest = new OapiOrgUnionTrunkGetRequest();
+            OapiOrgUnionTrunkGetResponse oapiOrgUnionTrunkGetResponse = dingTalkClient.Execute(oapiOrgUnionTrunkGetRequest, tokenResponse.AccessToken);
+
+            return Ok(new { oapiOrgUnionTrunkGetResponse.RequestId , oapiOrgUnionTrunkGetResponse.Body, oapiOrgUnionTrunkGetResponse.Result });
+
+        }
+
+        /// <summary>
+        /// 获取分支组织列表信息
+        /// </summary>
+        /// <returns></returns>
+        [ProducesDefaultResponseType]
+        [HttpPost("ddbranchstruc")]
+        public async Task<IActionResult> DDBranchStruc()
+        {
+            string str_appKey = _configuration["DingDingAuth:appKey"];
+            string str_appSecret = _configuration["DingDingAuth:appSecret"];
+
+            //获取企业内部应用的accessToken
+            IDingTalkClient Iclient = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");
+            OapiGettokenRequest request = new OapiGettokenRequest();
+            request.Appkey = str_appKey;
+            request.Appsecret = str_appSecret;
+            request.SetHttpMethod("GET");
+            OapiGettokenResponse tokenResponse = Iclient.Execute(request);
+            if (tokenResponse.IsError)
+            {
+                return Ok(new { status = 0, message = "请检查配置" });
+            }
+            IDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/org/union/branch/get");
+            OapiOrgUnionBranchGetRequest req = new OapiOrgUnionBranchGetRequest();
+            OapiOrgUnionBranchGetResponse rsp = client.Execute(req, tokenResponse.AccessToken);
+
+
+            return Ok(new { Result = rsp.Result, Body = rsp.Body, RequestId = rsp.RequestId, SubErrCode = rsp.SubErrCode, Success = rsp.Success });
+        }
+
+
+        /// <summary>
+        /// 获取企业部门列表
+        /// </summary>
+        [ProducesDefaultResponseType]
+        [HttpPost("get-deptlist")]
+        public async Task<IActionResult>  GetDeptList()
+        {
+            try
+            {
+                string appKey = _configuration["DingDingAuth:appKey"];
+                string appSecret = _configuration["DingDingAuth:appSecret"];
+
+                //获取access_token
+                DefaultDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");
+                OapiGettokenRequest request = new OapiGettokenRequest();
+                request.Appkey = appKey;
+                request.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 v2ListsubClient1 = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/department/listsub");
+                OapiV2DepartmentListsubRequest reqlistsub1 = new OapiV2DepartmentListsubRequest() { DeptId = 1L, Language = "zh_CN" };
+                OapiV2DepartmentListsubResponse rsplistsub1 = v2ListsubClient1.Execute(reqlistsub1, access_token);
+                List<DeptInfo> templsit = new List<DeptInfo>();
+
+                if (rsplistsub1.Result != null)
+                {
+                    foreach (var deptList in rsplistsub1.Result)
+                    {
+                        DeptInfo deptInfo = new DeptInfo();
+                        deptInfo.deptId = deptList.DeptId;
+                        deptInfo.deptName = deptList.Name;
+                        deptInfo.parentId = deptList.ParentId;
+
+                        //获取一级部门用户列表
+                        IDingTalkClient userListClient1 = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/user/listid");
+                        OapiUserListidRequest reqUserList1 = new OapiUserListidRequest() { DeptId = deptList.DeptId };
+                        OapiUserListidResponse rspUserList1 = userListClient1.Execute(reqUserList1, access_token);
+                        if (rspUserList1.Result != null)
+                        {
+                            deptInfo.ddUserList = rspUserList1.Result.UseridList;
+                        }
+
+                        //获取用户详细信息
+                        IDingTalkClient v2UserListClient = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/get");
+                        OapiV2UserGetRequest reqv2UserList = new OapiV2UserGetRequest();
+                        OapiV2UserGetResponse rspv2UserList = v2UserListClient.Execute(reqv2UserList, access_token);
+
+                        //获取二级部门列表
+                        OapiV2DepartmentListsubRequest reqlistsub = new OapiV2DepartmentListsubRequest() { DeptId = deptList.DeptId, Language = "zh_CN" };
+                        OapiV2DepartmentListsubResponse rsplistsub = v2ListsubClient1.Execute(reqlistsub, access_token);
+
+                        List<DeptBaseResponseDomain> deptBaseResponseDomainList = new List<DeptBaseResponseDomain>();
+                        if (rsplistsub.Result != null)
+                        {
+                            foreach (var deptlist2 in rsplistsub.Result)
+                            {
+                                //添加二级部门
+                                DeptBaseResponseDomain deptBaseResponseDomain2 = new DeptBaseResponseDomain();
+                                deptBaseResponseDomain2.deptId = deptlist2.DeptId;
+                                deptBaseResponseDomain2.Name = deptlist2.Name;
+                                deptBaseResponseDomain2.ParentId = deptlist2.ParentId;
+
+                                //获取三级部门用户列表
+                                OapiUserListidRequest reqUserList2 = new OapiUserListidRequest() { DeptId = deptlist2.DeptId };
+                                OapiUserListidResponse rspUserList2 = userListClient1.Execute(reqUserList2, access_token);
+                                if (rspUserList2.Result != null)
+                                {
+                                    //添加三级部门用户
+                                    deptBaseResponseDomain2.ddUserList = rspUserList2.Result.UseridList;
+                                }
+                                //获取三级部门列表
+                                OapiV2DepartmentListsubRequest reqlistsub3 = new OapiV2DepartmentListsubRequest() { DeptId = deptlist2.DeptId, Language = "zh_CN" };
+                                OapiV2DepartmentListsubResponse rsplistsub3 = v2ListsubClient1.Execute(reqlistsub3, access_token);
+
+                                List<DeptBaseResponseDomain> deptBaseResponseDomain3List = new List<DeptBaseResponseDomain>();
+
+                                if (rsplistsub3.Result != null)
+                                {
+                                    foreach (var dept3List in rsplistsub3.Result)
+                                    {
+                                        //添加三级部门
+                                        DeptBaseResponseDomain deptBaseResponseDomain3 = new DeptBaseResponseDomain();
+                                        deptBaseResponseDomain3.deptId = dept3List.DeptId;
+                                        deptBaseResponseDomain3.Name = dept3List.Name;
+                                        deptBaseResponseDomain3.ParentId = dept3List.ParentId;
+
+                                        //获取部门用户列表
+                                        OapiUserListidRequest reqUserList3 = new OapiUserListidRequest() { DeptId = dept3List.DeptId };
+                                        OapiUserListidResponse rspUserList3 = userListClient1.Execute(reqUserList3, access_token);
+                                        if (rspUserList3.Result != null)
+                                        {
+                                            //添加三级部门的用户
+                                            deptBaseResponseDomain3.ddUserList = rspUserList3.Result.UseridList;
+                                        }
+
+                                        //获取部门列表  四级目录
+                                        OapiV2DepartmentListsubRequest reqlistsub4 = new OapiV2DepartmentListsubRequest() { DeptId = dept3List.DeptId, Language = "zh_CN" };
+                                        OapiV2DepartmentListsubResponse rsplistsu4 = v2ListsubClient1.Execute(reqlistsub4, access_token);
+
+                                        List<DeptBaseResponseDomain> deptBaseResponseDomain4List = new List<DeptBaseResponseDomain>();
+                                        if (rsplistsu4.Result != null)
+                                        {
+
+                                            foreach (var dept4List in rsplistsu4.Result)
+                                            {
+                                                DeptBaseResponseDomain deptBaseResponseDomain4 = new DeptBaseResponseDomain();
+                                                deptBaseResponseDomain4.deptId = dept4List.DeptId;
+                                                deptBaseResponseDomain4.Name = dept4List.Name;
+                                                deptBaseResponseDomain4.ParentId = dept4List.ParentId;
+                                                deptBaseResponseDomain4List.Add(deptBaseResponseDomain4);
+
+                                                //获取三级部门用户列表
+                                                OapiUserListidRequest reqUserList4 = new OapiUserListidRequest() { DeptId = dept4List.DeptId };
+                                                OapiUserListidResponse rspUserList4 = userListClient1.Execute(reqUserList4, access_token);
+                                                if (rspUserList4.Result != null)
+                                                {
+                                                    //添加四级部门的用户
+                                                    deptBaseResponseDomain4.ddUserList = rspUserList4.Result.UseridList;
+                                                }
+                                            }
+                                        }
+                                        //添加四级部门列表
+                                        deptBaseResponseDomain3.LowerDeip_List = deptBaseResponseDomain4List;
+                                        deptBaseResponseDomain3List.Add(deptBaseResponseDomain3);
+                                    }
+                                }
+                                //添加三级部门列表
+                                deptBaseResponseDomain2.LowerDeip_List = deptBaseResponseDomain3List;
+                                deptBaseResponseDomainList.Add(deptBaseResponseDomain2);
+                            }
+                        }
+                        //添加二级部门列表
+                        deptInfo.deptList = deptBaseResponseDomainList;
+                        templsit.Add(deptInfo);
+                    }
+                }
+                return Ok(new { state = 200, deptlist = templsit });
+            }
+            catch (Exception ex)
+            {
+                return Ok(new { state = 1, message=$"查询失败!:状态:{ex.StackTrace}错误:{ex.Message}" }) ;
+            }
+        }
+
+        /// <summary>
+        /// 获取当前用户的父级集合
+        /// </summary>
+        /// <param name="jsonElement"></param>
+        /// <returns></returns>
+        [ProducesDefaultResponseType]
+        [HttpPost("get-parentdept")]
+        public async Task<IActionResult> GetParentDept(JsonElement jsonElement) 
+        {
+            try
+            {
+                if (!jsonElement.TryGetProperty("userId", out JsonElement userId)) return Ok(new { state = 1, message = "参数错误!" });
+
+                string appKey = _configuration["DingDingAuth:appKey"];
+                string appSecret = _configuration["DingDingAuth:appSecret"];
+
+                //获取access_token
+                DefaultDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");
+                OapiGettokenRequest request = new OapiGettokenRequest();
+                request.Appkey = appKey;
+                request.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 v2DeartDeptClient = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/department/listparentbyuser");
+                OapiV2DepartmentListparentbyuserRequest reqDeartDept = new OapiV2DepartmentListparentbyuserRequest() { Userid = userId.ToString() };
+                OapiV2DepartmentListparentbyuserResponse rspDeartDept = v2DeartDeptClient.Execute(reqDeartDept, access_token);
+
+                if (rspDeartDept.Result != null)
+                {
+                    List<long> userParentDept = new List<long>();
+                    //var parentDept = rspDeartDept.Result.ParentList;
+                    foreach (var temp in rspDeartDept.Result.ParentList)
+                    {
+                        foreach (var deptTemp in temp.ParentDeptIdList)
+                        {
+                            userParentDept.Add(deptTemp);
+                        }
+                    }
+
+                    return Ok(new { state = 200, parentList = userParentDept });
+                }
+
+                return Ok(new { state = 2, message = "访问失败!" });
+            }
+            catch (Exception ex)
+            {
+                return Ok(new { state = 2, message = $"访问失败!状态:{ex.StackTrace} 错误:{ex.Message}" });
+            }
+        }
+
+        /// <summary>
+        /// 
+        /// </summary>
+        /// <returns></returns>
+        public async Task<IActionResult> SetDeptList(JsonElement jsonElement) 
+        {
+            try
+            {
+                string appKey = _configuration["DingDingAuth:appKey"];
+                string appSecret = _configuration["DingDingAuth:appSecret"];
+
+                //获取access_token
+                DefaultDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");
+                OapiGettokenRequest request = new OapiGettokenRequest();
+                request.Appkey = appKey;
+                request.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 v2ListsubClient1 = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/department/listsub");
+                OapiV2DepartmentListsubRequest reqlistsub1 = new OapiV2DepartmentListsubRequest() { DeptId = 1L, Language = "zh_CN" };
+                OapiV2DepartmentListsubResponse rsplistsub1 = v2ListsubClient1.Execute(reqlistsub1, access_token);
+                List<DeptInfo> templsit = new List<DeptInfo>();
+
+                if (rsplistsub1.Result != null)
+                {
+                    foreach (var deptList in rsplistsub1.Result)
+                    {
+                        DeptInfo deptInfo = new DeptInfo();
+                        deptInfo.deptId = deptList.DeptId;
+                        deptInfo.deptName = deptList.Name;
+                        deptInfo.parentId = deptList.ParentId;
+
+                        //获取一级部门用户列表
+                        IDingTalkClient userListClient1 = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/user/listid");
+                        OapiUserListidRequest reqUserList1 = new OapiUserListidRequest() { DeptId = deptList.DeptId };
+                        OapiUserListidResponse rspUserList1 = userListClient1.Execute(reqUserList1, access_token);
+                        if (rspUserList1.Result != null)
+                        {
+                            deptInfo.ddUserList = rspUserList1.Result.UseridList;
+                        }
+
+                        //获取用户详细信息
+                        IDingTalkClient v2UserListClient = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/get");
+                        OapiV2UserGetRequest reqv2UserList = new OapiV2UserGetRequest();
+                        OapiV2UserGetResponse rspv2UserList = v2UserListClient.Execute(reqv2UserList, access_token);
+
+                        //获取二级部门列表
+                        OapiV2DepartmentListsubRequest reqlistsub = new OapiV2DepartmentListsubRequest() { DeptId = deptList.DeptId, Language = "zh_CN" };
+                        OapiV2DepartmentListsubResponse rsplistsub = v2ListsubClient1.Execute(reqlistsub, access_token);
+
+                        List<DeptBaseResponseDomain> deptBaseResponseDomainList = new List<DeptBaseResponseDomain>();
+                        if (rsplistsub.Result != null)
+                        {
+                            foreach (var deptlist2 in rsplistsub.Result)
+                            {
+                                //添加二级部门
+                                DeptBaseResponseDomain deptBaseResponseDomain2 = new DeptBaseResponseDomain();
+                                deptBaseResponseDomain2.deptId = deptlist2.DeptId;
+                                deptBaseResponseDomain2.Name = deptlist2.Name;
+                                deptBaseResponseDomain2.ParentId = deptlist2.ParentId;
+
+                                //获取三级部门用户列表
+                                OapiUserListidRequest reqUserList2 = new OapiUserListidRequest() { DeptId = deptlist2.DeptId };
+                                OapiUserListidResponse rspUserList2 = userListClient1.Execute(reqUserList2, access_token);
+                                if (rspUserList2.Result != null)
+                                {
+                                    //添加三级部门用户
+                                    deptBaseResponseDomain2.ddUserList = rspUserList2.Result.UseridList;
+                                }
+                                //获取三级部门列表
+                                OapiV2DepartmentListsubRequest reqlistsub3 = new OapiV2DepartmentListsubRequest() { DeptId = deptlist2.DeptId, Language = "zh_CN" };
+                                OapiV2DepartmentListsubResponse rsplistsub3 = v2ListsubClient1.Execute(reqlistsub3, access_token);
+
+                                List<DeptBaseResponseDomain> deptBaseResponseDomain3List = new List<DeptBaseResponseDomain>();
+
+                                if (rsplistsub3.Result != null)
+                                {
+                                    foreach (var dept3List in rsplistsub3.Result)
+                                    {
+                                        //添加三级部门
+                                        DeptBaseResponseDomain deptBaseResponseDomain3 = new DeptBaseResponseDomain();
+                                        deptBaseResponseDomain3.deptId = dept3List.DeptId;
+                                        deptBaseResponseDomain3.Name = dept3List.Name;
+                                        deptBaseResponseDomain3.ParentId = dept3List.ParentId;
+
+                                        //获取部门用户列表
+                                        OapiUserListidRequest reqUserList3 = new OapiUserListidRequest() { DeptId = dept3List.DeptId };
+                                        OapiUserListidResponse rspUserList3 = userListClient1.Execute(reqUserList3, access_token);
+                                        if (rspUserList3.Result != null)
+                                        {
+                                            //添加三级部门的用户
+                                            deptBaseResponseDomain3.ddUserList = rspUserList3.Result.UseridList;
+                                        }
+
+                                        //获取部门列表  四级目录
+                                        OapiV2DepartmentListsubRequest reqlistsub4 = new OapiV2DepartmentListsubRequest() { DeptId = dept3List.DeptId, Language = "zh_CN" };
+                                        OapiV2DepartmentListsubResponse rsplistsu4 = v2ListsubClient1.Execute(reqlistsub4, access_token);
+
+                                        List<DeptBaseResponseDomain> deptBaseResponseDomain4List = new List<DeptBaseResponseDomain>();
+                                        if (rsplistsu4.Result != null)
+                                        {
+
+                                            foreach (var dept4List in rsplistsu4.Result)
+                                            {
+                                                DeptBaseResponseDomain deptBaseResponseDomain4 = new DeptBaseResponseDomain();
+                                                deptBaseResponseDomain4.deptId = dept4List.DeptId;
+                                                deptBaseResponseDomain4.Name = dept4List.Name;
+                                                deptBaseResponseDomain4.ParentId = dept4List.ParentId;
+                                                deptBaseResponseDomain4List.Add(deptBaseResponseDomain4);
+
+                                                //获取三级部门用户列表
+                                                OapiUserListidRequest reqUserList4 = new OapiUserListidRequest() { DeptId = dept4List.DeptId };
+                                                OapiUserListidResponse rspUserList4 = userListClient1.Execute(reqUserList4, access_token);
+                                                if (rspUserList4.Result != null)
+                                                {
+                                                    //添加四级部门的用户
+                                                    deptBaseResponseDomain4.ddUserList = rspUserList4.Result.UseridList;
+                                                }
+                                            }
+                                        }
+                                        //添加四级部门列表
+                                        deptBaseResponseDomain3.LowerDeip_List = deptBaseResponseDomain4List;
+                                        deptBaseResponseDomain3List.Add(deptBaseResponseDomain3);
+                                    }
+                                }
+                                //添加三级部门列表
+                                deptBaseResponseDomain2.LowerDeip_List = deptBaseResponseDomain3List;
+                                deptBaseResponseDomainList.Add(deptBaseResponseDomain2);
+                            }
+                        }
+                        //添加二级部门列表
+                        deptInfo.deptList = deptBaseResponseDomainList;
+                        templsit.Add(deptInfo);
+                    }
+                }
+                return Ok(new { state = 200, deptlist = templsit });
+            }
+            catch (Exception ex)
+            {
+                return Ok(new { state = 1, message = $"查询失败!:状态:{ex.StackTrace}错误:{ex.Message}" });
+            }
+        }
+
+
+
+
+
+        public record DeptInfo 
+        {
+            /// <summary>
+            /// 部门ID
+            /// </summary>
+            public long deptId { get; set; }
+
+            /// <summary>
+            /// 部门名称
+            /// </summary>
+            public string deptName { get; set; }
+
+            /// <summary>
+            /// 父部门id,根部门为1
+            /// </summary>
+            public long parentId { get; set; }
+
+            /// <summary>
+            /// 部门集合
+            /// </summary>
+            public List<DeptBaseResponseDomain> deptList { get; set; }
+
+            /// <summary>
+            /// 钉钉用户列表
+            /// </summary>
+            public List<string> ddUserList { get; set; }
+
+
+        }
+
+        public record DeptBaseResponseDomain 
+        {
+            /// <summary>
+            /// 部门ID
+            /// </summary>
+            public long deptId { get; set; }
+
+            /// <summary>
+            /// 部门名称
+            /// </summary>
+            public string Name { get; set; }
+
+            /// <summary>
+            /// 父部门ID
+            /// </summary>
+            public long ParentId { get; set; }
+
+            /// <summary>
+            /// 下级列表
+            /// </summary>
+            public List<DeptBaseResponseDomain> LowerDeip_List { get; set; }
+
+            /// <summary>
+            /// 钉钉用户列表
+            /// </summary>
+            public List<string> ddUserList { get; set; }
+
+        }
+
+    }
+}

+ 1 - 1
TEAMModeBI/Controllers/LoginController.cs

@@ -324,7 +324,7 @@ namespace TEAMModeBI.Controllers
                         var ddbind = teacher.ddbinds.Find(x => x.userid.Equals($"{dingDingBind.userid}") && x.unionid.Equals($"{dingDingBind.unionid}"));
                         if (ddbind != null)
                         {
-                            return Ok(new { status = 200, id_token = implicit_token.id_token, access_token = implicit_token.access_token, expires_in = implicit_token.expires_in, token_type = implicit_token.token_type });
+                            return Ok(new { status = 200, teacher = teacher, id_token = implicit_token.id_token, access_token = implicit_token.access_token, expires_in = implicit_token.expires_in, token_type = implicit_token.token_type });
                         }
                     }
                     return Ok(new { status = 1, dingdinginfo = dingDingBind });

+ 41 - 0
TEAMModelOS.SDK/Models/Cosmos/BI/DeptNode.cs

@@ -0,0 +1,41 @@
+using System;
+using System.Collections.Generic;
+using System.ComponentModel.DataAnnotations;
+using System.Text;
+
+namespace TEAMModelOS.SDK.Models.Cosmos.BI
+{
+    /// <summary>
+    /// 所有部门信息
+    /// </summary>
+    public class DeptNode: CosmosEntity
+    {
+        public List<DeptInfo> depts { get; set; }
+    }
+
+    /// <summary>
+    /// 单个部门信息
+    /// </summary>
+    public class DeptInfo
+    {
+
+        [Required(ErrorMessage = "{0} 必须填写")]
+        public long id { get; set; }
+
+        /// <summary>
+        /// 父级
+        /// </summary>
+        [Required(ErrorMessage = "{0} 必须填写")]
+        public long pid { get; set; }
+
+        /// <summary>
+        /// 部门名称
+        /// </summary>
+        public string name { get; set; }
+
+        /// <summary>
+        /// 用户列表
+        /// </summary>
+        public List<string> users { get; set; }
+    }
+}