123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280 |
- 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("bizuser")]
- [ApiController]
- public class BusinessUsersController : ControllerBase
- {
- public readonly AzureCosmosFactory _azureCosmos;
- public readonly AzureStorageFactory _azureStorage;
- public readonly DingDing _dingDing;
- public readonly Option _option;
- public BusinessUsersController(AzureCosmosFactory azureCosmos, AzureStorageFactory azureStorage, DingDing dingDing, IOptionsSnapshot<Option> option)
- {
- _azureCosmos = azureCosmos;
- _azureStorage = azureStorage;
- _dingDing = dingDing;
- _option = option?.Value;
- }
- /// <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<BusinessUsers> bizUsers = new();
- await foreach (var items in cosmosClient.GetContainer("TEAMModelOS", "Normal").GetItemQueryIterator<BusinessUsers>(queryText: sqlTxt.ToString(), requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("BizUsers") }))
- {
- bizUsers.Add(items);
- }
- return Ok(new { state = RespondCode.Ok, bizUsers });
- }
- /// <summary>
- /// 用户信息企业信息
- /// </summary>
- /// <param name="bizUsers"></param>
- /// <param name="site"></param>
- /// <returns></returns>
- [ProducesDefaultResponseType]
- [AuthToken(Roles = "admin,rdc,assist")]
- [HttpPost("set-info")]
- public async Task<IActionResult> SetInfos([FromBody] BizUsers bizUsers, [FromHeader] string site)
- {
- 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);
- }
- var (tmdId, tmdName, pic, _, _, _) = HttpJwtAnalysis.JwtXAuthBI(HttpContext.GetXAuth("AuthToken"), _option);
- StringBuilder strMsg = new($"{tmdName}[{tmdId}]操作:");
- string salt = Utils.CreatSaltString(8);
- string type = "";
- if (string.IsNullOrEmpty(bizUsers.id))
- {
- bizUsers.id = Guid.NewGuid().ToString();
- bizUsers.code = "BizUsers";
- bizUsers.name = string.IsNullOrEmpty(bizUsers.name)? bizUsers.mobile.ToString(): bizUsers.name;
- bizUsers.salt = salt;
- bizUsers.pwd = string.IsNullOrEmpty(bizUsers.pwd) ? Utils.HashedPassword(bizUsers.mobile.ToString(), salt) : Utils.HashedPassword(bizUsers.pwd, salt);
- bizUsers = await cosmosClient.GetContainer("TEAMModelOS", "Normal").CreateItemAsync<BizUsers>(bizUsers, new PartitionKey("BizUsers"));
- strMsg.Append($"{bizUsers.name}【{bizUsers.id}】新增第三方用户信息基础信息。");
- type = "bizuser-add";
- }
- else
- {
- var response = await cosmosClient.GetContainer("TEAMModelOS", "Normal").ReadItemStreamAsync(bizUsers.id, new PartitionKey("BizUsers"));
- if (response.Status == RespondCode.Ok)
- {
- using var json = await JsonDocument.ParseAsync(response.ContentStream);
- BizUsers tempbizUsers = json.ToObject<BizUsers>();
- bizUsers.pk = "Business";
- bizUsers.code = "BizUsers";
- bizUsers.ttl = -1;
- bizUsers.salt = tempbizUsers.salt;
- bizUsers.pwd = tempbizUsers.pwd;
- bizUsers = await cosmosClient.GetContainer("TEAMModelOS", "Normal").ReplaceItemAsync<BizUsers>(bizUsers, bizUsers.id, new PartitionKey("BizUsers"));
- strMsg.Append($"{bizUsers.name}【{bizUsers.id}】修改第三方用户信息基础信息。");
- type = "bizuser-add";
- }
- else Ok(new { state = RespondCode.NotFound ,msg="未找到id用户。"});
- }
- //保存操作记录
- await AzureStorageBlobExtensions.SaveBILog(blobClient, tableClient, type, strMsg.ToString(), _dingDing, httpContext: HttpContext);
- return Ok(new { state = RespondCode.Ok, bizUsers });
- }
- /// <summary>
- /// 重置密码
- /// </summary>
- /// <param name="jsonElement"></param>
- /// <returns></returns>
- [ProducesDefaultResponseType]
- [AuthToken(Roles = "admin,rdc,assist")]
- [HttpPost("reset-pwd")]
- public async Task<IActionResult> ResetPassWord(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);
- }
- BizUsers bizUsers = new();
- StringBuilder strMsg = new($"{tmdName}[{tmdId}]操作:");
- string salt = Utils.CreatSaltString(8);
- var response = await cosmosClient.GetContainer("TEAMModelOS", "Normal").ReadItemStreamAsync($"{id}", new PartitionKey("BizConfig"));
- if (response.Status == 200)
- {
- using var json = await JsonDocument.ParseAsync(response.ContentStream);
- bizUsers = json.ToObject<BizUsers>();
- bizUsers.salt = salt;
- bizUsers.pwd = Utils.HashedPassword(bizUsers.mobile.ToString(), salt);
- strMsg.Append($"重置{bizUsers.name}【{bizUsers.id}】的密码,重置成功!");
- bizUsers = await cosmosClient.GetContainer("TEAMModelOS", "Normal").ReplaceItemAsync<BizUsers>(bizUsers, bizUsers.id, new PartitionKey("BizUsers"));
- }
- //保存操作记录
- await AzureStorageBlobExtensions.SaveBILog(blobClient, tableClient, "bizuser-reset", strMsg.ToString(), _dingDing, httpContext: HttpContext);
- return Ok(new { state = RespondCode.Ok, bizUsers });
- }
- /// <summary>
- /// 用户关联/移除企业信息
- /// </summary>
- /// <param name="jsonElement"></param>
- /// <returns></returns>
- [ProducesDefaultResponseType]
- [AuthToken(Roles = "admin,rdc,assist")]
- [HttpPost("rel-biz")]
- public async Task<ActionResult> RelationBusiness(JsonElement jsonElement)
- {
- if(!jsonElement.TryGetProperty("id", out JsonElement id)) return BadRequest();
- if (!jsonElement.TryGetProperty("bizs", out JsonElement _bizs)) return BadRequest();
- jsonElement.TryGetProperty("site", out JsonElement site);
- var (tmdId, tmdName, pic, _, _, _) = HttpJwtAnalysis.JwtXAuthBI(HttpContext.GetXAuth("AuthToken"), _option);
- List<BizRel> bizRels = _bizs.ToObject<List<BizRel>>();
- 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}]");
- if (string.IsNullOrEmpty("add"))
- {
- strMsg.Append($"关联企业ID:{id},学校列表:");
- }
- else if (string.IsNullOrEmpty("del"))
- {
- strMsg.Append("移除企业学校信息,学校列表:");
- }
- else { return Ok(new { state = RespondCode.ParamsError, msg = "类型错误" }); }
- List<BizRel> noBizRel = new();
- BizUsers bizUsers = new();
- var response = await cosmosClient.GetContainer("TEAMModelOS", "Normal").ReadItemStreamAsync($"{id}", new PartitionKey("BizUsers"));
- if (response.Status == RespondCode.Ok)
- {
- using var json = await JsonDocument.ParseAsync(response.ContentStream);
- bizUsers = json.ToObject<BizUsers>();
- foreach (var item in bizRels)
- {
- var temp = bizUsers.relation.Find(f => f.bizId.Equals(item.bizId));
- if (string.IsNullOrEmpty("add"))
- {
- if (temp == null)
- {
- bizUsers.relation.Add(item);
- strMsg.Append($"{item.name}[{item.bizId}]|");
- }
- else
- noBizRel.Add(temp);
- }
- else if (string.IsNullOrEmpty("del"))
- {
- if (temp != null)
- {
- bizUsers.relation.Remove(temp);
- strMsg.Append($"{item.name}[{item.bizId}]|");
- }
- }
- }
- bizUsers = await cosmosClient.GetContainer("TEAMModelOS", "Normal").ReplaceItemAsync<BizUsers>(bizUsers, bizUsers.id, new PartitionKey("BizUsers"));
- }
- else return Ok(new { state = RespondCode.NotFound, msg = "未找到该用户" });
- //保存操作记录
- await AzureStorageBlobExtensions.SaveBILog(blobClient, tableClient, "bizconfig-addSchool", strMsg.ToString(), _dingDing, httpContext: HttpContext);
- if (noBizRel.Count > 0)
- return Ok(new { state = RespondCode.Created, bizUsers, noBizRel });
- return Ok(new { state = RespondCode.Ok, bizUsers });
- }
- /// <summary>
- /// 通过企业Id查询用户信息
- /// </summary>
- /// <param name="jsonElement"></param>
- /// <returns></returns>
- [ProducesDefaultResponseType]
- [HttpPost("get-bizid")]
- public async Task<IActionResult> GetBizIdUsers(JsonElement jsonElement)
- {
- if (!jsonElement.TryGetProperty("id", out JsonElement id)) return BadRequest();
- jsonElement.TryGetProperty("site", out JsonElement site);
- var cosmosClient = _azureCosmos.GetCosmosClient();
- if ($"{site}".Equals(BIConst.Global))
- cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
- List<BusinessUsers> businessUsers = new();
- string sql = $"select value(c) from c join s in c.relation where c.code='BizUsers' and s.bizId = '80e1bb6c-acba-46ab-9939-4851c4ef2158'";
- await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "Normal").GetItemQueryIterator<BusinessUsers>(queryText: sql, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("BizUsers") }))
- {
- businessUsers.Add(item);
- }
- return Ok(new { state = RespondCode.Ok, businessUsers });
- }
- }
- }
|