123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399 |
- using Azure.Cosmos;
- 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;
- using System.Text.Json;
- using System.Threading.Tasks;
- using TEAMModelBI.Filter;
- using TEAMModelBI.Tool.Extension;
- using TEAMModelOS.Models;
- using TEAMModelOS.SDK.DI;
- using TEAMModelOS.SDK.Extension;
- using TEAMModelOS.SDK.Models.Cosmos.BI;
- using TEAMModelOS.SDK.Models.Service;
- namespace TEAMModelBI.Controllers.BINormal
- {
- [Route("appcompany")]
- [ApiController]
- public class AppCompanyController : ControllerBase
- {
- public readonly AzureCosmosFactory _azureCosmos;
- public readonly AzureStorageFactory _azureStorage;
- public readonly DingDing _dingDing;
- public readonly Option _option;
- private readonly IConfiguration _configuration;
- private readonly NotificationService _notificationService;
- public AppCompanyController(AzureCosmosFactory azureCosmos, AzureStorageFactory azureStorage, DingDing dingDing, IOptionsSnapshot<Option> option, IConfiguration configuration, NotificationService notificationService)
- {
- _azureCosmos = azureCosmos;
- _azureStorage = azureStorage;
- _dingDing = dingDing;
- _option = option?.Value;
- _configuration = configuration;
- _notificationService = notificationService;
- }
- /// <summary>
- /// 查询应用信息
- /// </summary>
- /// <param name="jsonElement"></param>
- /// <returns></returns>
- [ProducesDefaultResponseType]
- [HttpPost("get-info")]
- public async Task<IActionResult> GetInfo(JsonElement jsonElement)
- {
- jsonElement.TryGetProperty("appId", out JsonElement appId);
- jsonElement.TryGetProperty("eid", out JsonElement eid);
- jsonElement.TryGetProperty("audit", out JsonElement audit);
- var cosmosClient = _azureCosmos.GetCosmosClient();
- 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'");
- if (!string.IsNullOrEmpty($"{appId}"))
- {
- sqlTxt.Append($" and id='{appId}'");
- }
- if (!string.IsNullOrEmpty($"{audit}"))
- {
- sqlTxt.Append($" and audit='{audit}'");
- }
- List<ReadCompany> appCompanys = new();
- 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}") }))
- {
- using var json = await JsonDocument.ParseAsync(item.ContentStream);
- if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
- {
- foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
- {
- ReadCompany readCompany = new()
- {
- id = obj.GetProperty("id").GetString(),
- pk = obj.GetProperty("pk").GetString(),
- code = obj.GetProperty("code").GetString(),
- name = obj.GetProperty("name").GetString(),
- descr = obj.GetProperty("descr").GetString(),
- picture = obj.GetProperty("picture").GetString(),
- jwtKey = obj.GetProperty("jwtKey").GetString(),
- status = obj.GetProperty("status").GetInt32(),
- audit = obj.GetProperty("audit").GetInt32(),
- refuseDesc = obj.GetProperty("refuseDesc").GetString(),
- gateways = obj.GetProperty("gateways").GetString(),
- apis = obj.GetProperty("apis").ToObject<List<AppApiState>>(),
- webhookDomain = obj.GetProperty("webhookDomain").GetString(),
- webHooks = obj.GetProperty("webHooks").ToObject<List<WebHookState>>(),
- schools = obj.GetProperty("schools").ToObject<List<ApplySchool>>()
- };
- appCompanys.Add(readCompany);
- }
- }
- }
- return Ok(new { state = 200, appCompanys });
- }
- /// <summary>
- /// 新增或者修改应用
- /// </summary>
- /// <param name="appCompany"></param>
- /// <returns></returns>
- [ProducesDefaultResponseType]
- [AuthToken(Roles = "assist")]
- [HttpPost("set-info")]
- public async Task<IActionResult> SetAppInfo(AppCompany appCompany)
- {
- var cosmosClient = _azureCosmos.GetCosmosClient();
- StringBuilder stringBuilder = new();
- //新建
- if (string.IsNullOrEmpty($"{appCompany.id}"))
- {
- appCompany.id = GenerateRandom.StrRandom(8, large: true, small: true);
- appCompany.code = $"App-{appCompany.code}";
- appCompany.createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
- appCompany.status = -1;
- appCompany.audit = -1;
- appCompany = await cosmosClient.GetContainer("TEAMModelOS", "Normal").CreateItemAsync<AppCompany>(appCompany, new PartitionKey(appCompany.code));
- }
- //修改
- else
- {
- var response = await cosmosClient.GetContainer("TEAMModelOS", "Normal").ReadItemStreamAsync(appCompany.id, new PartitionKey(appCompany.code));
- if (response.Status == 200)
- {
- appCompany.pk = "App";
- appCompany.ttl = -1;
- appCompany = await cosmosClient.GetContainer("TEAMModelOS", "Normal").ReplaceItemAsync<AppCompany>(appCompany, appCompany.id, new PartitionKey(appCompany.code));
- }
- else return Ok(new { state = 404, msg = "未找到该id相关的企业应用信息" });
- }
- return Ok(new { state = 200, appCompany });
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="jsonElement"></param>
- /// <returns></returns>
- [HttpPost("get-applyapi")]
- public async Task<IActionResult> SetAuditApp(JsonElement jsonElement)
- {
- if (!jsonElement.TryGetProperty("appIds", out JsonElement appIds)) return BadRequest();
- if (!jsonElement.TryGetProperty("isAudit", out JsonElement isAudit)) return BadRequest();
- jsonElement.TryGetProperty("refuseDesc", out JsonElement refuseDesc);
- var cosmosClient = _azureCosmos.GetCosmosClient();
- List<AppIdOrCode> idOrCode = appIds.ToObject<List<AppIdOrCode>>();
- List<AppIdOrCode> haveIds = new();
- if (idOrCode.Count > 0)
- {
- foreach (var idCode in idOrCode)
- {
- AppCompany appCompany = await cosmosClient.GetContainer("TEAMModelOS", "Normal").ReadItemAsync<AppCompany>(idCode.id, new PartitionKey(idCode.code));
- //var response = await cosmosClient.GetContainer("TEAMModelOS", "Normal").ReadItemStreamAsync(idCode.id, new PartitionKey(idCode.code));
- if (bool.Parse($"{isAudit}") == true)
- {
- appCompany.audit = 1;
- }
- else
- {
- appCompany.audit = 0;
- appCompany.refuseDesc = $"{refuseDesc}";
- }
- try
- {
- await cosmosClient.GetContainer("TEAMModelOS", "Normal").ReplaceItemAsync<AppCompany>(appCompany, appCompany.id, new PartitionKey(idCode.code));
- }
- catch
- {
- haveIds.Add(idCode);
- }
- }
- }
- else return Ok(new { state = 404, msg = "appIds参数错误" });
- return Ok(new { state = 200});
- }
- /// <summary>
- /// 应用申请Api接口信息
- /// 审核应用api接口信息
- /// </summary>
- /// <param name="jsonElement"></param>
- /// <returns></returns>
- [ProducesDefaultResponseType]
- [HttpPost("set-applyapi")]
- public async Task<IActionResult> SetApplyApi(JsonElement jsonElement)
- {
- if (!jsonElement.TryGetProperty("appId", out JsonElement appId)) return BadRequest();
- if (!jsonElement.TryGetProperty("appCode", out JsonElement appCode)) return BadRequest();
- if (!jsonElement.TryGetProperty("apiIds", out JsonElement apiIds)) return BadRequest();
- if (!jsonElement.TryGetProperty("operate", out JsonElement operate)) return BadRequest();
- List<string> tempApi = apiIds.ToObject<List<string>>();
- var cosmosClient = _azureCosmos.GetCosmosClient();
- List<string> haveApi = new(); //存在api接口
- List<string> sendWhom = new();//消息分发给谁 待完善
- string bizcode = ""; //消息名称
- AppCompany appCompany = await cosmosClient.GetContainer("TEAMModelOS", "Normal").ReadItemAsync<AppCompany>($"{appId}", new PartitionKey($"{appCode}"));
- if (appCompany != null)
- {
- switch (operate.GetString())
- {
- case "apply":
- jsonElement.TryGetProperty("applyDesc", out JsonElement applyDesc);
- tempApi.ForEach(x =>
- {
- var strt = appCompany.apis.Find(y => y.no.Equals($"{x}")).no;
- if (strt == null)
- {
- appCompany.apis.Add(new AppApiState() { no = $"{x}", applyDesc = $"{applyDesc}", status = -1 });
- }
- else haveApi.Add(x);
- });
- sendWhom = new List<string> { "1528783103", "1636016499" };
- bizcode = "applyapi";
- break;
- case "audit":
- jsonElement.TryGetProperty("isAudit", out JsonElement isAudit);
- jsonElement.TryGetProperty("refuseDesc", out JsonElement refuseDesc);
- tempApi.ForEach(x =>
- {
- var temp = appCompany.apis.Find(n => n.no == x);
- if (temp != null)
- {
- AppApiState appApiState = appCompany.apis.Single(a => a.no == x);
- if (bool.Parse($"{isAudit}") == true)
- {
- appApiState.status = 1;
- appApiState.refuseDesc = null;
- }
- else
- {
- appApiState.status = 0;
- appApiState.refuseDesc = $"{refuseDesc}";
- }
- }
- else haveApi.Add(x);
- });
- sendWhom = new List<string> { "1528783103", "1636016499" };
- bizcode = "auditapi";
- break;
- default:
- return Ok(new { state = 400, msg = "operate参数错误" });
- }
- appCompany = await cosmosClient.GetContainer("TEAMModelOS", "Normal").ReplaceItemAsync<AppCompany>(appCompany, appCompany.id, new PartitionKey(appCompany.code));
- }
- //发送消息
- var location = _option.Location;
- Notification notification = new()
- {
- hubName = bizcode,
- type = "msg",
- from = $"BI:{_option.Location}:private",
- to = sendWhom,
- label = $"{bizcode}-appCompany",
- body = new { location = location, biz = bizcode, appid = appCompany.id, appName = appCompany.name, status = 1, time = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() }.ToJsonString(),
- };
- var url = _configuration.GetValue<string>("HaBookAuth:CoreService:sendnotification");
- var clientID = _configuration.GetValue<string>("HaBookAuth:CoreService:clientID");
- var clientSecret = _configuration.GetValue<string>("HaBookAuth:CoreService:clientSecret");
- await _notificationService.SendNotification(clientID, clientSecret, location, url, notification); //站内发送消息
- if (haveApi.Count > 0)
- return Ok(new { state = 201, haveApi , appCompany});
- else return Ok(new { state = 200, appCompany });
- }
- /// <summary>
- /// 应用申请学校
- /// 应用审核申请的学校
- /// </summary>
- /// <param name="jsonElement"></param>
- /// <returns></returns>
- [ProducesDefaultResponseType]
- [HttpPost("set-auditschool")]
- public async Task<IActionResult> SetAuditSchool(JsonElement jsonElement)
- {
- if (!jsonElement.TryGetProperty("appId", out JsonElement appId)) return BadRequest();
- if (!jsonElement.TryGetProperty("appCode", out JsonElement appCode)) return BadRequest();
- if (!jsonElement.TryGetProperty("schooCode", out JsonElement schooCode)) return BadRequest();
- if(!jsonElement.TryGetProperty("operate", out JsonElement operate)) return BadRequest();
- var cosmosClient = _azureCosmos.GetCosmosClient();
- List<string> haveSchool = new();
- List<string> sendWhom = new();//消息分发给谁 待完善
- string bizcode = ""; //消息名称
- AppCompany appCompany = await cosmosClient.GetContainer("TEAMModelOS", "Normal").ReadItemAsync<AppCompany>($"{appId}", new PartitionKey($"{appCode}"));
- if (appCompany != null)
- {
- switch (operate.GetString())
- {
- case "apply":
- var aSchool = appCompany.schools.Find(x => x.id.Equals($"{schooCode}"));
- if (aSchool == null)
- {
- jsonElement.TryGetProperty("name", out JsonElement name);
- jsonElement.TryGetProperty("picture", out JsonElement picture);
- appCompany.schools.Add(new ApplySchool() { id = $"{schooCode}", name = $"{name}", picture = $"{picture}" });
- }
- else haveSchool.Add(schooCode.GetString());
- sendWhom = new List<string> { "1528783103", "1636016499" };
- bizcode = "applyschool";
- break;
- case "audit":
- jsonElement.TryGetProperty("isAudit", out JsonElement isAudit);
- jsonElement.TryGetProperty("refuseDesc", out JsonElement refuseDesc);
- var applySchool = appCompany.schools.Find(x => x.id.Equals($"{schooCode}"));
- if (applySchool != null)
- {
- if (bool.Parse($"{isAudit}") == true)
- {
- applySchool.status = 1;
- applySchool.refuseDesc = null;
- }
- else
- {
- applySchool.status = 0;
- applySchool.refuseDesc = $"{refuseDesc}";
- }
- }
- else haveSchool.Add(schooCode.GetString());
- sendWhom = new List<string> { "1528783103", "1636016499" };
- bizcode = "auditschool";
- break;
- default:
- return Ok(new { state = 400, msg = "operate参数错误" });
- }
- appCompany = await cosmosClient.GetContainer("TEAMModelOS", "Normal").ReplaceItemAsync<AppCompany>(appCompany, appCompany.id, new PartitionKey(appCompany.code));
- }
- else return Ok(new { state = 404, msg = "未找到该应用" });
- //发送消息
- var location = _option.Location;
- Notification notification = new()
- {
- hubName = bizcode,
- type = "msg",
- from = $"BI:{_option.Location}:private",
- to = sendWhom,
- label = $"{bizcode}-appCompany",
- body = new { location = location, biz = bizcode, appid = appCompany.id, appName = appCompany.name, status = 1, time = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() }.ToJsonString(),
- };
- var url = _configuration.GetValue<string>("HaBookAuth:CoreService:sendnotification");
- var clientID = _configuration.GetValue<string>("HaBookAuth:CoreService:clientID");
- var clientSecret = _configuration.GetValue<string>("HaBookAuth:CoreService:clientSecret");
- await _notificationService.SendNotification(clientID, clientSecret, location, url, notification); //站内发送消息
- return Ok(new { state = 200 , appCompany });
- }
- public record AppIdOrCode
- {
- public string id { get; set; }
- public string code { get; set; }
- }
- /// <summary>
- /// 显示应用
- /// </summary>
- public record ReadCompany
- {
- public string id { get; set; }
- public string pk { get; set; }
- public string code { get; set; }
- public string name { get; set; }
- public string descr { get; set; }
- public string picture { get; set; }
- public string jwtKey { get; set; }
- public int status { get; set; }
- public int audit { get; set; }
- public string refuseDesc { get; set; }
- public string gateways { get; set; }
- public List<AppApiState> apis { get; set; }
- public string webhookDomain { get; set; }
- public List<WebHookState> webHooks { get; set; }
- public List<ApplySchool> schools { get; set; }
- }
- }
- }
|