AbilityController.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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.Linq;
  8. using System.Text;
  9. using System.Text.Json;
  10. using System.Threading.Tasks;
  11. using TEAMModelOS.Models;
  12. using TEAMModelOS.SDK.DI;
  13. using TEAMModelOS.SDK;
  14. using TEAMModelOS.SDK.Models;
  15. using HTEXLib.COMM.Helpers;
  16. using TEAMModelOS.Filter;
  17. namespace TEAMModelOS.Controllers.Research
  18. {
  19. [ProducesResponseType(StatusCodes.Status200OK)]
  20. [ProducesResponseType(StatusCodes.Status400BadRequest)]
  21. //[Authorize(Roles = "IES5")]
  22. [Route("research/ability")]
  23. [ApiController]
  24. public class AbilityController : ControllerBase
  25. {
  26. private readonly AzureCosmosFactory _azureCosmos;
  27. private readonly SnowflakeId _snowflakeId;
  28. private readonly DingDing _dingDing;
  29. private readonly Option _option;
  30. public AbilityController(AzureCosmosFactory azureCosmos, SnowflakeId snowflakeId, DingDing dingDing, IOptionsSnapshot<Option> option)
  31. {
  32. _azureCosmos = azureCosmos;
  33. _snowflakeId = snowflakeId;
  34. _dingDing = dingDing;
  35. _option = option?.Value; ;
  36. }
  37. /*
  38. {"abilityId":"册别id:0baf00db-0768-4b62-a8f7-280f6bcebf71","scope":"school","abilityCode":"册别分区键"}
  39. */
  40. /// <summary>
  41. /// 查找能力点
  42. /// </summary>
  43. /// <param name="request"></param>
  44. /// <returns></returns>
  45. //[ProducesDefaultResponseType]
  46. //[HttpPost("mark-uncount-taskid")]
  47. //[AuthToken(Roles = "area")]
  48. //public async Task<IActionResult> MarkUncountTaskid(JsonElement request) {
  49. // if (!request.TryGetProperty("standard", out JsonElement standard)) return BadRequest();
  50. // var client = _azureCosmos.GetCosmosClient();
  51. // if (!request.TryGetProperty("abilityId", out JsonElement abilityId)) return BadRequest();
  52. // if (!request.TryGetProperty("taskId", out JsonElement taskId)) return BadRequest();
  53. //}
  54. /*
  55. {"abilityId":"册别id:0baf00db-0768-4b62-a8f7-280f6bcebf71","scope":"school","abilityCode":"册别分区键"}
  56. */
  57. /// <summary>
  58. /// 查找能力点
  59. /// </summary>
  60. /// <param name="request"></param>
  61. /// <returns></returns>
  62. [ProducesDefaultResponseType]
  63. [HttpPost("find-id")]
  64. [AuthToken(Roles = "teacher,student,admin,area")]
  65. public async Task<IActionResult> FindId(JsonElement request)
  66. {
  67. //if (!HttpContext.Items.TryGetValue("Standard", out object standard)) return BadRequest();
  68. if (!request.TryGetProperty("standard", out JsonElement standard)) return BadRequest();
  69. var client = _azureCosmos.GetCosmosClient();
  70. if (!request.TryGetProperty("abilityId", out JsonElement abilityId)) return BadRequest();
  71. //if (!request.TryGetProperty("schoolCode", out JsonElement schoolCode)) return BadRequest();
  72. // if (!request.TryGetProperty("scope", out JsonElement scope)) return BadRequest();
  73. Ability ability = null;
  74. try
  75. {
  76. ability = await client.GetContainer("TEAMModelOS", "Normal").ReadItemAsync<Ability>($"{abilityId}", new PartitionKey($"Ability-{standard}"));
  77. return Ok(new { ability = ability });
  78. }
  79. catch (Exception ex)
  80. {
  81. return Ok(new { ability = ability });
  82. }
  83. }
  84. /*
  85. {
  86. "id": "册别id",
  87. "code": "学校编码/教师编码",
  88. "scope": "school/private"
  89. }
  90. */
  91. /// <summary>
  92. /// 删除技能点
  93. /// </summary>
  94. /// <param name="request"></param>
  95. /// <returns></returns>
  96. [ProducesDefaultResponseType]
  97. //[AuthToken(Roles = "Teacher")]
  98. [HttpPost("delete")]
  99. [AuthToken(Roles = "area")]
  100. public async Task<IActionResult> Delete(JsonElement request)
  101. {
  102. try
  103. {
  104. //if (!HttpContext.Items.TryGetValue("Standard", out object standard)) return BadRequest();
  105. if (!request.TryGetProperty("standard", out JsonElement standard)) return BadRequest();
  106. if (!request.TryGetProperty("id", out JsonElement id)) return BadRequest();
  107. //if (!request.TryGetProperty("code", out JsonElement code)) return BadRequest();
  108. // if (!request.TryGetProperty("scope", out JsonElement scope)) return BadRequest();
  109. var client = _azureCosmos.GetCosmosClient();
  110. string sql = $"select value(c) from c where c.abilityId='{id}'";
  111. List<AbilityTask> syllabus = new List<AbilityTask>();
  112. var response = await client.GetContainer("TEAMModelOS", "Normal").DeleteItemStreamAsync(id.ToString(), new PartitionKey($"Ability-{standard}"));
  113. await foreach (var item in client.GetContainer("TEAMModelOS", "Normal").GetItemQueryIterator<AbilityTask>(queryText: sql, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"AbilityTask-{standard}") }))
  114. {
  115. syllabus.Add(item);
  116. }
  117. if (syllabus.IsNotEmpty())
  118. {
  119. var sresponse = await client.GetContainer("TEAMModelOS", "Normal").DeleteItemsStreamAsync(syllabus.Select(x => x.id).ToList(), $"AbilityTask-{standard}");
  120. }
  121. return Ok(new { code = response.Status });
  122. }
  123. catch (Exception ex)
  124. {
  125. await _dingDing.SendBotMsg($"OS,{_option.Location},AbilityController:Delete\n{ex.Message}{ex.StackTrace}", GroupNames.成都开发測試群組);
  126. return BadRequest();
  127. }
  128. }
  129. /*
  130. {
  131. "periodId": "学段id",
  132. "subjectId": "科目id",//
  133. "status": 1,//状态
  134. "code": "学校编码或者tmdid",
  135. "scope": "school/private" 如果是私人课纲 则学段科目id可以为空
  136. }
  137. */
  138. /// <summary>
  139. /// 查找册别
  140. /// </summary>
  141. /// <param name="request"></param>
  142. /// <returns></returns>
  143. [ProducesDefaultResponseType]
  144. //[AuthToken(Roles = "Teacher")]
  145. [HttpPost("find")]
  146. [AuthToken(Roles = "teacher,student,admin,area")]
  147. public async Task<IActionResult> Find(JsonElement request)
  148. {
  149. try
  150. {
  151. //if (!HttpContext.Items.TryGetValue("Standard", out object standard)) return BadRequest();
  152. if (!request.TryGetProperty("standard", out JsonElement standard)) return BadRequest();
  153. List<Ability> abilities = new List<Ability>();
  154. request.TryGetProperty("dimension", out JsonElement _dimension);
  155. StringBuilder sql = new StringBuilder("select value(c) from c ");
  156. if (!string.IsNullOrEmpty($"{_dimension}")) {
  157. sql.Append($" where c.dimension='{_dimension}' ");
  158. }
  159. await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "Normal")
  160. .GetItemQueryIterator<Ability>(queryText: sql.ToString(), requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Ability-{standard}") }))
  161. {
  162. abilities.Add(item);
  163. }
  164. return Ok(new { abilities });
  165. }
  166. catch (Exception ex)
  167. {
  168. await _dingDing.SendBotMsg($"OS,{_option.Location},AbilityController:find\n{ex.Message}{ex.StackTrace}", GroupNames.成都开发測試群組);
  169. return BadRequest();
  170. }
  171. }
  172. /*
  173. {
  174. "id": "册别id",
  175. "code": "学校编码或教师id",
  176. "periodId": "学段id",
  177. "subjectId": "学科id",
  178. "gradeId": "年级id",
  179. "semesterId": "学期id",
  180. "status": 1,
  181. "name": "册别名",
  182. "creatorId": "创建者id",
  183. "school": "学校编码",
  184. "scope": "school|private"
  185. }
  186. */
  187. /// <summary>
  188. /// 新增册别
  189. /// </summary>
  190. /// <param name="request"></param>
  191. /// <returns></returns>
  192. [ProducesDefaultResponseType]
  193. //[AuthToken(Roles = "Teacher")]
  194. [HttpPost("upsert")]
  195. [AuthToken(Roles = "area")]
  196. public async Task<IActionResult> Upsert(Ability request)
  197. {
  198. //if (!HttpContext.Items.TryGetValue("Standard", out object standard)) return BadRequest();
  199. //if (!request.TryGetProperty("standard", out JsonElement standard)) return BadRequest();
  200. request.pk = "Ability";
  201. request.ttl = -1;
  202. if (!string.IsNullOrEmpty(request.code))
  203. {
  204. request.code = request.code.StartsWith("Ability-") ? request.code : $"Ability-{request.code}";
  205. }
  206. else {
  207. return BadRequest("参数异常");
  208. }
  209. // 检查册别条件相同的是否存在
  210. ///表示更新
  211. if (!string.IsNullOrEmpty(request.id))
  212. {
  213. try
  214. {
  215. await _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "Normal").ReplaceItemAsync<Ability>(request, request.id, new Azure.Cosmos.PartitionKey(request.code));
  216. }
  217. catch (Exception ex)
  218. {
  219. return BadRequest(new { error = ResponseCode.FAILED });
  220. }
  221. }
  222. //表示新增,则需要检查是否重复
  223. else
  224. {
  225. try
  226. {
  227. request.comid = Guid.NewGuid().ToString();
  228. StringBuilder sql = new StringBuilder("select value(c) from c where c.status = 1 ");
  229. if ( !string.IsNullOrEmpty(request.dimension))
  230. {
  231. sql.Append($" and c.dimension = '{request.dimension}' and c.name = '{request.name}' ");
  232. List<Ability> volumes = new List<Ability>();
  233. await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "Normal")
  234. .GetItemQueryIterator<Ability>(queryText: sql.ToString(), requestOptions: new Azure.Cosmos.QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey(request.code) }))
  235. {
  236. volumes.Add(item);
  237. }
  238. if (volumes.Count > 0)
  239. {
  240. return Ok(new { error = ResponseCode.DATA_EXIST });
  241. }
  242. }
  243. else
  244. {
  245. return BadRequest(new { error = ResponseCode.PARAMS_ERROR });
  246. }
  247. request.id = System.Guid.NewGuid().ToString();
  248. await _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "Normal").CreateItemAsync(request, new PartitionKey(request.code));
  249. }
  250. catch (Exception ex)
  251. {
  252. await _dingDing.SendBotMsg($"OS,{_option.Location},AbilityController:Upsert\n{ex.Message}{ex.StackTrace}", GroupNames.成都开发測試群組);
  253. }
  254. }
  255. return Ok(request);
  256. }
  257. }
  258. }