BusinessConfigController.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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("bizconfig")]
  21. [ApiController]
  22. public class BusinessConfigController : ControllerBase
  23. {
  24. public readonly AzureCosmosFactory _azureCosmos;
  25. public readonly AzureStorageFactory _azureStorage;
  26. public readonly DingDing _dingDing;
  27. public readonly Option _option;
  28. public BusinessConfigController(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="bizConfig"></param>
  39. /// <returns></returns>
  40. [ProducesDefaultResponseType]
  41. [AuthToken(Roles = "admin,rdc,assist")]
  42. [HttpPost("set-info")]
  43. public async Task<IActionResult> SetInfo([FromBody] BizConfig bizConfig, [FromHeader] string site)
  44. {
  45. StringBuilder strMsg = new();
  46. var cosmosClient = _azureCosmos.GetCosmosClient();
  47. var tableClient = _azureStorage.GetCloudTableClient();
  48. var blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public");
  49. if ($"{site}".Equals(BIConst.Global))
  50. {
  51. cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
  52. tableClient = _azureStorage.GetCloudTableClient(BIConst.Global);
  53. blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public", BIConst.Global);
  54. }
  55. string salt = Utils.CreatSaltString(8);
  56. List<BizUsers> bizUsers = new();
  57. string type = "";
  58. //新增企业信息
  59. if (string.IsNullOrEmpty(bizConfig.id))
  60. {
  61. bizConfig.id = Guid.NewGuid().ToString();
  62. bizConfig.code = "BizConfig";
  63. bizConfig.pk = "BizConfig";
  64. bizConfig.createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
  65. var auth_token = JwtAuthExtension.CreateBusinessApiToken(_option.Location, bizConfig.id, _option.JwtSecretKey, bizConfig.isCustomize);
  66. bizConfig.jti = auth_token.jti;
  67. bizConfig.token = auth_token.jwt;
  68. await cosmosClient.GetContainer("TEAMModelOS", "Normal").CreateItemAsync<BizConfig>(bizConfig, new PartitionKey("BizConfig"));
  69. await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "Normal").GetItemQueryIterator<BizUsers>(queryText: $"select value(c) from c where c.mobile ={bizConfig.mobile}", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("BizUsers") }))
  70. {
  71. bizUsers.Add(item);
  72. }
  73. BizRel bizRel = new() { bizId = bizConfig.id, role = new List<string>() { "admin" } };
  74. if (bizUsers.Count > 0)
  75. {
  76. foreach (var item in bizUsers)
  77. {
  78. BizRel temp = item.relation.Find(f => f.bizId.Equals(bizConfig.id));
  79. if (temp == null)
  80. {
  81. item.relation.Add(bizRel);
  82. await cosmosClient.GetContainer("TEAMModelOS", "Normal").ReplaceItemAsync<BizUsers>(item, item.id, new PartitionKey("BizUsers"));
  83. }
  84. }
  85. }
  86. else
  87. {
  88. BizUsers tBizUsers = new() { id = Guid.NewGuid().ToString(), code= "BizUsers", name = bizConfig.mobile.ToString(), mobile = bizConfig.mobile, salt = salt, pwd = Utils.HashedPassword($"{bizConfig.mobile}", salt),relation= new List<BizRel>() { { bizRel } } };
  89. await cosmosClient.GetContainer("TEAMModelOS", "Normal").CreateItemAsync<BizUsers>(tBizUsers, new PartitionKey("BizUsers"));
  90. }
  91. strMsg.Append($"{bizConfig.name}【{bizConfig.id}】新增企业基础信息。");
  92. type = "bizconfig-add";
  93. }
  94. //修改企业信息
  95. else
  96. {
  97. var response = await cosmosClient.GetContainer("TEAMModelOS", "Normal").ReadItemStreamAsync(bizConfig.id, new PartitionKey("BizConfig"));
  98. if (response.Status == 200)
  99. {
  100. using var json = await JsonDocument.ParseAsync(response.ContentStream);
  101. BizConfig tempBizConfig = json.ToObject<BizConfig>();
  102. bizConfig.pk = "BizConfig";
  103. bizConfig.code = "BizConfig";
  104. bizConfig.ttl = -1;
  105. bizConfig.createTime = tempBizConfig.createTime;
  106. bizConfig.jti = tempBizConfig.jti;
  107. bizConfig.token = tempBizConfig.token;
  108. bizConfig = await cosmosClient.GetContainer("TEAMModelOS", "Normal").ReplaceItemAsync<BizConfig>(bizConfig, bizConfig.id, new PartitionKey("BizConfig"));
  109. strMsg.Append($"{bizConfig.name}【{bizConfig.id}】修改企业基础信息。");
  110. type = "bizconfig-update";
  111. }
  112. }
  113. //保存操作记录
  114. await AzureStorageBlobExtensions.SaveBILog(blobClient, tableClient, type, strMsg.ToString(), _dingDing, httpContext: HttpContext);
  115. return Ok(new { state = RespondCode.Ok, bizConfig });
  116. }
  117. /// <summary>
  118. /// 获取企业信息列表 //已对接
  119. /// </summary>
  120. /// <param name="jsonElement"></param>
  121. /// <returns></returns>
  122. [ProducesDefaultResponseType]
  123. [HttpPost("get-infos")]
  124. public async Task<IActionResult> GetInfos(JsonElement jsonElement)
  125. {
  126. jsonElement.TryGetProperty("id", out JsonElement id);
  127. //jsonElement.TryGetProperty("site", out JsonElement site);//分开部署,就不需要,一站多用时,取消注释
  128. var cosmosClient = _azureCosmos.GetCosmosClient();
  129. ////分开部署,就不需要,一站多用时,取消注释
  130. //if ($"{site}".Equals(BIConst.Global))
  131. // cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
  132. StringBuilder sqlTxt = new("select value(c) from c");
  133. if (!string.IsNullOrEmpty($"{id}"))
  134. {
  135. sqlTxt.Append($" where c.id='{id}'");
  136. }
  137. List<Business> businesses = new();
  138. await foreach (var items in cosmosClient.GetContainer("TEAMModelOS", "Normal").GetItemQueryIterator<Business>(queryText: sqlTxt.ToString(), requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("BizConfig") }))
  139. {
  140. businesses.Add(items);
  141. }
  142. return Ok(new { state = RespondCode.Ok, businesses });
  143. }
  144. /// <summary>
  145. /// 重置秘钥 //已对接
  146. /// </summary>
  147. /// <param name="jsonElement"></param>
  148. /// <returns></returns>
  149. [ProducesDefaultResponseType]
  150. [AuthToken(Roles = "admin,rdc,assist")]
  151. [HttpPost("reset-secretkey")]
  152. public async Task<IActionResult> ResetSecretKey(JsonElement jsonElement)
  153. {
  154. if (!jsonElement.TryGetProperty("id", out JsonElement id)) return BadRequest();
  155. //jsonElement.TryGetProperty("site", out JsonElement site);//分开部署,就不需要,一站多用时,取消注释
  156. var (tmdId, tmdName, pic, _, _, _) = HttpJwtAnalysis.JwtXAuthBI(HttpContext.GetXAuth("AuthToken"), _option);
  157. var cosmosClient = _azureCosmos.GetCosmosClient();
  158. var tableClient = _azureStorage.GetCloudTableClient();
  159. var blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public");
  160. ////分开部署,就不需要,一站多用时,取消注释
  161. //if ($"{site}".Equals(BIConst.Global))
  162. //{
  163. // cosmosClient = _azureCosmos.GetCosmosClient(BIConst.Global);
  164. // tableClient = _azureStorage.GetCloudTableClient(BIConst.Global);
  165. // blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public", BIConst.Global);
  166. //}
  167. BizConfig bizConfig = new();
  168. var response = await cosmosClient.GetContainer("TEAMModelOS", "Normal").ReadItemStreamAsync($"{id}", new PartitionKey("BizConfig"));
  169. if (response.Status == 200)
  170. {
  171. using var json = await JsonDocument.ParseAsync(response.ContentStream);
  172. bizConfig = json.ToObject<BizConfig>();
  173. var auth_token = JwtAuthExtension.CreateBusinessApiToken(_option.Location, bizConfig.id, _option.JwtSecretKey, bizConfig.isCustomize);
  174. bizConfig.jti = auth_token.jti;
  175. bizConfig.token = auth_token.jwt;
  176. bizConfig = await cosmosClient.GetContainer("TEAMModelOS", "Normal").ReplaceItemAsync<BizConfig>(bizConfig, bizConfig.id, new PartitionKey("BizConfig"));
  177. }
  178. return Ok(new { state =RespondCode.Ok, bizConfig });
  179. }
  180. /// <summary>
  181. /// 关联企业学校 //已对接
  182. /// </summary>
  183. /// <param name="jsonElement"></param>
  184. /// <returns></returns>
  185. [ProducesDefaultResponseType]
  186. [AuthToken(Roles = "admin,rdc,assist")]
  187. [HttpPost("rel-school")]
  188. public async Task<IActionResult> RelationSchool(JsonElement jsonElement)
  189. {
  190. if (!jsonElement.TryGetProperty("id", out JsonElement id)) return BadRequest();
  191. if (!jsonElement.TryGetProperty("schools", out JsonElement _schools)) return BadRequest();
  192. if (!jsonElement.TryGetProperty("type", out JsonElement type)) return BadRequest();
  193. //jsonElement.TryGetProperty("site", out JsonElement site); //分开部署,就不需要,一站多用时,取消注释
  194. var (tmdId, tmdName, pic, _, _, _) = HttpJwtAnalysis.JwtXAuthBI(HttpContext.GetXAuth("AuthToken"), _option);
  195. List<BizSchool> bizSchool = _schools.ToObject<List<BizSchool>>();
  196. var cosmosClient = _azureCosmos.GetCosmosClient();
  197. var tableClient = _azureStorage.GetCloudTableClient();
  198. var blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public");
  199. ////分开部署,就不需要,一站多用时,取消注释
  200. //if ($"{site}".Equals(BIConst.Global))
  201. //{
  202. // cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
  203. // tableClient = _azureStorage.GetCloudTableClient(BIConst.Global);
  204. // blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public", BIConst.Global);
  205. //}
  206. StringBuilder strMsg = new($"{tmdName}[{tmdId}]给");
  207. if ($"{type}".Equals("add"))
  208. {
  209. strMsg.Append($"企业关联学校;学校信息列表:");
  210. }
  211. else if ($"{type}".Equals("del"))
  212. {
  213. strMsg.Append("企业移除学校,学校信息列表:");
  214. }
  215. else { return Ok(new { state = RespondCode.ParamsError, msg = "类型错误" }); }
  216. List<BizSchool> noBizSc = new();
  217. BizConfig bizConfig = new();
  218. var response = await cosmosClient.GetContainer("TEAMModelOS", "Normal").ReadItemStreamAsync($"{id}", new PartitionKey("BizConfig"));
  219. if (response.Status == RespondCode.Ok)
  220. {
  221. using var json = await JsonDocument.ParseAsync(response.ContentStream);
  222. bizConfig = json.ToObject<BizConfig>();
  223. foreach (var item in bizSchool)
  224. {
  225. var temp = bizConfig.schools.Find(f => f.id.Equals(item.id));
  226. if ($"{type}".Equals("add"))
  227. {
  228. if (temp == null)
  229. {
  230. bizConfig.schools.Add(item);
  231. strMsg.Append($"{item.name}[{item.id}]|");
  232. }
  233. else
  234. noBizSc.Add(temp);
  235. }
  236. else if ($"{type}".Equals("del"))
  237. {
  238. if (temp != null)
  239. {
  240. bizConfig.schools.Remove(temp);
  241. strMsg.Append($"{item.name}[{item.id}]|");
  242. }
  243. }
  244. }
  245. bizConfig = await cosmosClient.GetContainer("TEAMModelOS", "Normal").ReplaceItemAsync<BizConfig>(bizConfig, bizConfig.id, new PartitionKey("BizConfig"));
  246. }
  247. else return Ok(new { state = RespondCode.NotFound, msg = "未找到该企业" });
  248. //保存操作记录
  249. await AzureStorageBlobExtensions.SaveBILog(blobClient, tableClient, $"bizconfig-{type}School", strMsg.ToString(), _dingDing, httpContext: HttpContext);
  250. if (noBizSc.Count > 0)
  251. return Ok(new { state = RespondCode.Created, bizConfig , noBizSc });
  252. return Ok(new { state = RespondCode.Ok, bizConfig });
  253. }
  254. /// <summary>
  255. /// 查询所有学校 如果传企业ID则查询与企业没有关联的学校 //已对接
  256. /// </summary>
  257. /// <param name="jsonElement"></param>
  258. /// <returns></returns>
  259. [ProducesDefaultResponseType]
  260. [HttpPost("get-schools")]
  261. public async Task<IActionResult> GetSchools(JsonElement jsonElement)
  262. {
  263. jsonElement.TryGetProperty("id", out JsonElement id);
  264. //jsonElement.TryGetProperty("site", out JsonElement site);//分开部署,就不需要,一站多用时,取消注释
  265. var cosmosClient = _azureCosmos.GetCosmosClient();
  266. var tableClient = _azureStorage.GetCloudTableClient();
  267. var blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public");
  268. ////分开部署,就不需要,一站多用时,取消注释
  269. //if ($"{site}".Equals(BIConst.Global))
  270. //{
  271. // cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
  272. // tableClient = _azureStorage.GetCloudTableClient(BIConst.Global);
  273. // blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public", BIConst.Global);
  274. //}
  275. List<OpenSchool> openSchools = new();
  276. await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "School").GetItemQueryIterator<OpenSchool>(queryText: "select c.id,c.code,c.name,c.picture from c", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("Base") }))
  277. {
  278. openSchools.Add(item);
  279. }
  280. BizConfig bizConfig = new();
  281. if (!string.IsNullOrEmpty($"id"))
  282. {
  283. var response = await cosmosClient.GetContainer("TEAMModelOS", "Normal").ReadItemStreamAsync($"{id}", new PartitionKey("BizConfig"));
  284. if (response.Status == RespondCode.Ok)
  285. {
  286. using var json = await JsonDocument.ParseAsync(response.ContentStream);
  287. bizConfig = json.ToObject<BizConfig>();
  288. if (bizConfig.schools.Count > 0)
  289. {
  290. foreach (var item in bizConfig.schools)
  291. {
  292. var temp = openSchools.Find(f => f.id.Equals(item.id));
  293. if (temp != null)
  294. openSchools.Remove(temp);
  295. }
  296. }
  297. }
  298. }
  299. return Ok(new { state = RespondCode.Ok , openSchools });
  300. }
  301. public record OpenSchool
  302. {
  303. public string id { get; set; }
  304. public string code { get; set; }
  305. public string name { get; set; }
  306. public string picture { get; set; }
  307. }
  308. }
  309. }