AbilityMgmtController.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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 Azure.Cosmos;
  14. using HTEXLib.COMM.Helpers;
  15. using System.Text;
  16. using TEAMModelOS.SDK.Extension;
  17. using TEAMModelBI.Filter;
  18. using TEAMModelBI.Tool.Extension;
  19. using TEAMModelOS.SDK.Context.BI;
  20. namespace TEAMModelBI.Controllers.BIAbility
  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. if ($"{site}".Equals(BIConst.Global))
  57. cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
  58. List<Ability> abilities = new List<Ability>();
  59. string sqltxt = "select value(c) from c";
  60. await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").GetItemQueryIterator<Ability>(queryText: sqltxt, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Ability-{standard}") }))
  61. {
  62. abilities.Add(item);
  63. }
  64. return Ok(new { state = 200, abilities });
  65. }
  66. catch (Exception ex)
  67. {
  68. await _dingDing.SendBotMsg($"BI,{_option.Location} /biabilitymgmt/get-abilitysbystandard \n {ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
  69. return BadRequest();
  70. }
  71. }
  72. /// <summary>
  73. /// 依据能力标准的编号查询某个能力标准信息 //已对接
  74. /// </summary>
  75. /// <param name="jsonElement"></param>
  76. /// <returns></returns>
  77. [ProducesDefaultResponseType]
  78. [HttpPost("get-abulityid")]
  79. public async Task<IActionResult> GetAbilityByID(JsonElement jsonElement)
  80. {
  81. try
  82. {
  83. if (!jsonElement.TryGetProperty("standard", out JsonElement standard)) return BadRequest();
  84. if (!jsonElement.TryGetProperty("abilityId", out JsonElement abilityId)) return BadRequest();
  85. jsonElement.TryGetProperty("site", out JsonElement site);
  86. var cosmosClient = _azureCosmos.GetCosmosClient();
  87. if ($"{site}".Equals(BIConst.Global))
  88. cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
  89. Ability ability = await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").ReadItemAsync<Ability>($"{abilityId}", new PartitionKey($"Ability-{standard}"));
  90. return Ok(new { state = 200, ability = ability });
  91. }
  92. catch (Exception ex)
  93. {
  94. await _dingDing.SendBotMsg($"BI,{_option.Location} /biabilitymgmt/get-abulityid \n {ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
  95. return BadRequest();
  96. }
  97. }
  98. /// <summary>
  99. /// 依据区标准点和ID 删除册别 //已对接
  100. /// </summary>
  101. /// <param name="jsonElement"></param>
  102. /// <returns></returns>
  103. [ProducesDefaultResponseType]
  104. [AuthToken(Roles = "admin,rdc,assist,sales")]
  105. [HttpPost("del-ability")]
  106. public async Task<IActionResult> DelAbility(JsonElement jsonElement)
  107. {
  108. try
  109. {
  110. if (!jsonElement.TryGetProperty("standard", out JsonElement standard)) return BadRequest();
  111. if (!jsonElement.TryGetProperty("id", out JsonElement id)) return BadRequest();
  112. jsonElement.TryGetProperty("site", out JsonElement site);
  113. var (_tmdId, _tmdName, pic, did, dname, dpic) = HttpJwtAnalysis.JwtXAuthBI(HttpContext.GetXAuth("AuthToken"), _option);
  114. if (string.IsNullOrEmpty($"{standard}"))
  115. {
  116. return Ok(new { state = 0,message= "standard 参数异常" });
  117. }
  118. var cosmosClient = _azureCosmos.GetCosmosClient();
  119. var tableClient = _azureStorage.GetCloudTableClient();
  120. var blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public");
  121. if ($"{site}".Equals(BIConst.Global))
  122. {
  123. cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
  124. tableClient = _azureStorage.GetCloudTableClient(BIConst.Global);
  125. blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public", BIConst.Global);
  126. }
  127. List<AbilityTask> syllabus = new List<AbilityTask>();
  128. string sql = $"select value(c) from c where c.abilityId='{id}'";
  129. var response = await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").DeleteItemStreamAsync(id.ToString(), new PartitionKey($"Ability-{standard}"));
  130. await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").GetItemQueryIterator<AbilityTask>(queryText: sql, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"AbilityTask-{standard}") }))
  131. {
  132. syllabus.Add(item);
  133. }
  134. if (syllabus.IsNotEmpty())
  135. {
  136. var sresponse = await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").DeleteItemsStreamAsync(syllabus.Select(x => x.id).ToList(), $"AbilityTask-{standard}");
  137. }
  138. if (response.Status == 200)
  139. {
  140. //保存操作记录
  141. //await _azureStorage.SaveBILog("abilityTask-del", $"{_tmdName}【{_tmdId}】删除册别,删除ID:{id}", _dingDing, httpContext: HttpContext);
  142. await AzureStorageBlobExtensions.SaveBILog(blobClient, tableClient, "abilityTask-del", $"{_tmdName}【{_tmdId}】删除册别,删除ID:{id}", _dingDing, httpContext: HttpContext);
  143. return Ok(new { state = 200 });
  144. }
  145. else return Ok(new { state = response.Status });
  146. }
  147. catch (Exception ex)
  148. {
  149. await _dingDing.SendBotMsg($"BI,{_option.Location} /biabilitymgmt/del-ability \n {ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
  150. return BadRequest();
  151. }
  152. }
  153. /// <summary>
  154. /// 更新和新增册别信息 //已经对接
  155. /// </summary>
  156. /// <param name="request"></param>
  157. /// <returns></returns>
  158. [ProducesDefaultResponseType]
  159. [AuthToken(Roles = "admin,rdc,assist,sales")]
  160. [HttpPost("upd-ability")]
  161. public async Task<IActionResult> UpdAbility(Ability ability, [FromHeader] string site)
  162. {
  163. try
  164. {
  165. var (_tmdId, _tmdName, pic, did, dname, dpic) = HttpJwtAnalysis.JwtXAuthBI(HttpContext.GetXAuth("AuthToken"), _option);
  166. StringBuilder stringBuilder = new StringBuilder($"{_tmdName}【{_tmdId}】");
  167. //Ability ability = recordAbility.ability;
  168. ability.pk = "Ability";
  169. ability.ttl = -1;
  170. Ability tempAbility = new Ability();
  171. if (!string.IsNullOrEmpty(ability.code) && !string.IsNullOrEmpty(ability.standard))
  172. {
  173. ability.code = ability.code.StartsWith("Ability-") ? ability.code : $"Ability-{ability.code}";
  174. }
  175. else return Ok(new { state = 0, message = "参数异常" });
  176. var azureClient = _azureCosmos.GetCosmosClient();
  177. var tableClient = _azureStorage.GetCloudTableClient();
  178. var blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public");
  179. if ($"{site}".Equals(BIConst.Global))
  180. {
  181. azureClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
  182. tableClient = _azureStorage.GetCloudTableClient(BIConst.Global);
  183. blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public", BIConst.Global);
  184. }
  185. string tempType = "";
  186. //新增册别
  187. if (string.IsNullOrEmpty(ability.id))
  188. {
  189. ability.comid = Guid.NewGuid().ToString();
  190. StringBuilder sqltxt = new StringBuilder("select value(c) from c where c.status = 1 ");
  191. if (!string.IsNullOrEmpty(ability.dimension))
  192. {
  193. sqltxt.Append($" and c.dimension = '{ability.dimension}' and c.name = '{ability.name}' and c.no = '{ability.no}' ");
  194. List<Ability> abilities = new List<Ability>();
  195. await foreach (var item in azureClient.GetContainer(Constant.TEAMModelOS, "Normal").GetItemQueryIterator<Ability>(queryText: sqltxt.ToString(), requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey(ability.code) }))
  196. {
  197. abilities.Add(item);
  198. }
  199. if (abilities.Count > 0)
  200. {
  201. return Ok(new { state = 1, message = "数据已存在" });
  202. }
  203. }
  204. else
  205. {
  206. return Ok(new { state = 2, mesages = "参数异常" });
  207. }
  208. ability.id = Guid.NewGuid().ToString();
  209. ability.creatorId = string.IsNullOrEmpty($"{ability.creatorId}") ? $"{_tmdId}" : ability.creatorId;
  210. ability.creatorName = string.IsNullOrEmpty($"{ability.creatorName}") ? $"{_tmdName}" : ability.creatorName;
  211. tempAbility = await azureClient.GetContainer("TEAMModelOS", "Normal").CreateItemAsync(ability, new PartitionKey(ability.code));
  212. stringBuilder.Append($"新增册别信息,册别编号:{tempAbility.id},册别名称:{tempAbility.name}");
  213. tempType = "ability-add";
  214. }
  215. //更新册别
  216. else
  217. {
  218. try
  219. {
  220. tempAbility = await azureClient.GetContainer(Constant.TEAMModelOS, "Normal").ReplaceItemAsync<Ability>(ability, ability.id, new PartitionKey(ability.code));
  221. stringBuilder.Append($"更新册别信息,册别编号:{tempAbility.id},册别名称:{tempAbility.name}");
  222. tempType = "ability-update";
  223. }
  224. catch (Exception ex)
  225. {
  226. await _dingDing.SendBotMsg($"BI,{_option.Location} /biabilitymgmt/upd-ability \n {ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
  227. return BadRequest();
  228. }
  229. }
  230. //保存操作记录
  231. //await _azureStorage.SaveBILog(tempType?.ToString(), stringBuilder?.ToString(), _dingDing, httpContext: HttpContext);
  232. await AzureStorageBlobExtensions.SaveBILog(blobClient, tableClient, tempType?.ToString(), stringBuilder?.ToString(), _dingDing, httpContext: HttpContext);
  233. return Ok(new { state = 200, Ability = tempAbility });
  234. }
  235. catch (Exception ex)
  236. {
  237. await _dingDing.SendBotMsg($"BI,{_option.Location} biabilitymgmt/upd-ability \n {ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
  238. return BadRequest();
  239. }
  240. }
  241. /// <summary>
  242. /// 修改是否必修的接口 //已对接
  243. /// </summary>
  244. /// <param name="jsonElement"></param>
  245. /// <returns></returns>
  246. [ProducesDefaultResponseType]
  247. [AuthToken(Roles = "admin,rdc,assist,sales")]
  248. [HttpPost("upd-currency")]
  249. public async Task<IActionResult> UpdateCurrency(JsonElement jsonElement)
  250. {
  251. try
  252. {
  253. if (!jsonElement.TryGetProperty("isRequired", out JsonElement isRequired)) return BadRequest(); //是否是必修集合
  254. jsonElement.TryGetProperty("site", out JsonElement site);
  255. var (_tmdId, _tmdName, pic, did, dname, dpic) = HttpJwtAnalysis.JwtXAuthBI(HttpContext.GetXAuth("AuthToken"), _option);
  256. var currencys = isRequired.ToObject<List<IsRequired>>();
  257. var cosmosClient = _azureCosmos.GetCosmosClient();
  258. var tableClient = _azureStorage.GetCloudTableClient();
  259. var blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public");
  260. if ($"{site}".Equals(BIConst.Global))
  261. {
  262. cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
  263. tableClient = _azureStorage.GetCloudTableClient(BIConst.Global);
  264. blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public", BIConst.Global);
  265. }
  266. foreach (var item in currencys)
  267. {
  268. Ability ability = await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").ReadItemAsync<Ability>(item.abilityId, new PartitionKey($"Ability-{item.standard}"));
  269. ability.currency = item.currency;
  270. await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").ReplaceItemAsync(ability, ability.id, new PartitionKey(ability.code));
  271. }
  272. //保存操作记录
  273. //await _azureStorage.SaveBILog("ability-update", $"{_tmdName}【{_tmdId}】设置是否必修状态。标准:{currencys[0].standard}", _dingDing, httpContext: HttpContext);
  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. }