TableDingDingInfoController.cs 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113
  1. using Microsoft.AspNetCore.Http;
  2. using Microsoft.AspNetCore.Mvc;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Threading.Tasks;
  7. using TEAMModelOS.Models;
  8. using TEAMModelOS.SDK.DI;
  9. using Microsoft.Extensions.Options;
  10. using System.Net.Http;
  11. using Microsoft.Extensions.Configuration;
  12. using DingTalk.Api;
  13. using DingTalk.Api.Request;
  14. using DingTalk.Api.Response;
  15. using System.Text.Json;
  16. using System.Net.Http.Json;
  17. using TEAMModelOS.SDK.Models.Cosmos.BI;
  18. using System.Net;
  19. using TEAMModelOS.SDK.Extension;
  20. using OpenXmlPowerTools;
  21. using System.Text;
  22. using Azure.Cosmos;
  23. using Microsoft.Azure.Cosmos.Table;
  24. using TEAMModelBI.Filter;
  25. using TEAMModelBI.Tool.Extension;
  26. namespace TEAMModelBI.Controllers.DingDingStruc
  27. {
  28. [Route("tabledd")]
  29. [ApiController]
  30. public class TableDingDingInfoController : ControllerBase
  31. {
  32. //读取配置文件
  33. private readonly IConfiguration _configuration;
  34. //数据容器
  35. private readonly AzureCosmosFactory _azureCosmos;
  36. //blob和table容器
  37. private readonly AzureStorageFactory _azureStorage;
  38. //钉钉提示信息
  39. private readonly DingDing _dingDing;
  40. private readonly Option _option;
  41. private readonly IHttpClientFactory _http;
  42. public TableDingDingInfoController(AzureCosmosFactory azureCosmos, DingDing dingDing, AzureStorageFactory azureStorage, IOptionsSnapshot<Option> option, IConfiguration configuration, IHttpClientFactory http)
  43. {
  44. _azureCosmos = azureCosmos;
  45. _dingDing = dingDing;
  46. _azureStorage = azureStorage;
  47. _option = option?.Value;
  48. _http = http;
  49. _configuration = configuration;
  50. }
  51. /// <summary>
  52. /// 从钉钉的组织架构中人员信息数据,并保存或者更新至Blob中
  53. /// </summary>
  54. /// <returns></returns>
  55. [ProducesDefaultResponseType]
  56. [AuthToken(Roles = "admin")]
  57. [HttpPost("get-dingdingusers")]
  58. public async Task<IActionResult> GetDingDingUser(JsonElement jsonElement)
  59. {
  60. try
  61. {
  62. string appKey = _configuration["DingDingAuth:appKey"];
  63. string appSecret = _configuration["DingDingAuth:appSecret"];
  64. //string divide = _configuration["CustomParam:SiteScope"];
  65. string divide = _option.Location;
  66. var (_tmdId, _tmdName, pic, did, dname, dpic) = HttpJwtAnalysis.JwtXAuthBI(HttpContext.GetXAuth("AuthToken"), _option);
  67. HttpClient httpClient = _http.CreateClient();
  68. var table = _azureStorage.GetCloudTableClient().GetTableReference("BIDDUserInfo");
  69. string url = _configuration.GetValue<string>("HaBookAuth:CoreId:userinfo");
  70. //获取access_token
  71. IDingTalkClient tokenClient = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");
  72. OapiGettokenRequest tokenRequest = new OapiGettokenRequest() { Appkey = appKey, Appsecret = appSecret };
  73. tokenRequest.SetHttpMethod("Get");
  74. OapiGettokenResponse tokenRespone = tokenClient.Execute(tokenRequest);
  75. if (tokenRespone.IsError)
  76. {
  77. return BadRequest();
  78. }
  79. //access_token的有效期为7200秒(2小时),有效期内重复获取会返回相同结果并自动续期,过期后获取会返回新的access_token
  80. string access_token = tokenRespone.AccessToken;
  81. //获取部门接口
  82. IDingTalkClient deptListClient = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/department/listsub");
  83. //一级部门
  84. OapiV2DepartmentListsubRequest reqDeptList1 = new OapiV2DepartmentListsubRequest() { DeptId = 1L, Language = "zh_CN" };
  85. OapiV2DepartmentListsubResponse rspDeptList1 = deptListClient.Execute(reqDeptList1, access_token);
  86. List<DingDingUserInfo> ddUserInfos = new();
  87. if (rspDeptList1.Result != null)
  88. {
  89. foreach (var tempDept1 in rspDeptList1.Result)
  90. {
  91. //获取一级部门用户信息
  92. List<DingDingUserInfo> dingDingUserInfos1 = await GetDingDingUserInfo(divide, tempDept1.DeptId, tempDept1.ParentId, tempDept1.Name, access_token);
  93. //if (dingDingUserInfos1.Count > 0) ddUserInfos.AddRange(ddUserInfos.Union(dingDingUserInfos1.ToList()));
  94. if (dingDingUserInfos1.Count > 0) ddUserInfos.AddRange(dingDingUserInfos1);
  95. //获取二级部门
  96. OapiV2DepartmentListsubRequest reqDeptList2 = new OapiV2DepartmentListsubRequest() { DeptId = tempDept1.DeptId, Language = "zh_CN" };
  97. OapiV2DepartmentListsubResponse rspDeptList2 = deptListClient.Execute(reqDeptList2, access_token);
  98. if (rspDeptList2.Result != null)
  99. {
  100. foreach (var tempDept2 in rspDeptList2.Result)
  101. {
  102. //获取二级部门用户信息
  103. List<DingDingUserInfo> dingDingUserInfos2 = await GetDingDingUserInfo(divide, tempDept2.DeptId, tempDept2.ParentId, tempDept2.Name, access_token);
  104. //if (dingDingUserInfos2.Count > 0) ddUserInfos.AddRange(ddUserInfos.Union(dingDingUserInfos2.ToList()));
  105. if (dingDingUserInfos2.Count > 0) ddUserInfos.AddRange(dingDingUserInfos2);
  106. //获取三级部门
  107. OapiV2DepartmentListsubRequest reqDeptList3 = new OapiV2DepartmentListsubRequest() { DeptId = tempDept2.DeptId, Language = "zh_CN" };
  108. OapiV2DepartmentListsubResponse rspDeptList3 = deptListClient.Execute(reqDeptList3, access_token);
  109. if (rspDeptList3.Result != null)
  110. {
  111. foreach (var tempDept3 in rspDeptList3.Result)
  112. {
  113. //获取三级部门用户信息
  114. List<DingDingUserInfo> dingDingUserInfos3 = await GetDingDingUserInfo(divide, tempDept3.DeptId, tempDept3.ParentId, tempDept3.Name, access_token);
  115. //if (dingDingUserInfos3.Count > 0) ddUserInfos.AddRange(ddUserInfos.Union(dingDingUserInfos3.ToList()));
  116. if (dingDingUserInfos3.Count > 0) ddUserInfos.AddRange(dingDingUserInfos3);
  117. //获取四级部门
  118. OapiV2DepartmentListsubRequest reqDeptList4 = new OapiV2DepartmentListsubRequest() { DeptId = tempDept3.DeptId, Language = "zh_CN" };
  119. OapiV2DepartmentListsubResponse rspDeptList4 = deptListClient.Execute(reqDeptList4, access_token);
  120. if (rspDeptList4.Result != null)
  121. {
  122. foreach (var tempDept4 in rspDeptList4.Result)
  123. {
  124. //获取四级部门用户信息
  125. List<DingDingUserInfo> dingDingUserInfos4 = await GetDingDingUserInfo(divide, tempDept4.DeptId, tempDept4.ParentId, tempDept4.Name, access_token);
  126. //if (dingDingUserInfos4.Count > 0) ddUserInfos.AddRange(ddUserInfos.Union(dingDingUserInfos4.ToList()));
  127. if (dingDingUserInfos4.Count > 0) ddUserInfos.AddRange(dingDingUserInfos4);
  128. //获取五级部门
  129. OapiV2DepartmentListsubRequest reqDeptList5 = new OapiV2DepartmentListsubRequest() { DeptId = tempDept4.DeptId, Language = "zh_CN" };
  130. OapiV2DepartmentListsubResponse rspDeptList5 = deptListClient.Execute(reqDeptList5, access_token);
  131. if (rspDeptList5.Result != null)
  132. {
  133. foreach (var tempDept5 in rspDeptList5.Result)
  134. {
  135. //获取五级部门用户信息
  136. List<DingDingUserInfo> dingDingUserInfos5 = await GetDingDingUserInfo(divide, tempDept5.DeptId, tempDept5.ParentId, tempDept5.Name, access_token);
  137. //if (dingDingUserInfos5.Count > 0) ddUserInfos.AddRange(ddUserInfos.Union(dingDingUserInfos5).ToList());
  138. if (dingDingUserInfos5.Count > 0) ddUserInfos.AddRange(dingDingUserInfos5);
  139. }
  140. }
  141. }
  142. }
  143. }
  144. }
  145. }
  146. }
  147. }
  148. }
  149. //保存操作记录
  150. await _azureStorage.SaveBILog("tabledd-update", $"{_tmdName}【{_tmdId}】从钉钉组织结构更新至Azure Table表【DDUserInfo】中。", _dingDing, httpContext: HttpContext);
  151. var tempddUserInfos = ddUserInfos.GroupBy(c => c.userId).Select(c => c.First()).ToList();//去重
  152. //List<DingDingUserInfo> TempdingDingUserInfos = await _azureStorage.SaveOrUpdateAll(dingDingUserInfos); //只是保存至Table
  153. //查询数据的数据 并和钉钉查询的数据对比,找出不同的数据,并删除 待后期测试
  154. var users = await table.FindListByDict<DingDingUserInfo>(new Dictionary<string, object> { { "PartitionKey", $"{divide}" } });
  155. List<DingDingUserInfo> noExisits = new();
  156. if (users.Count > 0)
  157. {
  158. List<DingDingUserInfo> existsUserInfo = users.Where(u => !tempddUserInfos.Exists(e => u.userId.Equals(e.userId) && u.PartitionKey.Equals(e.PartitionKey))).ToList();
  159. //existsUserInfo.AddRange(tempddUserInfos.Where(e => !users.Exists(u => e.userId.Equals(u.userId) && e.PartitionKey.Equals(u.PartitionKey))).ToList());
  160. ////List<DingDingUserInfo> existsUserInfo = users.Where((x, i) => users.FindIndex(z => z.userId.Equals(x.userId) && x.PartitionKey.Equals(divide)) == i).Select(x => x).ToList();
  161. noExisits = await table.DeleteAll(users);
  162. }
  163. List<DingDingUserInfo> UserInfo = await table.SaveOrUpdateAll(tempddUserInfos); //保存和修改至Table
  164. return Ok(new { state = 200, UserInfo, noExisits });
  165. }
  166. catch (Exception ex)
  167. {
  168. await _dingDing.SendBotMsg($"BI,{_option.Location} /tabledd/get-dingdingusers \n {ex.Message}{ex.StackTrace}", GroupNames.成都开发測試群組);
  169. return BadRequest();
  170. }
  171. }
  172. /// <summary>
  173. /// 查询钉钉信息和醍摩豆信息
  174. /// </summary>
  175. /// <returns></returns>
  176. [ProducesDefaultResponseType]
  177. [HttpPost("get-ddusers")]
  178. public async Task<IActionResult> GetDingDingUsers(JsonElement jsonElement)
  179. {
  180. try
  181. {
  182. jsonElement.TryGetProperty("busy", out JsonElement busy);
  183. var table = _azureStorage.GetCloudTableClient().GetTableReference("BIDDUserInfo");
  184. //string divide = _configuration["CustomParam:SiteScope"];
  185. string divide = _option.Location;
  186. Dictionary<string, object> dic = new Dictionary<string, object> { { "PartitionKey", $"{divide}" } };
  187. List<DingDingUserInfo> ddUserInfoList = await table.FindListByDict<DingDingUserInfo>(dic);
  188. List<DDUserInfo> ddUserInfos = new();
  189. switch ($"{busy}")
  190. {
  191. case "admin":
  192. foreach (var item in ddUserInfoList)
  193. {
  194. if (item.roles.Contains("admin"))
  195. {
  196. DDUserInfo ddUserInfo = new()
  197. {
  198. partitionKey = item.PartitionKey,
  199. rowKey = item.RowKey,
  200. userId = item.userId,
  201. unionId = item.unionId,
  202. name = item.name,
  203. title = item.title,
  204. stateCode = item.stateCode,
  205. mobile = item.mobile,
  206. jobNumber = item.jobNumber,
  207. pid = item.pid,
  208. deptId = item.deptId,
  209. deptName = item.deptName,
  210. depts = item.depts,
  211. avatar = item.avatar,
  212. isAdmin = item.isAdmin,
  213. tmdId = item.tmdId,
  214. tmdName = item.tmdName,
  215. tmdMobile = item.tmdMobile,
  216. mail = item.mail,
  217. picture = item.picture,
  218. roles = item.roles,
  219. permissions = item.permissions,
  220. handleRoles = !string.IsNullOrEmpty($"{item.roles}") ? new List<string>(item.roles.Split(",")) : new List<string>(),
  221. handlePermissions = !string.IsNullOrEmpty($"{item.permissions}") ? new List<string>(item.permissions.Split(",")) : new List<string>(),
  222. };
  223. ddUserInfos.Add(ddUserInfo);
  224. }
  225. }
  226. break;
  227. default:
  228. foreach (var item in ddUserInfoList)
  229. {
  230. DDUserInfo ddUserInfo = new()
  231. {
  232. partitionKey = item.PartitionKey,
  233. rowKey = item.RowKey,
  234. userId = item.userId,
  235. unionId = item.unionId,
  236. name = item.name,
  237. title = item.title,
  238. stateCode = item.stateCode,
  239. mobile = item.mobile,
  240. jobNumber = item.jobNumber,
  241. pid = item.pid,
  242. deptId = item.deptId,
  243. deptName = item.deptName,
  244. depts = item.depts,
  245. avatar = item.avatar,
  246. isAdmin = item.isAdmin,
  247. tmdId = item.tmdId,
  248. tmdName = item.tmdName,
  249. tmdMobile = item.tmdMobile,
  250. mail = item.mail,
  251. picture = item.picture,
  252. roles = item.roles,
  253. permissions = item.permissions,
  254. handleRoles = !string.IsNullOrEmpty($"{item.roles}") ? new List<string>(item.roles.Split(",")) : new List<string>(),
  255. handlePermissions = !string.IsNullOrEmpty($"{item.permissions}") ? new List<string>(item.permissions.Split(",")) : new List<string>(),
  256. };
  257. ddUserInfos.Add(ddUserInfo);
  258. }
  259. break;
  260. }
  261. return Ok(new { state = 200, ddUserInfos = ddUserInfos });
  262. }
  263. catch (Exception ex)
  264. {
  265. await _dingDing.SendBotMsg($"BI,{_option.Location} /tabledd/get-ddusers \n {ex.Message}{ex.StackTrace}", GroupNames.成都开发測試群組);
  266. return BadRequest();
  267. }
  268. }
  269. /// <summary>
  270. /// 查询钉钉待入职人员的ID添加至Table数据表中
  271. /// </summary>
  272. /// <returns></returns>
  273. [ProducesDefaultResponseType]
  274. [AuthToken(Roles = "assist")]
  275. [HttpPost("set-ddinductionuser")]
  276. public async Task<IActionResult> SetDingDingInductionUser(JsonElement jsonElement)
  277. {
  278. try
  279. {
  280. var table = _azureStorage.GetCloudTableClient().GetTableReference("BIDDUserInfo");
  281. string appKey = _configuration["DingDingAuth:appKey"];
  282. string appSecret = _configuration["DingDingAuth:appSecret"];
  283. //string divide = _configuration["CustomParam:SiteScope"];
  284. string divide = _option.Location;
  285. var (_tmdId, _tmdName, pic, did, dname, dpic) = HttpJwtAnalysis.JwtXAuthBI(HttpContext.GetXAuth("AuthToken"), _option);
  286. //获取access_token
  287. IDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");
  288. OapiGettokenRequest request = new OapiGettokenRequest() { Appkey = appKey, Appsecret = appSecret };
  289. request.SetHttpMethod("Get");
  290. OapiGettokenResponse response = client.Execute(request);
  291. if (response.IsError)
  292. {
  293. return BadRequest();
  294. }
  295. //access_token的有效期为7200秒(2小时),有效期内重复获取会返回相同结果并自动续期,过期后获取会返回新的access_token
  296. string access_token = response.AccessToken;
  297. IDingTalkClient InductionClient = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/smartwork/hrm/employee/querypreentry");
  298. OapiSmartworkHrmEmployeeQuerypreentryRequest reqInduction = new OapiSmartworkHrmEmployeeQuerypreentryRequest() { Offset = 0L, Size = 50 };
  299. reqInduction.SetHttpMethod("GET");
  300. OapiSmartworkHrmEmployeeQuerypreentryResponse rspInduction = InductionClient.Execute(reqInduction, access_token);
  301. if (rspInduction.Result.DataList != null)
  302. {
  303. List<DingDingUserInfo> ddUserInfos = new List<DingDingUserInfo>();
  304. foreach (var itemId in rspInduction.Result.DataList)
  305. {
  306. DingDingUserInfo ddUserInfo = new DingDingUserInfo();
  307. ddUserInfo.PartitionKey = divide;
  308. ddUserInfo.RowKey = itemId;
  309. ddUserInfos.Add(ddUserInfo);
  310. }
  311. List<DingDingUserInfo> tempddUserInfos = await table.SaveAll(ddUserInfos);
  312. //保存操作记录
  313. await _azureStorage.SaveBILog("tabledd-add", $"{_tmdName}【{_tmdId}】添加待入职员工至table数据表中", _dingDing, httpContext: HttpContext);
  314. if (ddUserInfos.Count == tempddUserInfos.Count)
  315. {
  316. return Ok(new { state = 200, UserInfo = tempddUserInfos });
  317. }
  318. else
  319. {
  320. var diffArr = tempddUserInfos.Where(c => !ddUserInfos.Contains(c)).ToList();
  321. return Ok(new { state = 201, notUserInfo = diffArr });
  322. }
  323. }
  324. else
  325. {
  326. return Ok(new { state = 400, rspInduction.SubErrCode, rspInduction.SubErrMsg });
  327. }
  328. }
  329. catch (Exception ex)
  330. {
  331. await _dingDing.SendBotMsg($"BI,{_option.Location} /tabledd/set-ddinductionuser \n {ex.Message}{ex.StackTrace}", GroupNames.成都开发測試群組);
  332. return BadRequest();
  333. }
  334. }
  335. /// <summary>
  336. /// 获取钉钉离职人员ID并删除Table表中的数据
  337. /// </summary>
  338. /// <returns></returns>
  339. [ProducesDefaultResponseType]
  340. [AuthToken(Roles = "assist")]
  341. [HttpPost("del-ddquituser")]
  342. public async Task<IActionResult> DeleteDDQuitUser(JsonElement jsonElement)
  343. {
  344. try
  345. {
  346. var table = _azureStorage.GetCloudTableClient().GetTableReference("BIDDUserInfo");
  347. string appKey = _configuration["DingDingAuth:appKey"];
  348. string appSecret = _configuration["DingDingAuth:appSecret"];
  349. //string divide = _configuration["CustomParam:SiteScope"];
  350. string divide = _option.Location;
  351. var (_tmdId, _tmdName, pic, did, dname, dpic) = HttpJwtAnalysis.JwtXAuthBI(HttpContext.GetXAuth("AuthToken"), _option);
  352. //获取access_token
  353. IDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");
  354. OapiGettokenRequest request = new() { Appkey = appKey, Appsecret = appSecret };
  355. request.SetHttpMethod("Get");
  356. OapiGettokenResponse response = client.Execute(request);
  357. if (response.IsError)
  358. {
  359. return BadRequest();
  360. }
  361. //access_token的有效期为7200秒(2小时),有效期内重复获取会返回相同结果并自动续期,过期后获取会返回新的access_token
  362. string access_token = response.AccessToken;
  363. IDingTalkClient quitStaffClient = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/smartwork/hrm/employee/querydimission");
  364. OapiSmartworkHrmEmployeeQuerydimissionRequest reqDimission = new OapiSmartworkHrmEmployeeQuerydimissionRequest() { Offset = 0L, Size = 50L };
  365. OapiSmartworkHrmEmployeeQuerydimissionResponse rspDimission = quitStaffClient.Execute(reqDimission, access_token);
  366. if (rspDimission.Result != null)
  367. {
  368. List<DingDingUserInfo> ddUserInfos = new();
  369. foreach (var itemId in rspDimission.Result.DataList)
  370. {
  371. await table.DeleteSingle<DingDingUserInfo>(divide, $"{itemId}");
  372. }
  373. //保存操作记录
  374. await _azureStorage.SaveBILog("tabledd-del", $"{_tmdName}【{_tmdId}】从table数据表中删除离职员工", _dingDing, httpContext: HttpContext);
  375. return Ok(new { state = 200 });
  376. }
  377. else
  378. {
  379. return Ok(new { state = rspDimission.SubErrCode });
  380. }
  381. }
  382. catch (Exception ex)
  383. {
  384. await _dingDing.SendBotMsg($"BI,{_option.Location} /tabledd/del-ddquituser \n {ex.Message}{ex.StackTrace}", GroupNames.成都开发測試群組);
  385. return BadRequest();
  386. }
  387. }
  388. /// <summary>
  389. /// 更新钉钉和醍摩豆的BI权限
  390. /// </summary>
  391. /// <param name="jsonElement"></param>
  392. /// <returns></returns>
  393. [ProducesDefaultResponseType]
  394. [AuthToken(Roles = "admin")]
  395. [HttpPost("set-permissions")]
  396. public async Task<IActionResult> SetPermissions(JsonElement jsonElement)
  397. {
  398. try
  399. {
  400. if (!jsonElement.TryGetProperty("partitionKey", out JsonElement partitionKey)) return BadRequest();
  401. if (!jsonElement.TryGetProperty("rowKey", out JsonElement userId)) return BadRequest();
  402. if (!jsonElement.TryGetProperty("permissions", out JsonElement _permissions)) return BadRequest();
  403. var (_tmdId, _tmdName, pic, did, dname, dpic) = HttpJwtAnalysis.JwtXAuthBI(HttpContext.GetXAuth("AuthToken"), _option);
  404. var table = _azureStorage.GetCloudTableClient().GetTableReference("BIDDUserInfo");
  405. StringBuilder stringBuilder = new($"{_tmdName}【{_tmdId}】醍摩豆账号");
  406. List<DingDingUserInfo> ddUserInfo = new();
  407. var tempUser = await table.FindListByDict<DingDingUserInfo>(new Dictionary<string, object> { { "PartitionKey", $"{partitionKey}" }, { "RowKey", $"{userId}" } });
  408. List<string> listper = _permissions.ToObject<List<string>>();
  409. List<string> roles = new();//角色列表
  410. List<string> permissions = new ();//权限列表
  411. foreach (var item in tempUser)
  412. {
  413. stringBuilder.Append($"操作醍摩豆账户{item.tmdName}【{item.tmdId}】修改权限:{string.Join("|", listper.ToArray())}");
  414. if (string.IsNullOrEmpty($"{item.roles}"))
  415. {
  416. item.roles = "assist";
  417. }
  418. string tempPermissions = "";
  419. foreach (var itemPer in listper)
  420. {
  421. tempPermissions += !string.IsNullOrEmpty($"{tempPermissions}") ? $",{itemPer}" : $"{itemPer}";
  422. }
  423. item.permissions = tempPermissions;
  424. ddUserInfo.Add(item);
  425. roles = !string.IsNullOrEmpty($"{item.roles}") ? new List<string>(item.roles.Split(",")) : new List<string>();
  426. permissions = !string.IsNullOrEmpty($"{item.permissions}") ? new List<string>(item.permissions.Split(",")) : new List<string>();
  427. }
  428. ddUserInfo = await table.UpdateAll<DingDingUserInfo>(ddUserInfo);
  429. //保存操作记录
  430. await _azureStorage.SaveBILog("tabledd-update", stringBuilder?.ToString(), _dingDing, httpContext: HttpContext);
  431. return Ok(new { state = 200, ddUserInfo, roles, permissions });
  432. }
  433. catch (Exception ex)
  434. {
  435. await _dingDing.SendBotMsg($"BI,{_option.Location} /tabledd/set-permissions \n {ex.Message}{ex.StackTrace} ", GroupNames.成都开发測試群組);
  436. return BadRequest();
  437. }
  438. }
  439. /// <summary>
  440. /// 依据部门ID获取部门列表
  441. /// </summary>
  442. /// <param name="jsonElement"></param>
  443. /// <returns></returns>
  444. [ProducesDefaultResponseType]
  445. [HttpPost("get-dddeptiduser")]
  446. public async Task<IActionResult> GetDDDeptIdUser(JsonElement jsonElement)
  447. {
  448. try
  449. {
  450. if (!jsonElement.TryGetProperty("deptId", out JsonElement deptId)) return BadRequest();
  451. //string divide = _configuration["CustomParam:SiteScope"];
  452. string divide = _option.Location;
  453. Dictionary<string, object> dic = new Dictionary<string, object> { { "PartitionKey", $"{divide}" } };
  454. var table = _azureStorage.GetCloudTableClient().GetTableReference("BIDDUserInfo");
  455. List<DingDingUserInfo> tempUserInfos = await table.FindListByDict<DingDingUserInfo>(dic);
  456. List<DingDingUserInfo> userInfo = new();
  457. tempUserInfos.ForEach(x => {
  458. if (x.depts.Contains($"{deptId}"))
  459. {
  460. userInfo.Add(x);
  461. }
  462. if (x.pid.Equals(long.Parse($"{deptId}")))
  463. {
  464. userInfo.Add(x);
  465. }
  466. });
  467. //userInfo.Distinct().ToList(); //Equals实现去重
  468. userInfo.Where((x, i) => userInfo.FindIndex(z => z.RowKey.Equals(x.RowKey)) == i);//Lambda表达式去重
  469. //userInfo.GroupBy(p => p).Select(p => p.Key).ToList();//去重复
  470. //List<DingDingUserInfo> ddUserInfo = new();
  471. //List<DingDingUserInfo> tempUser = new();
  472. //tempUser = await _azureStorage.FindListByDict<DingDingUserInfo>(new Dictionary<string, object> { { "deptId", $"{deptId}" } });
  473. //if (tempUser.Count == 0)
  474. //{
  475. // tempUser = await _azureStorage.FindListByDict<DingDingUserInfo>(new Dictionary<string, object> { { "pid", $"{deptId}" } });
  476. //}
  477. //foreach (var itemUser in tempUser)
  478. //{
  479. // var tempUser1 = await _azureStorage.FindListByDict<DingDingUserInfo>(new Dictionary<string, object> { { "pid", $"{deptId}" } });
  480. // foreach (var itemUser1 in tempUser1)
  481. // {
  482. // if (!long.Parse($"{deptId}").Equals(itemUser1.pid))
  483. // {
  484. // var tempUser2 = await _azureStorage.FindListByDict<DingDingUserInfo>(new Dictionary<string, object> { { "pid", $"{itemUser1.pid}" } });
  485. // foreach (var itemUser2 in tempUser2)
  486. // {
  487. // if (!itemUser1.pid.Equals(itemUser2.pid))
  488. // {
  489. // var tempUser3 = await _azureStorage.FindListByDict<DingDingUserInfo>(new Dictionary<string, object> { { "pid", $"{itemUser2.pid}" } });
  490. // foreach (var itemUser3 in tempUser3)
  491. // {
  492. // if (!itemUser2.pid.Equals(itemUser3.pid))
  493. // {
  494. // var tempUser4 = await _azureStorage.FindListByDict<DingDingUserInfo>(new Dictionary<string, object> { { "pid", $"{itemUser3.pid}" } });
  495. // foreach (var itemUser4 in tempUser4)
  496. // {
  497. // if (!itemUser3.pid.Equals(itemUser4.pid)) { }
  498. // if (ddUserInfo.Find(x => x.RowKey.Equals(itemUser4.RowKey)) == null)
  499. // {
  500. // ddUserInfo.Add(itemUser4);
  501. // }
  502. // }
  503. // }
  504. // if (ddUserInfo.Find(x => x.RowKey.Equals(itemUser3.RowKey)) == null)
  505. // {
  506. // ddUserInfo.Add(itemUser3);
  507. // }
  508. // }
  509. // }
  510. // if (ddUserInfo.Find(x => x.RowKey.Equals(itemUser2.RowKey)) == null)
  511. // {
  512. // ddUserInfo.Add(itemUser2);
  513. // }
  514. // }
  515. // }
  516. // if (ddUserInfo.Find(x => x.RowKey.Equals(itemUser1.RowKey)) == null)
  517. // {
  518. // ddUserInfo.Add(itemUser1);
  519. // }
  520. // }
  521. // if (ddUserInfo.Find(x => x.RowKey.Equals(itemUser.RowKey)) == null)
  522. // {
  523. // ddUserInfo.Add(itemUser);
  524. // }
  525. //}
  526. List<DDUserInfo> ddUserInfos = new();
  527. foreach (var item in userInfo)
  528. {
  529. DDUserInfo tempUserInfo = new DDUserInfo()
  530. {
  531. partitionKey = item.PartitionKey,
  532. rowKey = item.RowKey,
  533. userId = item.userId,
  534. unionId = item.unionId,
  535. name = item.name,
  536. title = item.title,
  537. stateCode = item.stateCode,
  538. mobile = item.mobile,
  539. jobNumber = item.jobNumber,
  540. pid = item.pid,
  541. deptId = item.deptId,
  542. deptName = item.deptName,
  543. depts = item.depts,
  544. avatar = item.avatar,
  545. isAdmin = item.isAdmin,
  546. tmdId = item.tmdId,
  547. tmdName = item.tmdName,
  548. tmdMobile = item.tmdMobile,
  549. mail = item.mail,
  550. picture = item.picture,
  551. roles = item.roles,
  552. permissions = item.permissions,
  553. handleRoles = !string.IsNullOrEmpty($"{item.roles}") ? new List<string>(item.roles.Split(",")) : new List<string>(),
  554. handlePermissions = !string.IsNullOrEmpty($"{item.permissions}") ? new List<string>(item.permissions.Split(",")) : new List<string>(),
  555. };
  556. ddUserInfos.Add(tempUserInfo);
  557. }
  558. return Ok(new { state = 200, ddUserInfos });
  559. }
  560. catch (Exception ex)
  561. {
  562. await _dingDing.SendBotMsg($"BI, {_option.Location} /tabledd/get-dddeptiduser \n {ex.Message}{ex.StackTrace}", GroupNames.成都开发測試群組);
  563. return BadRequest();
  564. }
  565. }
  566. /// <summary>
  567. /// 后端钉钉账户和醍摩豆账户进行绑定
  568. /// </summary>
  569. /// <param name="jsonElement"></param>
  570. /// <returns></returns>
  571. [ProducesDefaultResponseType]
  572. [AuthToken(Roles = "assist")]
  573. [HttpPost("set-backenbind")]
  574. public async Task<IActionResult> SetBackenBind(JsonElement jsonElement)
  575. {
  576. try
  577. {
  578. if (!jsonElement.TryGetProperty("rowKey", out JsonElement rowKey)) return BadRequest();
  579. if (!jsonElement.TryGetProperty("mobile", out JsonElement mobile)) return BadRequest();
  580. var (_tmdId, _tmdName, pic, did, dname, dpic) = HttpJwtAnalysis.JwtXAuthBI(HttpContext.GetXAuth("AuthToken"), _option);
  581. var table = _azureStorage.GetCloudTableClient().GetTableReference("BIDDUserInfo");
  582. List<DingDingUserInfo> tempddUsers = null;
  583. List<DingDingUserInfo> ddUsers = new();
  584. StringBuilder tableSql = new();
  585. if (!string.IsNullOrEmpty($"{rowKey}"))
  586. tableSql.Append($"RowKey {QueryComparisons.Equal} '{rowKey}'");
  587. string tmdId = null;
  588. string tmdName = null;
  589. string tmdMobile = null;
  590. string mail = null;
  591. string picture = null;
  592. HttpClient httpClient = _http.CreateClient();
  593. string url = _configuration.GetValue<string>("HaBookAuth:CoreId:userinfo");
  594. List<string> mobiles = new List<string>() { $"{mobile}" };
  595. HttpResponseMessage responseMessage = await httpClient.PostAsJsonAsync(url, mobiles);
  596. if (responseMessage.StatusCode == HttpStatusCode.OK)
  597. {
  598. string temp = responseMessage.Content.ReadAsStringAsync().Result;
  599. List<JsonElement> json_id = temp.ToObject<List<JsonElement>>();
  600. if (json_id.Count > 0)
  601. {
  602. foreach (var item in json_id)
  603. {
  604. tmdId = item.GetProperty("id").ToString();
  605. tmdName = item.GetProperty("name").ToString();
  606. tmdMobile = item.GetProperty("mobile").ToString();
  607. mail = item.GetProperty("mail").ToString();
  608. picture = item.GetProperty("picture").ToString();
  609. }
  610. }
  611. else return Ok(new { state = 1, message = "该手机号未找到醍摩豆账户" });
  612. }
  613. tempddUsers = await table.QueryWhereString<DingDingUserInfo>(tableSql.ToString());
  614. if (tempddUsers.Count > 0)
  615. {
  616. foreach (var item in tempddUsers)
  617. {
  618. if (item.RowKey.Equals($"{rowKey}"))
  619. {
  620. item.tmdId = tmdId;
  621. item.tmdName = tmdName;
  622. item.tmdMobile = tmdMobile;
  623. item.mail = mail;
  624. item.picture = picture;
  625. ddUsers.Add(item);
  626. }
  627. }
  628. }
  629. else return Ok(new { state = 2, message = "钉钉ID错误请检查钉钉ID" });
  630. if (ddUsers.Count > 0) ddUsers = await table.SaveOrUpdateAll(ddUsers);
  631. //保存操作记录
  632. await _azureStorage.SaveBILog("tabledd-update", $"{_tmdName}【{_tmdId}】操作:绑定钉钉账户[{rowKey}]和醍摩豆账户[{tmdId}]", _dingDing, httpContext: HttpContext);
  633. return Ok(new { state = 200, ddUsers });
  634. }
  635. catch (Exception ex)
  636. {
  637. await _dingDing.SendBotMsg($"BI, {_option.Location} /tabledd/set-backenbind \n {ex.Message}{ex.StackTrace}", GroupNames.成都开发測試群組);
  638. return BadRequest();
  639. }
  640. }
  641. /// <summary>
  642. /// 设置系统管理员
  643. /// </summary>
  644. /// <param name="jsonElement"></param>
  645. /// <returns></returns>
  646. [ProducesDefaultResponseType]
  647. [AuthToken(Roles = "admin")]
  648. [HttpPost("set-backend")]
  649. public async Task<IActionResult> SetBackendAdmin(JsonElement jsonElement)
  650. {
  651. try
  652. {
  653. if (!jsonElement.TryGetProperty("partitionKey", out JsonElement partitionKey)) return BadRequest();
  654. if (!jsonElement.TryGetProperty("rowKey", out JsonElement rowKey)) return BadRequest();
  655. if (!jsonElement.TryGetProperty("isAdmin", out JsonElement isAdmin)) return BadRequest();
  656. var (_tmdId, _tmdName, pic, did, dname, dpic) = HttpJwtAnalysis.JwtXAuthBI(HttpContext.GetXAuth("AuthToken"), _option);
  657. var table = _azureStorage.GetCloudTableClient().GetTableReference("BIDDUserInfo");
  658. var tempUser = await table.FindListByDict<DingDingUserInfo>(new Dictionary<string, object> { { "PartitionKey", $"{partitionKey}" }, { "RowKey", $"{rowKey}" } });
  659. List<DDUserInfo> ddUserInfos = new();
  660. List<string> roles = new();//角色列表
  661. List<string> permissions = new();//权限列表
  662. StringBuilder msg = new($"{_tmdName}【{_tmdId}】");
  663. if (bool.Parse($"{isAdmin}") == true)
  664. {
  665. foreach (var user in tempUser)
  666. {
  667. if (string.IsNullOrEmpty(user.roles))
  668. {
  669. user.roles = "admin,assist";
  670. }
  671. List<string> tempRoles = new(user.roles.Split(","));
  672. if (!tempRoles.Contains("admin"))
  673. {
  674. tempRoles.Add("admin");
  675. }
  676. user.roles = string.Join(",", tempRoles);
  677. user.joinTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
  678. DingDingUserInfo respUser = await table.SaveOrUpdate<DingDingUserInfo>(user);
  679. if (respUser != null)
  680. {
  681. roles = !string.IsNullOrEmpty($"{respUser.roles}") ? new List<string>(respUser.roles.Split(",")) : new List<string>();
  682. }
  683. msg.Append($"添加{respUser.name}【{respUser.RowKey}】账号的BI管理员");
  684. }
  685. }
  686. else
  687. {
  688. foreach (var user in tempUser)
  689. {
  690. List<string> tempRoles = new(user.roles.Split(","));
  691. if (tempRoles.Contains("admin"))
  692. {
  693. tempRoles.Remove("admin");
  694. }
  695. user.roles = string.Join(",", tempRoles);
  696. DingDingUserInfo respUser = await table.SaveOrUpdate<DingDingUserInfo>(user);
  697. if (respUser != null)
  698. {
  699. roles = !string.IsNullOrEmpty($"{respUser.roles}") ? new List<string>(respUser.roles.Split(",")) : new List<string>();
  700. msg.Append($"取消{respUser.name}【{respUser.RowKey}】账号的BI管理员");
  701. }
  702. }
  703. }
  704. //保存操作记录
  705. await _azureStorage.SaveBILog("tabledd-update", msg.ToString(), _dingDing, httpContext: HttpContext);
  706. return Ok(new { state = 200, roles });
  707. }
  708. catch (Exception ex)
  709. {
  710. await _dingDing.SendBotMsg($"BI, {_option.Location} /tabledd/set-backend \n {ex.Message}{ex.StackTrace}", GroupNames.成都开发測試群組);
  711. return BadRequest();
  712. }
  713. }
  714. /// <summary>
  715. /// 通过醍摩豆账户信息 设置BI后台管理员
  716. /// </summary>
  717. /// <param name="jsonElement"></param>
  718. /// <returns></returns>
  719. [ProducesDefaultResponseType]
  720. [AuthToken(Roles = "admin")]
  721. [HttpPost("set-tmdadmin")]
  722. public async Task<IActionResult> SetTmdBackendAdmin(JsonElement jsonElement)
  723. {
  724. try
  725. {
  726. if (!jsonElement.TryGetProperty("tmdId", out JsonElement tmdId)) return BadRequest();
  727. if (!jsonElement.TryGetProperty("tmdName", out JsonElement tmdName)) return BadRequest();
  728. if (!jsonElement.TryGetProperty("mobile", out JsonElement mobile)) return BadRequest();
  729. jsonElement.TryGetProperty("picture ", out JsonElement picture);
  730. jsonElement.TryGetProperty("mail ", out JsonElement mail);
  731. var (_tmdId, _tmdName, pic, did, dname, dpic) = HttpJwtAnalysis.JwtXAuthBI(HttpContext.GetXAuth("AuthToken"), _option);
  732. List<DDUserInfo> ddUserInfos = new();
  733. List<string> roles = new();//角色列表
  734. List<string> permissions = new();//权限列表
  735. StringBuilder msg = new($"{_tmdName}【{_tmdId}】");
  736. var table = _azureStorage.GetCloudTableClient().GetTableReference("BIDDUserInfo");
  737. //string divide = _configuration["CustomParam:SiteScope"];
  738. string divide = _option.Location;
  739. Dictionary<string, object> dic = new Dictionary<string, object> { { "PartitionKey", $"{divide}" }, { "mobile", $"{mobile}" } };
  740. List<DingDingUserInfo> ddUserInfoList = await table.FindListByDict<DingDingUserInfo>(dic);
  741. if (ddUserInfoList.Count > 0)
  742. {
  743. foreach (var user in ddUserInfoList)
  744. {
  745. List<string> tempRoles = new(user.roles.Split(","));
  746. if (tempRoles.Count > 0)
  747. {
  748. if (!tempRoles.Contains("admin"))
  749. {
  750. tempRoles.Add("admin");
  751. }
  752. }
  753. else
  754. tempRoles.Add("admin");
  755. user.roles = string.Join(",", tempRoles);
  756. user.joinTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
  757. user.tmdId = $"{tmdId}";
  758. user.tmdName = $"{tmdName}";
  759. user.tmdMobile = $"{mobile}";
  760. if (!string.IsNullOrEmpty($"{picture}"))
  761. {
  762. user.picture = $"{picture}";
  763. }
  764. if (!string.IsNullOrEmpty($"{mail}"))
  765. {
  766. user.mail = $"{mail}";
  767. }
  768. msg.Append($" 将:{tmdName}【{tmdId}】醍摩豆账号和({user.name}【{user.unionId}】) 钉钉账户绑定,并设置管理员");
  769. DingDingUserInfo respUser = await table.SaveOrUpdate<DingDingUserInfo>(user);
  770. if (respUser != null)
  771. {
  772. roles = !string.IsNullOrEmpty($"{respUser.roles}") ? new List<string>(respUser.roles.Split(",")) : new List<string>();
  773. }
  774. DDUserInfo dDUserInfo = new()
  775. {
  776. partitionKey = respUser.PartitionKey,
  777. rowKey = respUser.RowKey,
  778. userId = respUser.userId,
  779. unionId = respUser.unionId,
  780. name = respUser.name,
  781. title = respUser.title,
  782. stateCode = respUser.stateCode,
  783. mobile = respUser.mobile,
  784. jobNumber = respUser.jobNumber,
  785. pid = respUser.pid,
  786. deptId = respUser.deptId,
  787. deptName = respUser.deptName,
  788. depts = respUser.depts,
  789. avatar = respUser.avatar,
  790. isAdmin = respUser.isAdmin,
  791. tmdId = respUser.tmdId,
  792. tmdName = respUser.tmdName,
  793. tmdMobile = respUser.tmdMobile,
  794. mail = respUser.mail,
  795. picture = respUser.picture,
  796. roles = respUser.roles,
  797. permissions = respUser.permissions,
  798. handleRoles = !string.IsNullOrEmpty($"{respUser.roles}") ? new List<string>(respUser.roles.Split(",")) : new List<string>(),
  799. handlePermissions = !string.IsNullOrEmpty($"{respUser.permissions}") ? new List<string>(respUser.permissions.Split(",")) : new List<string>()
  800. };
  801. ddUserInfos.Add(dDUserInfo);
  802. }
  803. }
  804. else
  805. {
  806. DingDingUserInfo dingDingUserInfo = new()
  807. {
  808. PartitionKey = divide,
  809. RowKey = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().ToString(),
  810. roles = "admin",
  811. tmdId = $"{tmdId}",
  812. tmdName = $"{tmdName}",
  813. tmdMobile = $"{mobile}",
  814. };
  815. if (!string.IsNullOrEmpty($"{picture}"))
  816. {
  817. dingDingUserInfo.picture = $"{picture}";
  818. }
  819. if (!string.IsNullOrEmpty($"{mail}"))
  820. {
  821. dingDingUserInfo.mail = $"{mail}";
  822. }
  823. DingDingUserInfo respUser = await table.SaveOrUpdate<DingDingUserInfo>(dingDingUserInfo);
  824. return Ok(new { state = 201, msg = "新生成的BIadmin", respUser });
  825. }
  826. //保存操作记录
  827. await _azureStorage.SaveBILog("tabledd-update",msg.ToString(), _dingDing, httpContext: HttpContext);
  828. return Ok(new { state = 200, ddUserInfos });
  829. }
  830. catch (Exception ex)
  831. {
  832. await _dingDing.SendBotMsg($"BI,{_option.Location} /tabledd/set-tmdadmin \n {ex.Message}{ex.StackTrace}", GroupNames.成都开发測試群組);
  833. return BadRequest();
  834. }
  835. }
  836. /// <summary>
  837. /// 解除钉钉和醍摩豆的绑定
  838. /// </summary>
  839. /// <param name="jsonElement"></param>
  840. /// <returns></returns>
  841. [ProducesDefaultResponseType]
  842. [HttpPost("set-unbind")]
  843. public async Task<IActionResult> SetUnbind(JsonElement jsonElement)
  844. {
  845. jsonElement.TryGetProperty("mobile", out JsonElement mobile);
  846. //string divide = _configuration["CustomParam:SiteScope"];
  847. string divide = _option.Location;
  848. Dictionary<string, object> dic = new Dictionary<string, object> { { "PartitionKey", $"{divide}" } };
  849. if (!string.IsNullOrEmpty($"{mobile}"))
  850. dic.Add("tmdMobile", $"{mobile}");
  851. else
  852. dic.Add("tmdMobile", "18281911681");
  853. var table = _azureStorage.GetCloudTableClient().GetTableReference("BIDDUserInfo");
  854. List<DingDingUserInfo> ddUserInfoList = await table.FindListByDict<DingDingUserInfo>(dic);
  855. List<DingDingUserInfo> tempDDUI = new();
  856. if (ddUserInfoList.Count > 0)
  857. {
  858. foreach (var item in ddUserInfoList)
  859. {
  860. item.tmdId = null;
  861. item.tmdName = null;
  862. item.tmdMobile = null;
  863. item.picture = null;
  864. tempDDUI.Add(item);
  865. }
  866. }
  867. else return Ok(new { state = 404, msg = "未找到手机号匹配的绑定号码!,请检查手机号" });
  868. var userInfos = await table.SaveOrUpdateAll(tempDDUI); //保存和修改至Table
  869. return Ok(new { state = 200, userInfos });
  870. }
  871. /// <summary>
  872. /// 获取钉钉用户信息
  873. /// 并查询本地Table数据表中是否存在
  874. /// </summary>
  875. /// <param name="deptId"></param>
  876. /// <param name="parentId"></param>
  877. /// <param name="name"></param>
  878. /// <param name="access_token"></param>
  879. /// <returns></returns>
  880. public async Task<List<DingDingUserInfo>> GetDingDingUserInfo(string partitionKey, long deptId, long parentId, string name, string access_token)
  881. {
  882. List<DingDingUserInfo> ddUserInfos = new();
  883. //获取部门人员信息
  884. IDingTalkClient userListClient = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/list");
  885. //获取部门用户
  886. OapiV2UserListRequest reqUserList = new()
  887. {
  888. DeptId = deptId,
  889. Cursor = 0L,
  890. Size = 50L,
  891. OrderField = "custom",
  892. Language = "zh_CN"
  893. };
  894. reqUserList.SetHttpMethod("GET");
  895. OapiV2UserListResponse rspV2UserList = userListClient.Execute(reqUserList, access_token);
  896. if (rspV2UserList.Result.List != null)
  897. {
  898. foreach (var itemUser in rspV2UserList.Result.List)
  899. {
  900. var tempInfo = ddUserInfos.Find(x => x.RowKey.Equals(itemUser.Unionid));
  901. if (string.IsNullOrEmpty($"{tempInfo}"))
  902. {
  903. var table = _azureStorage.GetCloudTableClient().GetTableReference("BIDDUserInfo");
  904. var users = await table.FindListByDict<DingDingUserInfo>(new Dictionary<string, object> { { "userId", $"{itemUser.Userid}" }, { "unionId", $"{itemUser.Unionid}" } });
  905. if (users.Count > 0)
  906. {
  907. foreach (var user in users)
  908. {
  909. DingDingUserInfo ddUserInfo = new();
  910. ddUserInfo.PartitionKey = user.PartitionKey;
  911. ddUserInfo.RowKey = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().ToString();
  912. ddUserInfo.userId = itemUser.Userid;
  913. ddUserInfo.unionId = itemUser.Unionid;
  914. ddUserInfo.name = itemUser.Name;
  915. ddUserInfo.email = itemUser.Email;
  916. ddUserInfo.title = itemUser.Title;
  917. ddUserInfo.stateCode = itemUser.StateCode;
  918. ddUserInfo.mobile = itemUser.Mobile;
  919. ddUserInfo.jobNumber = itemUser.JobNumber;
  920. ddUserInfo.pid = parentId;
  921. ddUserInfo.deptId = deptId;
  922. ddUserInfo.deptName = name;
  923. ddUserInfo.depts = string.Join(",", itemUser.DeptIdList.ToArray());
  924. ddUserInfo.avatar = itemUser.Avatar;
  925. ddUserInfo.isAdmin = itemUser.Admin;
  926. ddUserInfo.tmdId = user.tmdId;
  927. ddUserInfo.tmdName = user.tmdName;
  928. ddUserInfo.tmdMobile = user.tmdMobile;
  929. ddUserInfo.mail = user.mail;
  930. ddUserInfo.picture = user.picture;
  931. ddUserInfo.roles = user.roles;
  932. ddUserInfo.permissions = user.permissions;
  933. ddUserInfos.Add(ddUserInfo);
  934. }
  935. }
  936. else
  937. {
  938. DingDingUserInfo ddUserInfo = new()
  939. {
  940. RowKey = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().ToString(),
  941. userId = itemUser.Userid,
  942. unionId = itemUser.Unionid,
  943. name = itemUser.Name,
  944. email = itemUser.Email,
  945. title = itemUser.Title,
  946. mobile = itemUser.Mobile,
  947. jobNumber = itemUser.JobNumber,
  948. pid = parentId,
  949. deptId = deptId,
  950. deptName = name,
  951. depts = string.Join(",", itemUser.DeptIdList.ToArray()),
  952. avatar = itemUser.Avatar,
  953. isAdmin = itemUser.Admin,
  954. PartitionKey = partitionKey,
  955. tmdId = "",
  956. tmdName = "",
  957. tmdMobile = "",
  958. mail = "",
  959. picture = "",
  960. roles = "assist",
  961. permissions = "abilitystandard-read,batcharea-read,batchschool-read,orgusers-read"
  962. };
  963. ddUserInfos.Add(ddUserInfo);
  964. }
  965. }
  966. }
  967. }
  968. return ddUserInfos;
  969. }
  970. public record DDUserInfo
  971. {
  972. public string partitionKey { get; set; }
  973. public string rowKey { get; set; }
  974. public string userId { get; set; }
  975. public string unionId { get; set; }
  976. public string name { get; set; }
  977. public string title { get; set; }
  978. public string stateCode { get; set; }
  979. public string mobile { get; set; }
  980. public string jobNumber { get; set; }
  981. public long pid { get; set; }
  982. public long deptId { get; set; }
  983. public string deptName { get; set; }
  984. public string depts { get; set; }
  985. public string avatar { get; set; }
  986. public bool isAdmin { get; set; }
  987. public string tmdId { get; set; }
  988. public string tmdName { get; set; }
  989. public string tmdMobile { get; set; }
  990. public string mail { get; set; }
  991. public string picture { get; set; }
  992. public string roles { get; set; }
  993. public string permissions { get; set; }
  994. public List<string> handleRoles { get; set; }
  995. public List<string> handlePermissions { get; set; }
  996. }
  997. }
  998. }