BusinessUsersController.cs 13 KB

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