CompanyController.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. using Azure.Cosmos;
  2. using Microsoft.AspNetCore.Hosting;
  3. using Microsoft.AspNetCore.Http;
  4. using Microsoft.AspNetCore.Mvc;
  5. using Microsoft.Extensions.Options;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Net.Http;
  9. using System.Text;
  10. using System.Text.Json;
  11. using System.Threading.Tasks;
  12. using TEAMModelBI.DI.BIAzureStorage;
  13. using TEAMModelBI.Filter;
  14. using TEAMModelBI.Models.Extension;
  15. using TEAMModelBI.Tool.Context;
  16. using TEAMModelBI.Tool.Extension;
  17. using TEAMModelOS.Models;
  18. using TEAMModelOS.SDK.DI;
  19. using TEAMModelOS.SDK.Extension;
  20. using TEAMModelOS.SDK.Models.Cosmos.BI;
  21. using TEAMModelOS.SDK.Models.Service;
  22. namespace TEAMModelBI.Controllers.BINormal
  23. {
  24. [Route("business")]
  25. [ApiController]
  26. public class CompanyController : ControllerBase
  27. {
  28. public readonly AzureCosmosFactory _azureCosmos;
  29. public readonly AzureStorageFactory _azureStorage;
  30. public readonly DingDing _dingDing;
  31. public readonly Option _option;
  32. private readonly IWebHostEnvironment _environment; //读取文件
  33. public CompanyController(AzureCosmosFactory azureCosmos, AzureStorageFactory azureStorage, DingDing dingDing, IOptionsSnapshot<Option> option, IWebHostEnvironment environment)
  34. {
  35. _azureCosmos = azureCosmos;
  36. _azureStorage = azureStorage;
  37. _dingDing = dingDing;
  38. _option = option?.Value;
  39. _environment = environment;
  40. }
  41. /// <summary>
  42. /// 查询企业信息结集合 若传ID查查询该账户的企业信息
  43. /// </summary>
  44. /// <param name="jsonElenent"></param>
  45. /// <returns></returns>
  46. [ProducesDefaultResponseType]
  47. [AuthToken(Roles = "admin,rdc,assist,sales,company")]
  48. [HttpPost("get-info")]
  49. public async Task<IActionResult> GetInfo(JsonElement jsonElenent)
  50. {
  51. jsonElenent.TryGetProperty("eid", out JsonElement eid);
  52. jsonElenent.TryGetProperty("site", out JsonElement site);
  53. var cosmosClient = _azureCosmos.GetCosmosClient();
  54. if ($"{site}".Equals(BIConst.GlobalSite))
  55. cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.GlobalSite);
  56. List<ReadCompany> companys = new();
  57. StringBuilder sqlTxt = new("select c.id,c.pk,c.code,c.name,c.credit,c.picture,c.emall,c.mobile,c.password from c");
  58. if (!string.IsNullOrEmpty($"{eid}"))
  59. {
  60. sqlTxt.Append($" where c.id='{eid}'");
  61. }
  62. await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "Normal").GetItemQueryStreamIterator(queryText: sqlTxt.ToString(), requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("Company") }))
  63. {
  64. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  65. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  66. {
  67. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  68. {
  69. ReadCompany readCompany = new()
  70. {
  71. id= obj.GetProperty("id").GetString(),
  72. pk = obj.GetProperty("pk").GetString(),
  73. code = obj.GetProperty("code").GetString(),
  74. name = obj.GetProperty("name").GetString(),
  75. credit = obj.GetProperty("credit").GetString(),
  76. picture = obj.GetProperty("picture").GetString(),
  77. emall = obj.GetProperty("emall").GetString(),
  78. mobile = obj.GetProperty("mobile").GetString(),
  79. };
  80. companys.Add(readCompany);
  81. }
  82. }
  83. }
  84. return Ok(new { state = 200, companys });
  85. }
  86. /// <summary>
  87. /// 新增企业信息和修改企业信息
  88. /// </summary>
  89. /// <param name="appCompany"></param>
  90. /// <returns></returns>
  91. //[ProducesDefaultResponseType]
  92. //[AuthToken(Roles = "admin,rdc,assist,sales,company")]
  93. [HttpPost("set-info")]
  94. public async Task<IActionResult> SetCompany([FromBody]Company company, [FromHeader] string site)
  95. {
  96. try
  97. {
  98. //var (loginId, loginName, pic, did, dname, dpic) = HttpJwtAnalysis.JwtXAuthBI(HttpContext.GetXAuth("AuthToken"), _option);
  99. StringBuilder strMsg = new();
  100. var cosmosClient = _azureCosmos.GetCosmosClient();
  101. var tableClient = _azureStorage.GetCloudTableClient();
  102. var blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public");
  103. if ($"{site}".Equals(BIConst.GlobalSite))
  104. {
  105. cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.GlobalSite);
  106. tableClient = _azureStorage.GetCloudTableClient(BIConst.GlobalSite);
  107. blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public", BIConst.GlobalSite);
  108. }
  109. string salt = Utils.CreatSaltString(8);
  110. string type = "";
  111. //新增企业信息
  112. if (string.IsNullOrEmpty(company.id))
  113. {
  114. company.id = Guid.NewGuid().ToString();
  115. company.code = "Company";
  116. company.salt = salt;
  117. company.password = string.IsNullOrWhiteSpace(company.password) ? Utils.HashedPassword("123456", salt) : Utils.HashedPassword(company.password, salt);//Password,若梦没有则是默认密码:123456
  118. company.createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
  119. company.jti = Guid.NewGuid().ToString();
  120. company.secretKey = JwtAuth.CreateBusinessJwtKeyBI(_option.Location, _option.JwtSecretKey, company.id, company.jti);
  121. await cosmosClient.GetContainer("TEAMModelOS", "Normal").CreateItemAsync<Company>(company, new PartitionKey(company.code));
  122. strMsg.Append($"{company.name}【{company.id}】新增企业基础信息。");
  123. type = "company-add";
  124. }
  125. //修改企业信息
  126. else
  127. {
  128. var respone = await cosmosClient.GetContainer("TEAMModelOS", "Normal").ReadItemStreamAsync(company.id, new PartitionKey($""));
  129. if (respone.Status == 200)
  130. {
  131. company.pk = "Company";
  132. company.code = "Company";
  133. company.ttl = -1;
  134. company.salt = salt;
  135. company.password = string.IsNullOrWhiteSpace(company.password) ? Utils.HashedPassword("123456", salt) : Utils.HashedPassword(company.password, salt);//Password,若梦没有则是默认密码:123456
  136. company = await cosmosClient.GetContainer("TEAMModelOS", "Normal").ReplaceItemAsync<Company>(company, company.id, new PartitionKey(company.code));
  137. strMsg.Append($"{company.name}【{company.id}】修改企业基础信息。");
  138. type = "company-update";
  139. }
  140. else return Ok(new { state = 404, msg = "未找到该id相关的企业信息" });
  141. }
  142. //保存操作记录
  143. //await _azureStorage.SaveBILog(type, strMsg.ToString(), _dingDing, httpContext: HttpContext);
  144. await AzureStorageBlobExtensions.SaveBILog(blobClient, tableClient, type, strMsg.ToString(), _dingDing, httpContext: HttpContext);
  145. return Ok(new { state = 200, company });
  146. }
  147. catch (Exception e)
  148. {
  149. //await _dingDing.SendBotMsg($"BI,{_option.Location} , /company/set-info \n {e.Message}\n{e.StackTrace} \n ", GroupNames.成都开发測試群組);
  150. return BadRequest();
  151. }
  152. }
  153. public record ReadCompany
  154. {
  155. public string id { get; set; }
  156. public string pk { get; set; }
  157. public string code { get; set; }
  158. public string name { get; set; }
  159. public string credit { get; set; }
  160. public string picture { get; set; }
  161. public string emall { get; set; }
  162. public string mobile { get; set; }
  163. }
  164. }
  165. }