12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565 |
- 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 Microsoft.Azure.Cosmos;
- using System.Text;
- using Microsoft.Extensions.Options;
- using Azure.Messaging.ServiceBus;
- using Microsoft.Extensions.Configuration;
- using Microsoft.AspNetCore.Authorization;
- using TEAMModelOS.SDK.Services;
- using System.Text.RegularExpressions;
- using TEAMModelOS.Filter;
- using TEAMModelOS.SDK;
- 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")]
- [AuthToken(Roles = "teacher,admin,student")]
- #if !DEBUG
- [Authorize(Roles = "IES")]
- #endif
- 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").GetItemQueryIteratorSql<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<SubjectItemCount>()
- };
- 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").GetItemQueryIteratorSql<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<SubjectItemCount>() };
- 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<SubjectItemCount>() };
- 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").GetItemQueryIteratorSql<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")]
- [AuthToken(Roles = "teacher,admin")]
- #if !DEBUG
- [Authorize(Roles = "IES")]
- #endif
- 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>>();
- if (itemInfos.IsNotEmpty())
- {
- IEnumerable<string> ids = itemInfos.Where(x => !string.IsNullOrWhiteSpace(x.id)).Select(x => x.id);
- List<ItemInfo> dbIDs = new List<ItemInfo>();
- if (ids!=null && ids.Count()>0)
- {
- string scope = itemInfos[0].scope.Equals("school") ? Constant.School : Constant.Teacher;
- string code = !itemInfos[0].code.Contains("Item") ? $"Item-" + itemInfos[0].code : itemInfos[0].code;
- string sql = $"select value c from c where c.id in ({string.Join(",", ids.Select(x => $"'{x}'"))} )";
- var result = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, scope).GetList<ItemInfo>(sql, code);
- if (result.list.IsNotEmpty())
- {
- dbIDs= result.list;
- }
- }
- {
- //处理数据库没有的数据
- var notinDB = itemInfos.ExceptBy(dbIDs.Select(x=>x.id),x=>x.id);
- if (notinDB!=null && notinDB.Count()>0)
- {
- foreach (var itemInfo in notinDB)
- {
- try {
- itemInfo.ttl = -1;
- itemInfo.size = await _azureStorage.GetBlobContainerClient(itemInfo.code).GetBlobsSize($"item/{itemInfo.id}");
- await BlobService.RefreshBlobRoot(new BlobRefreshMessage { progress = "update", root = $"item", name = $"{itemInfo.code}".Replace("Item-", "") }, _serviceBus, _configuration, _azureRedis);
- itemInfo.createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
- if (!itemInfo.code.Contains("Item"))
- {
- itemInfo.code = "Item-" + itemInfo.code;
- }
- 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}"));
- }
- } catch (Exception ex)
- {
- await _dingDing.SendBotMsg($"{_option.Location},题目批量保存报错:,{ex.Message}\n{ex.StackTrace}\n{itemInfo.ToJsonString()}", GroupNames.成都开发測試群組);
- }
- }
- }
- }
- {
- //处理更新的
- var dbs = dbIDs.Select(x => x.id);
- var inDB = itemInfos.Where(x => dbs.Contains(x.id));
- foreach (var itemInfo in inDB) {
- itemInfo.ttl = -1;
- itemInfo.size = await _azureStorage.GetBlobContainerClient(itemInfo.code).GetBlobsSize($"item/{itemInfo.id}");
- await BlobService.RefreshBlobRoot(new BlobRefreshMessage { progress = "update", root = $"item", name = $"{itemInfo.code}".Replace("Item-", "") }, _serviceBus, _configuration, _azureRedis);
- 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}"));
- ItemInfo olditemInfo = dbIDs.Find(x => x.id.Equals(itemInfo.id) && x.code.Equals(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}"));
- ItemInfo olditemInfo = dbIDs.Find(x=>x.id.Equals(itemInfo.id) && x.code.Equals(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}"));
- }
- }
- }
- /*
- foreach (var itemInfo in itemInfos)
- {
- itemInfo.ttl = -1;
- itemInfo.size = await _azureStorage.GetBlobContainerClient(itemInfo.code).GetBlobsSize($"item/{itemInfo.id}");
- await BlobService.RefreshBlobRoot(new BlobRefreshMessage { progress = "update", root = $"item", name = $"{itemInfo.code}".Replace("Item-", "") }, _serviceBus, _configuration, _azureRedis);
- 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.StatusCode==System.Net.HttpStatusCode.OK)
- {
- 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 });
- }
- else {
- return BadRequest();
- }
- }
- catch (Exception ex)
- {
- await _dingDing.SendBotMsg($"OS,{_option.Location},item/upsert()\n{ex.Message}\n{ex.StackTrace}\n{request}", GroupNames.醍摩豆服務運維群組);
- return BadRequest();
- }
- }
- [ProducesDefaultResponseType]
- [HttpPost("upsert")]
- [AuthToken(Roles = "teacher,admin,student")]
- #if !DEBUG
- [Authorize(Roles = "IES")]
- #endif
- 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}");
- await BlobService.RefreshBlobRoot(new BlobRefreshMessage { progress = "update", root = $"item", name = $"{itemInfo.code}".Replace("Item-", "") }, _serviceBus, _configuration, _azureRedis);
- 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.StatusCode==System.Net.HttpStatusCode.OK)
- {
- return Ok(new { code = 409, msg = "题目已经存在" });
- }
- 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}\n{ex.StackTrace}", GroupNames.醍摩豆服務運維群組);
- return BadRequest();
- }
- }
- /// <summary>
- //获取题目摘要信息
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- [ProducesDefaultResponseType]
- //[AuthToken(Roles = "teacher")]
- [HttpPost("find-summary")]
- [AuthToken(Roles = "teacher,admin,student")]
- #if !DEBUG
- [Authorize(Roles = "IES")]
- #endif
- public async Task<IActionResult> FindSummary(JsonElement request)
- {
- 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 (!request.TryGetProperty("code", out JsonElement code)) return BadRequest();
- /* if (!request.TryGetProperty("@CURRPAGE", out JsonElement page)) return BadRequest();
- if (!request.TryGetProperty("@PAGESIZE", out JsonElement size)) return BadRequest();*/
- if (!request.TryGetProperty("@DESC", out JsonElement desc)) return BadRequest();
- if (!request.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 (request.TryGetProperty("periodId", out JsonElement periodId))
- {
- dict.Add("periodId", periodId);
- }
- if (request.TryGetProperty("subjectId", out JsonElement subjectId))
- {
- dict.Add("subjectId", subjectId);
- }
- if (request.TryGetProperty("level", out JsonElement level))
- {
- dict.Add("level", level);
- }
- if (request.TryGetProperty("type", out JsonElement type))
- {
- dict.Add("type", type);
- }
- if (request.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").GetItemQueryStreamIteratorQuery(queryDefinition: cosmosDbQuery.CosmosQueryDefinition, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Item-{code}") }))
- {
- using var json = await JsonDocument.ParseAsync(item.Content);
- 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").GetItemQueryStreamIteratorQuery(queryDefinition: cosmosDbQuery.CosmosQueryDefinition, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Item-{code}") }))
- {
- using var json = await JsonDocument.ParseAsync(item.Content);
- 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}\n{ex.StackTrace}", GroupNames.醍摩豆服務運維群組);
- return BadRequest();
- }
- }
- /// <summary>
- /// 删除
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- [ProducesDefaultResponseType]
- //[AuthToken(Roles = "teacher")]
- [HttpPost("delete")]
- [AuthToken(Roles = "teacher,admin")]
- #if !DEBUG
- [Authorize(Roles = "IES")]
- #endif
- public async Task<IActionResult> Delete(JsonElement request)
- {
- try
- {
- Dictionary<string, ItemCondDto> dict = new Dictionary<string, ItemCondDto>();
- var ItemCondQueue = _configuration.GetValue<string>("Azure:ServiceBus:ItemCondQueue");
- List<string> ids = new List<string>();
- request.TryGetProperty("id", out JsonElement _id);
- if (_id.ValueKind.Equals(JsonValueKind.String) && !string.IsNullOrWhiteSpace(_id.GetString()))
- {
- ids.Add(_id.GetString());
- }
- request.TryGetProperty("ids", out JsonElement _ids);
- if (_ids.ValueKind.Equals(JsonValueKind.Array))
- {
- var __ids = _ids.ToObject<List<string>>();
- var ds = __ids.Where(z => !string.IsNullOrWhiteSpace(z));
- if (ds.Any())
- {
- ids.AddRange(ds);
- }
- }
- if (!request.TryGetProperty("code", out JsonElement code)) return BadRequest();
- if (!request.TryGetProperty("scope", out JsonElement scope)) return BadRequest();
- var client = _azureCosmos.GetCosmosClient();
- //删除blob 相关资料
- List<dynamic> codes = new List<dynamic>();
- foreach (var id in ids)
- {
- await _azureStorage.GetBlobServiceClient().DeleteBlobs(_dingDing, code.ToString().Replace("Item-", ""), new List<string> { $"item/{id}" });
- await BlobService.RefreshBlobRoot(new BlobRefreshMessage { progress = "delete", root = $"item", name = code.ToString().Replace("Item-", "") }, _serviceBus, _configuration, _azureRedis);
- 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}"));
- codes.Add(new { id, code = response.StatusCode });
- }
- 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}"));
- codes.Add(new { id, code = response.StatusCode });
- }
- }
- return Ok(new { code = 200, codes });
- }
- catch (Exception e)
- {
- await _dingDing.SendBotMsg($"OS,{_option.Location},item/delete()\n{e.Message}\n{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("research")]
- [AuthToken(Roles = "teacher,admin")]
- #if !DEBUG
- [Authorize(Roles = "IES")]
- #endif
- public async Task<IActionResult> Research(JsonElement request)
- {
- var client = _azureCosmos.GetCosmosClient();
- StringBuilder sql = new StringBuilder();
- sql.Append("select distinct 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 where 1=1 and c.pid=null ");
- if (!request.TryGetProperty("code", out JsonElement code)) return BadRequest();
- if (!request.TryGetProperty("scope", out JsonElement scope)) return BadRequest();
- if (!request.TryGetProperty("researchKey", out JsonElement researchKey)) return BadRequest();
- request.TryGetProperty("orOpt", out JsonElement _orOpt);
- string _researchKey = Regex.Replace($"{researchKey}", "[ \\[ \\] \\^ \\-|()【】/' {}_*×――(^)$%~!@#$…&%¥—+=<>《》!!???::•`·、。,;,.;\"‘’“”-]", " ");
- var keys = Regex.Split(_researchKey, "\\s+", RegexOptions.IgnoreCase).Where(y => !string.IsNullOrWhiteSpace(y));
- if (keys.Count() ==0)
- {
- return BadRequest();
- }
- else if (keys.Count() == 1)
- {
- sql.Append($" and contains(c.question,'{keys.First()}')");
- }
- else
- {
- string join = " and ";
- if (_orOpt.ValueKind.Equals(JsonValueKind.True))
- {
- join = " or ";
- }
- sql.Append($" and ( {string.Join(join, keys.Select(x => $" contains(c.question,'{x}') "))})");
- }
- request.TryGetProperty("type", out JsonElement type);
- if (!string.IsNullOrWhiteSpace($"{type}"))
- {
- sql.Append($" and c.type='{type}' ");
- }
- request.TryGetProperty("level", out JsonElement level);
- if (!string.IsNullOrWhiteSpace($"{level}"))
- {
- sql.Append($" and c.level={level} ");
- }
- request.TryGetProperty("field", out JsonElement field);
- if (!string.IsNullOrWhiteSpace($"{field}"))
- {
- sql.Append($" and c.level={field} ");
- }
- request.TryGetProperty("continuationToken", out JsonElement _continuationToken);
- string continuationToken = null;
- if (!string.IsNullOrWhiteSpace($"{_continuationToken}"))
- {
- continuationToken = $"{_continuationToken}";
- }
- int pageCount = 10;
- if (request.TryGetProperty("pageCount", out JsonElement _pageCount))
- {
- int.TryParse($"{_pageCount}", out int pcount);
- if (pcount > 0)
- {
- pageCount = pcount;
- }
- }
- List<object> items = new List<object>();
- if (scope.ToString().Equals("private"))
- {
- await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryStreamIteratorSql(queryText: sql.ToString(), requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Item-{code}") }))
- {
- using var json = await JsonDocument.ParseAsync(item.Content);
- 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>());
- }
- //continuationToken=item.ContinuationToken;
- //break;
- }
- }
- }
- if (scope.ToString().Equals("school"))
- {
- await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryStreamIteratorSql(queryText: sql.ToString(), requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Item-{code}") }))
- {
- using var json = await JsonDocument.ParseAsync(item.Content);
- 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>());
- }
- //continuationToken = item.ContinuationToken;
- //break;
- }
- }
- }
- //需要处理 图片中的 base64
- return Ok(new { items, continuationToken });
- }
- /// <summary>
- /// 根据条件随机挑选一个题目
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- [ProducesDefaultResponseType]
- //[AuthToken(Roles = "teacher")]
- [HttpPost("find-random-one")]
- [AuthToken(Roles = "teacher,admin,student")]
- #if !DEBUG
- [Authorize(Roles = "IES")]
- #endif
- public async Task<IActionResult> FindRandomOne(JsonElement request)
- {
- try
- {
- ItemInfo itemInfo = null;
- var client = _azureCosmos.GetCosmosClient();
- string sql = $"select value(c.id) from c joingradeIds joinknowledge where 1=1";
- if (!request.TryGetProperty("code", out JsonElement code)) return BadRequest();
- if (!request.TryGetProperty("scope", out JsonElement scope)) return BadRequest();
- Dictionary<string, object> dict = new Dictionary<string, object>();
- if (request.TryGetProperty("periodId", out JsonElement periodId))
- {
- sql=$"{sql} and c.periodId='{periodId}'";
- }
- if (request.TryGetProperty("subjectId", out JsonElement subjectId))
- {
- sql = $"{sql} and c.subjectId='{subjectId}'";
- }
- if (request.TryGetProperty("level", out JsonElement level))
- {
- sql = $"{sql} and c.level={level}";
- }
- if (request.TryGetProperty("type", out JsonElement type))
- {
- sql = $"{sql} and c.type='{type}'";
- }
- if (request.TryGetProperty("field", out JsonElement field))
- {
- sql = $"{sql} and c.field={field}";
- }
- if (request.TryGetProperty("gradeIds", out JsonElement _gradeIds) && _gradeIds.ValueKind.Equals(JsonValueKind.Array))
- {
- List<string> gradeIds = _gradeIds.ToObject<List<string>>();
- if (gradeIds.IsNotEmpty())
- {
- sql = $"{sql} and g in ({string.Join(",", gradeIds.Select(x => $"'{x}'"))})";
- sql= sql.Replace("joingradeIds", " join g in c.gradeIds");
- }
- }
- if (request.TryGetProperty("knowledge", out JsonElement _knowledge) && _knowledge.ValueKind.Equals(JsonValueKind.Array))
- {
- List<string> knowledge = _knowledge.ToObject<List<string>>();
- if (knowledge.IsNotEmpty())
- {
- sql = $"{sql} and k in ({string.Join(",", knowledge.Select(x => $"'{x}'"))})";
- sql= sql.Replace("joinknowledge", " join k in c.knowledge");
- }
- }
- if (request.TryGetProperty("pid", out JsonElement pd))
- {
- if (pd.ValueKind != JsonValueKind.Null)
- {
- sql = $"{sql} and c.pid='{pd}'";
- }
- else
- {
- sql = $"{sql} and c.pid= null ";
- }
- }
- List<string> notinIds = null;
- if (request.TryGetProperty("notinIds", out JsonElement _notinIds) && _notinIds.ValueKind.Equals(JsonValueKind.Array))
- {
- notinIds = _notinIds.ToObject<List<string>>();
- if (notinIds.IsNotEmpty())
- {
- sql = $"{sql} and c.id not in ({string.Join(",", notinIds.Select(x => $"'{x}'"))})";
- }
- }
- string sqlstr = sql.ToString().Replace("joingradeIds", " ").Replace("joinknowledge", " ");
- if (sqlstr.EndsWith("1=1") || !sqlstr.Contains("and"))
- {
- return Ok(new { itemInfo = itemInfo });
- }
- if (scope.ToString().Equals("private"))
- {
- List<string> items = new List<string>();
- await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryIteratorSql<string>(queryText: sqlstr, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Item-{code}") }))
- {
- items.Add(item);
- }
- if (notinIds.IsNotEmpty())
- {
- items = items.Except(notinIds).ToList();
- }
- string id = items.OrderBy(x => Guid.NewGuid().ToString()).Take(1).FirstOrDefault();
- if (!string.IsNullOrWhiteSpace(id))
- {
- try
- {
- itemInfo = await client.GetContainer(Constant.TEAMModelOS, "Teacher").ReadItemAsync<ItemInfo>(id, new PartitionKey($"Item-{code}"));
- }
- catch (Exception ex)
- {
- itemInfo = null;
- }
- }
- }
- if (scope.ToString().Equals("school"))
- {
- List<string> items = new List<string>();
- await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIteratorSql<string>(queryText: sqlstr, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Item-{code}") }))
- {
- items.Add(item);
- }
- if (notinIds.IsNotEmpty())
- {
- items = items.Except(notinIds).ToList();
- }
- string id = items.OrderBy(x => Guid.NewGuid().ToString()).Take(1).FirstOrDefault();
- if (!string.IsNullOrWhiteSpace(id))
- {
- try
- {
- itemInfo = await client.GetContainer(Constant.TEAMModelOS, "School").ReadItemAsync<ItemInfo>(id, new PartitionKey($"Item-{code}"));
- }
- catch (Exception ex)
- {
- itemInfo = null;
- }
- }
- }
- return Ok(new { itemInfo = itemInfo });
- }
- catch (Exception e)
- {
- await _dingDing.SendBotMsg($"OS,{_option.Location},item/Find()\n{e.Message}\n{e.StackTrace}\n", GroupNames.醍摩豆服務運維群組);
- return BadRequest();
- }
- }
- /// <summary>
- /// 查找题目
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- [ProducesDefaultResponseType]
- //[AuthToken(Roles = "teacher")]
- [HttpPost("find-ids")]
- [AuthToken(Roles = "teacher,admin,student")]
- #if !DEBUG
- [Authorize(Roles = "IES")]
- #endif
- public async Task<IActionResult> FindIds(JsonElement request)
- {
- var client = _azureCosmos.GetCosmosClient();
- if (!request.TryGetProperty("scope", out JsonElement scope)) return BadRequest();
- if (!request.TryGetProperty("code", out JsonElement code)) return BadRequest();
- if (!request.TryGetProperty("ids", out JsonElement _ids)) return BadRequest();
- string tbname = $"{scope}".Equals("school") ? Constant.School : Constant.Teacher;
- List<ItemInfo> items = new List<ItemInfo>();
- List<string> ids = _ids.ToObject<List<string>>();
- if (ids.IsNotEmpty())
- {
- string sql = $"select value c from c where c.id in ({string.Join(",", ids.Select(z => $"'{z}'"))})";
- await foreach (var item in client.GetContainer(Constant.TEAMModelOS, tbname).GetItemQueryIteratorSql<ItemInfo>(sql, requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey($"Item-{code}") }))
- {
- items.Add(item);
- }
- return Ok(new { items });
- }
- else
- {
- return Ok(new { items });
- }
- }
- /// <summary>
- /// 手动挑题
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- [ProducesDefaultResponseType]
- //[AuthToken(Roles = "teacher")]
- [HttpPost("find")]
- [Authorize(Roles = "IES")]
- public async Task<IActionResult> Find(JsonElement request)
- {
- 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 (!request.TryGetProperty("code", out JsonElement code)) return BadRequest();
- /* if (!request.TryGetProperty("@CURRPAGE", out JsonElement page)) return BadRequest();
- if (!request.TryGetProperty("@PAGESIZE", out JsonElement size)) return BadRequest();*/
- if (!request.TryGetProperty("scope", out JsonElement scope)) return BadRequest();
- Dictionary<string, object> dict = new Dictionary<string, object>();
- /*var emobj = request.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 (request.TryGetProperty("@DESC", out JsonElement desc))
- {
- dict.Add("@DESC", desc.ToString());
- }
- if (request.TryGetProperty("@ASC", out JsonElement asc))
- {
- dict.Add("@ASC", asc.ToString());
- }
- if (request.TryGetProperty("periodId", out JsonElement periodId))
- {
- dict.Add("periodId", periodId);
- }
- if (request.TryGetProperty("subjectId", out JsonElement subjectId))
- {
- dict.Add("subjectId", subjectId);
- }
- if (request.TryGetProperty("level", out JsonElement level))
- {
- dict.Add("level", level);
- }
- if (request.TryGetProperty("type", out JsonElement type))
- {
- dict.Add("type", type);
- }
- if (request.TryGetProperty("field", out JsonElement field))
- {
- dict.Add("field", field);
- }
- if (request.TryGetProperty("gradeIds[*]", out JsonElement gradeIds))
- {
- dict.Add("gradeIds[*]", gradeIds);
- }
- if (request.TryGetProperty("knowledge[*]", out JsonElement knowledge))
- {
- dict.Add("knowledge[*]", knowledge);
- }
- if (request.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").GetItemQueryStreamIteratorQuery(queryDefinition: cosmosDbQuery.CosmosQueryDefinition, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Item-{code}") }))
- {
- using var json = await JsonDocument.ParseAsync(item.Content);
- 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"))
- {
- // var s = client.GetContainer(Constant.TEAMModelOS, Constant.School).GetItemQueryStreamIterator(queryText: "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 where 1=1 and c.pid = null and c.subjectId in ( '8b94c6b6-2572-41e5-89b9-a82fcf13891e' ) Order By c.createTime DESC ",
- var s = client.GetContainer(Constant.TEAMModelOS, Constant.School).GetItemQueryStreamIterator(queryDefinition:cosmosDbQuery.CosmosQueryDefinition ,
- requestOptions: new QueryRequestOptions() { PartitionKey= new PartitionKey("Item-hbcn") });
- while (s.HasMoreResults) {
- var response = await s.ReadNextAsync();
- if (response.Content!=null)
- {
- using var json = await JsonDocument.ParseAsync(response.Content);
- 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>());
- }
- }
- }
- }
- //await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryStreamIteratorQuery(queryDefinition: cosmosDbQuery.CosmosQueryDefinition, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Item-{code}") }))
- //{
- // using var json = await JsonDocument.ParseAsync(item.Content);
- // 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(request) > 0)
- {
- items = await _azureCosmos.FindByDict<ItemInfo>(request);
- }*/
- 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}\n", GroupNames.醍摩豆服務運維群組);
- return BadRequest();
- }
- }
- /// <summary>
- /// 自动组题
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- [HttpPost("automatic")]
- [AuthToken(Roles = "teacher,admin,student")]
- #if !DEBUG
- [Authorize(Roles = "IES")]
- #endif
- 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").GetItemQueryStreamIteratorQuery(queryDefinition: cosmosDbQuery.CosmosQueryDefinition, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"{request[i].code}") }))
- {
- using var json = await JsonDocument.ParseAsync(item.Content);
- 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").GetItemQueryStreamIteratorQuery(queryDefinition: cosmosDbQuery.CosmosQueryDefinition, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"{request[i].code}") }))
- {
- using var json = await JsonDocument.ParseAsync(item.Content);
- 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").GetItemQueryStreamIteratorQuery(queryDefinition: cosmosDbQuery.CosmosQueryDefinition, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"{request[i].code}") }))
- {
- using var json = await JsonDocument.ParseAsync(item.Content);
- 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").GetItemQueryStreamIteratorQuery(queryDefinition: cosmosDbQuery.CosmosQueryDefinition, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"{request[i].code}") }))
- {
- using var json = await JsonDocument.ParseAsync(item.Content);
- 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去重
- Console.WriteLine(items.Count);
- 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"))
- {
- ItemInfo iteme = await client.GetContainer(Constant.TEAMModelOS, "School").ReadItemAsync<ItemInfo>(item.id, new PartitionKey(item.code));
- if (iteme != null)
- {
- restItem.Add(iteme);
- }
- }
- else if (item.scope.Equals("private"))
- {
- ItemInfo iteme = await client.GetContainer(Constant.TEAMModelOS, "Teacher").ReadItemAsync<ItemInfo>(item.id, new PartitionKey(item.code));
- if (iteme != null)
- {
- restItem.Add(iteme);
- }
- }
- }
- 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}\n{e.StackTrace}", 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; }
- }
- }
|