BusinessUsersController.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. using Azure.Cosmos;
  2. using Microsoft.AspNetCore.Http;
  3. using Microsoft.AspNetCore.Mvc;
  4. using Microsoft.Extensions.Options;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Text.Json;
  10. using System.Threading.Tasks;
  11. using TEAMModelBI.Filter;
  12. using TEAMModelBI.Tool.Extension;
  13. using TEAMModelOS.Models;
  14. using TEAMModelOS.SDK.Context.BI;
  15. using TEAMModelOS.SDK.Context.Constant;
  16. using TEAMModelOS.SDK.DI;
  17. using TEAMModelOS.SDK.Extension;
  18. using TEAMModelOS.SDK.Models.Cosmos.BI.BINormal;
  19. namespace TEAMModelBI.Controllers.BINormal
  20. {
  21. [Route("bizuser")]
  22. [ApiController]
  23. public class BusinessUsersController : ControllerBase
  24. {
  25. public readonly AzureCosmosFactory _azureCosmos;
  26. public readonly AzureStorageFactory _azureStorage;
  27. public readonly DingDing _dingDing;
  28. public readonly Option _option;
  29. public BusinessUsersController(AzureCosmosFactory azureCosmos, AzureStorageFactory azureStorage, DingDing dingDing, IOptionsSnapshot<Option> option)
  30. {
  31. _azureCosmos = azureCosmos;
  32. _azureStorage = azureStorage;
  33. _dingDing = dingDing;
  34. _option = option?.Value;
  35. }
  36. /// <summary>
  37. /// 获取第三方用户信息
  38. /// </summary>
  39. /// <param name="jsonElement"></param>
  40. /// <returns></returns>
  41. [ProducesDefaultResponseType]
  42. [HttpPost("get-infos")]
  43. public async Task<IActionResult> GetInfos(JsonElement jsonElement)
  44. {
  45. jsonElement.TryGetProperty("id", out JsonElement id);
  46. jsonElement.TryGetProperty("site", out JsonElement site);
  47. var cosmosClient = _azureCosmos.GetCosmosClient();
  48. if ($"{site}".Equals(BIConst.Global))
  49. cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
  50. StringBuilder sqlTxt = new("select value(c) from c");
  51. if (!string.IsNullOrEmpty($"{id}"))
  52. {
  53. sqlTxt.Append($" where c.id='{id}'");
  54. }
  55. List<BusinessUsers> bizUsers = new();
  56. await foreach (var items in cosmosClient.GetContainer("TEAMModelOS", "Normal").GetItemQueryIterator<BusinessUsers>(queryText: sqlTxt.ToString(), requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("BizUsers") }))
  57. {
  58. bizUsers.Add(items);
  59. }
  60. return Ok(new { state = RespondCode.Ok, bizUsers });
  61. }
  62. /// <summary>
  63. /// 用户信息企业信息
  64. /// </summary>
  65. /// <param name="bizUsers"></param>
  66. /// <param name="site"></param>
  67. /// <returns></returns>
  68. [ProducesDefaultResponseType]
  69. [AuthToken(Roles = "admin,rdc,assist")]
  70. [HttpPost("set-info")]
  71. public async Task<IActionResult> SetInfos([FromBody] BizUsers bizUsers, [FromHeader] string site)
  72. {
  73. var cosmosClient = _azureCosmos.GetCosmosClient();
  74. var tableClient = _azureStorage.GetCloudTableClient();
  75. var blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public");
  76. if ($"{site}".Equals(BIConst.Global))
  77. {
  78. cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
  79. tableClient = _azureStorage.GetCloudTableClient(BIConst.Global);
  80. blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public", BIConst.Global);
  81. }
  82. var (tmdId, tmdName, pic, _, _, _) = HttpJwtAnalysis.JwtXAuthBI(HttpContext.GetXAuth("AuthToken"), _option);
  83. StringBuilder strMsg = new($"{tmdName}[{tmdId}]操作:");
  84. string salt = Utils.CreatSaltString(8);
  85. string type = "";
  86. if (string.IsNullOrEmpty(bizUsers.id))
  87. {
  88. bizUsers.id = Guid.NewGuid().ToString();
  89. bizUsers.code = "BizUsers";
  90. bizUsers.name = string.IsNullOrEmpty(bizUsers.name)? bizUsers.mobile.ToString(): bizUsers.name;
  91. bizUsers.salt = salt;
  92. bizUsers.pwd = string.IsNullOrEmpty(bizUsers.pwd) ? Utils.HashedPassword(bizUsers.mobile.ToString(), salt) : Utils.HashedPassword(bizUsers.pwd, salt);
  93. bizUsers = await cosmosClient.GetContainer("TEAMModelOS", "Normal").CreateItemAsync<BizUsers>(bizUsers, new PartitionKey("BizUsers"));
  94. strMsg.Append($"{bizUsers.name}【{bizUsers.id}】新增第三方用户信息基础信息。");
  95. type = "bizuser-add";
  96. }
  97. else
  98. {
  99. var response = await cosmosClient.GetContainer("TEAMModelOS", "Normal").ReadItemStreamAsync(bizUsers.id, new PartitionKey("BizUsers"));
  100. if (response.Status == RespondCode.Ok)
  101. {
  102. using var json = await JsonDocument.ParseAsync(response.ContentStream);
  103. BizUsers tempbizUsers = json.ToObject<BizUsers>();
  104. bizUsers.pk = "BizUsers";
  105. bizUsers.code = "BizUsers";
  106. bizUsers.ttl = -1;
  107. bizUsers.relation = tempbizUsers.relation;
  108. bizUsers.salt = tempbizUsers.salt;
  109. bizUsers.pwd = tempbizUsers.pwd;
  110. bizUsers = await cosmosClient.GetContainer("TEAMModelOS", "Normal").ReplaceItemAsync<BizUsers>(bizUsers, bizUsers.id, new PartitionKey("BizUsers"));
  111. strMsg.Append($"{bizUsers.name}【{bizUsers.id}】修改第三方用户信息基础信息。");
  112. type = "bizuser-add";
  113. }
  114. else Ok(new { state = RespondCode.NotFound ,msg="未找到id用户。"});
  115. }
  116. //保存操作记录
  117. await AzureStorageBlobExtensions.SaveBILog(blobClient, tableClient, type, strMsg.ToString(), _dingDing, httpContext: HttpContext);
  118. return Ok(new { state = RespondCode.Ok, bizUsers });
  119. }
  120. /// <summary>
  121. /// 重置密码
  122. /// </summary>
  123. /// <param name="jsonElement"></param>
  124. /// <returns></returns>
  125. [ProducesDefaultResponseType]
  126. [AuthToken(Roles = "admin,rdc,assist")]
  127. [HttpPost("reset-pwd")]
  128. public async Task<IActionResult> ResetPassWord(JsonElement jsonElement)
  129. {
  130. if (!jsonElement.TryGetProperty("id", out JsonElement id)) return BadRequest();
  131. jsonElement.TryGetProperty("site", out JsonElement site);
  132. var (tmdId, tmdName, pic, _, _, _) = HttpJwtAnalysis.JwtXAuthBI(HttpContext.GetXAuth("AuthToken"), _option);
  133. var cosmosClient = _azureCosmos.GetCosmosClient();
  134. var tableClient = _azureStorage.GetCloudTableClient();
  135. var blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public");
  136. if ($"{site}".Equals(BIConst.Global))
  137. {
  138. cosmosClient = _azureCosmos.GetCosmosClient(BIConst.Global);
  139. tableClient = _azureStorage.GetCloudTableClient(BIConst.Global);
  140. blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public", BIConst.Global);
  141. }
  142. BizUsers bizUsers = new();
  143. StringBuilder strMsg = new($"{tmdName}[{tmdId}]操作:");
  144. string salt = Utils.CreatSaltString(8);
  145. var response = await cosmosClient.GetContainer("TEAMModelOS", "Normal").ReadItemStreamAsync($"{id}", new PartitionKey("BizUsers"));
  146. if (response.Status == 200)
  147. {
  148. using var json = await JsonDocument.ParseAsync(response.ContentStream);
  149. bizUsers = json.ToObject<BizUsers>();
  150. bizUsers.salt = salt;
  151. bizUsers.pwd = Utils.HashedPassword(bizUsers.mobile.ToString(), salt);
  152. strMsg.Append($"重置{bizUsers.name}【{bizUsers.id}】的密码,重置成功!");
  153. bizUsers = await cosmosClient.GetContainer("TEAMModelOS", "Normal").ReplaceItemAsync<BizUsers>(bizUsers, bizUsers.id, new PartitionKey("BizUsers"));
  154. }else return Ok(new { state = RespondCode.NotFound, msg="未找到该用户信息" });
  155. //保存操作记录
  156. await AzureStorageBlobExtensions.SaveBILog(blobClient, tableClient, "bizuser-reset", strMsg.ToString(), _dingDing, httpContext: HttpContext);
  157. return Ok(new { state = RespondCode.Ok, bizUsers });
  158. }
  159. /// <summary>
  160. /// 用户关联/移除企业信息
  161. /// </summary>
  162. /// <param name="jsonElement"></param>
  163. /// <returns></returns>
  164. [ProducesDefaultResponseType]
  165. [AuthToken(Roles = "admin,rdc,assist")]
  166. [HttpPost("rel-biz")]
  167. public async Task<ActionResult> RelationBusiness(JsonElement jsonElement)
  168. {
  169. if(!jsonElement.TryGetProperty("id", out JsonElement id)) return BadRequest();
  170. if (!jsonElement.TryGetProperty("bizs", out JsonElement _bizs)) return BadRequest();
  171. if (!jsonElement.TryGetProperty("type", out JsonElement type)) return BadRequest();
  172. jsonElement.TryGetProperty("site", out JsonElement site);
  173. var (tmdId, tmdName, pic, _, _, _) = HttpJwtAnalysis.JwtXAuthBI(HttpContext.GetXAuth("AuthToken"), _option);
  174. List<BizRel> bizRels = _bizs.ToObject<List<BizRel>>();
  175. var cosmosClient = _azureCosmos.GetCosmosClient();
  176. var tableClient = _azureStorage.GetCloudTableClient();
  177. var blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public");
  178. if ($"{site}".Equals(BIConst.Global))
  179. {
  180. cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
  181. tableClient = _azureStorage.GetCloudTableClient(BIConst.Global);
  182. blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public", BIConst.Global);
  183. }
  184. StringBuilder strMsg = new($"{tmdName}[{tmdId}]给");
  185. if ($"{type}".Equals("add"))
  186. {
  187. strMsg.Append($"用户【{id}】关联企业信息:添加企业:");
  188. }
  189. else if ($"{type}".Equals("del"))
  190. {
  191. strMsg.Append($"用户【{id}】移除企业信息,移除企业:");
  192. }
  193. else { return Ok(new { state = RespondCode.ParamsError, msg = "类型错误" }); }
  194. List<BizRel> noBizRel = new();
  195. BizUsers bizUsers = new();
  196. var response = await cosmosClient.GetContainer("TEAMModelOS", "Normal").ReadItemStreamAsync($"{id}", new PartitionKey("BizUsers"));
  197. if (response.Status == RespondCode.Ok)
  198. {
  199. using var json = await JsonDocument.ParseAsync(response.ContentStream);
  200. bizUsers = json.ToObject<BizUsers>();
  201. foreach (var item in bizRels)
  202. {
  203. var temp = bizUsers.relation.Find(f => f.bizId == item.bizId);
  204. if ($"{type}".Equals("add"))
  205. {
  206. if (temp != null)
  207. noBizRel.Add(item);
  208. else
  209. {
  210. bizUsers.relation.Add(item);
  211. strMsg.Append($"{item.name}[{item.bizId}]|");
  212. }
  213. }
  214. else if ($"{type}".Equals("del"))
  215. {
  216. if (temp != null)
  217. {
  218. bizUsers.relation.Add(item);
  219. strMsg.Append($"{item.name}[{item.bizId}]|");
  220. }
  221. else
  222. noBizRel.Add(item);
  223. }
  224. }
  225. bizUsers = await cosmosClient.GetContainer("TEAMModelOS", "Normal").ReplaceItemAsync<BizUsers>(bizUsers, bizUsers.id, new PartitionKey("BizUsers"));
  226. }
  227. else return Ok(new { state = RespondCode.NotFound, msg = "未找到该用户" });
  228. //保存操作记录
  229. await AzureStorageBlobExtensions.SaveBILog(blobClient, tableClient, "bizconfig-addSchool", strMsg.ToString(), _dingDing, httpContext: HttpContext);
  230. if (noBizRel.Count > 0)
  231. return Ok(new { state = RespondCode.Created, bizUsers, noBizRel });
  232. return Ok(new { state = RespondCode.Ok, bizUsers });
  233. }
  234. /// <summary>
  235. /// 通过企业Id查询用户信息
  236. /// </summary>
  237. /// <param name="jsonElement"></param>
  238. /// <returns></returns>
  239. [ProducesDefaultResponseType]
  240. [HttpPost("get-bizid")]
  241. public async Task<IActionResult> GetBizIdUsers(JsonElement jsonElement)
  242. {
  243. if (!jsonElement.TryGetProperty("id", out JsonElement id)) return BadRequest();
  244. jsonElement.TryGetProperty("site", out JsonElement site);
  245. var cosmosClient = _azureCosmos.GetCosmosClient();
  246. if ($"{site}".Equals(BIConst.Global))
  247. cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
  248. List<BusinessUsers> businessUsers = new();
  249. string sql = $"select value(c) from c join s in c.relation where c.code='BizUsers' and s.bizId = '80e1bb6c-acba-46ab-9939-4851c4ef2158'";
  250. await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "Normal").GetItemQueryIterator<BusinessUsers>(queryText: sql, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("BizUsers") }))
  251. {
  252. businessUsers.Add(item);
  253. }
  254. return Ok(new { state = RespondCode.Ok, businessUsers });
  255. }
  256. }
  257. }