VolumeController.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. using Azure;
  2. using Azure.Cosmos;
  3. using Microsoft.AspNetCore.Http;
  4. using Microsoft.AspNetCore.Mvc;
  5. using Microsoft.Extensions.Options;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Text.Json;
  11. using System.Threading.Tasks;
  12. using TEAMModelOS.Models;
  13. using TEAMModelOS.SDK.DI;
  14. using TEAMModelOS.SDK.Extension;
  15. using TEAMModelOS.SDK;
  16. using TEAMModelOS.SDK.Models;
  17. using TEAMModelOS.SDK.Models.Cosmos;
  18. using TEAMModelOS.SDK.Models.Cosmos.Common;
  19. using HTEXLib.COMM.Helpers;
  20. namespace TEAMModelOS.Controllers
  21. {
  22. [ProducesResponseType(StatusCodes.Status200OK)]
  23. [ProducesResponseType(StatusCodes.Status400BadRequest)]
  24. //[Authorize(Roles = "IES5")]
  25. [Route("common/volume")]
  26. [ApiController]
  27. public class VolumeController : ControllerBase
  28. {
  29. private readonly AzureCosmosFactory _azureCosmos;
  30. private readonly SnowflakeId _snowflakeId;
  31. private readonly DingDing _dingDing;
  32. private readonly Option _option;
  33. public VolumeController(AzureCosmosFactory azureCosmos, SnowflakeId snowflakeId, DingDing dingDing, IOptionsSnapshot<Option> option)
  34. {
  35. _azureCosmos = azureCosmos;
  36. _snowflakeId = snowflakeId;
  37. _dingDing = dingDing;
  38. _option = option?.Value; ;
  39. }
  40. /*
  41. {
  42. "id": "册别id",
  43. "code": "学校编码/教师编码",
  44. "scope": "school/private"
  45. }
  46. */
  47. /// <summary>
  48. /// 删除册别
  49. /// </summary>
  50. /// <param name="request"></param>
  51. /// <returns></returns>
  52. [ProducesDefaultResponseType]
  53. //[AuthToken(Roles = "Teacher")]
  54. [HttpPost("delete")]
  55. public async Task<IActionResult> Delete(JsonElement request)
  56. {
  57. try
  58. {
  59. if (!request.TryGetProperty("id", out JsonElement id)) return BadRequest();
  60. if (!request.TryGetProperty("code", out JsonElement code)) return BadRequest();
  61. if (!request.TryGetProperty("scope", out JsonElement scope)) return BadRequest();
  62. var client = _azureCosmos.GetCosmosClient();
  63. string sql =$"select value(c) from c where c.volumeId='{id}'";
  64. List<Syllabus> syllabus = new List<Syllabus>();
  65. if (scope.ToString().Equals("school"))
  66. {
  67. var response = await client.GetContainer(Constant.TEAMModelOS, "School").DeleteItemStreamAsync(id.ToString(), new PartitionKey($"Volume-{code}"));
  68. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<Syllabus>(queryText: sql, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Syllabus-{code}") })) {
  69. syllabus.Add(item);
  70. }
  71. if (syllabus.IsNotEmpty())
  72. {
  73. foreach (var s in syllabus) {
  74. if (s.auth.IsNotEmpty()) {
  75. foreach (var a in s.auth) {
  76. try {
  77. Share share = await client.GetContainer(Constant.TEAMModelOS, "Teacher").ReadItemAsync<Share>(s.id, new PartitionKey($"Share-{a.type}-{a.tmdid}"));
  78. share.agree = -1;
  79. await client.GetContainer(Constant.TEAMModelOS, "Teacher").ReplaceItemAsync<Share>(share,s.id, new PartitionKey($"Share-{a.type}-{a.tmdid}"));
  80. } catch (Exception ex) {
  81. ///仅用于处理查不到该数据的问题
  82. }
  83. }
  84. }
  85. }
  86. var sresponse = await client.GetContainer(Constant.TEAMModelOS, "School").DeleteItemsStreamAsync(syllabus.Select(x => x.id).ToList(), $"Syllabus-{code}");
  87. }
  88. return Ok(new { code = response.Status });
  89. }
  90. else
  91. {
  92. var response = await client.GetContainer(Constant.TEAMModelOS, "Teacher").DeleteItemStreamAsync(id.ToString(), new PartitionKey($"Volume-{code}"));
  93. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryIterator<Syllabus>(queryText: sql, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Syllabus-{code}") }))
  94. {
  95. syllabus.Add(item);
  96. }
  97. if (syllabus.IsNotEmpty())
  98. {
  99. foreach (var s in syllabus)
  100. {
  101. if (s.auth.IsNotEmpty())
  102. {
  103. foreach (var a in s.auth)
  104. {
  105. try
  106. {
  107. Share share = await client.GetContainer(Constant.TEAMModelOS, "Teacher").ReadItemAsync<Share>(s.id, new PartitionKey($"Share-{a.type}-{a.tmdid}"));
  108. share.agree = -1;
  109. await client.GetContainer(Constant.TEAMModelOS, "Teacher").ReplaceItemAsync<Share>(share, s.id, new PartitionKey($"Share-{a.type}-{a.tmdid}"));
  110. }
  111. catch (Exception ex)
  112. {
  113. ///仅用于处理查不到该数据的问题
  114. }
  115. }
  116. }
  117. }
  118. var sresponse = await client.GetContainer(Constant.TEAMModelOS, "Teacher").DeleteItemsStreamAsync(syllabus.Select(x => x.id).ToList(), $"Syllabus-{code}");
  119. }
  120. return Ok(new { code = response.Status });
  121. }
  122. }
  123. catch (Exception ex)
  124. {
  125. await _dingDing.SendBotMsg($"OS,{_option.Location},VolumeController: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. public async Task<IActionResult> Find(JsonElement request) {
  147. try {
  148. List<Volume> volumes = new List<Volume>();
  149. request.TryGetProperty("periodId", out JsonElement periodCode);
  150. request.TryGetProperty("subjectId", out JsonElement subjectCode);
  151. request.TryGetProperty("status", out JsonElement status);
  152. if (request.TryGetProperty("code", out JsonElement code))
  153. {
  154. request.TryGetProperty("scope", out JsonElement scope);
  155. //私有课纲
  156. if (scope.GetString() .Equals("private"))
  157. {
  158. await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryIterator<Volume>(queryText: $"select value(c) from c where c.status = {status}", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Volume-{code}") }))
  159. {
  160. volumes.Add(item);
  161. }
  162. }
  163. else if (scope.GetString() .Equals("school"))
  164. {
  165. await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<Volume>(queryText: $"select value(c) from c where c.status = {status} and c.periodId = '{periodCode}' and c.subjectId = '{subjectCode}'", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Volume-{code}") }))
  166. {
  167. volumes.Add(item);
  168. }
  169. }
  170. }
  171. else { return BadRequest(); };
  172. return Ok(new { volumes });
  173. } catch (Exception ex) {
  174. await _dingDing.SendBotMsg($"OS,{_option.Location},VolumeController:find\n{ex.Message}{ex.StackTrace}", GroupNames.成都开发測試群組);
  175. return BadRequest();
  176. }
  177. }
  178. /*
  179. {
  180. "id": "册别id",
  181. "code": "学校编码或教师id",
  182. "periodId": "学段id",
  183. "subjectId": "学科id",
  184. "gradeId": "年级id",
  185. "semesterId": "学期id",
  186. "status": 1,
  187. "name": "册别名",
  188. "creatorId": "创建者id",
  189. "school": "学校编码",
  190. "scope": "school|private"
  191. }
  192. */
  193. /// <summary>
  194. /// 新增册别
  195. /// </summary>
  196. /// <param name="request"></param>
  197. /// <returns></returns>
  198. [ProducesDefaultResponseType]
  199. //[AuthToken(Roles = "Teacher")]
  200. [HttpPost("upsert")]
  201. public async Task<IActionResult> Upsert(Volume request) {
  202. //var client = _azureCosmos.GetCosmosClient();
  203. //if (request.editors != null && request.editors.Count > 5)
  204. //{
  205. // return BadRequest("共编人数大于5人!");
  206. // // throw new BizException("共编人数大于5人!");
  207. //}
  208. request.pk = "Volume";
  209. request.ttl = -1;
  210. request.code = "Volume-" + request.code;
  211. // 检查册别条件相同的是否存在
  212. string code = "Volume-";
  213. if (request.scope.Equals("private"))
  214. {
  215. code = $"Volume-{request.creatorId}";
  216. }
  217. else if (request.scope.Equals("school"))
  218. {
  219. code = $"Volume-{request.school}";
  220. }
  221. request.code = code;
  222. ///表示更新
  223. if (!string.IsNullOrEmpty(request.id))
  224. {
  225. try {
  226. if (request.scope.Equals("school"))
  227. {
  228. await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "School").ReplaceItemAsync<Volume>(request, request.id, new Azure.Cosmos.PartitionKey(request.code));
  229. }
  230. else if (request.scope.Equals("private")) {
  231. await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "Teacher").ReplaceItemAsync<Volume>(request, request.id, new Azure.Cosmos.PartitionKey(request.code));
  232. }
  233. }
  234. catch (Exception ex) {
  235. return BadRequest(new { error = ResponseCode.FAILED });
  236. }
  237. }
  238. //表示新增,则需要检查是否重复
  239. else {
  240. try
  241. {
  242. StringBuilder sql = new StringBuilder("select value(c) from c where c.status = 1 ");
  243. //私人课纲 只检查是否重名
  244. if (request.scope.Equals("private"))
  245. {
  246. sql.Append($" and c.name = '{request.name}' ");
  247. List<Volume> volumes = new List<Volume>();
  248. await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "Teacher")
  249. .GetItemQueryIterator<Volume>(queryText: sql.ToString(), requestOptions: new Azure.Cosmos.QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey(request.code) }))
  250. {
  251. volumes.Add(item);
  252. }
  253. if (volumes.Count > 0)
  254. {
  255. return Ok(new { error = ResponseCode.DATA_EXIST });
  256. }
  257. }
  258. //学校课纲检查 学段 科目 年级 学期
  259. else if (request.scope.Equals("school")
  260. && !string.IsNullOrEmpty(request.periodId)
  261. && !string.IsNullOrEmpty(request.subjectId)
  262. && request.gradeId>=0
  263. && !string.IsNullOrEmpty(request.semesterId))
  264. {
  265. sql.Append($" and c.periodId = '{request.periodId}' and c.subjectId = '{request.subjectId}'" +
  266. $" and c.gradeId = {request.gradeId} and c.semesterId = '{request.semesterId}' and c.name = '{request.name}' ");
  267. List<Volume> volumes = new List<Volume>();
  268. await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "School")
  269. .GetItemQueryIterator<Volume>(queryText: sql.ToString(), requestOptions: new Azure.Cosmos.QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey(request.code) })) {
  270. volumes.Add(item);
  271. }
  272. if (volumes.Count>0)
  273. {
  274. return Ok(new { error = ResponseCode.DATA_EXIST });
  275. }
  276. }
  277. else {
  278. return BadRequest(new { error = ResponseCode.PARAMS_ERROR });
  279. }
  280. request.id = System.Guid.NewGuid().ToString();
  281. if (request.scope.Equals("private"))
  282. {
  283. await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "Teacher").CreateItemAsync(request, new PartitionKey(request.code));
  284. }
  285. else {
  286. await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "School").CreateItemAsync(request, new PartitionKey(request.code));
  287. }
  288. }
  289. catch (Exception ex)
  290. {
  291. await _dingDing.SendBotMsg($"OS,{_option.Location},VolumeController:Upsert\n{ex.Message}{ex.StackTrace}", GroupNames.成都开发測試群組);
  292. }
  293. }
  294. ///处理更新 分享及共编的数据
  295. //if (request.auth.IsNotEmpty()) {
  296. // long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
  297. // foreach (var au in request.auth) {
  298. // var share= new Share
  299. // {
  300. // id = request.id,
  301. // code = $"Share-{au.tmdid}",
  302. // pk = "Share",
  303. // ttl = -1,
  304. // scode=request.code,
  305. // issuer=request.creatorId,
  306. // createTime= now,
  307. // school=request.school,
  308. // scope=request.scope
  309. // };
  310. // await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "Teacher").UpsertItemAsync(share, new PartitionKey($"{share.code}"));
  311. // }
  312. //}
  313. return Ok(request);
  314. }
  315. }
  316. }