DDStructController.cs 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  1. using DingTalk.Api;
  2. using DingTalk.Api.Request;
  3. using DingTalk.Api.Response;
  4. using Microsoft.AspNetCore.Http;
  5. using Microsoft.AspNetCore.Mvc;
  6. using Microsoft.Extensions.Configuration;
  7. using Microsoft.Extensions.Options;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using System.Text.Json;
  12. using System.Threading.Tasks;
  13. using TEAMModelOS.Models;
  14. using TEAMModelOS.SDK.DI;
  15. using TEAMModelOS.SDK.Extension;
  16. using TEAMModelOS.SDK.Models.Service;
  17. using Azure.Cosmos;
  18. using TEAMModelOS.SDK.Models;
  19. using TEAMModelBI.Controllers.BISchool;
  20. using TEAMModelOS.SDK.Models.Cosmos.BI;
  21. using Microsoft.Azure.Cosmos.Table;
  22. using TEAMModelOS.SDK;
  23. using TEAMModelOS.SDK.Context.BI;
  24. namespace TEAMModelBI.Controllers.BITable
  25. {
  26. [ProducesResponseType(StatusCodes.Status200OK)]
  27. [ProducesResponseType(StatusCodes.Status400BadRequest)]
  28. [Route("dd")]
  29. [ApiController]
  30. public class DDStructController : ControllerBase
  31. {
  32. private readonly IConfiguration _configuration;
  33. //数据容器
  34. private readonly AzureCosmosFactory _azureCosmos;
  35. //文件容器
  36. private readonly AzureStorageFactory _azureStorage;
  37. //钉钉提示信息
  38. private readonly DingDing _dingDing;
  39. //雪花ID
  40. private readonly SnowflakeId _snowflakeId;
  41. private readonly Option _option;
  42. public DDStructController(IConfiguration configuration, AzureCosmosFactory azureCosmos, AzureStorageFactory azureStorage, DingDing dingDing, IOptionsSnapshot<Option> option, CoreAPIHttpService aoreAPIHttpService, SnowflakeId snowflakeId)
  43. {
  44. _configuration = configuration;
  45. _azureCosmos = azureCosmos;
  46. _azureStorage = azureStorage;
  47. _dingDing = dingDing;
  48. _option = option?.Value;
  49. _snowflakeId = snowflakeId;
  50. }
  51. /// <summary>
  52. /// 获取企业部门列表 //已对接
  53. /// </summary>
  54. [ProducesDefaultResponseType]
  55. [HttpPost("get-deptlist")]
  56. public async Task<IActionResult> GetDeptList()
  57. {
  58. try
  59. {
  60. string appKey = _configuration["DingDingAuth:appKey"];
  61. string appSecret = _configuration["DingDingAuth:appSecret"];
  62. //获取access_token
  63. DefaultDingTalkClient client = new("https://oapi.dingtalk.com/gettoken");
  64. OapiGettokenRequest request = new();
  65. request.Appkey = appKey;
  66. request.Appsecret = appSecret;
  67. request.SetHttpMethod("Get");
  68. OapiGettokenResponse response = client.Execute(request);
  69. if (response.IsError)
  70. {
  71. return BadRequest();
  72. }
  73. //access_token的有效期为7200秒(2小时),有效期内重复获取会返回相同结果并自动续期,过期后获取会返回新的access_token
  74. string access_token = response.AccessToken;
  75. //获取一级部门列表
  76. IDingTalkClient v2ListsubClient1 = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/department/listsub");
  77. OapiV2DepartmentListsubRequest reqlistsub1 = new OapiV2DepartmentListsubRequest() { DeptId = 1L, Language = "zh_CN" };
  78. OapiV2DepartmentListsubResponse rsplistsub1 = v2ListsubClient1.Execute(reqlistsub1, access_token);
  79. List<DeptInfo> templsit = new List<DeptInfo>();
  80. if (rsplistsub1.Result != null)
  81. {
  82. foreach (var deptList in rsplistsub1.Result)
  83. {
  84. DeptInfo deptInfo = new DeptInfo();
  85. deptInfo.deptId = deptList.DeptId;
  86. deptInfo.deptName = deptList.Name;
  87. deptInfo.parentId = deptList.ParentId;
  88. //获取一级部门用户列表
  89. IDingTalkClient userListClient1 = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/user/listid");
  90. OapiUserListidRequest reqUserList1 = new() { DeptId = deptList.DeptId };
  91. OapiUserListidResponse rspUserList1 = userListClient1.Execute(reqUserList1, access_token);
  92. if (rspUserList1.Result != null)
  93. {
  94. deptInfo.ddUserList = rspUserList1.Result.UseridList;
  95. }
  96. //获取用户详细信息
  97. IDingTalkClient v2UserListClient = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/get");
  98. OapiV2UserGetRequest reqv2UserList = new();
  99. OapiV2UserGetResponse rspv2UserList = v2UserListClient.Execute(reqv2UserList, access_token);
  100. //获取二级部门列表
  101. OapiV2DepartmentListsubRequest reqlistsub = new() { DeptId = deptList.DeptId, Language = "zh_CN" };
  102. OapiV2DepartmentListsubResponse rsplistsub = v2ListsubClient1.Execute(reqlistsub, access_token);
  103. List<DeptBaseResponseDomain> deptBaseResponseDomainList = new();
  104. if (rsplistsub.Result != null)
  105. {
  106. foreach (var deptlist2 in rsplistsub.Result)
  107. {
  108. //添加二级部门
  109. DeptBaseResponseDomain deptBaseResponseDomain2 = new();
  110. deptBaseResponseDomain2.deptId = deptlist2.DeptId;
  111. deptBaseResponseDomain2.Name = deptlist2.Name;
  112. deptBaseResponseDomain2.ParentId = deptlist2.ParentId;
  113. //获取三级部门用户列表
  114. OapiUserListidRequest reqUserList2 = new() { DeptId = deptlist2.DeptId };
  115. OapiUserListidResponse rspUserList2 = userListClient1.Execute(reqUserList2, access_token);
  116. if (rspUserList2.Result != null)
  117. {
  118. //添加三级部门用户
  119. deptBaseResponseDomain2.ddUserList = rspUserList2.Result.UseridList;
  120. }
  121. //获取三级部门列表
  122. OapiV2DepartmentListsubRequest reqlistsub3 = new() { DeptId = deptlist2.DeptId, Language = "zh_CN" };
  123. OapiV2DepartmentListsubResponse rsplistsub3 = v2ListsubClient1.Execute(reqlistsub3, access_token);
  124. List<DeptBaseResponseDomain> deptBaseResponseDomain3List = new();
  125. if (rsplistsub3.Result != null)
  126. {
  127. foreach (var dept3List in rsplistsub3.Result)
  128. {
  129. //添加三级部门
  130. DeptBaseResponseDomain deptBaseResponseDomain3 = new();
  131. deptBaseResponseDomain3.deptId = dept3List.DeptId;
  132. deptBaseResponseDomain3.Name = dept3List.Name;
  133. deptBaseResponseDomain3.ParentId = dept3List.ParentId;
  134. //获取部门用户列表
  135. OapiUserListidRequest reqUserList3 = new() { DeptId = dept3List.DeptId };
  136. OapiUserListidResponse rspUserList3 = userListClient1.Execute(reqUserList3, access_token);
  137. if (rspUserList3.Result != null)
  138. {
  139. //添加三级部门的用户
  140. deptBaseResponseDomain3.ddUserList = rspUserList3.Result.UseridList;
  141. }
  142. //获取部门列表 四级目录
  143. OapiV2DepartmentListsubRequest reqlistsub4 = new() { DeptId = dept3List.DeptId, Language = "zh_CN" };
  144. OapiV2DepartmentListsubResponse rsplistsu4 = v2ListsubClient1.Execute(reqlistsub4, access_token);
  145. List<DeptBaseResponseDomain> deptBaseResponseDomain4List = new();
  146. if (rsplistsu4.Result != null)
  147. {
  148. foreach (var dept4List in rsplistsu4.Result)
  149. {
  150. DeptBaseResponseDomain deptBaseResponseDomain4 = new();
  151. deptBaseResponseDomain4.deptId = dept4List.DeptId;
  152. deptBaseResponseDomain4.Name = dept4List.Name;
  153. deptBaseResponseDomain4.ParentId = dept4List.ParentId;
  154. deptBaseResponseDomain4List.Add(deptBaseResponseDomain4);
  155. //获取四级部门用户列表
  156. OapiUserListidRequest reqUserList4 = new() { DeptId = dept4List.DeptId };
  157. OapiUserListidResponse rspUserList4 = userListClient1.Execute(reqUserList4, access_token);
  158. if (rspUserList4.Result != null)
  159. {
  160. //添加四级部门的用户
  161. deptBaseResponseDomain4.ddUserList = rspUserList4.Result.UseridList;
  162. }
  163. }
  164. }
  165. //添加四级部门列表
  166. deptBaseResponseDomain3.LowerDeip_List = deptBaseResponseDomain4List;
  167. deptBaseResponseDomain3List.Add(deptBaseResponseDomain3);
  168. }
  169. }
  170. //添加三级部门列表
  171. deptBaseResponseDomain2.LowerDeip_List = deptBaseResponseDomain3List;
  172. deptBaseResponseDomainList.Add(deptBaseResponseDomain2);
  173. }
  174. }
  175. //添加二级部门列表
  176. deptInfo.deptList = deptBaseResponseDomainList;
  177. templsit.Add(deptInfo);
  178. }
  179. }
  180. return Ok(new { state = 200, deptlist = templsit });
  181. }
  182. catch (Exception ex)
  183. {
  184. return Ok(new { state = 1, message=$"查询失败!:状态:错误:{ex.Message}\n{ex.StackTrace}" }) ;
  185. }
  186. }
  187. /// <summary>
  188. /// 查询钉钉的研发中心B人员 //已对接
  189. /// </summary>
  190. /// <param name="jsonElement"></param>
  191. /// <returns></returns>
  192. [ProducesDefaultResponseType]
  193. [HttpPost("get-tmdandddusers")]
  194. public async Task<IActionResult> GetTmdAndDdUsers(JsonElement jsonElement)
  195. {
  196. try
  197. {
  198. jsonElement.TryGetProperty("deptId", out JsonElement deptId);
  199. jsonElement.TryGetProperty("site", out JsonElement site);
  200. string tempDeptId = string.IsNullOrEmpty($"{deptId}") ? "67863053" : $"{deptId}";
  201. string appKey = _configuration["DingDingAuth:appKey"];
  202. string appSecret = _configuration["DingDingAuth:appSecret"];
  203. Dictionary<string, object> dic = new() { { "PartitionKey", "authority-bi" } };
  204. var table = _azureStorage.GetCloudTableClient().GetTableReference("SchoolSetting");
  205. List<Authority> authorityBIList = await table.FindListByDict<Authority>(dic);
  206. //获取access_token
  207. DefaultDingTalkClient client = new("https://oapi.dingtalk.com/gettoken");
  208. OapiGettokenRequest request = new();
  209. request.Appkey = appKey;
  210. request.Appsecret = appSecret;
  211. request.SetHttpMethod("Get");
  212. OapiGettokenResponse response = client.Execute(request);
  213. if (response.IsError) return BadRequest();
  214. //access_token的有效期为7200秒(2小时),有效期内重复获取会返回相同结果并自动续期,过期后获取会返回新的access_token
  215. string access_token = response.AccessToken;
  216. IDingTalkClient userListClient = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/list");
  217. OapiV2UserListRequest reqUserList1 = new()
  218. {
  219. DeptId = long.Parse($"{tempDeptId}"),
  220. Cursor = 0L,
  221. Size = 50L,
  222. ContainAccessLimit = false,
  223. OrderField = "custom",
  224. Language = "zh_CN"
  225. };
  226. reqUserList1.SetHttpMethod("GET");
  227. OapiV2UserListResponse rspV2UserList1 = userListClient.Execute(reqUserList1, access_token);
  228. List<DDUserInfoAndTMD> dDAndTmdInfos = new();
  229. List<DingDingUserInfo> ddUserInfos = new();
  230. var tabledd = _azureStorage.GetCloudTableClient().GetTableReference("BIDDUserInfo");
  231. if ($"{site}".Equals(BIConst.Global))
  232. table = _azureStorage.GetCloudTableClient(BIConst.Global).GetTableReference("BIDDUserInfo");
  233. if (rspV2UserList1.Result.List != null)
  234. {
  235. foreach (var itemUser in rspV2UserList1.Result.List)
  236. {
  237. List<DingDingUserInfo> temp = await tabledd.FindListByDict<DingDingUserInfo>(new Dictionary<string, object> { { "RowKey", $"{itemUser.Userid}" } });
  238. foreach (var item in temp)
  239. {
  240. ddUserInfos.Add(item);
  241. }
  242. //DDUserInfoAndTMD dDAndTmdInfo = new DDUserInfoAndTMD();
  243. //dDAndTmdInfo.unionid = itemUser.Unionid;
  244. //dDAndTmdInfo.userid = itemUser.Userid;
  245. //dDAndTmdInfo.title = itemUser.Title;
  246. //dDAndTmdInfo.name = itemUser.Name;
  247. //dDAndTmdInfo.mobile = itemUser.Mobile;
  248. //dDAndTmdInfo.jobNumber = itemUser.JobNumber;
  249. //dDAndTmdInfo.avatar = itemUser.Avatar;
  250. //dDAndTmdInfo.depts = itemUser.DeptIdList;
  251. //List<string> roles = new List<string>();//角色列表
  252. //List<string> power = new List<string>();//权限列表
  253. //string sqltxt = $"select distinct value(c) from c join A1 in c.ddbinds where A1.userid ='{itemUser.Userid}'";
  254. //try
  255. //{
  256. // await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryIterator<Teacher>(queryText: sqltxt, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("Base") }))
  257. // {
  258. // dDAndTmdInfo.tmdId = item.id;
  259. // dDAndTmdInfo.isexist = true;
  260. // if (!string.IsNullOrEmpty($"{item.defaultSchool}"))
  261. // {
  262. // var schoolRoles = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync(item.id, new PartitionKey($"Teacher-{item.defaultSchool}"));
  263. // if (schoolRoles.Status == 200)
  264. // {
  265. // using var json = await JsonDocument.ParseAsync(schoolRoles.ContentStream);
  266. // if (json.RootElement.TryGetProperty("roles", out JsonElement _roles) && _roles.ValueKind != JsonValueKind.Null)
  267. // {
  268. // foreach (var obj in _roles.EnumerateArray())
  269. // {
  270. // //初始定义顾问的assistant 更改为assist
  271. // if (obj.GetString().Equals($"assist"))
  272. // {
  273. // roles.Add(obj.GetString());
  274. // }
  275. // }
  276. // }
  277. // if (json.RootElement.TryGetProperty("permissions", out JsonElement _permissions) && _permissions.ValueKind != JsonValueKind.Null)
  278. // {
  279. // foreach (var obj in _permissions.EnumerateArray())
  280. // {
  281. // //显示BI权限
  282. // foreach (var aut in authorityBIList)
  283. // {
  284. // if (aut.RowKey.Equals(obj.GetString()))
  285. // {
  286. // power.Add(obj.GetString());
  287. // }
  288. // }
  289. // }
  290. // }
  291. // }
  292. // dDAndTmdInfo.tmdroles = roles;
  293. // dDAndTmdInfo.tmdpower = power;
  294. // }
  295. // }
  296. //}
  297. //catch { }
  298. }
  299. }
  300. if (!string.IsNullOrEmpty($"{tempDeptId}"))
  301. {
  302. //获取下级部门列表
  303. IDingTalkClient v2DeptListClient2 = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/department/listsub");
  304. OapiV2DepartmentListsubRequest reqDeptList2 = new() { DeptId = long.Parse($"{tempDeptId}"), Language = "zh_CN" };
  305. OapiV2DepartmentListsubResponse rspDeptLis2 = v2DeptListClient2.Execute(reqDeptList2, access_token);
  306. if (rspDeptLis2.Result != null)
  307. {
  308. foreach (var tempDept in rspDeptLis2.Result)
  309. {
  310. OapiV2UserListRequest reqUserList2 = new();
  311. reqUserList2.DeptId = long.Parse($"{tempDept.DeptId}");
  312. reqUserList2.Cursor = 0L;
  313. reqUserList2.Size = 50L;
  314. reqUserList2.ContainAccessLimit = false;
  315. reqUserList2.OrderField = "custom";
  316. reqUserList2.Language = "zh_CN";
  317. reqUserList2.SetHttpMethod("GET");
  318. OapiV2UserListResponse rspV2UserList2 = userListClient.Execute(reqUserList2, access_token);
  319. if (rspV2UserList2.Result.List != null)
  320. {
  321. foreach (var itemUser2 in rspV2UserList2.Result.List)
  322. {
  323. var tempInfo = dDAndTmdInfos.Find(x => x.unionid.Equals(itemUser2.Unionid));
  324. if (string.IsNullOrEmpty($"{tempInfo}"))
  325. {
  326. DDUserInfoAndTMD dDAndTmdInfo2 = new();
  327. dDAndTmdInfo2.unionid = itemUser2.Unionid;
  328. dDAndTmdInfo2.userid = itemUser2.Userid;
  329. dDAndTmdInfo2.title = itemUser2.Title;
  330. dDAndTmdInfo2.name = itemUser2.Name;
  331. dDAndTmdInfo2.mobile = itemUser2.Mobile;
  332. dDAndTmdInfo2.jobNumber = itemUser2.JobNumber;
  333. dDAndTmdInfo2.avatar = itemUser2.Avatar;
  334. dDAndTmdInfo2.depts = itemUser2.DeptIdList;
  335. List<string> roles = new List<string>();//角色列表
  336. List<string> power = new List<string>();//权限列表
  337. string sqltxt = $"select distinct value(c) from c join A1 in c.ddbinds where A1.userid ='{itemUser2.Userid}'";
  338. try
  339. {
  340. await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryIterator<Teacher>(queryText: sqltxt, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("Base") }))
  341. {
  342. dDAndTmdInfo2.tmdId = item.id;
  343. dDAndTmdInfo2.isexist = true;
  344. if (!string.IsNullOrEmpty($"{item.defaultSchool}"))
  345. {
  346. var schoolRoles = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync(item.id, new PartitionKey($"Teacher-{item.defaultSchool}"));
  347. if (schoolRoles.Status == 200)
  348. {
  349. using var json = await JsonDocument.ParseAsync(schoolRoles.ContentStream);
  350. if (json.RootElement.TryGetProperty("roles", out JsonElement _roles) && _roles.ValueKind != JsonValueKind.Null)
  351. {
  352. foreach (var obj in _roles.EnumerateArray())
  353. {
  354. //初始定义顾问的assistant 更改为assist
  355. if (obj.GetString().Equals($"assist"))
  356. {
  357. roles.Add(obj.GetString());
  358. }
  359. }
  360. }
  361. if (json.RootElement.TryGetProperty("permissions", out JsonElement _permissions) && _permissions.ValueKind != JsonValueKind.Null)
  362. {
  363. foreach (var obj in _permissions.EnumerateArray())
  364. {
  365. //显示BI权限
  366. foreach (var aut in authorityBIList)
  367. {
  368. if (aut.RowKey.Equals(obj.GetString()))
  369. {
  370. power.Add(obj.GetString());
  371. }
  372. }
  373. }
  374. }
  375. }
  376. dDAndTmdInfo2.tmdroles = roles;
  377. dDAndTmdInfo2.tmdpower = power;
  378. }
  379. }
  380. }
  381. catch { }
  382. dDAndTmdInfos.Add(dDAndTmdInfo2);
  383. }
  384. }
  385. }
  386. }
  387. }
  388. }
  389. return Ok(new { state = 200, count = ddUserInfos.Count, ddUserInfos }) ;
  390. }
  391. catch (Exception ex)
  392. {
  393. await _dingDing.SendBotMsg($"BI,{_option.Location} dd/get-tmdandddusers \n {ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
  394. return BadRequest();
  395. }
  396. }
  397. /// <summary>
  398. /// 依据钉钉UserID查询钉钉用户信息
  399. /// </summary>
  400. /// <param name="jsonElement"></param>
  401. /// <returns></returns>
  402. [ProducesDefaultResponseType]
  403. [HttpPost("get-dduserinfo")]
  404. public async Task<IActionResult> GetDDUserInfo(JsonElement jsonElement)
  405. {
  406. try
  407. {
  408. if (!jsonElement.TryGetProperty("userids", out JsonElement userIds)) return Ok(new { state = 1, message = "参数问题" });
  409. jsonElement.TryGetProperty("site", out JsonElement site);
  410. List<DDUserInfoAndTMD> dDUserInfoAndTMDs = new List<DDUserInfoAndTMD>();//返回钉钉信息和查询的醍摩豆信息
  411. string appKey = _configuration["DingDingAuth:appKey"];
  412. string appSecret = _configuration["DingDingAuth:appSecret"];
  413. //获取access_token
  414. DefaultDingTalkClient client = new("https://oapi.dingtalk.com/gettoken");
  415. OapiGettokenRequest request = new();
  416. request.Appkey = appKey;
  417. request.Appsecret = appSecret;
  418. request.SetHttpMethod("Get");
  419. OapiGettokenResponse response = client.Execute(request);
  420. if (response.IsError)
  421. {
  422. return BadRequest();
  423. }
  424. //access_token的有效期为7200秒(2小时),有效期内重复获取会返回相同结果并自动续期,过期后获取会返回新的access_token
  425. string access_token = response.AccessToken;
  426. IDingTalkClient userInfoClient = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/get");
  427. Dictionary<string, object> dic = new() { { "PartitionKey", "authority-bi" } };
  428. var table = _azureStorage.GetCloudTableClient().GetTableReference("SchoolSetting");
  429. var cosmosClient = _azureCosmos.GetCosmosClient();
  430. if ($"{site}".Equals(BIConst.Global))
  431. {
  432. cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
  433. table = _azureStorage.GetCloudTableClient(BIConst.Global).GetTableReference("SchoolSetting");
  434. }
  435. List<Authority> authorityBIList = await table.FindListByDict<Authority>(dic);
  436. if (!string.IsNullOrEmpty($"{userIds}"))
  437. {
  438. //List<string> str_userids = userIds.ToObject<List<string>>().Distinct().ToList();//通过数组的Equals实现去重
  439. List<string> str_userids1 = userIds.ToObject<List<string>>();
  440. List<string> str_userids = str_userids1.Where((x, i) => str_userids1.FindIndex(z => z == x) == i).ToList();//Lambda表达式去重
  441. foreach (var tempid in str_userids)
  442. {
  443. OapiV2UserGetRequest reqUserInfo = new() { Userid = $"{tempid}", Language = "zh_CN" };
  444. OapiV2UserGetResponse rspUserInfo = userInfoClient.Execute(reqUserInfo, access_token);
  445. if (rspUserInfo.Result != null)
  446. {
  447. List<string> roles = new();//角色列表
  448. List<string> power = new();//权限列表
  449. DDUserInfoAndTMD dDUserInfoAndTMD = new();
  450. dDUserInfoAndTMD.unionid = rspUserInfo.Result.Unionid;
  451. dDUserInfoAndTMD.title = rspUserInfo.Result.Title;
  452. dDUserInfoAndTMD.userid = rspUserInfo.Result.Userid;
  453. dDUserInfoAndTMD.jobNumber = rspUserInfo.Result.JobNumber;
  454. dDUserInfoAndTMD.name = rspUserInfo.Result.Name;
  455. dDUserInfoAndTMD.depts = rspUserInfo.Result.DeptIdList;
  456. dDUserInfoAndTMD.mobile = rspUserInfo.Result.Mobile;
  457. dDUserInfoAndTMD.avatar = rspUserInfo.Result.Avatar;
  458. string sqltxt = $"select distinct value(c) from c join A1 in c.ddbinds where A1.userid ='{tempid}'";
  459. try
  460. {
  461. await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryIterator<Teacher>(queryText: sqltxt, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("Base") }))
  462. {
  463. dDUserInfoAndTMD.tmdId = item.id;
  464. dDUserInfoAndTMD.isexist = true;
  465. if (!string.IsNullOrEmpty($"{item.defaultSchool}"))
  466. {
  467. var schoolRoles = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync(item.id, new PartitionKey($"Teacher-{item.defaultSchool}"));
  468. if (schoolRoles.Status == 200)
  469. {
  470. using var json = await JsonDocument.ParseAsync(schoolRoles.ContentStream);
  471. if (json.RootElement.TryGetProperty("roles", out JsonElement _roles) && _roles.ValueKind != JsonValueKind.Null)
  472. {
  473. foreach (var obj in _roles.EnumerateArray())
  474. {
  475. //初始定义顾问的assistant 更改为assist
  476. if (obj.GetString().Equals($"assist"))
  477. {
  478. roles.Add(obj.GetString());
  479. }
  480. }
  481. }
  482. if (json.RootElement.TryGetProperty("permissions", out JsonElement _permissions) && _permissions.ValueKind != JsonValueKind.Null)
  483. {
  484. foreach (var obj in _permissions.EnumerateArray())
  485. {
  486. //显示BI权限
  487. foreach (var aut in authorityBIList)
  488. {
  489. if (aut.RowKey.Equals(obj.GetString()))
  490. {
  491. power.Add(obj.GetString());
  492. }
  493. }
  494. }
  495. }
  496. }
  497. dDUserInfoAndTMD.tmdroles = roles;
  498. dDUserInfoAndTMD.tmdpower = power;
  499. }
  500. }
  501. }
  502. catch { }
  503. dDUserInfoAndTMDs.Add(dDUserInfoAndTMD);
  504. }
  505. else return Ok(new { state = 2, message = "访问失败!" });
  506. }
  507. }
  508. return Ok(new { state = 200, ddUserInfos = dDUserInfoAndTMDs});
  509. }
  510. catch (Exception ex)
  511. {
  512. await _dingDing.SendBotMsg($"BI,{_option.Location},dd/get-dduserinfo \n{ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
  513. return BadRequest();
  514. }
  515. }
  516. /// <summary>
  517. /// 获取分支组织列表信息
  518. /// </summary>
  519. /// <returns></returns>
  520. [ProducesDefaultResponseType]
  521. [HttpPost("ddbranchstruc")]
  522. public async Task<IActionResult> DDBranchStruc()
  523. {
  524. string str_appKey = _configuration["DingDingAuth:appKey"];
  525. string str_appSecret = _configuration["DingDingAuth:appSecret"];
  526. //获取企业内部应用的accessToken
  527. IDingTalkClient Iclient = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");
  528. OapiGettokenRequest request = new();
  529. request.Appkey = str_appKey;
  530. request.Appsecret = str_appSecret;
  531. request.SetHttpMethod("GET");
  532. OapiGettokenResponse tokenResponse = Iclient.Execute(request);
  533. if (tokenResponse.IsError)
  534. {
  535. return Ok(new { state = 0, message = "请检查配置" });
  536. }
  537. IDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/org/union/branch/get");
  538. OapiOrgUnionBranchGetRequest req = new();
  539. OapiOrgUnionBranchGetResponse rsp = client.Execute(req, tokenResponse.AccessToken);
  540. return Ok(new { Result = rsp.Result, Body = rsp.Body, RequestId = rsp.RequestId, SubErrCode = rsp.SubErrCode, Success = rsp.Success });
  541. }
  542. /// <summary>
  543. /// 返回钉钉和能查询到醍摩豆信息
  544. /// </summary>
  545. public class DDUserInfoAndTMD
  546. {
  547. /// <summary>
  548. /// 是否存在醍摩豆账户
  549. /// </summary>
  550. public bool isexist { get; set; }
  551. /// <summary>
  552. /// 绑定的醍摩豆账户
  553. /// </summary>
  554. public string tmdId { get; set; }
  555. /// <summary>
  556. /// 醍摩豆角色
  557. /// </summary>
  558. public List<string> tmdroles { get; set; }
  559. /// <summary>
  560. /// 醍摩豆角色权限
  561. /// </summary>
  562. public List<string> tmdpower { get; set; }
  563. /// <summary>
  564. /// 员工在当前开发者企业账号范围内的唯一标识
  565. /// </summary>
  566. public string unionid { get; set; }
  567. /// <summary>
  568. /// 用户ID
  569. /// </summary>
  570. public string userid { get; set; }
  571. /// <summary>
  572. /// 员工名称
  573. /// </summary>
  574. public string name { get; set; }
  575. /// <summary>
  576. /// 职位
  577. /// </summary>
  578. public string title { get; set; }
  579. /// <summary>
  580. /// 手机号
  581. /// </summary>
  582. public string mobile { get; set; }
  583. /// <summary>
  584. /// 员工工号
  585. /// </summary>
  586. public string jobNumber { get; set; }
  587. /// <summary>
  588. /// 所属部门
  589. /// </summary>
  590. public long deptId { get; set; }
  591. /// <summary>
  592. /// 部门名称
  593. /// </summary>
  594. public string deptName { get; set; }
  595. /// <summary>
  596. /// 所属部门id列表
  597. /// </summary>
  598. public List<long> depts { get; set; }
  599. /// <summary>
  600. /// 钉钉头像
  601. /// </summary>
  602. public string avatar { get; set; }
  603. }
  604. /// <summary>
  605. /// 部门信息
  606. /// </summary>
  607. public record DeptInfo
  608. {
  609. /// <summary>
  610. /// 部门ID
  611. /// </summary>
  612. public long deptId { get; set; }
  613. /// <summary>
  614. /// 部门名称
  615. /// </summary>
  616. public string deptName { get; set; }
  617. /// <summary>
  618. /// 父部门id,根部门为1
  619. /// </summary>
  620. public long parentId { get; set; }
  621. /// <summary>
  622. /// 部门集合
  623. /// </summary>
  624. public List<DeptBaseResponseDomain> deptList { get; set; }
  625. /// <summary>
  626. /// 钉钉用户列表
  627. /// </summary>
  628. public List<string> ddUserList { get; set; }
  629. }
  630. public record DeptBaseResponseDomain
  631. {
  632. /// <summary>
  633. /// 部门ID
  634. /// </summary>
  635. public long deptId { get; set; }
  636. /// <summary>
  637. /// 部门名称
  638. /// </summary>
  639. public string Name { get; set; }
  640. /// <summary>
  641. /// 父部门ID
  642. /// </summary>
  643. public long ParentId { get; set; }
  644. /// <summary>
  645. /// 下级列表
  646. /// </summary>
  647. public List<DeptBaseResponseDomain> LowerDeip_List { get; set; }
  648. /// <summary>
  649. /// 钉钉用户列表
  650. /// </summary>
  651. public List<string> ddUserList { get; set; }
  652. }
  653. }
  654. }