123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- using Azure.Cosmos;
- using Microsoft.AspNetCore.Hosting;
- using Microsoft.AspNetCore.Http;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.Extensions.Options;
- using System;
- using System.Collections.Generic;
- using System.Net.Http;
- using System.Text;
- using System.Text.Json;
- using System.Threading.Tasks;
- using TEAMModelBI.DI.BIAzureStorage;
- using TEAMModelBI.Filter;
- using TEAMModelBI.Models.Extension;
- using TEAMModelBI.Tool.Context;
- 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("business")]
- [ApiController]
- public class CompanyController : ControllerBase
- {
- public readonly AzureCosmosFactory _azureCosmos;
- public readonly AzureStorageFactory _azureStorage;
- public readonly DingDing _dingDing;
- public readonly Option _option;
- private readonly IWebHostEnvironment _environment; //读取文件
- public CompanyController(AzureCosmosFactory azureCosmos, AzureStorageFactory azureStorage, DingDing dingDing, IOptionsSnapshot<Option> option, IWebHostEnvironment environment)
- {
- _azureCosmos = azureCosmos;
- _azureStorage = azureStorage;
- _dingDing = dingDing;
- _option = option?.Value;
- _environment = environment;
- }
- /// <summary>
- /// 查询企业信息结集合 若传ID查查询该账户的企业信息
- /// </summary>
- /// <param name="jsonElenent"></param>
- /// <returns></returns>
- [ProducesDefaultResponseType]
- [AuthToken(Roles = "admin,rdc,assist,sales,company")]
- [HttpPost("get-info")]
- public async Task<IActionResult> GetInfo(JsonElement jsonElenent)
- {
- jsonElenent.TryGetProperty("eid", out JsonElement eid);
- jsonElenent.TryGetProperty("site", out JsonElement site);
- var cosmosClient = _azureCosmos.GetCosmosClient();
- if ($"{site}".Equals(BIConst.GlobalSite))
- cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.GlobalSite);
- List<ReadCompany> companys = new();
- StringBuilder sqlTxt = new("select c.id,c.pk,c.code,c.name,c.credit,c.picture,c.emall,c.mobile,c.password from c");
- if (!string.IsNullOrEmpty($"{eid}"))
- {
- sqlTxt.Append($" where c.id='{eid}'");
- }
- await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "Normal").GetItemQueryStreamIterator(queryText: sqlTxt.ToString(), requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("Company") }))
- {
- 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(),
- credit = obj.GetProperty("credit").GetString(),
- picture = obj.GetProperty("picture").GetString(),
- emall = obj.GetProperty("emall").GetString(),
- mobile = obj.GetProperty("mobile").GetString(),
- };
- companys.Add(readCompany);
- }
- }
- }
- return Ok(new { state = 200, companys });
- }
- /// <summary>
- /// 新增企业信息和修改企业信息
- /// </summary>
- /// <param name="appCompany"></param>
- /// <returns></returns>
- //[ProducesDefaultResponseType]
- //[AuthToken(Roles = "admin,rdc,assist,sales,company")]
- [HttpPost("set-info")]
- public async Task<IActionResult> SetCompany([FromBody]Company company, [FromHeader] string site)
- {
- try
- {
- //var (loginId, loginName, pic, did, dname, dpic) = HttpJwtAnalysis.JwtXAuthBI(HttpContext.GetXAuth("AuthToken"), _option);
- StringBuilder strMsg = new();
- var cosmosClient = _azureCosmos.GetCosmosClient();
- var tableClient = _azureStorage.GetCloudTableClient();
- var blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public");
- if ($"{site}".Equals(BIConst.GlobalSite))
- {
- cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.GlobalSite);
- tableClient = _azureStorage.GetCloudTableClient(BIConst.GlobalSite);
- blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public", BIConst.GlobalSite);
- }
- string salt = Utils.CreatSaltString(8);
- string type = "";
- //新增企业信息
- if (string.IsNullOrEmpty(company.id))
- {
- company.id = Guid.NewGuid().ToString();
- company.code = "Company";
- company.salt = salt;
- company.password = string.IsNullOrWhiteSpace(company.password) ? Utils.HashedPassword("123456", salt) : Utils.HashedPassword(company.password, salt);//Password,若梦没有则是默认密码:123456
- company.createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
- company.jti = Guid.NewGuid().ToString();
- company.secretKey = JwtAuth.CreateBusinessJwtKeyBI(_option.Location, _option.JwtSecretKey, company.id, company.jti);
- await cosmosClient.GetContainer("TEAMModelOS", "Normal").CreateItemAsync<Company>(company, new PartitionKey(company.code));
- strMsg.Append($"{company.name}【{company.id}】新增企业基础信息。");
- type = "company-add";
- }
- //修改企业信息
- else
- {
- var respone = await cosmosClient.GetContainer("TEAMModelOS", "Normal").ReadItemStreamAsync(company.id, new PartitionKey($""));
- if (respone.Status == 200)
- {
- company.pk = "Company";
- company.code = "Company";
- company.ttl = -1;
- company.salt = salt;
- company.password = string.IsNullOrWhiteSpace(company.password) ? Utils.HashedPassword("123456", salt) : Utils.HashedPassword(company.password, salt);//Password,若梦没有则是默认密码:123456
- company = await cosmosClient.GetContainer("TEAMModelOS", "Normal").ReplaceItemAsync<Company>(company, company.id, new PartitionKey(company.code));
- strMsg.Append($"{company.name}【{company.id}】修改企业基础信息。");
- type = "company-update";
- }
- else return Ok(new { state = 404, msg = "未找到该id相关的企业信息" });
- }
- //保存操作记录
- //await _azureStorage.SaveBILog(type, strMsg.ToString(), _dingDing, httpContext: HttpContext);
- await AzureStorageBlobExtensions.SaveBILog(blobClient, tableClient, type, strMsg.ToString(), _dingDing, httpContext: HttpContext);
- return Ok(new { state = 200, company });
- }
- catch (Exception e)
- {
- //await _dingDing.SendBotMsg($"BI,{_option.Location} , /company/set-info \n {e.Message}\n{e.StackTrace} \n ", GroupNames.成都开发測試群組);
- return BadRequest();
- }
- }
- 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 credit { get; set; }
- public string picture { get; set; }
- public string emall { get; set; }
- public string mobile { get; set; }
- }
- }
- }
|