VolumeController.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. if (scope.ToString().Equals("school"))
  63. {
  64. var response = await client.GetContainer("TEAMModelOS", "School").DeleteItemStreamAsync(id.ToString(), new PartitionKey($"Volume-{code}"));
  65. return Ok(new { code = response.Status });
  66. }
  67. else
  68. {
  69. var response = await client.GetContainer("TEAMModelOS", "Teacher").DeleteItemStreamAsync(id.ToString(), new PartitionKey($"Volume-{code}"));
  70. return Ok(new { code = response.Status });
  71. }
  72. }
  73. catch (Exception e)
  74. {
  75. return BadRequest();
  76. }
  77. }
  78. /*
  79. {
  80. "periodId": "学段id",
  81. "subjectId": "科目id",//
  82. "status": 1,//状态
  83. "code": "学校编码或者tmdid",
  84. "scope": "school/private" 如果是私人课纲 则学段科目id可以为空
  85. }
  86. */
  87. /// <summary>
  88. /// 查找册别
  89. /// </summary>
  90. /// <param name="request"></param>
  91. /// <returns></returns>
  92. [ProducesDefaultResponseType]
  93. //[AuthToken(Roles = "Teacher")]
  94. [HttpPost("find")]
  95. public async Task<IActionResult> Find(JsonElement request) {
  96. // List<Syllabus> syllabuses = new List<Syllabus>();
  97. List<Volume> volumes = new List<Volume>();
  98. request.TryGetProperty("periodId", out JsonElement periodCode);
  99. request.TryGetProperty("subjectId", out JsonElement subjectCode);
  100. request.TryGetProperty("status", out JsonElement status);
  101. if (request.TryGetProperty("code", out JsonElement code))
  102. {
  103. request.TryGetProperty("scope", out JsonElement scope);
  104. //私有课纲
  105. if (scope.GetString()== "private")
  106. {
  107. //await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "Teacher").GetItemQueryStreamIterator(queryText: $"select value(c) from c ", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Syllabus-{code}") }))
  108. //{
  109. // using var json = await JsonDocument.ParseAsync(item.ContentStream);
  110. // if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  111. // {
  112. // foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  113. // {
  114. // syllabuses.Add(obj.ToObject<Syllabus>());
  115. // }
  116. // }
  117. //}
  118. 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}") }))
  119. {
  120. volumes.Add(item);
  121. //using var json = await JsonDocument.ParseAsync(item.ContentStream);
  122. //if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  123. //{
  124. // foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  125. // {
  126. // volumes.Add(obj.ToObject<Volume>());
  127. // }
  128. //}
  129. }
  130. }
  131. else if(scope.GetString() == "school") {
  132. //await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "School").GetItemQueryStreamIterator(queryText: $"select value(c) from c ", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Syllabus-{code}") }))
  133. //{
  134. // using var json = await JsonDocument.ParseAsync(item.ContentStream);
  135. // if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  136. // {
  137. // foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  138. // {
  139. // syllabuses.Add(obj.ToObject<Syllabus>());
  140. // }
  141. // }
  142. //}
  143. 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}") }))
  144. {
  145. volumes.Add(item);
  146. //using var json = await JsonDocument.ParseAsync(item.ContentStream);
  147. //if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  148. //{
  149. // foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  150. // {
  151. // volumes.Add(obj.ToObject<Volume>());
  152. // }
  153. //}
  154. }
  155. }
  156. }
  157. else { return BadRequest(); };
  158. return Ok(new { volumes });
  159. }
  160. /*
  161. {
  162. "id": "册别id",
  163. "code": "学校编码或教师id",
  164. "periodId": "学段id",
  165. "subjectId": "学科id",
  166. "gradeId": "年级id",
  167. "semesterId": "学期id",
  168. "status": 1,
  169. "name": "册别名",
  170. "creatorId": "创建者id",
  171. "school": "学校编码",
  172. "scope": "school|private"
  173. }
  174. */
  175. /// <summary>
  176. /// 新增册别
  177. /// </summary>
  178. /// <param name="request"></param>
  179. /// <returns></returns>
  180. [ProducesDefaultResponseType]
  181. //[AuthToken(Roles = "Teacher")]
  182. [HttpPost("upsert")]
  183. public async Task<IActionResult> Upsert(Volume request) {
  184. //var client = _azureCosmos.GetCosmosClient();
  185. //if (request.editors != null && request.editors.Count > 5)
  186. //{
  187. // return BadRequest("共编人数大于5人!");
  188. // // throw new BizException("共编人数大于5人!");
  189. //}
  190. request.pk = "Volume";
  191. request.ttl = -1;
  192. request.code = "Volume-" + request.code;
  193. // 检查册别条件相同的是否存在
  194. string code = "Volume-";
  195. if (request.scope.Equals("private"))
  196. {
  197. code = $"Volume-{request.creatorId}";
  198. }
  199. else if (request.scope.Equals("school"))
  200. {
  201. code = $"Volume-{request.school}";
  202. }
  203. request.code = code;
  204. ///表示更新
  205. if (!string.IsNullOrEmpty(request.id))
  206. {
  207. try {
  208. Volume volume = await _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "Teacher").ReadItemAsync<Volume>(request.id, new PartitionKey(request.code));
  209. //保留授权
  210. request.auth = volume.auth;
  211. await _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "School").ReplaceItemAsync<Volume>(request,request.id, new Azure.Cosmos.PartitionKey(request.code));
  212. }
  213. catch (Exception ex) {
  214. return BadRequest(new { error = ResponseCode.FAILED });
  215. }
  216. }
  217. //表示新增,则需要检查是否重复
  218. else {
  219. try
  220. {
  221. StringBuilder sql = new StringBuilder("select value(c) from c where c.status = 1 ");
  222. //私人课纲 只检查是否重名
  223. if (request.scope.Equals("private"))
  224. {
  225. sql.Append($" and c.name = '{request.name}' ");
  226. List<Volume> volumes = new List<Volume>();
  227. await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "Teacher")
  228. .GetItemQueryIterator<Volume>(queryText: sql.ToString(), requestOptions: new Azure.Cosmos.QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey(request.code) }))
  229. {
  230. volumes.Add(item);
  231. }
  232. if (volumes.Count > 0)
  233. {
  234. return BadRequest(new { error = ResponseCode.DATA_EXIST });
  235. }
  236. }
  237. //学校课纲检查 学段 科目 年级 学期
  238. else if (request.scope.Equals("school")
  239. && !string.IsNullOrEmpty(request.periodId)
  240. && !string.IsNullOrEmpty(request.subjectId)
  241. && !string.IsNullOrEmpty(request.gradeId)
  242. && !string.IsNullOrEmpty(request.semesterId))
  243. {
  244. sql.Append($" and c.periodId = '{request.periodId}' and c.subjectId = '{request.subjectId}'" +
  245. $" and c.gradeId = '{request.gradeId}' and c.semesterId = '{request.semesterId}' and c.name = '{request.name}' ");
  246. List<Volume> volumes = new List<Volume>();
  247. await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "School")
  248. .GetItemQueryIterator<Volume>(queryText: sql.ToString(), requestOptions: new Azure.Cosmos.QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey(request.code) })) {
  249. volumes.Add(item);
  250. }
  251. if (volumes.Count>0)
  252. {
  253. return BadRequest(new { error = ResponseCode.DATA_EXIST });
  254. }
  255. }
  256. else {
  257. return BadRequest(new { error = ResponseCode.PARAMS_ERROR });
  258. }
  259. request.id = System.Guid.NewGuid().ToString();
  260. if (request.scope.Equals("private"))
  261. {
  262. await _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "Teacher").CreateItemAsync(request, new PartitionKey(request.code));
  263. }
  264. else {
  265. await _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "School").CreateItemAsync(request, new PartitionKey(request.code));
  266. }
  267. }
  268. catch (Exception ex)
  269. {
  270. await _dingDing.SendBotMsg($"OS,{_option.Location},VolumeController:Upsert\n{ex.Message}{ex.StackTrace}", GroupNames.成都开发測試群組);
  271. }
  272. }
  273. ///处理更新 分享及共编的数据
  274. //if (request.auth.IsNotEmpty()) {
  275. // long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
  276. // foreach (var au in request.auth) {
  277. // var share= new Share
  278. // {
  279. // id = request.id,
  280. // code = $"Share-{au.tmdid}",
  281. // pk = "Share",
  282. // ttl = -1,
  283. // scode=request.code,
  284. // issuer=request.creatorId,
  285. // createTime= now,
  286. // school=request.school,
  287. // scope=request.scope
  288. // };
  289. // await _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "Teacher").UpsertItemAsync(share, new PartitionKey($"{share.code}"));
  290. // }
  291. //}
  292. return Ok(request);
  293. }
  294. }
  295. }