123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375 |
- using Microsoft.AspNetCore.Mvc;
- using System.Collections.Generic;
- using System.Threading.Tasks;
- using TEAMModelOS.SDK;
- using TEAMModelOS.SDK.DI;
- using TEAMModelOS.SDK.Helper.Common.CollectionHelper;
- using System;
- using TEAMModelOS.SDK.Helper.Common.JsonHelper;
- using System.Linq;
- using TEAMModelOS.Models;
- using System.Text.Json;
- using TEAMModelOS.SDK.Extension;
- using TEAMModelOS.SDK.Models;
- using Microsoft.AspNetCore.Http;
- using TEAMModelOS.SDK.Models.Cosmos.Common;
- using Azure.Cosmos;
- using TEAMModelOS.Services.Common;
- namespace TEAMModelOS.Controllers
- {
- [ProducesResponseType(StatusCodes.Status200OK)]
- [ProducesResponseType(StatusCodes.Status400BadRequest)]
- //[Authorize(Roles = "IES5")]
- [Route("common/syllabus")]
- [ApiController]
- //[Authorize]
- public class SyllabusController : ControllerBase
- {
- private readonly AzureCosmosFactory _azureCosmos;
- public SyllabusController(AzureCosmosFactory azureCosmos)
- {
- _azureCosmos = azureCosmos;
- }
- /*
- {
- "id": "0baf00db-0768-4b62-a8f7-280f6bcebf71",
- "scope": "school",
- "trees": [
- {
- "children": [
- {
- "children": [],
- "id": "AC4BA269-541B-4DFC-92A5-D773068A6439",
- "pid": "2dfcc62e-8eea-9881-dc79-384b2f0afbec",
- "order": 0,
- "rnodes": [
- {
- "type": "doc",
- "id": "a2bee388-5584-72cc-1d9a-d8a77d255364",
- "code": "hbcn",
- "scope": "private",
- "cntr": "1595321354",
- "link": [
- "https://teammodelstorage.blob.core.chinacloudapi.cn/1595321354/doc/2020智慧課堂與智慧教研.pptx"
- ],
- "title": "2020智慧課堂與智慧教研.pptx"
- }
- ],
- "cids": [],
- "creatorId": null,
- "updateTime": 0,
- "title": "1-1 新冠疫情小贴士"
- }
- ],
- "id": "2dfcc62e-8eea-9881-dc79-384b2f0afbec",
- "pid": "0baf00db-0768-4b62-a8f7-280f6bcebf71",
- "order": 0,
- "rnodes": [
- {
- "type": "doc",
- "id": "cf1b4d21-66e1-e6c7-c944-30a03e419fa6",
- "code": "hbcn",
- "scope": "school",
- "cntr": "hbcn",
- "link": [
- "https://teammodelstorage.blob.core.chinacloudapi.cn/hbcn/syllabus/IES5试卷模板制作说明(终).pdf"
- ],
- "title": "IES5试卷模板制作说明(终).pdf"
- },
- {
- "type": "doc",
- "id": "f3e82595-7340-a5fe-1004-04538ca09b86",
- "code": "hbcn",
- "scope": "school",
- "cntr": "hbcn",
- "link": [
- "https://teammodelstorage.blob.core.chinacloudapi.cn/hbcn/syllabus/111.pdf"
- ],
- "title": "111.pdf"
- },
- {
- "type": "doc",
- "id": "e1b31639-dad9-9efb-020b-159dd045f238",
- "code": "hbcn",
- "scope": "school",
- "cntr": "hbcn",
- "link": [
- "https://teammodelstorage.blob.core.chinacloudapi.cn/hbcn/doc/6789.pdf"
- ],
- "title": "6789.pdf"
- },
- {
- "type": "doc",
- "id": "aaeb4b5c-0450-cb4d-a1ac-244f3d115c4a",
- "code": "hbcn",
- "scope": "private",
- "cntr": "1595321354",
- "link": [
- "https://teammodelstorage.blob.core.chinacloudapi.cn/1595321354/doc/422北京培新活动邀请函.pdf"
- ],
- "title": "422北京培新活动邀请函.pdf"
- },
- {
- "type": "doc",
- "id": "c5f5838c-5c2f-5e9b-a734-a473cd2cad2e",
- "code": "hbcn",
- "scope": "school",
- "cntr": "hbcn",
- "link": [
- "https://teammodelstorage.blob.core.chinacloudapi.cn/hbcn/doc/6666.pdf"
- ],
- "title": "6666.pdf"
- },
- {
- "type": "doc",
- "id": "4a106c8e-6831-5f4b-4ec3-3c18f778cab2",
- "code": "hbcn",
- "scope": "school",
- "cntr": "hbcn",
- "link": [
- "https://teammodelstorage.blob.core.chinacloudapi.cn/hbcn/doc/6.pdf"
- ],
- "title": "6.pdf"
- }
- ],
- "cids": [
- "AC4BA269-541B-4DFC-92A5-D773068A6439"
- ],
- "creatorId": null,
- "updateTime": 0,
- "title": "第一单元 新冠疫情防控"
- },
- {
- "children": [
- {
- "children": [],
- "id": "FC5132E1-9723-4875-B3B5-D3DC98D194FA",
- "pid": "4f12752c-852f-6e90-a3df-1f1f710af23d",
- "order": 0,
- "rnodes": [],
- "cids": [],
- "creatorId": null,
- "updateTime": 0,
- "title": "2-1 全球醍摩豆智慧教育研究院"
- },
- {
- "children": [],
- "id": "3CAD52BE-67B6-4EDB-8EFB-9122960D816A",
- "pid": "4f12752c-852f-6e90-a3df-1f1f710af23d",
- "order": 1,
- "rnodes": [],
- "cids": [],
- "creatorId": null,
- "updateTime": 0,
- "title": "2-2 醍摩豆研究院课程体系"
- }
- ],
- "id": "4f12752c-852f-6e90-a3df-1f1f710af23d",
- "pid": "0baf00db-0768-4b62-a8f7-280f6bcebf71",
- "order": 1,
- "rnodes": [],
- "cids": [
- "FC5132E1-9723-4875-B3B5-D3DC98D194FA",
- "3CAD52BE-67B6-4EDB-8EFB-9122960D816A"
- ],
- "creatorId": null,
- "updateTime": 0,
- "title": "第二单元 醍摩豆手册学习"
- }
- ]
- }
- */
- /// <summary>
- /// 批量保存或更新课纲
- ///
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- [ProducesDefaultResponseType]
- [HttpPost("upsert-tree")]
- public async Task<IActionResult> SaveOrUpdateAsTree(SyllabusTreeNode request)
- {
- List<List<SyllabusTree>> tts = new List<List<SyllabusTree>>();
- foreach (var tree in request.trees) {
- Syllabus syllabus = new Syllabus();
- List<Tnode> nodes = new List<Tnode>();
- SyllabusService.TreeToList(new List<SyllabusTree> { tree }, nodes);
- syllabus.children = nodes;
- syllabus.id = !string.IsNullOrEmpty(tree.id) ? tree.id : Guid.NewGuid().ToString();
- syllabus.code = $"Syllabus-{request.id}";
- syllabus.pk = "Syllabus";
- syllabus.ttl = -1;
- syllabus.volumeId = request.id;
- if (request.scope == "school")
- {
- await _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "School").UpsertItemAsync<Syllabus>(syllabus, new Azure.Cosmos.PartitionKey($"Syllabus-{request.id}"));
- }
- else
- {
- await _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "Teacher").UpsertItemAsync<Syllabus>(syllabus, new Azure.Cosmos.PartitionKey($"Syllabus-{request.id}"));
- }
- List<SyllabusTree> trees = SyllabusService.ListToTree(nodes);
- tts.Add(trees);
- }
- request.trees = new List<SyllabusTree>() ;
- tts.ForEach(x => request.trees.AddRange(x));
- return Ok(request);
- }
-
- /// <summary>
- /// 查找课纲
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- [ProducesDefaultResponseType]
- [HttpPost("find-id")]
- public async Task<IActionResult> Find(JsonElement request)
- {
- var client = _azureCosmos.GetCosmosClient();
- if (!request.TryGetProperty("code", out JsonElement code)) return BadRequest();
- if (!request.TryGetProperty("scope", out JsonElement scope)) return BadRequest();
- SyllabusTreeNode tree = null;
- string vcode = null;
- List<List<SyllabusTree>> tts = new List<List<SyllabusTree>>();
- try {
- if (scope.ToString().Equals("school"))
- {
- await foreach (var item in client.GetContainer("TEAMModelOS", "School").GetItemQueryIterator<Syllabus>(queryText: $"select value(c) from c ",
- requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"Syllabus-{code}") }))
- {
- vcode = item.volumeId;
- List<SyllabusTree> trees = SyllabusService.ListToTree(item.children);
- tts.Add(trees);
- }
- tree = new SyllabusTreeNode() { id = vcode, scope = "school", trees= new List<SyllabusTree>() };
- tts.ForEach(x => tree.trees.AddRange(x));
- }
- else
- {
- await foreach (var item in client.GetContainer("TEAMModelOS", "Teacher").GetItemQueryIterator<Syllabus>(queryText: $"select value(c) from c ",
- requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"Syllabus-{code}") }))
- {
- vcode = item.volumeId;
- List<SyllabusTree> trees = SyllabusService.ListToTree(item.children);
- tts.Add(trees);
- }
- tree = new SyllabusTreeNode() { id = vcode, scope = "school", trees = new List<SyllabusTree>() };
- tts.ForEach(x => tree.trees.AddRange(x));
- }
- return Ok(new { tree });
- } catch (Exception ex) {
- return Ok(new { tree});
- }
- }
- /// <summary>
- /// 删除章节
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- [ProducesDefaultResponseType]
- [HttpPost("delete")]
- public async Task<IActionResult> Delete(JsonElement request)
- {
- if (!request.TryGetProperty("id", out JsonElement id)) return BadRequest();
- if (!request.TryGetProperty("code", out JsonElement code)) return BadRequest();
- if (!request.TryGetProperty("scope", out JsonElement scope)) return BadRequest();
- var client = _azureCosmos.GetCosmosClient();
- if (scope.ToString().Equals("school"))
- {
- var response = await client.GetContainer("TEAMModelOS", "School").DeleteItemStreamAsync(id.ToString(), new PartitionKey($"Syllabus-{code}"));
- return Ok(new { code = response.Status });
- }
- else
- {
- var response = await client.GetContainer("TEAMModelOS", "Teacher").DeleteItemStreamAsync(id.ToString(), new PartitionKey($"Syllabus-{code}"));
- return Ok(new { code = response.Status });
- }
- }
-
- private async Task<List<Tnode>> FindByPid(Tnode data, List<Tnode> nodes, List<Tnode> nodedata)
- {
- foreach (Tnode syllabus in nodedata)
- {
- if (syllabus.pid == data.id)
- {
- nodes.Add(syllabus);
- await FindByPid(syllabus, nodes, nodedata);
- }
- }
- return nodes;
- }
-
- //private async Task<List<Syllabus>> SaveOrUpdateToNodes(List<Tnode> syllabusNodes)
- //{
- // List<Syllabus> syllabuses;
- // var data = await _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "School").ReadItemAsync<Syllabus>( syllabusNodes[0].code ,new Azure.Cosmos.PartitionKey(""));
- // if (data.IsEmpty())
- // {
- // var syllabus = new Syllabus { id = syllabusNodes[0].code, code = syllabusNodes[0].code, children = new List<SyllabusNode>() };
- // await _azureCosmos.SaveOrUpdate(syllabus);
- // data = new List<Syllabus>() { syllabus };
- // }
- // List<SyllabusNode> syllabusNodes1 = new List<SyllabusNode>();
- // if (data.IsNotEmpty())
- // {
- // syllabusNodes1.AddRange(data[0].children);
- // //replace
- // for (int i = 0; i < data[0].children.Count; i++)
- // {
- // for (int j = 0; j < syllabusNodes.Count; j++)
- // {
- // if (data[0].children[i].id == syllabusNodes[j].id && data[0].children[i].code == syllabusNodes[j].code)
- // {
- // syllabusNodes1.Remove(data[0].children[i]);
- // syllabusNodes1.Add(syllabusNodes[j]);
- // syllabusNodes.Remove(syllabusNodes[j]);
- // }
- // }
- // }
- // data[0].children = syllabusNodes1;
- // //新增
- // data[0].children.AddRange(syllabusNodes);
- // if (data[0].children.IsNotEmpty())
- // {
- // var len = data[0].children.Count;
- // for (int i = 0; i < len; i++)
- // {
- // if (data[0].children[i].items.IsNotEmpty())
- // {
- // data[0].children[i].item = true;
- // }
- // if (data[0].children[i].resources.IsNotEmpty())
- // {
- // data[0].children[i].resource = true;
- // }
- // }
- // }
- // syllabuses = await _azureCosmos.SaveOrUpdateAll<Syllabus>(data);
- // }
- // else
- // {
- // throw new BizException("保存失败", ResponseCode.FAILED);
- // }
- // return syllabuses;
- //}
- }
- }
|