VolumeController.cs 15 KB

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