123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122 |
- using Microsoft.AspNetCore.Mvc;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using TEAMModelOS.Models;
- using TEAMModelOS.SDK.DI;
- using System.Text.Json;
- using TEAMModelOS.SDK.Models;
- using Microsoft.AspNetCore.Http;
- using TEAMModelOS.SDK.Extension;
- using Azure.Cosmos;
- using System.Text;
- using TEAMModelOS.SDK.DI.AzureCosmos.Inner;
- using Microsoft.Extensions.Options;
- using Azure.Messaging.ServiceBus;
- using Microsoft.Extensions.Configuration;
- using HTEXLib.COMM.Helpers;
- using StackExchange.Redis;
- using Microsoft.AspNetCore.Authorization;
- using TEAMModelOS.SDK.Services;
- namespace TEAMModelOS.Controllers
- {
- [ProducesResponseType(StatusCodes.Status200OK)]
- [ProducesResponseType(StatusCodes.Status400BadRequest)]
-
- [Route("item")]
- [ApiController]
- public class ItemController : ControllerBase
- {
- private readonly SnowflakeId _snowflakeId;
- private readonly AzureCosmosFactory _azureCosmos;
- private readonly DingDing _dingDing;
- private readonly Option _option;
- private readonly AzureStorageFactory _azureStorage;
- private readonly AzureServiceBusFactory _serviceBus;
- private readonly AzureRedisFactory _azureRedis;
- public IConfiguration _configuration { get; set; }
- public ItemController(AzureCosmosFactory azureCosmos, SnowflakeId snowflakeId, DingDing dingDing, IOptionsSnapshot<Option> option, AzureStorageFactory azureStorage,
- AzureRedisFactory azureRedis,AzureServiceBusFactory serviceBus, IConfiguration configuration)
- {
- _azureCosmos = azureCosmos;
- _snowflakeId = snowflakeId;
- _dingDing = dingDing;
- _option = option?.Value;
- _azureStorage = azureStorage;
- _serviceBus = serviceBus;
- _configuration = configuration;
- _azureRedis = azureRedis;
- }
- /// <summary>
- /// 参数{"periodId":"学段id","schoolCode":"学校编码"} 其中periodId 不填则是全部学段的。
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- [ProducesDefaultResponseType]
- [HttpPost("cond-count")]
- public async Task<IActionResult> CondCount(JsonElement request) {
- try
- {
- var client = _azureCosmos.GetCosmosClient();
- request.TryGetProperty("periodId", out JsonElement periodId);
- request.TryGetProperty("schoolCode", out JsonElement schoolCode);
- request.TryGetProperty("tmdid", out JsonElement tmdid);
- if (!string.IsNullOrEmpty($"{periodId}") && !string.IsNullOrEmpty($"{schoolCode}"))
- {
- var value = await _azureRedis.GetRedisClient(8).HashGetAsync($"ItemCond:{schoolCode}", $"{periodId}");
- ItemCond itemCond = null;
- if (value != default && !value.IsNullOrEmpty)
- {
- itemCond = value.ToString().ToObject<ItemCond>();
- }
- else {
- List<ItemInfo> items = new List<ItemInfo>();
- var queryslt = $"SELECT c.gradeIds,c.subjectId,c.periodId,c.type,c.level,c.field ,c.scope FROM c where c.periodId='{periodId}' and c.pid= null ";
- await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<ItemInfo>(queryText: queryslt, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Item-{schoolCode}") }))
- {
- items.Add(item);
- }
- itemCond = new ItemCond()
- {
- id = $"{periodId}",
- code = $"ItemCond-{schoolCode}",
- pk = "ItemCond",
- ttl = -1,
- count = items.Count,
- grades = new List<GradeCount>(),
- subjects = new List<SubjectCount>()
- };
- items.ForEach(z =>
- {
- if (!string.IsNullOrEmpty(z.type))
- {
- ItemService.CountItemCond(z, null, itemCond);
- }
- });
- await _azureRedis.GetRedisClient(8).HashSetAsync($"ItemCond:{schoolCode}", $"{periodId}", itemCond.ToJsonString());
- }
- return Ok(new { itemConds = new List<ItemCond>() { itemCond } });
- }
- else if (!string.IsNullOrEmpty($"{schoolCode}"))
- {
- List<ItemCond> itemConds = new List<ItemCond>();
- School school =await client.GetContainer(Constant.TEAMModelOS, "School").ReadItemAsync<School>($"{schoolCode}", new PartitionKey("Base"));
- List<string> nocachePeriods = new List<string>();
- foreach (var period in school.period) {
- var value = await _azureRedis.GetRedisClient(8).HashGetAsync($"ItemCond:{schoolCode}", $"{period.id}");
- ItemCond itemCond = null;
- if (value != default && !value.IsNullOrEmpty)
- {
- itemCond = value.ToString().ToObject<ItemCond>();
- itemConds.Add(itemCond);
- }
- else {
- nocachePeriods.Add(period.id);
- }
- }
- string nocachePeriodsql = "";
- if (nocachePeriods.IsNotEmpty())
- {
- string sql = string.Join(',', nocachePeriods.Select(x => $"'{x}'"));
- nocachePeriodsql = $" and c.periodId in ({sql})";
- List<ItemInfo> items = new List<ItemInfo>();
- var queryslt = $"SELECT c.gradeIds,c.subjectId,c.periodId,c.type,c.level,c.field,c.scope FROM c where c.pid= null {nocachePeriodsql}";
- await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<ItemInfo>(queryText: queryslt, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Item-{schoolCode}") }))
- {
- items.Add(item);
- }
- var list = items.GroupBy(x => x.periodId).Select(y => new { key = y.Key, list = y.ToList() }).ToList();
- foreach (var z in list)
- {
- ItemCond cond = new ItemCond() { id = z.key, code = $"ItemCond-{school.id}", pk = "ItemCond", ttl = -1, count = z.list.Count, grades = new List<GradeCount>(), subjects = new List<SubjectCount>() };
- z.list.ForEach(y =>
- {
- if (!string.IsNullOrEmpty(y.type))
- {
- ItemService.CountItemCond(y, null, cond);
- }
- });
- await _azureRedis.GetRedisClient(8).HashSetAsync($"ItemCond:{schoolCode}", $"{z.key}", cond.ToJsonString());
- itemConds.Add(cond);
- }
- if (school != null)
- {
- foreach (var period in school.period)
- {
- var cond = itemConds.Find(x => x.id.Equals(period.id));
- if (cond == null)
- {
- ItemCond condn = new ItemCond() { id = period.id, code = $"ItemCond-{school.id}", pk = "ItemCond", ttl = -1, count = 0, grades = new List<GradeCount>(), subjects = new List<SubjectCount>() };
- itemConds.Add(condn);
- }
- }
- }
- }
- return Ok(new { itemConds = itemConds });
- }
- else if (!string.IsNullOrEmpty($"{tmdid}"))
- {
- var value = await _azureRedis.GetRedisClient(8).HashGetAsync($"ItemCond:ItemCond", $"{tmdid}");
- ItemCond itemCond = null;
- if (value != default && !value.IsNullOrEmpty)
- {
- itemCond = value.ToString().ToObject<ItemCond>();
- }
- else {
- List<ItemInfo> items = new List<ItemInfo>();
- var queryslt = $"SELECT c.gradeIds,c.subjectId,c.periodId,c.type,c.level,c.field ,c.scope FROM c where c.pid= null ";
- await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryIterator<ItemInfo>(queryText: queryslt, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Item-{tmdid}") }))
- {
- items.Add(item);
- }
- itemCond = new ItemCond() { id = $"{tmdid}", code = $"ItemCond", pk = "ItemCond", ttl = -1, count = items.Count };
- items.ForEach(z =>
- {
- if (!string.IsNullOrEmpty(z.type)) {
- ItemService.CountItemCond(z, null, itemCond);
- }
- });
- await _azureRedis.GetRedisClient(8).HashSetAsync($"ItemCond:ItemCond", $"{tmdid}", itemCond.ToJsonString());
- }
- return Ok(new { itemConds = new List<ItemCond>() { itemCond } });
- }
- else
- {
- return Ok(new { itemConds = new List<ItemCond>() { } });
- }
- }
- catch (CosmosException ex)
- {
- return Ok(new { itemConds = new List<ItemCond>() { } });
- }
- catch (Exception ex)
- {
- return Ok(new { itemConds = new List<ItemCond>() { } });
- }
- }
- [ProducesDefaultResponseType]
- [HttpPost("upsert-all")]
- [Authorize(Roles = "IES")]
- public async Task<IActionResult> UpsertAll(JsonElement request)
- {
- var ItemCondQueue = _configuration.GetValue<string>("Azure:ServiceBus:ItemCondQueue");
- var client = _azureCosmos.GetCosmosClient();
- List<ItemInfo> itemInfos = null;
- if (!request.TryGetProperty("itemInfos", out JsonElement items)) return BadRequest();
- if (!request.TryGetProperty("option", out JsonElement option)) return BadRequest();
- try
- {
- Dictionary<string, ItemCondDto> dict = new Dictionary<string, ItemCondDto>();
- itemInfos = items.ToObject<List<ItemInfo>>();
- foreach (var itemInfo in itemInfos) {
- itemInfo.ttl = -1;
- itemInfo.size = await _azureStorage.GetBlobContainerClient(itemInfo.code).GetBlobsSize($"item/{itemInfo.id}");
- var messageBlob = new ServiceBusMessage(new { id = Guid.NewGuid().ToString(), progress = "update", root = $"item/{itemInfo.id}", name = $"{itemInfo.code}" }.ToJsonString());
- messageBlob.ApplicationProperties.Add("name", "BlobRoot");
- var ActiveTask = _configuration.GetValue<string>("Azure:ServiceBus:ActiveTask");
- await _serviceBus.GetServiceBusClient().SendMessageAsync(ActiveTask, messageBlob);
- if (option.ToString().Equals("insert"))
- {
- itemInfo.createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
- if (!itemInfo.code.Contains("Item"))
- {
- itemInfo.code = "Item-" + itemInfo.code;
- }
- var response = await client.GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync(itemInfo.id, new PartitionKey($"{itemInfo.code}"));
- if (response.Status == 200)
- {
- return Ok();
- }
- else
- {
- if (itemInfo.scope.Equals("private"))
- {
- if (string.IsNullOrEmpty(itemInfo.pid))
- {
- if (!dict.ContainsKey($"{itemInfo.code.Replace("Item-", "")}"))
- {
- dict.Add($"{itemInfo.code.Replace("Item-", "")}", new ItemCondDto { key = $"{itemInfo.code.Replace("Item-", "")}",filed= $"{itemInfo.code.Replace("Item-", "")}", scope= "private" });
- }
- }
- await client.GetContainer(Constant.TEAMModelOS, "Teacher").CreateItemAsync(itemInfo, new PartitionKey($"{itemInfo.code}"));
- }
- else
- {
- if (string.IsNullOrEmpty(itemInfo.pid))
- {
- if (!dict.ContainsKey($"{itemInfo.code.Replace("Item-", "")}-{itemInfo.periodId}"))
- {
- dict.Add($"{itemInfo.code.Replace("Item-", "")}-{itemInfo.periodId}", new ItemCondDto { key = $"{itemInfo.code.Replace("Item-", "")}",filed=$"{itemInfo.periodId}", scope = "school" });
- }
- }
- await client.GetContainer(Constant.TEAMModelOS, "School").CreateItemAsync(itemInfo, new PartitionKey($"{itemInfo.code}"));
- }
- }
- }
- else
- {
- itemInfo.createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
- if (itemInfo.scope.Equals("private"))
- {
- if (!itemInfo.code.Contains("Item"))
- {
- itemInfo.code = "Item-" + itemInfo.code;
- }
- if (string.IsNullOrEmpty(itemInfo.pid))
- {
- if (!dict.ContainsKey($"{itemInfo.code.Replace("Item-", "")}"))
- {
- dict.Add($"{itemInfo.code.Replace("Item-", "")}", new ItemCondDto { key = $"{itemInfo.code.Replace("Item-", "")}", filed = $"{itemInfo.code.Replace("Item-", "")}", scope = "private" });
- }
- ItemInfo olditemInfo = await client.GetContainer(Constant.TEAMModelOS, "Teacher").ReadItemAsync<ItemInfo>(itemInfo.id, new PartitionKey($"{itemInfo.code}"));
- if (!dict.ContainsKey($"{olditemInfo.code.Replace("Item-", "")}"))
- {
- dict.Add($"{olditemInfo.code.Replace("Item-", "")}", new ItemCondDto { key = $"{olditemInfo.code.Replace("Item-", "")}", filed = $"{olditemInfo.code.Replace("Item-", "")}", scope = "private" });
- }
- }
- await client.GetContainer(Constant.TEAMModelOS, "Teacher").ReplaceItemAsync(itemInfo, itemInfo.id, new PartitionKey($"{itemInfo.code}"));
- }
- else
- {
- if (!itemInfo.code.Contains("Item"))
- {
- itemInfo.code = "Item-" + itemInfo.code;
- }
- if (string.IsNullOrEmpty(itemInfo.pid))
- {
- if (!dict.ContainsKey($"{itemInfo.code.Replace("Item-", "")}-{itemInfo.periodId}"))
- {
- dict.Add($"{itemInfo.code.Replace("Item-", "")}-{itemInfo.periodId}", new ItemCondDto { key = $"{itemInfo.code.Replace("Item-", "")}", filed = $"{itemInfo.periodId}", scope = "school" });
- }
- ItemInfo olditemInfo = await client.GetContainer(Constant.TEAMModelOS, "School").ReadItemAsync<ItemInfo>(itemInfo.id, new PartitionKey($"{itemInfo.code}"));
- if (!dict.ContainsKey($"{olditemInfo.code.Replace("Item-", "")}-{olditemInfo.periodId}"))
- {
- dict.Add($"{olditemInfo.code.Replace("Item-", "")}-{olditemInfo.periodId}", new ItemCondDto { key = $"{olditemInfo.code.Replace("Item-", "")}", filed = $"{olditemInfo.periodId}", scope = "school" });
- }
- }
- await client.GetContainer(Constant.TEAMModelOS, "School").ReplaceItemAsync(itemInfo, itemInfo.id, new PartitionKey($"{itemInfo.code}"));
- }
- }
- }
- var itemCondDtos= dict.Select(x => x.Value).ToList();
- var str = itemCondDtos.ToJsonString();
- var messageBlobItemCond = new ServiceBusMessage(str);
- await _serviceBus.GetServiceBusClient().SendMessageAsync(ItemCondQueue, messageBlobItemCond);
- return Ok(new { itemInfos });
- }
- catch (Exception ex)
- {
- await _dingDing.SendBotMsg($"OS,{_option.Location},item/upsert()\n{ex.Message}{ex.StackTrace}\n{request}", GroupNames.醍摩豆服務運維群組);
- return BadRequest();
- }
- }
-
- [ProducesDefaultResponseType]
- [HttpPost("upsert")]
- [Authorize(Roles = "IES")]
- public async Task<IActionResult> Upsert(JsonElement request)
- {
- var client = _azureCosmos.GetCosmosClient();
- if (!request.TryGetProperty("itemInfo", out JsonElement item)) return BadRequest();
- if (!request.TryGetProperty("option", out JsonElement option)) return BadRequest();
- try
- {
- Dictionary<string, ItemCondDto> dict = new Dictionary<string, ItemCondDto>();
- var ItemCondQueue = _configuration.GetValue<string>("Azure:ServiceBus:ItemCondQueue");
- ItemInfo itemInfo;
- itemInfo = item.ToObject<ItemInfo>();
- itemInfo.size = await _azureStorage.GetBlobContainerClient(itemInfo.code).GetBlobsSize($"item/{itemInfo.id}");
- var messageBlob = new ServiceBusMessage(new { id = Guid.NewGuid().ToString(), progress = "update", root = $"item/{itemInfo.id}", name = $"{itemInfo.code}" }.ToJsonString());
- messageBlob.ApplicationProperties.Add("name", "BlobRoot");
- var ActiveTask = _configuration.GetValue<string>("Azure:ServiceBus:ActiveTask");
- await _serviceBus.GetServiceBusClient().SendMessageAsync(ActiveTask, messageBlob);
- if (option.ToString().Equals("insert"))
- {
- itemInfo.createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
- if (!itemInfo.code.Contains("Item"))
- {
- itemInfo.code = "Item-" + itemInfo.code;
- }
- var response = await client.GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync(itemInfo.id, new PartitionKey($"{itemInfo.code}"));
- if (response.Status == 200)
- {
- return Ok();
- }
- else
- {
- if (itemInfo.scope.Equals("private"))
- {
- if (string.IsNullOrEmpty(itemInfo.pid))
- {
- if (!dict.ContainsKey($"{itemInfo.code.Replace("Item-", "")}"))
- {
- dict.Add($"{itemInfo.code.Replace("Item-", "")}", new ItemCondDto { key = $"{itemInfo.code.Replace("Item-", "")}", filed = $"{itemInfo.code.Replace("Item-", "")}", scope = "private" });
- }
- }
- itemInfo = await client.GetContainer(Constant.TEAMModelOS, "Teacher").CreateItemAsync(itemInfo, new PartitionKey($"{itemInfo.code}"));
- }
- else
- {
- if (string.IsNullOrEmpty(itemInfo.pid))
- {
- if (!dict.ContainsKey($"{itemInfo.code.Replace("Item-", "")}-{itemInfo.periodId}"))
- {
- dict.Add($"{itemInfo.code.Replace("Item-", "")}-{itemInfo.periodId}", new ItemCondDto { key = $"{itemInfo.code.Replace("Item-", "")}", filed = $"{itemInfo.periodId}", scope = "school" });
- }
- }
- itemInfo = await client.GetContainer(Constant.TEAMModelOS, "School").CreateItemAsync(itemInfo, new PartitionKey($"{itemInfo.code}"));
-
- }
- }
- }
- else
- {
- itemInfo.createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
- if (itemInfo.scope.Equals("private"))
- {
- if (!itemInfo.code.Contains("Item"))
- {
- itemInfo.code = "Item-" + itemInfo.code;
- }
- if (string.IsNullOrEmpty(itemInfo.pid))
- {
- if (!dict.ContainsKey($"{itemInfo.code.Replace("Item-", "")}"))
- {
- dict.Add($"{itemInfo.code.Replace("Item-", "")}", new ItemCondDto { key = $"{itemInfo.code.Replace("Item-", "")}", filed = $"{itemInfo.code.Replace("Item-", "")}", scope = "private" });
- }
- ItemInfo olditemInfo = await client.GetContainer(Constant.TEAMModelOS, "Teacher").ReadItemAsync<ItemInfo>(itemInfo.id, new PartitionKey($"{itemInfo.code}"));
- if (!dict.ContainsKey($"{olditemInfo.code.Replace("Item-", "")}"))
- {
- dict.Add($"{olditemInfo.code.Replace("Item-", "")}", new ItemCondDto { key = $"{olditemInfo.code.Replace("Item-", "")}", filed = $"{olditemInfo.code.Replace("Item-", "")}", scope = "private" });
- }
- }
- itemInfo = await client.GetContainer(Constant.TEAMModelOS, "Teacher").ReplaceItemAsync(itemInfo, itemInfo.id, new PartitionKey($"{itemInfo.code}"));
- }
- else
- {
- if (!itemInfo.code.Contains("Item"))
- {
- itemInfo.code = "Item-" + itemInfo.code;
- }
- if (string.IsNullOrEmpty(itemInfo.pid))
- {
- if (!dict.ContainsKey($"{itemInfo.code.Replace("Item-", "")}-{itemInfo.periodId}"))
- {
- dict.Add($"{itemInfo.code.Replace("Item-", "")}-{itemInfo.periodId}", new ItemCondDto { key = $"{itemInfo.code.Replace("Item-", "")}", filed = $"{itemInfo.periodId}", scope = "school" });
- }
- ItemInfo olditemInfo = await client.GetContainer(Constant.TEAMModelOS, "School").ReadItemAsync<ItemInfo>(itemInfo.id, new PartitionKey($"{itemInfo.code}"));
- if (!dict.ContainsKey($"{olditemInfo.code.Replace("Item-", "")}-{olditemInfo.periodId}"))
- {
- dict.Add($"{olditemInfo.code.Replace("Item-", "")}-{olditemInfo.periodId}", new ItemCondDto { key = $"{olditemInfo.code.Replace("Item-", "")}", filed = $"{olditemInfo.periodId}", scope = "school" });
- }
- }
- itemInfo = await client.GetContainer(Constant.TEAMModelOS, "School").ReplaceItemAsync(itemInfo, itemInfo.id, new PartitionKey($"{itemInfo.code}"));
- }
- }
- var itemCondDtos = dict.Select(x => x.Value).ToList();
- var str = itemCondDtos.ToJsonString();
- var messageBlobItemCond = new ServiceBusMessage(str);
- await _serviceBus.GetServiceBusClient().SendMessageAsync(ItemCondQueue, messageBlobItemCond);
- return Ok(new { itemInfo });
- }
- catch (Exception ex)
- {
- await _dingDing.SendBotMsg($"OS,{_option.Location},item/upsert()\n{ex.Message}", GroupNames.醍摩豆服務運維群組);
- return BadRequest();
- }
- }
-
- /// <summary>
- //获取题目摘要信息
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- [ProducesDefaultResponseType]
- //[AuthToken(Roles = "teacher")]
- [HttpPost("find-summary")]
- public async Task<IActionResult> FindSummary(JsonElement requert)
- {
- try
- {
- var client = _azureCosmos.GetCosmosClient();
- StringBuilder sql = new StringBuilder();
- sql.Append("select c.id, c.question,c.useCount,c.level,c.field,c.knowledge,c.type,c.option,c.createTime from c ");
- if (!requert.TryGetProperty("code", out JsonElement code)) return BadRequest();
- /* if (!requert.TryGetProperty("@CURRPAGE", out JsonElement page)) return BadRequest();
- if (!requert.TryGetProperty("@PAGESIZE", out JsonElement size)) return BadRequest();*/
- if (!requert.TryGetProperty("@DESC", out JsonElement desc)) return BadRequest();
- if (!requert.TryGetProperty("scope", out JsonElement scope)) return BadRequest();
- List<object> summary = new List<object>();
- Dictionary<string, object> dict = new Dictionary<string, object>();
- /* dict.Add("@CURRPAGE", page.GetInt32());
- dict.Add("@PAGESIZE", size.GetInt32());*/
- dict.Add("@DESC", desc.ToString());
- if (requert.TryGetProperty("periodId", out JsonElement periodId))
- {
- dict.Add("periodId", periodId);
- }
- if (requert.TryGetProperty("subjectId", out JsonElement subjectId))
- {
- dict.Add("subjectId", subjectId);
- }
- if (requert.TryGetProperty("level", out JsonElement level))
- {
- dict.Add("level", level);
- }
- if (requert.TryGetProperty("type", out JsonElement type))
- {
- dict.Add("type", type);
- }
- if (requert.TryGetProperty("field", out JsonElement field))
- {
- dict.Add("field", field);
- }
- AzureCosmosQuery cosmosDbQuery = SQLHelper.GetSQL(dict, sql);
- //List<object> items = new List<object>();
- if (scope.ToString().Equals("private"))
- {
- await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryStreamIterator(queryDefinition: cosmosDbQuery.CosmosQueryDefinition, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Item-{code}") }))
- {
- using var json = await JsonDocument.ParseAsync(item.ContentStream);
- if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
- {
- foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
- {
- summary.Add(obj.ToObject<object>());
- }
- }
- }
- }
- if (scope.ToString().Equals("school"))
- {
- await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryStreamIterator(queryDefinition: cosmosDbQuery.CosmosQueryDefinition, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Item-{code}") }))
- {
- using var json = await JsonDocument.ParseAsync(item.ContentStream);
- if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
- {
- foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
- {
- summary.Add(obj.ToObject<object>());
- }
- }
- }
- }
- return Ok(new { summary });
- }
- catch (Exception ex)
- {
- await _dingDing.SendBotMsg($"OS,{_option.Location},item/FindSummary()\n{ex.Message}", GroupNames.醍摩豆服務運維群組);
- return BadRequest();
- }
- }
- /// <summary>
- /// 删除
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- [ProducesDefaultResponseType]
- //[AuthToken(Roles = "teacher")]
- [HttpPost("delete")]
- [Authorize(Roles = "IES")]
- public async Task<IActionResult> Delete(JsonElement request)
- {
- try
- {
- Dictionary<string, ItemCondDto> dict = new Dictionary<string, ItemCondDto>();
- var ItemCondQueue = _configuration.GetValue<string>("Azure:ServiceBus:ItemCondQueue");
- 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();
- //删除blob 相关资料
- await _azureStorage.GetBlobServiceClient().DeleteBlobs(_dingDing, code.ToString().Replace("Item-",""),new List<string> { $"item/{id}" });
- //通知删除信息
- var messageBlob = new ServiceBusMessage(new { id = Guid.NewGuid().ToString(), progress = "delete", root = $"item/{id}", name = code.ToString().Replace("Item-", "") }.ToJsonString());
- messageBlob.ApplicationProperties.Add("name", "BlobRoot");
- var ActiveTask = _configuration.GetValue<string>("Azure:ServiceBus:ActiveTask");
- await _serviceBus.GetServiceBusClient().SendMessageAsync(ActiveTask, messageBlob);
- if (scope.ToString().Equals("school"))
- {
- ItemInfo olditemInfo = await client.GetContainer(Constant.TEAMModelOS, "School").ReadItemAsync<ItemInfo>($"{id}", new PartitionKey($"{code}"));
- if (!dict.ContainsKey($"{olditemInfo.code.Replace("Item-", "")}-{olditemInfo.periodId}"))
- {
- dict.Add($"{olditemInfo.code.Replace("Item-", "")}-{olditemInfo.periodId}", new ItemCondDto { key = $"{olditemInfo.code.Replace("Item-", "")}", filed = $"{olditemInfo.periodId}", scope = "school" });
- }
- var itemCondDtos = dict.Select(x => x.Value).ToList();
- var str = itemCondDtos.ToJsonString();
- var messageBlobItemCond = new ServiceBusMessage(str);
- await _serviceBus.GetServiceBusClient().SendMessageAsync(ItemCondQueue, messageBlobItemCond);
- var response = await client.GetContainer(Constant.TEAMModelOS, "School").DeleteItemStreamAsync(id.ToString(), new PartitionKey($"{code}"));
- return Ok(new { code = response.Status });
- }
- else
- {
- ItemInfo olditemInfo = await client.GetContainer(Constant.TEAMModelOS, "Teacher").ReadItemAsync<ItemInfo>($"{id}", new PartitionKey($"{code}"));
- if (!dict.ContainsKey($"{olditemInfo.code.Replace("Item-", "")}"))
- {
- dict.Add($"{olditemInfo.code.Replace("Item-", "")}", new ItemCondDto { key = $"{olditemInfo.code.Replace("Item-", "")}", filed = $"{olditemInfo.code.Replace("Item-", "")}", scope = "private" });
- }
- var itemCondDtos = dict.Select(x => x.Value).ToList();
- var str = itemCondDtos.ToJsonString();
- var messageBlobItemCond = new ServiceBusMessage(str);
- await _serviceBus.GetServiceBusClient().SendMessageAsync(ItemCondQueue, messageBlobItemCond);
- var response = await client.GetContainer(Constant.TEAMModelOS, "Teacher").DeleteItemStreamAsync(id.ToString(), new PartitionKey($"{code}"));
- return Ok(new { code = response.Status });
- }
-
- }
- catch (Exception e)
- {
- await _dingDing.SendBotMsg($"OS,{_option.Location},item/delete()\n{e.Message},{e.StackTrace}", GroupNames.醍摩豆服務運維群組);
- return BadRequest();
- }
- /* ResponseBuilder builder = ResponseBuilder.custom();
- IdPk idPk = await _azureCosmos.DeleteAsync<ItemInfo>( request );
- return Ok(idPk);*/
- }
- /// <summary>
- /// 手动挑题
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- [ProducesDefaultResponseType]
- //[AuthToken(Roles = "teacher")]
- [HttpPost("find")]
- public async Task<IActionResult> Find(JsonElement requert)
- {
- try
- {
- var client = _azureCosmos.GetCosmosClient();
- StringBuilder sql = new StringBuilder();
- sql.Append("select c.id,c.code,c.repairResource, c.periodId,c.question,c.useCount,c.level,c.field,c.knowledge,c.type,c.option,c.createTime,c.answer,c.explain,c.children,c.score,c.gradeIds,c.subjectId,c.blob,c.scope from c ");
- if (!requert.TryGetProperty("code", out JsonElement code)) return BadRequest();
- /* if (!requert.TryGetProperty("@CURRPAGE", out JsonElement page)) return BadRequest();
- if (!requert.TryGetProperty("@PAGESIZE", out JsonElement size)) return BadRequest();*/
-
- if (!requert.TryGetProperty("scope", out JsonElement scope)) return BadRequest();
- Dictionary<string, object> dict = new Dictionary<string, object>();
- /*var emobj = requert.EnumerateObject();
- while (emobj.MoveNext())
- {
- dict[emobj.Current.Name] = emobj.Current.Value;
- }
- //处理code
- if (dict.TryGetValue("code", out object _))
- {
- dict.Remove("code");
- }*/
- /* dict.Add("@CURRPAGE", page.GetInt32());
- dict.Add("@PAGESIZE", size.GetInt32());*/
- if (requert.TryGetProperty("@DESC", out JsonElement desc)) {
- dict.Add("@DESC", desc.ToString());
- }
- if (requert.TryGetProperty("@ASC", out JsonElement asc))
- {
- dict.Add("@ASC", asc.ToString());
- }
- if (requert.TryGetProperty("periodId", out JsonElement periodId))
- {
- dict.Add("periodId", periodId);
- }
- if (requert.TryGetProperty("subjectId", out JsonElement subjectId))
- {
- dict.Add("subjectId", subjectId);
- }
- if (requert.TryGetProperty("level", out JsonElement level))
- {
- dict.Add("level", level);
- }
- if (requert.TryGetProperty("type", out JsonElement type))
- {
- dict.Add("type", type);
- }
- if (requert.TryGetProperty("field", out JsonElement field))
- {
- dict.Add("field", field);
- }
- if (requert.TryGetProperty("gradeIds[*]", out JsonElement gradeIds))
- {
- dict.Add("gradeIds[*]", gradeIds);
- }
- if (requert.TryGetProperty("knowledge[*]", out JsonElement knowledge))
- {
- dict.Add("knowledge[*]", knowledge);
- }
- if (requert.TryGetProperty("pid", out JsonElement pd))
- {
- if (pd.ValueKind != JsonValueKind.Null)
- {
- dict.Add("pid", pd.ToString());
- }
- else
- {
- dict.Add("pid", null);
- }
- }
- AzureCosmosQuery cosmosDbQuery = SQLHelper.GetSQL(dict, sql);
- List<object> items = new List<object>();
- if (scope.ToString().Equals("private"))
- {
- await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryStreamIterator(queryDefinition: cosmosDbQuery.CosmosQueryDefinition, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Item-{code}") }))
- {
- using var json = await JsonDocument.ParseAsync(item.ContentStream);
- if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
- {
- foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
- {
- items.Add(obj.ToObject<object>());
- }
- }
- }
- }
- if (scope.ToString().Equals("school"))
- {
- await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryStreamIterator(queryDefinition: cosmosDbQuery.CosmosQueryDefinition, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Item-{code}") }))
- {
- using var json = await JsonDocument.ParseAsync(item.ContentStream);
- if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
- {
- foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
- {
- items.Add(obj.ToObject<object>());
- }
- }
- }
- }
- //ResponseBuilder builder = ResponseBuilder.custom();
- /* List<ItemInfo> items = new List<ItemInfo>();
- if (StringHelper.getKeyCount(requert) > 0)
- {
- items = await _azureCosmos.FindByDict<ItemInfo>(requert);
- }*/
- return Ok(new { items });
- //return builder.Data(items).build();
- }
- catch (Exception e)
- {
- await _dingDing.SendBotMsg($"OS,{_option.Location},item/Find()\n{e.Message}\n{e.StackTrace}", GroupNames.醍摩豆服務運維群組);
- return BadRequest();
- }
- }
- /// <summary>
- /// 手动挑题
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- [ProducesDefaultResponseType]
- //[AuthToken(Roles = "teacher")]
- [HttpPost("Find-cache-shell")]
- public async Task<IActionResult> FindCacheShell(JsonElement requert)
- {
- var client = _azureCosmos.GetCosmosClient();
- if (!requert.TryGetProperty("school_code", out JsonElement school_code)) return BadRequest();
- if (!requert.TryGetProperty("ids", out JsonElement id)) return BadRequest();
- //List<string> ids = new List<string>();
- string info = "";
- for (int i = 0; i < id.GetArrayLength(); i++)
- {
- //ids.Add(id[i].ToJsonString());
- info += id[i].ToJsonString() + ",";
- }
- List<object> items = new List<object>();
- var query = $"select c.id, c.question,c.useCount,c.level,c.field,c.knowledge,c.type,c.option,c.createTime from c where c.id in ({info[0..^1]})";
- await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Common").GetItemQueryStreamIterator(queryText: query, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Item-{school_code}") }))
- {
- using var json = await JsonDocument.ParseAsync(item.ContentStream);
- if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
- {
- foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
- {
- items.Add(obj.ToObject<object>());
- }
- }
- }
- return Ok(new { items });
- }
- /// <summary>
- /// 自动组题
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- [HttpPost("Automatic")]
- [Authorize(Roles = "IES")]
- public async Task<IActionResult> Automatic(List<Compose> request)
- {
- try
- {
- //ResponseBuilder builder = ResponseBuilder.custom();
- List<Dictionary<string, object>> list = new List<Dictionary<string, object>>();
- var client = _azureCosmos.GetCosmosClient();
- for (int i = 0; i < request.Count; i++)
- {
- ///处理知识点均分问题
- int avg = 0;
- Dictionary<string, int> point = new Dictionary<string, int>();
- if (request[i].points.IsNotEmpty())
- {
- avg = (int)Math.Ceiling(request[i].count * 1.0 / request[i].points.Count);
- foreach (string p in request[i].points)
- {
- point.TryAdd(p, avg);
- }
- }
- List<ItemInfo> retnInfos = new List<ItemInfo>();
- List<ItemInfo> itemInfos = new List<ItemInfo>();
- List<TempItem> tempItems = new List<TempItem>();
- if (request[i].quInfos.IsNotEmpty())
- {
- List<string> types = new List<string>();
- List<int> levels = new List<int>();
- foreach (QuInfo quInfo in request[i].quInfos)
- {
- StringBuilder sql = new StringBuilder();
- sql.Append("select c.id,c.code,c.repairResource, c.periodId,c.question,c.useCount,c.level,c.field,c.knowledge,c.type,c.option,c.createTime,c.answer,c.explain,c.children,c.score,c.gradeIds,c.subjectId,c.blob,c.scope from c ");
- // 自定义
- if (quInfo.custom.IsNotEmpty() && quInfo.policy.Equals("custom"))
- {
- foreach (Custom custom in quInfo.custom)
- {
- for (int j = 0; j < custom.count; j++)
- {
- tempItems.Add(new TempItem { level = custom.level, type = quInfo.type });
- }
- Dictionary<string, object> dict = new Dictionary<string, object>();
- if (!string.IsNullOrEmpty(request[i].code))
- {
- dict.Add("code", request[i].code);
- }
- if (!string.IsNullOrEmpty(request[i].period))
- {
- dict.Add("periodId", request[i].period);
- }
- if (request[i].points.IsNotEmpty())
- {
- dict.Add("knowledge[*]", request[i].points.ToArray());
- }
- //dict.Add("lite", false);
- dict.Add("pid", null);
- ///
- dict.Add("type", quInfo.type);
- dict.Add("level", custom.level);
- List<ItemInfo> items = new List<ItemInfo>();
- AzureCosmosQuery cosmosDbQuery = SQLHelper.GetSQL(dict, sql);
- if (request[i].scope.Equals("school"))
- {
- await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryStreamIterator(queryDefinition: cosmosDbQuery.CosmosQueryDefinition, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"{request[i].code}") }))
- {
- using var json = await JsonDocument.ParseAsync(item.ContentStream);
- if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
- {
- foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
- {
- items.Add(obj.ToObject<ItemInfo>());
- }
- }
- }
- }
- else
- {
- await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryStreamIterator(queryDefinition: cosmosDbQuery.CosmosQueryDefinition, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"{request[i].code}") }))
- {
- using var json = await JsonDocument.ParseAsync(item.ContentStream);
- if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
- {
- foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
- {
- items.Add(obj.ToObject<ItemInfo>());
- }
- }
- }
- }
- //List<ItemInfo> items = await _azureCosmos.FindByDict<ItemInfo>(dict);
- //id去重
- items = items.Where((x, i) => items.FindIndex(z => z.id == x.id) == i).ToList();
- ////均分知识点题目
- itemInfos.AddRange(items);
- }
- }
- else
- {
- Dictionary<string, object> dict = new Dictionary<string, object>();
- if (!string.IsNullOrEmpty(request[i].code))
- {
- dict.Add("code", request[i].code);
- }
- if (!string.IsNullOrEmpty(request[i].period))
- {
- dict.Add("periodId", request[i].period);
- }
- if (request[i].points.IsNotEmpty())
- {
- dict.Add("knowledge[*]", request[i].points.ToArray());
- }
- if (!string.IsNullOrEmpty(request[i].subject))
- {
- dict.Add("subjectId", request[i].subject);
- }
- //dict.Add("lite", false);
- dict.Add("pid", null);
- dict.Add("type", quInfo.type);
- List<ItemInfo> items = new List<ItemInfo>();
- AzureCosmosQuery cosmosDbQuery = SQLHelper.GetSQL(dict, sql);
- if (request[i].scope.Equals("school"))
- {
- await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryStreamIterator(queryDefinition: cosmosDbQuery.CosmosQueryDefinition, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"{request[i].code}") }))
- {
- using var json = await JsonDocument.ParseAsync(item.ContentStream);
- if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
- {
- foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
- {
- items.Add(obj.ToObject<ItemInfo>());
- }
- }
- }
- }
- else
- {
- await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryStreamIterator(queryDefinition: cosmosDbQuery.CosmosQueryDefinition, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"{request[i].code}") }))
- {
- using var json = await JsonDocument.ParseAsync(item.ContentStream);
- if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
- {
- foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
- {
- items.Add(obj.ToObject<ItemInfo>());
- }
- }
- }
- }
- //List<ItemInfo> items = await _azureCosmos.FindByDict<ItemInfo>(dict);
- //id去重
- items = items.Where((x, i) => items.FindIndex(z => z.id == x.id) == i).ToList();
- itemInfos.AddRange(items);
- //均分
- if (quInfo.policy.Equals("average"))
- {
- //按等级去重 获取所有等级
- List<int> lvls = items.Where((x, i) => items.FindIndex(z => z.level == x.level) == i).Select(x => x.level).ToList();
- foreach (int k in lvls)
- {
- int count = quInfo.count / lvls.Count;
- for (int j = 0; j < count; j++)
- {
- tempItems.Add(new TempItem { level = k, type = quInfo.type });
- }
- }
- // 余数 取模 随机处理
- if (lvls.Count != 0)
- {
- int mod = quInfo.count % lvls.Count;
- for (int m = 0; m < mod; m++)
- {
- int lv = lvls.OrderBy(x => Guid.NewGuid()).Take(1).FirstOrDefault();
- tempItems.Add(new TempItem { level = lv, type = quInfo.type });
- lvls.Remove(lv);
- }
- }
- }
- //随机
- if (quInfo.policy.Equals("random"))
- {
- List<int> lvls = items.Where((x, i) => items.FindIndex(z => z.level == x.level) == i).Select(x => x.level).ToList();
- for (int n = 0; n < quInfo.count; n++)
- {
- int lv = lvls.OrderBy(x => Guid.NewGuid()).FirstOrDefault();
- tempItems.Add(new TempItem { level = lv, type = quInfo.type, policy = "random" });
- }
- }
- }
- }
- }
- itemInfos = itemInfos.OrderBy(x => Guid.NewGuid()).ToList();
- tempItems = tempItems.OrderBy(x => Guid.NewGuid()).ToList();
- foreach (TempItem temp in tempItems)
- {
- ItemInfo itemInfo = new ItemInfo();
- if (temp.policy != null && temp.policy.Equals("random"))
- {
- itemInfo = itemInfos.Where(x => x.type == temp.type).OrderBy(x => Guid.NewGuid()).FirstOrDefault();
- }
- else
- {
- itemInfo = itemInfos.Where(x => x.level == temp.level && x.type == temp.type).OrderBy(x => Guid.NewGuid()).FirstOrDefault();
- }
- if (itemInfo != null)
- {
- retnInfos.Add(itemInfo);
- itemInfos.Remove(itemInfo);
- }
- }
- List<ItemInfo> restItem = new List<ItemInfo>();
- //处理综合题问题
- foreach (var item in retnInfos)
- {
- if (!string.IsNullOrWhiteSpace(item.pid))
- {
- if (item.scope .Equals("school"))
- {
- var iteme = await client.GetContainer(Constant.TEAMModelOS, "School").ReadItemAsync<ItemInfo>(item.id, new PartitionKey(item.code));
- if (iteme != null)
- {
- restItem.Add(iteme.Value);
- }
- }
- else if (item.scope .Equals("private"))
- {
- var iteme = await client.GetContainer(Constant.TEAMModelOS, "Teacher").ReadItemAsync<ItemInfo>(item.id, new PartitionKey(item.code));
- if (iteme != null)
- {
- restItem.Add(iteme.Value);
- }
- }
- }
- else
- {
- restItem.Add(item);
- }
- }
- List<List<ItemInfo>> listInfo = new List<List<ItemInfo>>();
- //处理综合题id重复
- restItem = restItem.Where((x, i) => restItem.FindIndex(z => z.id == x.id) == i).ToList();
- foreach (IGrouping<string, ItemInfo> group in restItem.GroupBy(c => c.type))
- {
- Dictionary<string, object> dictInfo = new Dictionary<string, object>();
- listInfo.Add(group.ToList());
- }
- foreach (List<ItemInfo> infos in listInfo)
- {
- List<Dictionary<string, object>> dict = new List<Dictionary<string, object>>();
- foreach (IGrouping<int, ItemInfo> group in infos.GroupBy(c => c.level))
- {
- Dictionary<string, object> dictInfo = new Dictionary<string, object>();
- dictInfo.Add("level", group.Key);
- dictInfo.Add("count", group.Count());
- dict.Add(dictInfo);
- }
- Dictionary<string, object> typeDict = new Dictionary<string, object>();
- typeDict.Add("info", dict);
- typeDict.Add("item", infos);
- typeDict.Add("count", infos.Count);
- typeDict.Add("type", infos.FirstOrDefault().type);
- list.Add(typeDict);
- }
- }
- //return builder.Data(list).build();
- return Ok(list);
- }
- catch (Exception e)
- {
- await _dingDing.SendBotMsg($"OS,{_option.Location},item/Automatic()\n{e.Message}", GroupNames.醍摩豆服務運維群組);
- return BadRequest();
- }
- }
- }
- public class TempItem
- {
- public string type { get; set; }
- public int level { get; set; }
- public ItemInfo itemInfo { get; set; }
- public string policy { get; set; }
- }
- public class Compose
- {
- /// <summary>
- /// 科目
- /// </summary>
- public string subject { get; set; }
- /// <summary>
- /// 来源,个人题库,校本题库
- /// </summary>
- public string code { get; set; }
- /// <summary>
- /// 适用学段,小学,初中,高中
- /// </summary>
- public string period { get; set; }
- /// <summary>
- /// 关联知识点
- /// </summary>
- public List<string> points { get; set; }
- /// <summary>
- /// 题目组合
- /// </summary>
- public List<QuInfo> quInfos { get; set; }
- /// <summary>
- /// 题目总数
- /// </summary>
- public int count { get; set; }
- public string scope { get; set; }
- }
- public class QuInfo
- {
- /// <summary>
- /// 题目类型,单选,多选,判断,填空,问答,综合 Single单选,Multiple多选,Judge判断,Complete填空,Subjective问答,Compose综合
- /// </summary>
- public string type { get; set; }
- /// <summary>
- /// 随机 random 平均的 average ,自定义 custom
- /// </summary>
- public string policy { get; set; }
- /// <summary>
- /// 自定义题目类型
- /// </summary>
- public List<Custom> custom { get; set; }
- /// <summary>
- /// 总题
- /// </summary>
- public int count { get; set; }
- }
- public class Custom
- {
- /// <summary>
- /// 难易程度
- /// </summary>
- public int level { get; set; }
- /// <summary>
- /// 数量
- /// </summary>
- public int count { get; set; }
- }
- }
|