|
@@ -1,7 +1,17 @@
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
|
+using System.Linq;
|
|
|
using System.Text;
|
|
|
+using System.Threading.Tasks;
|
|
|
+using TEAMModelOS.Models;
|
|
|
+using TEAMModelOS.SDK.Extension.DataResult.JsonRpcRequest;
|
|
|
+using TEAMModelOS.SDK.Extension.DataResult.JsonRpcResponse;
|
|
|
+using TEAMModelOS.SDK.Helper.Common.CollectionHelper;
|
|
|
+using TEAMModelOS.SDK.Helper.Common.JsonHelper;
|
|
|
+using TEAMModelOS.SDK.Module.AzureCosmosDB.Interfaces;
|
|
|
+using TEAMModelOS.SDK.Module.AzureTable.Interfaces;
|
|
|
+using TEAMModelOS.Service.Models.Syllabus;
|
|
|
|
|
|
namespace TEAMModelOS.Controllers.Syllabus
|
|
|
{
|
|
@@ -10,6 +20,382 @@ namespace TEAMModelOS.Controllers.Syllabus
|
|
|
//[Authorize]
|
|
|
public class KnowledgeController : BaseController
|
|
|
{
|
|
|
+ private IAzureTableDBRepository _table;
|
|
|
+ private IAzureCosmosDBRepository _cosmos;
|
|
|
|
|
|
+ public KnowledgeController(IAzureTableDBRepository table, IAzureCosmosDBRepository cosmos)
|
|
|
+ {
|
|
|
+ _table = table;
|
|
|
+ _cosmos = cosmos;
|
|
|
+ }
|
|
|
+ /// <summary>
|
|
|
+ /// 删除学校 知识点 或 知识块 代理
|
|
|
+ /// </summary>
|
|
|
+ /// <typeparam name="T"></typeparam>
|
|
|
+ /// <param name="request"></param>
|
|
|
+ /// <param name="builder"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ delegate Task DeleteDelegate<T>(JosnRPCRequest<Dictionary<string, object>> request, JsonRPCResponseBuilder builder);
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 保存或者更新学校 知识点 或者 知识块 代理
|
|
|
+ /// </summary>
|
|
|
+ /// <typeparam name="T"></typeparam>
|
|
|
+ /// <param name="schoolBlock"></param>
|
|
|
+ /// <param name="_cosmos"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ delegate Task<string> SchoolSaveOrUpdate<T>(dynamic schoolBlock, IAzureCosmosDBRepository _cosmos);
|
|
|
+
|
|
|
+ // private delegate dynamic SaveOrUpdate(dynamic t);
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 获取标准知识点
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="request"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ [HttpPost("FindKnowledgePointByDict")]
|
|
|
+ public async Task<BaseJosnRPCResponse> FindKnowledgePointByDict(JosnRPCRequest<KnowledgeDto> request)
|
|
|
+ {
|
|
|
+ // request.@params.PointParams.TryAdd("PartitionKey", request.lang);
|
|
|
+ JsonRPCResponseBuilder builder = JsonRPCResponseBuilder.custom();
|
|
|
+ List<KnowledgePoint> data = new List<KnowledgePoint>();
|
|
|
+ if (request.@params != null && request.@params.PointParams.TryGetValue("SubjectCode", out _))
|
|
|
+ {
|
|
|
+ List<KnowledgePoint> points = await _table.FindListByDict<KnowledgePoint>(request.@params.PointParams);
|
|
|
+ if (request.@params.Periods.IsNotEmpty())
|
|
|
+ {
|
|
|
+ HashSet<KnowledgePoint> knowledges = new HashSet<KnowledgePoint>();
|
|
|
+ foreach (string period in request.@params.Periods)
|
|
|
+ {
|
|
|
+ if (!string.IsNullOrEmpty(period))
|
|
|
+ {
|
|
|
+ knowledges.UnionWith(points.Where(x => x.AdvicePeriodCode.Contains(period)).ToHashSet());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ data = knowledges.OrderBy(x => x.Order).ToList();
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ data = points.OrderBy(x => x.Order).ToList();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ builder.Error("SubjectCode is null or empty!", "SubjectCode is null or empty!");
|
|
|
+ }
|
|
|
+ return builder.Data(data).Extend(new Dictionary<string, object> { { "count", data.Count } }).build();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 获取标准的知识块及包含的知识点
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="request"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ [HttpPost("FindKnowledgeBlockAndPointByDict")]
|
|
|
+ public async Task<BaseJosnRPCResponse> FindKnowledgeBlockAndPointByDict(JosnRPCRequest<KnowledgeDto> request)
|
|
|
+ {
|
|
|
+ //request.@params.PointParams.TryAdd("PartitionKey", request.lang);
|
|
|
+ JsonRPCResponseBuilder builder = JsonRPCResponseBuilder.custom();
|
|
|
+ List<BlockPointDto> data = new List<BlockPointDto>();
|
|
|
+ if (request.@params != null && request.@params.PointParams.TryGetValue("SubjectCode", out _))
|
|
|
+ {
|
|
|
+ List<KnowledgeBlockPoint> blockPoints = await _table.FindListByDict<KnowledgeBlockPoint>(request.@params.PointParams);
|
|
|
+ List<KnowledgeBlock> blocks = await _table.FindListByDict<KnowledgeBlock>(request.@params.PointParams);
|
|
|
+ if (request.@params.Periods.IsNotEmpty())
|
|
|
+ {
|
|
|
+ HashSet<KnowledgeBlock> knowledges = new HashSet<KnowledgeBlock>();
|
|
|
+ HashSet<KnowledgeBlockPoint> knowledgeBlocks = new HashSet<KnowledgeBlockPoint>();
|
|
|
+ foreach (string period in request.@params.Periods)
|
|
|
+ {
|
|
|
+ if (!string.IsNullOrEmpty(period))
|
|
|
+ {
|
|
|
+ knowledges.UnionWith(blocks.Where(x => x.AdvicePeriodCode.Contains(period)).ToHashSet());
|
|
|
+ knowledgeBlocks.UnionWith(blockPoints.Where(x => x.AdvicePeriodCode.Contains(period)).ToHashSet());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ blocks = knowledges.OrderBy(x => x.Order).ToList();
|
|
|
+ blockPoints = knowledgeBlocks.OrderBy(x => x.Order).ToList();
|
|
|
+ }
|
|
|
+ List<BlockPointDto> blockPointDtos = new List<BlockPointDto>();
|
|
|
+ data = FindTree(ListToTree(blocks, blockPoints), request.@params.BlockId, blockPointDtos);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ builder.Error("SubjectCode is null or empty!", "SubjectCode is null or empty!");
|
|
|
+ }
|
|
|
+ return builder.Data(data).Extend(new Dictionary<string, object> { { "count", data.Count } }).build();
|
|
|
+ }
|
|
|
+ private static List<BlockPointDto> ListToTree(List<KnowledgeBlock> blocks, List<KnowledgeBlockPoint> points)
|
|
|
+ {
|
|
|
+ List<PointDto> pointList = points.ToJson().FromJson<List<PointDto>>();
|
|
|
+ List<BlockPointDto> blockList = points.ToJson().FromJson<List<BlockPointDto>>();
|
|
|
+ Dictionary<string, List<PointDto>> pointDict = new Dictionary<string, List<PointDto>>();
|
|
|
+ List<string> keys = (pointList.GroupBy(c => c.BlockId).Select(group => group.Key)).ToList();
|
|
|
+ foreach (string key in keys)
|
|
|
+ {
|
|
|
+ pointDict.Add(key, pointList.Where(x => x.BlockId.Equals(key)).ToList());
|
|
|
+ }
|
|
|
+ //var blockDict = blockList.ToDictionary(n => n.RowKey, n => n);
|
|
|
+
|
|
|
+ var res = from r in blockList group r by r.RowKey into g select g;
|
|
|
+ Dictionary<string, BlockPointDto> blockDict = new Dictionary<string, BlockPointDto>();
|
|
|
+ foreach (var s in res)
|
|
|
+ {
|
|
|
+ blockDict.TryAdd(s.First().RowKey, s.First());
|
|
|
+ }
|
|
|
+ return GetChild(blockList, blockDict, pointDict);
|
|
|
+ }
|
|
|
+ public static List<BlockPointDto> GetChild(List<BlockPointDto> list, Dictionary<string, BlockPointDto> blockDict, Dictionary<string, List<PointDto>> pointDict)
|
|
|
+ {
|
|
|
+ List<PointDto> empt = new List<PointDto>();
|
|
|
+ list = list.OrderBy(m => m.Order).ToList();
|
|
|
+ List<BlockPointDto> trees = new List<BlockPointDto>();
|
|
|
+ foreach (BlockPointDto node in list)
|
|
|
+ {
|
|
|
+ node.Type = 0;
|
|
|
+ bool fl = pointDict.TryGetValue(node.RowKey, out List<PointDto> points);
|
|
|
+ if (fl && points.IsNotEmpty())
|
|
|
+ {
|
|
|
+ points.ForEach(m =>
|
|
|
+ {
|
|
|
+ node.Children.Add(new BlockPointDto
|
|
|
+ {
|
|
|
+ PartitionKey = m.PartitionKey,
|
|
|
+ Pid = m.BlockId,
|
|
|
+ RowKey = m.PointId,
|
|
|
+ Type = 1,
|
|
|
+ Order = m.Order,
|
|
|
+ Name = m.Name,
|
|
|
+ SubjectCode = m.SubjectCode,
|
|
|
+ AdvicePeriodCode = m.AdvicePeriodCode,
|
|
|
+ //Timestamp = m.Timestamp
|
|
|
+ });
|
|
|
+ });
|
|
|
+ }
|
|
|
+ bool flag = blockDict.TryGetValue(node.Pid, out BlockPointDto syllabus);
|
|
|
+ if (flag && syllabus != null)
|
|
|
+ {
|
|
|
+ syllabus.Children.Add(node);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ trees.Add(node);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return trees.OrderBy(x => x.Order).ToList();
|
|
|
+ }
|
|
|
+ public static List<BlockPointDto> FindTree(List<BlockPointDto> trees, String rowKey, List<BlockPointDto> blockPoints)
|
|
|
+ {
|
|
|
+ if (!string.IsNullOrEmpty(rowKey))
|
|
|
+ {
|
|
|
+ foreach (BlockPointDto tree in trees)
|
|
|
+ {
|
|
|
+ if (tree.RowKey.Equals(rowKey))
|
|
|
+ {
|
|
|
+ blockPoints.Add(tree);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (null != tree.Children && tree.Children.Count > 0)
|
|
|
+ {
|
|
|
+ FindTree(tree.Children, rowKey, blockPoints);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ blockPoints = trees;
|
|
|
+ }
|
|
|
+ return blockPoints;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 获取学校知识点
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="request"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ [HttpPost("FindSchoolPointByDict")]
|
|
|
+ public async Task<BaseJosnRPCResponse> FindSchoolPointByDict(JosnRPCRequest<Dictionary<string, object>> request)
|
|
|
+ {
|
|
|
+ // request.@params.TryAdd("PartitionKey", request.lang);
|
|
|
+ JsonRPCResponseBuilder builder = JsonRPCResponseBuilder.custom();
|
|
|
+ List<SchoolPoint> data = new List<SchoolPoint>();
|
|
|
+ if (request.@params != null && request.@params.TryGetValue("subjectCode", out _) && request.@params.TryGetValue("schoolCode", out _))
|
|
|
+ {
|
|
|
+ data = await _cosmos.FindByParams<SchoolPoint>(request.@params);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ builder.Error("'SubjectCode' or 'SchoolCode' is null or empty!", "'SubjectCode' or 'SchoolCode' is null or empty!");
|
|
|
+ }
|
|
|
+ return builder.Data(data.OrderBy(m => m.order)).Extend(new Dictionary<string, object> { { "count", data.Count } }).build();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 获取学校的知识块及包含的知识点
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="request"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ [HttpPost("FindSchoolBlockAndPointByDict")]
|
|
|
+ public async Task<BaseJosnRPCResponse> FindSchoolBlockAndPointByDict(JosnRPCRequest<KnowledgeDto> request)
|
|
|
+ {
|
|
|
+
|
|
|
+ //request.@params.PartitionKey = "zh-CN";
|
|
|
+ //request.@params.PointParams.TryAdd("PartitionKey", request.lang);
|
|
|
+ // request.@params.PointParams.TryAdd("PartitionKey", "zh-CN");
|
|
|
+ JsonRPCResponseBuilder builder = JsonRPCResponseBuilder.custom();
|
|
|
+ List<SchoolBlock> data = new List<SchoolBlock>();
|
|
|
+ if (request.@params != null && request.@params.PointParams.TryGetValue("subjectCode", out _) && request.@params.PointParams.TryGetValue("schoolCode", out _))
|
|
|
+ {
|
|
|
+ data = await _cosmos.FindByDict<SchoolBlock>(request.@params.PointParams, true);//;knowledgeService.FindSchoolBlockAndPointByDict(request.@params);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ builder.Error("'SubjectCode' or 'SchoolCode' is null or empty!", "'SubjectCode' or 'SchoolCode' is null or empty!");
|
|
|
+ }
|
|
|
+ return builder.Data(data).Extend(new Dictionary<string, object> { { "count", data.Count } }).build();
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 保存或更新学校知识块及知识点
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="request"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ [HttpPost("SaveOrUpdateSchoolBlock")]
|
|
|
+ public async Task<BaseJosnRPCResponse> SaveOrUpdateSchoolBlock(JosnRPCRequest<List<SchoolBlock>> request)
|
|
|
+ {
|
|
|
+ JsonRPCResponseBuilder builder = JsonRPCResponseBuilder.custom();
|
|
|
+ if (request.@params.IsNotEmpty())// != null )
|
|
|
+ {
|
|
|
+ List<SchoolBlock> schoolBlocks = request.@params;
|
|
|
+ foreach (SchoolBlock item in schoolBlocks) {
|
|
|
+ if (item.id == null) {
|
|
|
+ item.id = item.schoolCode + "-" + item.subjectCode + "-" + item.knowledgeId.Replace("-", "");
|
|
|
+ foreach (SchoolPoint schoolPoint in item.points) {
|
|
|
+ if (schoolPoint.id == null)
|
|
|
+ {
|
|
|
+ schoolPoint.id = schoolPoint.schoolCode + "-" + schoolPoint.subjectCode + "-" + schoolPoint.knowledgeId.Replace("-", "");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ List<SchoolBlock> ts = await _cosmos.SaveAll(schoolBlocks);
|
|
|
+ if (ts.Count > 0) builder.Data("保存或新增成功");
|
|
|
+ else builder.Error(false,ResponseCode.FAILED,"失败");
|
|
|
+ }
|
|
|
+ return builder.build();
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 手动添加学校的私有知识点
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="request"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ [HttpPost("SaveOrUpdateAllSchoolPoint")]
|
|
|
+ public async Task<BaseJosnRPCResponse> SaveOrUpdateAllSchoolPoint(JosnRPCRequest<List<SchoolPoint>> request)
|
|
|
+ {
|
|
|
+ JsonRPCResponseBuilder builder = JsonRPCResponseBuilder.custom();
|
|
|
+ if (request.@params.IsNotEmpty())// != null&& request.@params.Count > 0)
|
|
|
+ {
|
|
|
+ List<SchoolPoint> schoolBlocks = request.@params;
|
|
|
+ foreach (SchoolPoint item in schoolBlocks) {
|
|
|
+ item.id = item.schoolCode + "-" + item.subjectCode + "-" + item.knowledgeId.Replace("-", "");
|
|
|
+ //item.source = 1;
|
|
|
+ SchoolSaveOrUpdate<SchoolPoint> schoolSaveOrUpdate = SaveOrUpdate<SchoolPoint>;
|
|
|
+ string data = await schoolSaveOrUpdate(item, _cosmos);
|
|
|
+ if (!data.Equals(""))
|
|
|
+ {
|
|
|
+ builder.Data(data);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ builder.Error(false, ResponseCode.FAILED, "失败");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return builder.build();
|
|
|
+ }else return builder.Error(false,ResponseCode.PARAMS_ERROR,"参数为空").build();
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ private static async Task<string> SaveOrUpdate<T>(dynamic schoolBlock, IAzureCosmosDBRepository _cosmos)
|
|
|
+ {
|
|
|
+ Type t = typeof(T);
|
|
|
+ string message = "";
|
|
|
+ List<T> schoolBlocks = await _cosmos.FindByParams<T>(new Dictionary<string, object> {
|
|
|
+ { "id", t.GetProperty("id").GetValue(schoolBlock) }
|
|
|
+ });
|
|
|
+ if (schoolBlocks.IsNotEmpty())
|
|
|
+ {
|
|
|
+ for (int i = 0;i< schoolBlocks.Count;i++) {
|
|
|
+ schoolBlocks[i] = schoolBlock;
|
|
|
+ string id = schoolBlocks[i].GetType().GetProperty("id").GetValue(schoolBlocks[i]).ToString();
|
|
|
+ string key = await _cosmos.ReplaceObject(schoolBlocks[i], id);
|
|
|
+ if (key.Equals(id))
|
|
|
+ {
|
|
|
+ message = "修改成功";
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ T a = await _cosmos.Save<T>(schoolBlock);
|
|
|
+ if (a != null)
|
|
|
+ {
|
|
|
+ message = "新增成功";
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return message;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 删除学校知识点
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="request"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ [HttpPost("DeleteSchoolPoint")]
|
|
|
+ public async Task<BaseJosnRPCResponse> DeleteSchoolPoint(JosnRPCRequest<Dictionary<string, object>> request)
|
|
|
+ {
|
|
|
+ JsonRPCResponseBuilder builder = JsonRPCResponseBuilder.custom();
|
|
|
+ DeleteDelegate<SchoolPoint> deleteDelegate1 = Delete<SchoolPoint>;
|
|
|
+ await deleteDelegate1(request, builder);
|
|
|
+ return builder.build();
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 删除学校知识块
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="request"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ [HttpPost("DeleteSchoolBlock")]
|
|
|
+ public async Task<BaseJosnRPCResponse> DeleteSchoolBlock(JosnRPCRequest<Dictionary<string, object>> request)
|
|
|
+ {
|
|
|
+ JsonRPCResponseBuilder builder = JsonRPCResponseBuilder.custom();
|
|
|
+ DeleteDelegate<SchoolBlock> deleteDelegate1 = Delete<SchoolBlock>;
|
|
|
+ await deleteDelegate1(request, builder);
|
|
|
+ return builder.build();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private async Task Delete<T>(JosnRPCRequest<Dictionary<string, object>> request, JsonRPCResponseBuilder builder)
|
|
|
+ {
|
|
|
+ List<T> schoolBlocks = await _cosmos.FindByParams<T>(request.@params);
|
|
|
+ if (schoolBlocks.IsNotEmpty())
|
|
|
+ {
|
|
|
+ schoolBlocks.ForEach(x => { _cosmos.DeleteAsync<T>(x); });
|
|
|
+ builder.Data("删除成功");
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ builder.Error(false, ResponseCode.NOT_FOUND, "未找到对应知识块");
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
}
|