AbilityMgmtController.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. using Microsoft.AspNetCore.Http;
  2. using Microsoft.AspNetCore.Mvc;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Threading.Tasks;
  7. using Microsoft.Extensions.Configuration;
  8. using TEAMModelOS.SDK.DI;
  9. using TEAMModelOS.SDK.Models;
  10. using TEAMModelOS.Models;
  11. using Microsoft.Extensions.Options;
  12. using System.Text.Json;
  13. using Microsoft.Azure.Cosmos;
  14. using System.Text;
  15. using TEAMModelOS.SDK.Extension;
  16. using TEAMModelBI.Filter;
  17. using TEAMModelBI.Tool.Extension;
  18. using TEAMModelOS.SDK.Context.BI;
  19. using TEAMModelOS.SDK;
  20. namespace TEAMModelBI.Controllers.BINormal
  21. {
  22. [Route("biabilitymgmt")]
  23. [ApiController]
  24. public class AbilityMgmtController : ControllerBase
  25. {
  26. //获取配置信息
  27. private readonly IConfiguration _configuration;
  28. //数据容器
  29. private readonly AzureCosmosFactory _azureCosmos;
  30. //钉钉提示信息
  31. private readonly DingDing _dingDing;
  32. private readonly Option _option;
  33. private readonly AzureStorageFactory _azureStorage;
  34. public AbilityMgmtController(IConfiguration configuration, AzureCosmosFactory azureCosmos, DingDing dingDing, IOptionsSnapshot<Option> option, AzureStorageFactory azureStorage)
  35. {
  36. _configuration = configuration;
  37. _azureCosmos = azureCosmos;
  38. _dingDing = dingDing;
  39. _option = option?.Value;
  40. _azureStorage = azureStorage;
  41. }
  42. /// <summary>
  43. /// 依据区级的标准编号获取标准的编号集合 //已对接
  44. /// </summary>
  45. /// <param name="jsonElement"></param>
  46. /// <returns></returns>
  47. [ProducesDefaultResponseType]
  48. [HttpPost("get-abilitys")]
  49. public async Task<IActionResult> GetAbilitysByStandard(JsonElement jsonElement)
  50. {
  51. try
  52. {
  53. if (!jsonElement.TryGetProperty("standard", out JsonElement standard)) return BadRequest();
  54. //jsonElement.TryGetProperty("site", out JsonElement site);//分开部署,就不需要,一站多用时,取消注释
  55. var cosmosClient = _azureCosmos.GetCosmosClient();
  56. ////分开部署,就不需要,一站多用时,取消注释
  57. //if ($"{site}".Equals(BIConst.Global))
  58. // cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
  59. List<Ability> abilities = new();
  60. string sqltxt = "select value(c) from c";
  61. await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").GetItemQueryIteratorSql<Ability>(queryText: sqltxt, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Ability-{standard}") }))
  62. {
  63. abilities.Add(item);
  64. }
  65. return Ok(new { state = 200, abilities });
  66. }
  67. catch (Exception ex)
  68. {
  69. await _dingDing.SendBotMsg($"BI,{_option.Location} /biabilitymgmt/get-abilitys \n {ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
  70. return BadRequest();
  71. }
  72. }
  73. /// <summary>
  74. /// 依据能力标准的编号查询某个能力标准信息 //已对接
  75. /// </summary>
  76. /// <param name="jsonElement"></param>
  77. /// <returns></returns>
  78. [ProducesDefaultResponseType]
  79. [HttpPost("get-abulityid")]
  80. public async Task<IActionResult> GetAbilityByID(JsonElement jsonElement)
  81. {
  82. try
  83. {
  84. if (!jsonElement.TryGetProperty("standard", out JsonElement standard)) return BadRequest();
  85. if (!jsonElement.TryGetProperty("abilityId", out JsonElement abilityId)) return BadRequest();
  86. //jsonElement.TryGetProperty("site", out JsonElement site);//分开部署,就不需要,一站多用时,取消注释
  87. var cosmosClient = _azureCosmos.GetCosmosClient();
  88. ////分开部署,就不需要,一站多用时,取消注释
  89. //if ($"{site}".Equals(BIConst.Global))
  90. // cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
  91. Ability ability = await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").ReadItemAsync<Ability>($"{abilityId}", new PartitionKey($"Ability-{standard}"));
  92. return Ok(new { state = 200, ability = ability });
  93. }
  94. catch (Exception ex)
  95. {
  96. await _dingDing.SendBotMsg($"BI,{_option.Location} /biabilitymgmt/get-abulityid \n {ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
  97. return BadRequest();
  98. }
  99. }
  100. /// <summary>
  101. /// 依据区标准点和ID 删除册别 //已对接
  102. /// </summary>
  103. /// <param name="jsonElement"></param>
  104. /// <returns></returns>
  105. [ProducesDefaultResponseType]
  106. [AuthToken(Roles = "admin,rdc,assist,sales")]
  107. [HttpPost("del-ability")]
  108. public async Task<IActionResult> DelAbility(JsonElement jsonElement)
  109. {
  110. try
  111. {
  112. if (!jsonElement.TryGetProperty("standard", out JsonElement standard)) return BadRequest();
  113. if (!jsonElement.TryGetProperty("id", out JsonElement id)) return BadRequest();
  114. //jsonElement.TryGetProperty("site", out JsonElement site); //分开部署,就不需要,一站多用时,取消注释
  115. var (_tmdId, _tmdName, pic, did, dname, dpic) = HttpJwtAnalysis.JwtXAuthBI(HttpContext.GetXAuth("AuthToken"), _option);
  116. if (string.IsNullOrEmpty($"{standard}"))
  117. {
  118. return Ok(new { state = 0,message= "standard 参数异常" });
  119. }
  120. var cosmosClient = _azureCosmos.GetCosmosClient();
  121. var tableClient = _azureStorage.GetCloudTableClient();
  122. var blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public");
  123. ////分开部署,就不需要,一站多用时,取消注释
  124. //if ($"{site}".Equals(BIConst.Global))
  125. //{
  126. // cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
  127. // tableClient = _azureStorage.GetCloudTableClient(BIConst.Global);
  128. // blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public", BIConst.Global);
  129. //}
  130. List<AbilityTask> syllabus = new();
  131. string sql = $"select value(c) from c where c.abilityId='{id}'";
  132. var response = await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").DeleteItemStreamAsync(id.ToString(), new PartitionKey($"Ability-{standard}"));
  133. await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").GetItemQueryIteratorSql<AbilityTask>(queryText: sql, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"AbilityTask-{standard}") }))
  134. {
  135. syllabus.Add(item);
  136. }
  137. if (syllabus.IsNotEmpty())
  138. {
  139. var sresponse = await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").DeleteItemsStreamAsync(syllabus.Select(x => x.id).ToList(), $"AbilityTask-{standard}");
  140. }
  141. if (response.StatusCode == System.Net.HttpStatusCode.OK)
  142. {
  143. //保存操作记录
  144. await AzureStorageBlobExtensions.SaveBILog(blobClient, tableClient, "abilityTask-del", $"{_tmdName}【{_tmdId}】删除册别,删除ID:{id}", _dingDing, httpContext: HttpContext);
  145. return Ok(new { state = 200 });
  146. }
  147. else return Ok(new { state = response.StatusCode });
  148. }
  149. catch (Exception ex)
  150. {
  151. await _dingDing.SendBotMsg($"BI,{_option.Location} /biabilitymgmt/del-ability \n {ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
  152. return BadRequest();
  153. }
  154. }
  155. /// <summary>
  156. /// 更新和新增册别信息 //已经对接
  157. /// </summary>
  158. /// <param name="request"></param>
  159. /// <returns></returns>
  160. [ProducesDefaultResponseType]
  161. [AuthToken(Roles = "admin,rdc,assist,sales")]
  162. [HttpPost("upd-ability")]
  163. public async Task<IActionResult> UpdAbility(Ability ability, [FromHeader] string site)
  164. {
  165. try
  166. {
  167. var (_tmdId, _tmdName, pic, did, dname, dpic) = HttpJwtAnalysis.JwtXAuthBI(HttpContext.GetXAuth("AuthToken"), _option);
  168. StringBuilder stringBuilder = new($"{_tmdName}【{_tmdId}】");
  169. //Ability ability = recordAbility.ability;
  170. ability.pk = "Ability";
  171. ability.ttl = -1;
  172. Ability tempAbility = new Ability();
  173. if (!string.IsNullOrEmpty(ability.code) && !string.IsNullOrEmpty(ability.standard))
  174. {
  175. ability.code = ability.code.StartsWith("Ability-") ? ability.code : $"Ability-{ability.code}";
  176. }
  177. else return Ok(new { state = 0, message = "参数异常" });
  178. var azureClient = _azureCosmos.GetCosmosClient();
  179. var tableClient = _azureStorage.GetCloudTableClient();
  180. var blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public");
  181. if ($"{site}".Equals(BIConst.Global))
  182. {
  183. azureClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
  184. tableClient = _azureStorage.GetCloudTableClient(BIConst.Global);
  185. blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public", BIConst.Global);
  186. }
  187. string tempType = "";
  188. //新增册别
  189. if (string.IsNullOrEmpty(ability.id))
  190. {
  191. ability.comid = Guid.NewGuid().ToString();
  192. StringBuilder sqltxt = new("select value(c) from c where c.status = 1 ");
  193. if (!string.IsNullOrEmpty(ability.dimension))
  194. {
  195. sqltxt.Append($" and c.dimension = '{ability.dimension}' and c.name = '{ability.name}' and c.no = '{ability.no}' ");
  196. List<Ability> abilities = new List<Ability>();
  197. await foreach (var item in azureClient.GetContainer(Constant.TEAMModelOS, "Normal").GetItemQueryIteratorSql<Ability>(queryText: sqltxt.ToString(), requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey(ability.code) }))
  198. {
  199. abilities.Add(item);
  200. }
  201. if (abilities.Count > 0)
  202. {
  203. return Ok(new { state = 1, message = "数据已存在" });
  204. }
  205. }
  206. else
  207. {
  208. return Ok(new { state = 2, mesages = "参数异常" });
  209. }
  210. ability.id = Guid.NewGuid().ToString();
  211. ability.creatorId = string.IsNullOrEmpty($"{ability.creatorId}") ? $"{_tmdId}" : ability.creatorId;
  212. ability.creatorName = string.IsNullOrEmpty($"{ability.creatorName}") ? $"{_tmdName}" : ability.creatorName;
  213. tempAbility = await azureClient.GetContainer("TEAMModelOS", "Normal").CreateItemAsync(ability, new PartitionKey(ability.code));
  214. stringBuilder.Append($"新增册别信息,册别编号:{tempAbility.id},册别名称:{tempAbility.name}");
  215. tempType = "ability-add";
  216. }
  217. //更新册别
  218. else
  219. {
  220. try
  221. {
  222. tempAbility = await azureClient.GetContainer(Constant.TEAMModelOS, "Normal").ReplaceItemAsync<Ability>(ability, ability.id, new PartitionKey(ability.code));
  223. stringBuilder.Append($"更新册别信息,册别编号:{tempAbility.id},册别名称:{tempAbility.name}");
  224. tempType = "ability-update";
  225. }
  226. catch (Exception ex)
  227. {
  228. await _dingDing.SendBotMsg($"BI,{_option.Location} /biabilitymgmt/upd-ability \n {ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
  229. return BadRequest();
  230. }
  231. }
  232. //保存操作记录
  233. await AzureStorageBlobExtensions.SaveBILog(blobClient, tableClient, tempType?.ToString(), stringBuilder?.ToString(), _dingDing, httpContext: HttpContext);
  234. return Ok(new { state = 200, Ability = tempAbility });
  235. }
  236. catch (Exception ex)
  237. {
  238. await _dingDing.SendBotMsg($"BI,{_option.Location} biabilitymgmt/upd-ability \n {ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
  239. return BadRequest();
  240. }
  241. }
  242. /// <summary>
  243. /// 修改是否必修的接口 //已对接
  244. /// </summary>
  245. /// <param name="jsonElement"></param>
  246. /// <returns></returns>
  247. [ProducesDefaultResponseType]
  248. [AuthToken(Roles = "admin,rdc,assist,sales")]
  249. [HttpPost("upd-currency")]
  250. public async Task<IActionResult> UpdateCurrency(JsonElement jsonElement)
  251. {
  252. try
  253. {
  254. if (!jsonElement.TryGetProperty("isRequired", out JsonElement isRequired)) return BadRequest(); //是否是必修集合
  255. //jsonElement.TryGetProperty("site", out JsonElement site); //分开部署,就不需要,一站多用时,取消注释
  256. var (_tmdId, _tmdName, pic, did, dname, dpic) = HttpJwtAnalysis.JwtXAuthBI(HttpContext.GetXAuth("AuthToken"), _option);
  257. var currencys = isRequired.ToObject<List<IsRequired>>();
  258. var cosmosClient = _azureCosmos.GetCosmosClient();
  259. var tableClient = _azureStorage.GetCloudTableClient();
  260. var blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public");
  261. //if ($"{site}".Equals(BIConst.Global))
  262. //{
  263. // cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
  264. // tableClient = _azureStorage.GetCloudTableClient(BIConst.Global);
  265. // blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public", BIConst.Global);
  266. //}
  267. foreach (var item in currencys)
  268. {
  269. Ability ability = await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").ReadItemAsync<Ability>(item.abilityId, new PartitionKey($"Ability-{item.standard}"));
  270. ability.currency = item.currency;
  271. await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").ReplaceItemAsync(ability, ability.id, new PartitionKey(ability.code));
  272. }
  273. //保存操作记录
  274. await AzureStorageBlobExtensions.SaveBILog(blobClient, tableClient, "ability-update", $"{_tmdName}【{_tmdId}】设置是否必修状态。标准:{currencys[0].standard}", _dingDing, httpContext: HttpContext);
  275. return Ok(new { state = 200, currencys });
  276. }
  277. catch (Exception ex)
  278. {
  279. await _dingDing.SendBotMsg($"BI,{_option.Location} /biabilitymgmt/upd-currency \n {ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
  280. return BadRequest();
  281. }
  282. }
  283. public record IsRequired()
  284. {
  285. public string abilityId { get; set; }
  286. public string standard { get; set; }
  287. public int currency { get; set; }
  288. }
  289. /// <summary>
  290. /// 新增和修改是使用
  291. /// </summary>
  292. public record RecordAbility()
  293. {
  294. public string tmdId { get; set; }
  295. public string tmdName { get; set; }
  296. public Ability ability { get; set; }
  297. }
  298. }
  299. }