123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using TEAMModelOS.Model.Syllabus.Dtos;
- using TEAMModelOS.Model.Syllabus.Models;
- using TEAMModelOS.SDK.Helper.Common.JsonHelper;
- using TEAMModelOS.SDK.Module.AzureTable.Interfaces;
- using TEAMModelOS.Service.Syllabus.Interfaces;
- namespace TEAMModelOS.Service.Syllabus.Implements
- {
- public class SyllabusService : ISyllabusService
- {
- IAzureTableDBRepository _azureTableDBRepository;
- public SyllabusService(IAzureTableDBRepository azureTableDBRepository) {
- _azureTableDBRepository = azureTableDBRepository;
- }
- public async Task<List<SyllabusTree>> SaveOrUpdateAll(List<SyllabusTree> trees)
- {
- List<SyllabusTree> nodes = new List<SyllabusTree>();
- TreeToList(trees, nodes);
- List<SyllabusNode> nods = MessagePackHelper.JsonToObject<List<SyllabusNode>>(MessagePackHelper.ObjectToJson(nodes));
- string pk = Guid.NewGuid().ToString();
- foreach (SyllabusNode node in nods) {
- node.RowKey = node.Id;
- node.PartitionKey = pk;
- }
- await _azureTableDBRepository.SaveOrUpdateAll<SyllabusNode>(nods);
- List<SyllabusTree> treess = ListToTree(nods);
- return treess;
- // return Task.Factory.StartNew(() => { return treess; });
- }
- public List<SyllabusTree> TreeToList(List<SyllabusTree> trees, List<SyllabusTree> nodes)
- {
- nodes.AddRange(MessagePackHelper.JsonToObject<List<SyllabusTree>>(MessagePackHelper.ObjectToJson(trees)));
- foreach (SyllabusTree tree in trees)
- {
- if (null != tree.Children && tree.Children.Count > 0)
- {
- TreeToList(tree.Children, nodes);
- }
- }
- return nodes;
- }
- private List<SyllabusTree> ListToTree(List<SyllabusNode> noes)
- {
- List<SyllabusTree> list = MessagePackHelper.JsonToObject<List<SyllabusTree>>(MessagePackHelper.ObjectToJson(noes));
-
- var lookup = list.ToDictionary(n => n.Id, n => n);
- return GetChild(list, lookup);
- }
- public List<SyllabusTree> GetChild(List<SyllabusTree> list ,Dictionary<string , SyllabusTree> dict) {
- List<SyllabusTree> trees = new List<SyllabusTree>();
- foreach (SyllabusTree node in list) {
- bool flag= dict.TryGetValue(node.Pid, out SyllabusTree syllabus);
- if (flag && syllabus != null)
- {
- syllabus.Children.Add(node);
- }
- else {
- trees.Add(node);
- }
- }
- return trees;
- }
- public async Task<SyllabusNode> SaveOrUpdate(SyllabusNode node )
- {
- return await _azureTableDBRepository.SaveOrUpdate<SyllabusNode>(node);
- }
- /// <summary>
- /// 获取教学段
- /// </summary>
- /// <param name="dict"></param>
- /// <returns></returns>
- public async Task<List<Period>> FindPeriodsByDict(Dictionary<string, object> dict){
- if (dict.Count <= 0)
- {
- return await _azureTableDBRepository.FindAll<Period>();
- }
- else {
- return await _azureTableDBRepository.FindListByDict<Period>(dict);
- }
-
- }
- /// <summary>
- /// 获取科目
- /// </summary>
- /// <param name="dict"></param>
- /// <returns></returns>
- public async Task<List<PeriodSubject>> FindSubjectsByDict(Dictionary<string, object> dict)
- {
- return await _azureTableDBRepository.FindListByDict<PeriodSubject>(dict);
- }
- /// <summary>
- /// 获取教材版本
- /// </summary>
- /// <param name="dict"></param>
- /// <returns></returns>
- public async Task<List<PeriodSubjectEdition>> FindEditionsByDict(Dictionary<string, object> dict)
- {
- return await _azureTableDBRepository.FindListByDict<PeriodSubjectEdition>(dict);
- }
- /// <summary>
- /// 获取册别
- /// </summary>
- /// <param name="dict"></param>
- /// <returns></returns>
- public async Task<List<PeriodSubjectEditionTerm>> FindTermsByDict(Dictionary<string, object> dict)
- {
- return await _azureTableDBRepository.FindListByDict<PeriodSubjectEditionTerm>(dict);
- }
- }
- }
|