AbilityMgmtController.cs 13 KB

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