|
@@ -0,0 +1,328 @@
|
|
|
|
+using Azure.Cosmos;
|
|
|
|
+using Microsoft.AspNetCore.Http;
|
|
|
|
+using Microsoft.AspNetCore.Mvc;
|
|
|
|
+using Microsoft.Extensions.Options;
|
|
|
|
+using System;
|
|
|
|
+using System.Collections.Generic;
|
|
|
|
+using System.Text;
|
|
|
|
+using System.Text.Json;
|
|
|
|
+using System.Threading.Tasks;
|
|
|
|
+using TEAMModelBI.Filter;
|
|
|
|
+using TEAMModelBI.Tool.Extension;
|
|
|
|
+using TEAMModelOS.Models;
|
|
|
|
+using TEAMModelOS.SDK.Context.BI;
|
|
|
|
+using TEAMModelOS.SDK.Context.Constant;
|
|
|
|
+using TEAMModelOS.SDK.DI;
|
|
|
|
+using TEAMModelOS.SDK.Extension;
|
|
|
|
+using TEAMModelOS.SDK.Models.Cosmos.BI.BINormal;
|
|
|
|
+
|
|
|
|
+namespace TEAMModelBI.Controllers.BINormal
|
|
|
|
+{
|
|
|
|
+ [Route("bizconfig")]
|
|
|
|
+ [ApiController]
|
|
|
|
+ public class BusinessController : ControllerBase
|
|
|
|
+ {
|
|
|
|
+ public readonly AzureCosmosFactory _azureCosmos;
|
|
|
|
+ public readonly AzureStorageFactory _azureStorage;
|
|
|
|
+ public readonly DingDing _dingDing;
|
|
|
|
+ public readonly Option _option;
|
|
|
|
+ public BusinessController(AzureCosmosFactory azureCosmos, AzureStorageFactory azureStorage, DingDing dingDing, IOptionsSnapshot<Option> option)
|
|
|
|
+ {
|
|
|
|
+ _azureCosmos = azureCosmos;
|
|
|
|
+ _azureStorage = azureStorage;
|
|
|
|
+ _dingDing = dingDing;
|
|
|
|
+ _option = option?.Value;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// 新增企业信息和修改企业信息
|
|
|
|
+ /// </summary>
|
|
|
|
+ /// <param name="bizConfig"></param>
|
|
|
|
+ /// <returns></returns>
|
|
|
|
+ [ProducesDefaultResponseType]
|
|
|
|
+ [AuthToken(Roles = "admin,rdc,assist")]
|
|
|
|
+ [HttpPost("set-info")]
|
|
|
|
+ public async Task<IActionResult> SetInfo([FromBody] BizConfig bizConfig, [FromHeader] string site)
|
|
|
|
+ {
|
|
|
|
+ StringBuilder strMsg = new();
|
|
|
|
+ var cosmosClient = _azureCosmos.GetCosmosClient();
|
|
|
|
+ var tableClient = _azureStorage.GetCloudTableClient();
|
|
|
|
+ var blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public");
|
|
|
|
+ if ($"{site}".Equals(BIConst.Global))
|
|
|
|
+ {
|
|
|
|
+ cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
|
|
|
|
+ tableClient = _azureStorage.GetCloudTableClient(BIConst.Global);
|
|
|
|
+ blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public", BIConst.Global);
|
|
|
|
+ }
|
|
|
|
+ string salt = Utils.CreatSaltString(8);
|
|
|
|
+ List<BizUsers> bizUsers = new();
|
|
|
|
+ string type = "";
|
|
|
|
+
|
|
|
|
+ //新增企业信息
|
|
|
|
+ if (string.IsNullOrEmpty(bizConfig.id))
|
|
|
|
+ {
|
|
|
|
+ bizConfig.id = Guid.NewGuid().ToString();
|
|
|
|
+ bizConfig.code = "BizConfig";
|
|
|
|
+ bizConfig.pk = "Business";
|
|
|
|
+ bizConfig.createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
|
|
|
|
+ var auth_token = JwtAuthExtension.CreateBusinessApiToken(_option.Location, bizConfig.id, _option.JwtSecretKey, "business");
|
|
|
|
+ bizConfig.jti = auth_token.jti;
|
|
|
|
+ bizConfig.token = auth_token.jwt;
|
|
|
|
+
|
|
|
|
+ await cosmosClient.GetContainer("TEAMModelOS", "Normal").CreateItemAsync<BizConfig>(bizConfig, new PartitionKey("BizConfig"));
|
|
|
|
+
|
|
|
|
+ await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "Normal").GetItemQueryIterator<BizUsers>(queryText: $"select value(c) from c where c.mobile ={bizConfig.mobile}", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("BizUsers") }))
|
|
|
|
+ {
|
|
|
|
+ bizUsers.Add(item);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ BizRel bizRel = new() { bizId = bizConfig.id, role = new List<string>() { "admin" } };
|
|
|
|
+ if (bizUsers.Count > 0)
|
|
|
|
+ {
|
|
|
|
+ foreach (var item in bizUsers)
|
|
|
|
+ {
|
|
|
|
+ BizRel temp = item.relation.Find(f => f.bizId.Equals(bizConfig.id));
|
|
|
|
+ if (temp == null)
|
|
|
|
+ {
|
|
|
|
+ item.relation.Add(bizRel);
|
|
|
|
+ await cosmosClient.GetContainer("TEAMModelOS", "Normal").ReplaceItemAsync<BizUsers>(item, item.id, new PartitionKey("BizUsers"));
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ {
|
|
|
|
+ BizUsers tBizUsers = new() { id = Guid.NewGuid().ToString(), code= "BizUsers", name = bizConfig.mobile.ToString(), mobile = bizConfig.mobile, salt = salt, pwd = Utils.HashedPassword($"{bizConfig.mobile}", salt),relation= new List<BizRel>() { { bizRel } } };
|
|
|
|
+
|
|
|
|
+ await cosmosClient.GetContainer("TEAMModelOS", "Normal").CreateItemAsync<BizUsers>(tBizUsers, new PartitionKey("BizUsers"));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ strMsg.Append($"{bizConfig.name}【{bizConfig.id}】新增企业基础信息。");
|
|
|
|
+ type = "bizconfig-add";
|
|
|
|
+ }
|
|
|
|
+ //修改企业信息
|
|
|
|
+ else
|
|
|
|
+ {
|
|
|
|
+ var response = await cosmosClient.GetContainer("TEAMModelOS", "Normal").ReadItemStreamAsync(bizConfig.id, new PartitionKey("BizConfig"));
|
|
|
|
+ if (response.Status == 200)
|
|
|
|
+ {
|
|
|
|
+ using var json = await JsonDocument.ParseAsync(response.ContentStream);
|
|
|
|
+ BizConfig tempBizConfig = json.ToObject<BizConfig>();
|
|
|
|
+
|
|
|
|
+ bizConfig.pk = "Business";
|
|
|
|
+ bizConfig.code = "BizConfig";
|
|
|
|
+ bizConfig.ttl = -1;
|
|
|
|
+
|
|
|
|
+ bizConfig.createTime = tempBizConfig.createTime;
|
|
|
|
+ bizConfig.jti = tempBizConfig.jti;
|
|
|
|
+ bizConfig.token = tempBizConfig.token;
|
|
|
|
+
|
|
|
|
+ bizConfig = await cosmosClient.GetContainer("TEAMModelOS", "Normal").ReplaceItemAsync<BizConfig>(bizConfig, bizConfig.id, new PartitionKey("BizConfig"));
|
|
|
|
+ strMsg.Append($"{bizConfig.name}【{bizConfig.id}】修改企业基础信息。");
|
|
|
|
+ type = "bizconfig-update";
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //保存操作记录
|
|
|
|
+ await AzureStorageBlobExtensions.SaveBILog(blobClient, tableClient, type, strMsg.ToString(), _dingDing, httpContext: HttpContext);
|
|
|
|
+ return Ok(new { state = RespondCode.Ok, bizConfig });
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// 获取企业信息列表
|
|
|
|
+ /// </summary>
|
|
|
|
+ /// <param name="jsonElement"></param>
|
|
|
|
+ /// <returns></returns>
|
|
|
|
+ [ProducesDefaultResponseType]
|
|
|
|
+ [HttpPost("get-infos")]
|
|
|
|
+ public async Task<IActionResult> GetInfos(JsonElement jsonElement)
|
|
|
|
+ {
|
|
|
|
+ jsonElement.TryGetProperty("id", out JsonElement id);
|
|
|
|
+ jsonElement.TryGetProperty("site", out JsonElement site);
|
|
|
|
+ var cosmosClient = _azureCosmos.GetCosmosClient();
|
|
|
|
+ if ($"{site}".Equals(BIConst.Global))
|
|
|
|
+ cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
|
|
|
|
+ StringBuilder sqlTxt = new("select value(c) from c");
|
|
|
|
+ if (!string.IsNullOrEmpty($"{id}"))
|
|
|
|
+ {
|
|
|
|
+ sqlTxt.Append($" where c.id='{id}'");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ List<Business> businesses = new();
|
|
|
|
+ await foreach (var items in cosmosClient.GetContainer("TEAMModelOS", "Normal").GetItemQueryIterator<Business>(queryText: sqlTxt.ToString(), requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("BizConfig") }))
|
|
|
|
+ {
|
|
|
|
+ businesses.Add(items);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return Ok(new { state = RespondCode.Ok, businesses });
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// 重置秘钥
|
|
|
|
+ /// </summary>
|
|
|
|
+ /// <param name="jsonElement"></param>
|
|
|
|
+ /// <returns></returns>
|
|
|
|
+ [ProducesDefaultResponseType]
|
|
|
|
+ [AuthToken(Roles = "admin,rdc,assist")]
|
|
|
|
+ [HttpPost("reset-secretkey")]
|
|
|
|
+ public async Task<IActionResult> ResetSecretKey(JsonElement jsonElement)
|
|
|
|
+ {
|
|
|
|
+ if (!jsonElement.TryGetProperty("id", out JsonElement id)) return BadRequest();
|
|
|
|
+ jsonElement.TryGetProperty("site", out JsonElement site);
|
|
|
|
+ var (tmdId, tmdName, pic, _, _, _) = HttpJwtAnalysis.JwtXAuthBI(HttpContext.GetXAuth("AuthToken"), _option);
|
|
|
|
+
|
|
|
|
+ var cosmosClient = _azureCosmos.GetCosmosClient();
|
|
|
|
+ var tableClient = _azureStorage.GetCloudTableClient();
|
|
|
|
+ var blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public");
|
|
|
|
+ if ($"{site}".Equals(BIConst.Global))
|
|
|
|
+ {
|
|
|
|
+ cosmosClient = _azureCosmos.GetCosmosClient(BIConst.Global);
|
|
|
|
+ tableClient = _azureStorage.GetCloudTableClient(BIConst.Global);
|
|
|
|
+ blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public", BIConst.Global);
|
|
|
|
+ }
|
|
|
|
+ BizConfig bizConfig = new();
|
|
|
|
+
|
|
|
|
+ var response = await cosmosClient.GetContainer("TEAMModelOS", "Normal").ReadItemStreamAsync($"{id}", new PartitionKey("BizConfig"));
|
|
|
|
+ if (response.Status == 200)
|
|
|
|
+ {
|
|
|
|
+ using var json = await JsonDocument.ParseAsync(response.ContentStream);
|
|
|
|
+ bizConfig = json.ToObject<BizConfig>();
|
|
|
|
+ var auth_token = JwtAuthExtension.CreateBusinessApiToken(_option.Location, bizConfig.id, _option.JwtSecretKey, "business");
|
|
|
|
+ bizConfig.jti = auth_token.jti;
|
|
|
|
+ bizConfig.token = auth_token.jwt;
|
|
|
|
+
|
|
|
|
+ bizConfig = await cosmosClient.GetContainer("TEAMModelOS", "Normal").ReplaceItemAsync<BizConfig>(bizConfig, bizConfig.id, new PartitionKey("BizConfig"));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return Ok(new { state =RespondCode.Ok, bizConfig });
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// 关联企业学校
|
|
|
|
+ /// </summary>
|
|
|
|
+ /// <param name="jsonElement"></param>
|
|
|
|
+ /// <returns></returns>
|
|
|
|
+ [ProducesDefaultResponseType]
|
|
|
|
+ [AuthToken(Roles = "admin,rdc,assist")]
|
|
|
|
+ [HttpPost("rel-school")]
|
|
|
|
+ public async Task<IActionResult> RelationSchool(JsonElement jsonElement)
|
|
|
|
+ {
|
|
|
|
+ if (!jsonElement.TryGetProperty("id", out JsonElement id)) return BadRequest();
|
|
|
|
+ if (!jsonElement.TryGetProperty("schools", out JsonElement _schools)) return BadRequest();
|
|
|
|
+ if (!jsonElement.TryGetProperty("type", out JsonElement type)) return BadRequest();
|
|
|
|
+ jsonElement.TryGetProperty("site", out JsonElement site);
|
|
|
|
+ var (tmdId, tmdName, pic, _, _, _) = HttpJwtAnalysis.JwtXAuthBI(HttpContext.GetXAuth("AuthToken"), _option);
|
|
|
|
+
|
|
|
|
+ List<BizSchool> bizSchool = _schools.ToObject<List<BizSchool>>();
|
|
|
|
+ var cosmosClient = _azureCosmos.GetCosmosClient();
|
|
|
|
+ var tableClient = _azureStorage.GetCloudTableClient();
|
|
|
|
+ var blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public");
|
|
|
|
+ if ($"{site}".Equals(BIConst.Global))
|
|
|
|
+ {
|
|
|
|
+ cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
|
|
|
|
+ tableClient = _azureStorage.GetCloudTableClient(BIConst.Global);
|
|
|
|
+ blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public", BIConst.Global);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ StringBuilder strMsg = new();
|
|
|
|
+ if (string.IsNullOrEmpty("add"))
|
|
|
|
+ {
|
|
|
|
+ strMsg.Append($"关联企业ID:{id},学校列表:");
|
|
|
|
+ }
|
|
|
|
+ else if (string.IsNullOrEmpty("del"))
|
|
|
|
+ {
|
|
|
|
+ strMsg.Append("移除企业学校信息,学校列表:");
|
|
|
|
+ }
|
|
|
|
+ else { return Ok(new { state = RespondCode.ParamsError, msg = "类型错误" }); }
|
|
|
|
+ List<BizSchool> noBizSc = new();
|
|
|
|
+ BizConfig bizConfig = new();
|
|
|
|
+ var response = await cosmosClient.GetContainer("TEAMModelOS", "Normal").ReadItemStreamAsync($"{id}", new PartitionKey("BizConfig"));
|
|
|
|
+ if (response.Status == RespondCode.Ok)
|
|
|
|
+ {
|
|
|
|
+ using var json = await JsonDocument.ParseAsync(response.ContentStream);
|
|
|
|
+ bizConfig = json.ToObject<BizConfig>();
|
|
|
|
+ foreach (var item in bizSchool)
|
|
|
|
+ {
|
|
|
|
+ var temp = bizConfig.schools.Find(f => f.id.Equals(item.id));
|
|
|
|
+ if (string.IsNullOrEmpty("add"))
|
|
|
|
+ {
|
|
|
|
+ if (temp == null)
|
|
|
|
+ {
|
|
|
|
+ bizConfig.schools.Add(item);
|
|
|
|
+ strMsg.Append($"{item.name}[{item.id}]|");
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ noBizSc.Add(temp);
|
|
|
|
+ }
|
|
|
|
+ else if (string.IsNullOrEmpty("del"))
|
|
|
|
+ {
|
|
|
|
+ if (temp != null)
|
|
|
|
+ {
|
|
|
|
+ bizConfig.schools.Remove(temp);
|
|
|
|
+ strMsg.Append($"{item.name}[{item.id}]|");
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ bizConfig = await cosmosClient.GetContainer("TEAMModelOS", "Normal").ReplaceItemAsync<BizConfig>(bizConfig, bizConfig.id, new PartitionKey("BizConfig"));
|
|
|
|
+ }
|
|
|
|
+ else return Ok(new { state = RespondCode.NotFound, msg = "未找到该企业" });
|
|
|
|
+
|
|
|
|
+ //保存操作记录
|
|
|
|
+ await AzureStorageBlobExtensions.SaveBILog(blobClient, tableClient, $"bizconfig-{type}School", strMsg.ToString(), _dingDing, httpContext: HttpContext);
|
|
|
|
+ if (noBizSc.Count > 0)
|
|
|
|
+ return Ok(new { state = RespondCode.Created, bizConfig , noBizSc });
|
|
|
|
+ return Ok(new { state = RespondCode.Ok, bizConfig });
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// 删除企业关联学校
|
|
|
|
+ /// </summary>
|
|
|
|
+ /// <param name="jsonElement"></param>
|
|
|
|
+ /// <returns></returns>
|
|
|
|
+ [ProducesDefaultResponseType]
|
|
|
|
+ [AuthToken(Roles = "admin,rdc,assist")]
|
|
|
|
+ [HttpPost("del-school")]
|
|
|
|
+ public async Task<IActionResult> DelSchool(JsonElement jsonElement)
|
|
|
|
+ {
|
|
|
|
+ if (!jsonElement.TryGetProperty("id", out JsonElement id)) return BadRequest();
|
|
|
|
+ if (!jsonElement.TryGetProperty("schools", out JsonElement _schools)) return BadRequest();
|
|
|
|
+ jsonElement.TryGetProperty("site", out JsonElement site);
|
|
|
|
+ var (tmdId, tmdName, pic, _, _, _) = HttpJwtAnalysis.JwtXAuthBI(HttpContext.GetXAuth("AuthToken"), _option);
|
|
|
|
+
|
|
|
|
+ List<BizSchool> bizSchool = _schools.ToObject<List<BizSchool>>();
|
|
|
|
+ var cosmosClient = _azureCosmos.GetCosmosClient();
|
|
|
|
+ var tableClient = _azureStorage.GetCloudTableClient();
|
|
|
|
+ var blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public");
|
|
|
|
+ if ($"{site}".Equals(BIConst.Global))
|
|
|
|
+ {
|
|
|
|
+ cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
|
|
|
|
+ tableClient = _azureStorage.GetCloudTableClient(BIConst.Global);
|
|
|
|
+ blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public", BIConst.Global);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ StringBuilder strMsg = new($"{tmdName}[{tmdId}]关联企业ID:{id},学校列表:");
|
|
|
|
+
|
|
|
|
+ BizConfig bizConfig = new();
|
|
|
|
+ var response = await cosmosClient.GetContainer("TEAMModelOS", "Normal").ReadItemStreamAsync($"{id}", new PartitionKey("BizConfig"));
|
|
|
|
+ if (response.Status == RespondCode.Ok)
|
|
|
|
+ {
|
|
|
|
+ using var json = await JsonDocument.ParseAsync(response.ContentStream);
|
|
|
|
+ bizConfig = json.ToObject<BizConfig>();
|
|
|
|
+ foreach (var item in bizSchool)
|
|
|
|
+ {
|
|
|
|
+ var temp = bizConfig.schools.Find(f => f.id.Equals(item.id));
|
|
|
|
+ }
|
|
|
|
+ bizConfig = await cosmosClient.GetContainer("TEAMModelOS", "Normal").ReplaceItemAsync<BizConfig>(bizConfig, bizConfig.id, new PartitionKey("BizConfig"));
|
|
|
|
+ }
|
|
|
|
+ else return Ok(new { state = RespondCode.NotFound, msg = "未找到该企业" });
|
|
|
|
+
|
|
|
|
+ //保存操作记录
|
|
|
|
+ await AzureStorageBlobExtensions.SaveBILog(blobClient, tableClient, "bizconfig-addSchool", strMsg.ToString(), _dingDing, httpContext: HttpContext);
|
|
|
|
+
|
|
|
|
+ return Ok(new { state = RespondCode.Ok, bizConfig });
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+}
|