AppCompanyController.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. using Azure.Cosmos;
  2. using Microsoft.AspNetCore.Http;
  3. using Microsoft.AspNetCore.Mvc;
  4. using Microsoft.Extensions.Configuration;
  5. using Microsoft.Extensions.Options;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Text.Json;
  11. using System.Threading.Tasks;
  12. using TEAMModelBI.Filter;
  13. using TEAMModelBI.Tool.Extension;
  14. using TEAMModelOS.Models;
  15. using TEAMModelOS.SDK.DI;
  16. using TEAMModelOS.SDK.Extension;
  17. using TEAMModelOS.SDK.Models.Cosmos.BI;
  18. using TEAMModelOS.SDK.Models.Service;
  19. namespace TEAMModelBI.Controllers.BINormal
  20. {
  21. [Route("appcompany")]
  22. [ApiController]
  23. public class AppCompanyController : ControllerBase
  24. {
  25. public readonly AzureCosmosFactory _azureCosmos;
  26. public readonly AzureStorageFactory _azureStorage;
  27. public readonly DingDing _dingDing;
  28. public readonly Option _option;
  29. private readonly IConfiguration _configuration;
  30. private readonly NotificationService _notificationService;
  31. public AppCompanyController(AzureCosmosFactory azureCosmos, AzureStorageFactory azureStorage, DingDing dingDing, IOptionsSnapshot<Option> option, IConfiguration configuration, NotificationService notificationService)
  32. {
  33. _azureCosmos = azureCosmos;
  34. _azureStorage = azureStorage;
  35. _dingDing = dingDing;
  36. _option = option?.Value;
  37. _configuration = configuration;
  38. _notificationService = notificationService;
  39. }
  40. /// <summary>
  41. /// 查询应用信息
  42. /// </summary>
  43. /// <param name="jsonElement"></param>
  44. /// <returns></returns>
  45. [ProducesDefaultResponseType]
  46. [HttpPost("get-info")]
  47. public async Task<IActionResult> GetInfo(JsonElement jsonElement)
  48. {
  49. jsonElement.TryGetProperty("appId", out JsonElement appId);
  50. jsonElement.TryGetProperty("eid", out JsonElement eid);
  51. jsonElement.TryGetProperty("audit", out JsonElement audit);
  52. var cosmosClient = _azureCosmos.GetCosmosClient();
  53. StringBuilder sqlTxt = new($"select c.id,c.pk,c.code,c.name,c.descr,c.picture,c.jwtKey,c.status,c.audit,c.refuseDesc,c.gateways,c.apis,c.webhookDomain,c.webHooks,c.schools from c where c.pk='App'");
  54. if (!string.IsNullOrEmpty($"{appId}"))
  55. {
  56. sqlTxt.Append($" and id='{appId}'");
  57. }
  58. if (!string.IsNullOrEmpty($"{audit}"))
  59. {
  60. sqlTxt.Append($" and audit='{audit}'");
  61. }
  62. List<ReadCompany> appCompanys = new();
  63. await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "Normal").GetItemQueryStreamIterator(queryText: sqlTxt.ToString(), requestOptions: string.IsNullOrEmpty($"{eid}") ? new QueryRequestOptions() { } : new QueryRequestOptions() { PartitionKey = new PartitionKey($"App-{eid}") }))
  64. {
  65. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  66. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  67. {
  68. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  69. {
  70. ReadCompany readCompany = new()
  71. {
  72. id = obj.GetProperty("id").GetString(),
  73. pk = obj.GetProperty("pk").GetString(),
  74. code = obj.GetProperty("code").GetString(),
  75. name = obj.GetProperty("name").GetString(),
  76. descr = obj.GetProperty("descr").GetString(),
  77. picture = obj.GetProperty("picture").GetString(),
  78. jwtKey = obj.GetProperty("jwtKey").GetString(),
  79. status = obj.GetProperty("status").GetInt32(),
  80. audit = obj.GetProperty("audit").GetInt32(),
  81. refuseDesc = obj.GetProperty("refuseDesc").GetString(),
  82. gateways = obj.GetProperty("gateways").GetString(),
  83. apis = obj.GetProperty("apis").ToObject<List<AppApiState>>(),
  84. webhookDomain = obj.GetProperty("webhookDomain").GetString(),
  85. webHooks = obj.GetProperty("webHooks").ToObject<List<WebHookState>>(),
  86. schools = obj.GetProperty("schools").ToObject<List<ApplySchool>>()
  87. };
  88. appCompanys.Add(readCompany);
  89. }
  90. }
  91. }
  92. return Ok(new { state = 200, appCompanys });
  93. }
  94. /// <summary>
  95. /// 新增或者修改应用
  96. /// </summary>
  97. /// <param name="appCompany"></param>
  98. /// <returns></returns>
  99. [ProducesDefaultResponseType]
  100. [AuthToken(Roles = "assist")]
  101. [HttpPost("set-info")]
  102. public async Task<IActionResult> SetAppInfo(AppCompany appCompany)
  103. {
  104. var cosmosClient = _azureCosmos.GetCosmosClient();
  105. StringBuilder stringBuilder = new();
  106. //新建
  107. if (string.IsNullOrEmpty($"{appCompany.id}"))
  108. {
  109. appCompany.id = GenerateRandom.StrRandom(8, large: true, small: true);
  110. appCompany.code = $"App-{appCompany.code}";
  111. appCompany.createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
  112. appCompany.status = -1;
  113. appCompany.audit = -1;
  114. appCompany = await cosmosClient.GetContainer("TEAMModelOS", "Normal").CreateItemAsync<AppCompany>(appCompany, new PartitionKey(appCompany.code));
  115. }
  116. //修改
  117. else
  118. {
  119. var response = await cosmosClient.GetContainer("TEAMModelOS", "Normal").ReadItemStreamAsync(appCompany.id, new PartitionKey(appCompany.code));
  120. if (response.Status == 200)
  121. {
  122. appCompany.pk = "App";
  123. appCompany.ttl = -1;
  124. appCompany = await cosmosClient.GetContainer("TEAMModelOS", "Normal").ReplaceItemAsync<AppCompany>(appCompany, appCompany.id, new PartitionKey(appCompany.code));
  125. }
  126. else return Ok(new { state = 404, msg = "未找到该id相关的企业应用信息" });
  127. }
  128. return Ok(new { state = 200, appCompany });
  129. }
  130. /// <summary>
  131. ///
  132. /// </summary>
  133. /// <param name="jsonElement"></param>
  134. /// <returns></returns>
  135. [HttpPost("get-applyapi")]
  136. public async Task<IActionResult> SetAuditApp(JsonElement jsonElement)
  137. {
  138. if (!jsonElement.TryGetProperty("appIds", out JsonElement appIds)) return BadRequest();
  139. if (!jsonElement.TryGetProperty("isAudit", out JsonElement isAudit)) return BadRequest();
  140. jsonElement.TryGetProperty("refuseDesc", out JsonElement refuseDesc);
  141. var cosmosClient = _azureCosmos.GetCosmosClient();
  142. List<AppIdOrCode> idOrCode = appIds.ToObject<List<AppIdOrCode>>();
  143. List<AppIdOrCode> haveIds = new();
  144. if (idOrCode.Count > 0)
  145. {
  146. foreach (var idCode in idOrCode)
  147. {
  148. AppCompany appCompany = await cosmosClient.GetContainer("TEAMModelOS", "Normal").ReadItemAsync<AppCompany>(idCode.id, new PartitionKey(idCode.code));
  149. //var response = await cosmosClient.GetContainer("TEAMModelOS", "Normal").ReadItemStreamAsync(idCode.id, new PartitionKey(idCode.code));
  150. if (bool.Parse($"{isAudit}") == true)
  151. {
  152. appCompany.audit = 1;
  153. }
  154. else
  155. {
  156. appCompany.audit = 0;
  157. appCompany.refuseDesc = $"{refuseDesc}";
  158. }
  159. try
  160. {
  161. await cosmosClient.GetContainer("TEAMModelOS", "Normal").ReplaceItemAsync<AppCompany>(appCompany, appCompany.id, new PartitionKey(idCode.code));
  162. }
  163. catch
  164. {
  165. haveIds.Add(idCode);
  166. }
  167. }
  168. }
  169. else return Ok(new { state = 404, msg = "appIds参数错误" });
  170. return Ok(new { state = 200});
  171. }
  172. /// <summary>
  173. /// 应用申请Api接口信息
  174. /// 审核应用api接口信息
  175. /// </summary>
  176. /// <param name="jsonElement"></param>
  177. /// <returns></returns>
  178. [ProducesDefaultResponseType]
  179. [HttpPost("set-applyapi")]
  180. public async Task<IActionResult> SetApplyApi(JsonElement jsonElement)
  181. {
  182. if (!jsonElement.TryGetProperty("appId", out JsonElement appId)) return BadRequest();
  183. if (!jsonElement.TryGetProperty("appCode", out JsonElement appCode)) return BadRequest();
  184. if (!jsonElement.TryGetProperty("apiIds", out JsonElement apiIds)) return BadRequest();
  185. if (!jsonElement.TryGetProperty("operate", out JsonElement operate)) return BadRequest();
  186. List<string> tempApi = apiIds.ToObject<List<string>>();
  187. var cosmosClient = _azureCosmos.GetCosmosClient();
  188. List<string> haveApi = new(); //存在api接口
  189. List<string> sendWhom = new();//消息分发给谁 待完善
  190. string bizcode = ""; //消息名称
  191. AppCompany appCompany = await cosmosClient.GetContainer("TEAMModelOS", "Normal").ReadItemAsync<AppCompany>($"{appId}", new PartitionKey($"{appCode}"));
  192. if (appCompany != null)
  193. {
  194. switch (operate.GetString())
  195. {
  196. case "apply":
  197. jsonElement.TryGetProperty("applyDesc", out JsonElement applyDesc);
  198. tempApi.ForEach(x =>
  199. {
  200. var strt = appCompany.apis.Find(y => y.no.Equals($"{x}")).no;
  201. if (strt == null)
  202. {
  203. appCompany.apis.Add(new AppApiState() { no = $"{x}", applyDesc = $"{applyDesc}", status = -1 });
  204. }
  205. else haveApi.Add(x);
  206. });
  207. sendWhom = new List<string> { "1528783103", "1636016499" };
  208. bizcode = "applyapi";
  209. break;
  210. case "audit":
  211. jsonElement.TryGetProperty("isAudit", out JsonElement isAudit);
  212. jsonElement.TryGetProperty("refuseDesc", out JsonElement refuseDesc);
  213. tempApi.ForEach(x =>
  214. {
  215. var temp = appCompany.apis.Find(n => n.no == x);
  216. if (temp != null)
  217. {
  218. AppApiState appApiState = appCompany.apis.Single(a => a.no == x);
  219. if (bool.Parse($"{isAudit}") == true)
  220. {
  221. appApiState.status = 1;
  222. appApiState.refuseDesc = null;
  223. }
  224. else
  225. {
  226. appApiState.status = 0;
  227. appApiState.refuseDesc = $"{refuseDesc}";
  228. }
  229. }
  230. else haveApi.Add(x);
  231. });
  232. sendWhom = new List<string> { "1528783103", "1636016499" };
  233. bizcode = "auditapi";
  234. break;
  235. default:
  236. return Ok(new { state = 400, msg = "operate参数错误" });
  237. }
  238. appCompany = await cosmosClient.GetContainer("TEAMModelOS", "Normal").ReplaceItemAsync<AppCompany>(appCompany, appCompany.id, new PartitionKey(appCompany.code));
  239. }
  240. //发送消息
  241. var location = _option.Location;
  242. Notification notification = new()
  243. {
  244. hubName = bizcode,
  245. type = "msg",
  246. from = $"BI:{_option.Location}:private",
  247. to = sendWhom,
  248. label = $"{bizcode}-appCompany",
  249. body = new { location = location, biz = bizcode, appid = appCompany.id, appName = appCompany.name, status = 1, time = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() }.ToJsonString(),
  250. };
  251. var url = _configuration.GetValue<string>("HaBookAuth:CoreService:sendnotification");
  252. var clientID = _configuration.GetValue<string>("HaBookAuth:CoreService:clientID");
  253. var clientSecret = _configuration.GetValue<string>("HaBookAuth:CoreService:clientSecret");
  254. await _notificationService.SendNotification(clientID, clientSecret, location, url, notification); //站内发送消息
  255. if (haveApi.Count > 0)
  256. return Ok(new { state = 201, haveApi , appCompany});
  257. else return Ok(new { state = 200, appCompany });
  258. }
  259. /// <summary>
  260. /// 应用申请学校
  261. /// 应用审核申请的学校
  262. /// </summary>
  263. /// <param name="jsonElement"></param>
  264. /// <returns></returns>
  265. [ProducesDefaultResponseType]
  266. [HttpPost("set-auditschool")]
  267. public async Task<IActionResult> SetAuditSchool(JsonElement jsonElement)
  268. {
  269. if (!jsonElement.TryGetProperty("appId", out JsonElement appId)) return BadRequest();
  270. if (!jsonElement.TryGetProperty("appCode", out JsonElement appCode)) return BadRequest();
  271. if (!jsonElement.TryGetProperty("schooCode", out JsonElement schooCode)) return BadRequest();
  272. if(!jsonElement.TryGetProperty("operate", out JsonElement operate)) return BadRequest();
  273. var cosmosClient = _azureCosmos.GetCosmosClient();
  274. List<string> haveSchool = new();
  275. List<string> sendWhom = new();//消息分发给谁 待完善
  276. string bizcode = ""; //消息名称
  277. AppCompany appCompany = await cosmosClient.GetContainer("TEAMModelOS", "Normal").ReadItemAsync<AppCompany>($"{appId}", new PartitionKey($"{appCode}"));
  278. if (appCompany != null)
  279. {
  280. switch (operate.GetString())
  281. {
  282. case "apply":
  283. var aSchool = appCompany.schools.Find(x => x.id.Equals($"{schooCode}"));
  284. if (aSchool == null)
  285. {
  286. jsonElement.TryGetProperty("name", out JsonElement name);
  287. jsonElement.TryGetProperty("picture", out JsonElement picture);
  288. appCompany.schools.Add(new ApplySchool() { id = $"{schooCode}", name = $"{name}", picture = $"{picture}" });
  289. }
  290. else haveSchool.Add(schooCode.GetString());
  291. sendWhom = new List<string> { "1528783103", "1636016499" };
  292. bizcode = "applyschool";
  293. break;
  294. case "audit":
  295. jsonElement.TryGetProperty("isAudit", out JsonElement isAudit);
  296. jsonElement.TryGetProperty("refuseDesc", out JsonElement refuseDesc);
  297. var applySchool = appCompany.schools.Find(x => x.id.Equals($"{schooCode}"));
  298. if (applySchool != null)
  299. {
  300. if (bool.Parse($"{isAudit}") == true)
  301. {
  302. applySchool.status = 1;
  303. applySchool.refuseDesc = null;
  304. }
  305. else
  306. {
  307. applySchool.status = 0;
  308. applySchool.refuseDesc = $"{refuseDesc}";
  309. }
  310. }
  311. else haveSchool.Add(schooCode.GetString());
  312. sendWhom = new List<string> { "1528783103", "1636016499" };
  313. bizcode = "auditschool";
  314. break;
  315. default:
  316. return Ok(new { state = 400, msg = "operate参数错误" });
  317. }
  318. appCompany = await cosmosClient.GetContainer("TEAMModelOS", "Normal").ReplaceItemAsync<AppCompany>(appCompany, appCompany.id, new PartitionKey(appCompany.code));
  319. }
  320. else return Ok(new { state = 404, msg = "未找到该应用" });
  321. //发送消息
  322. var location = _option.Location;
  323. Notification notification = new()
  324. {
  325. hubName = bizcode,
  326. type = "msg",
  327. from = $"BI:{_option.Location}:private",
  328. to = sendWhom,
  329. label = $"{bizcode}-appCompany",
  330. body = new { location = location, biz = bizcode, appid = appCompany.id, appName = appCompany.name, status = 1, time = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() }.ToJsonString(),
  331. };
  332. var url = _configuration.GetValue<string>("HaBookAuth:CoreService:sendnotification");
  333. var clientID = _configuration.GetValue<string>("HaBookAuth:CoreService:clientID");
  334. var clientSecret = _configuration.GetValue<string>("HaBookAuth:CoreService:clientSecret");
  335. await _notificationService.SendNotification(clientID, clientSecret, location, url, notification); //站内发送消息
  336. return Ok(new { state = 200 , appCompany });
  337. }
  338. public record AppIdOrCode
  339. {
  340. public string id { get; set; }
  341. public string code { get; set; }
  342. }
  343. /// <summary>
  344. /// 显示应用
  345. /// </summary>
  346. public record ReadCompany
  347. {
  348. public string id { get; set; }
  349. public string pk { get; set; }
  350. public string code { get; set; }
  351. public string name { get; set; }
  352. public string descr { get; set; }
  353. public string picture { get; set; }
  354. public string jwtKey { get; set; }
  355. public int status { get; set; }
  356. public int audit { get; set; }
  357. public string refuseDesc { get; set; }
  358. public string gateways { get; set; }
  359. public List<AppApiState> apis { get; set; }
  360. public string webhookDomain { get; set; }
  361. public List<WebHookState> webHooks { get; set; }
  362. public List<ApplySchool> schools { get; set; }
  363. }
  364. }
  365. }