|
@@ -83,127 +83,177 @@ namespace TEAMModelOS.FunctionV4.HttpTrigger
|
|
|
[Function("area-artsetting-change")]
|
|
|
public async Task<HttpResponseData> AreaArtSettingChange([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequestData req) {
|
|
|
var response = req.CreateResponse(HttpStatusCode.OK);
|
|
|
- string data = await new StreamReader(req.Body).ReadToEndAsync();
|
|
|
- var json = JsonDocument.Parse(data).RootElement;
|
|
|
- json.TryGetProperty("areaId", out JsonElement _areaId);
|
|
|
- string schoolSQL = $"select value c from c where c.areaId='{_areaId}'";
|
|
|
- List<School> schools = new List<School>();
|
|
|
- await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.School)
|
|
|
- .GetItemQueryIterator<School>(queryText: schoolSQL, requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey($"Base") }))
|
|
|
- {
|
|
|
- schools.Add(item);
|
|
|
- }
|
|
|
- ArtSetting artSetting= await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.Normal).ReadItemAsync<ArtSetting>($"{_areaId}", new PartitionKey("ArtSetting"));
|
|
|
- foreach (var school in schools) {
|
|
|
- List<Period> periods = new List<Period>();
|
|
|
- var hastype = school.period.Where(p => p.type.IsNotEmpty());
|
|
|
- if (hastype.Any()) {
|
|
|
- periods.AddRange(hastype);
|
|
|
- }
|
|
|
- var nottype = school.period.Where(p => p.type.IsEmpty());
|
|
|
- if (nottype.Any()) {
|
|
|
- foreach (var type in nottype) {
|
|
|
-
|
|
|
- }
|
|
|
+ try {
|
|
|
+ string data = await new StreamReader(req.Body).ReadToEndAsync();
|
|
|
+ var json = JsonDocument.Parse(data).RootElement;
|
|
|
+ json.TryGetProperty("areaId", out JsonElement _areaId);
|
|
|
+ string schoolSQL = $"select value c from c where c.areaId='{_areaId}'";
|
|
|
+ List<School> schools = new List<School>();
|
|
|
+ await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.School)
|
|
|
+ .GetItemQueryIterator<School>(queryText: schoolSQL, requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey($"Base") }))
|
|
|
+ {
|
|
|
+ schools.Add(item);
|
|
|
}
|
|
|
- foreach (var period in periods)
|
|
|
+ ArtSetting artSetting = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.Normal).ReadItemAsync<ArtSetting>($"{_areaId}", new PartitionKey("ArtSetting"));
|
|
|
+ foreach (var school in schools)
|
|
|
{
|
|
|
- var dimension = artSetting.dimensions.FindAll(x => x.type.Intersect(period.type).Any());
|
|
|
- var bindIds = period.subjects.Where(s => !string.IsNullOrWhiteSpace(s.bindId)).Select(x => x.bindId);
|
|
|
- //该学段未同步学科的。
|
|
|
- var unBindIds = dimension.Where(z => !string.IsNullOrWhiteSpace(z.subjectBind)).Select(x => x.subjectBind).ToHashSet().Except(bindIds);
|
|
|
- if (unBindIds.Any())
|
|
|
+ List<Period> periods = new List<Period>();
|
|
|
+ var hastype_period = school.period.Where(p => p.type.IsNotEmpty());
|
|
|
+ if (hastype_period.Any())
|
|
|
{
|
|
|
- //尝试寻找同名学科且没有设置bindId的
|
|
|
- foreach (var unBindId in unBindIds)
|
|
|
+ periods.AddRange(hastype_period);
|
|
|
+ }
|
|
|
+ var nottype_period = school.period.Where(p => p.type.IsEmpty());
|
|
|
+ if (nottype_period.Any())
|
|
|
+ {
|
|
|
+ foreach (var period in nottype_period)
|
|
|
{
|
|
|
- var subjects = artSetting.dimensions.FindAll(d => !string.IsNullOrWhiteSpace(d.subjectBind) && !string.IsNullOrWhiteSpace(d.subject) && d.subjectBind.Equals(unBindId))?.Select(m => m.subject);
|
|
|
- if (subjects != null)
|
|
|
+ if (period.name.Contains("小学"))
|
|
|
+ {
|
|
|
+ period.type.Add("primary");
|
|
|
+ }
|
|
|
+ if (period.name.Contains("初中"))
|
|
|
+ {
|
|
|
+ period.type.Add("junior");
|
|
|
+ }
|
|
|
+ if (period.name.Contains("高中"))
|
|
|
+ {
|
|
|
+ period.type.Add("senior");
|
|
|
+ }
|
|
|
+ if (period.type.IsEmpty() && school.period.Count == 1)
|
|
|
{
|
|
|
- foreach (var subject in subjects)
|
|
|
+ if (school.name.Contains("小学"))
|
|
|
{
|
|
|
- //获取同名学科,且没绑定的
|
|
|
- var sub = period.subjects.FindAll(sub => sub.name.Equals(subject) && string.IsNullOrWhiteSpace(sub.bindId));
|
|
|
- if (sub.IsNotEmpty())
|
|
|
- {
|
|
|
- sub[0].bindId = unBindId;
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- period.subjects.Add(new Subject { id = Guid.NewGuid().ToString(), name = subject, bindId = unBindId, type = 1 });
|
|
|
-
|
|
|
- }
|
|
|
- break;
|
|
|
+ period.type.Add("primary");
|
|
|
+ }
|
|
|
+ if (school.name.Contains("初中"))
|
|
|
+ {
|
|
|
+ period.type.Add("junior");
|
|
|
+ }
|
|
|
+ if (school.name.Contains("高中"))
|
|
|
+ {
|
|
|
+ period.type.Add("senior");
|
|
|
}
|
|
|
}
|
|
|
+ if (period.type.IsNotEmpty())
|
|
|
+ {
|
|
|
+ periods.Add(period);
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
- var period_subjects = period.subjects.Where(s => !string.IsNullOrWhiteSpace(s.bindId));
|
|
|
- foreach (var subject in period_subjects)
|
|
|
+ foreach (var period in periods)
|
|
|
{
|
|
|
- var dim= dimension.Where(x => x.subjectBind.Equals(subject.bindId));
|
|
|
- if (dim.Any()) {
|
|
|
- Knowledge old = null;
|
|
|
- string sql = $"select value(c) from c where c.periodId = '{period.id}'";
|
|
|
- string pk = $"Knowledge-{school.id}-{subject.id}";
|
|
|
- await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "School").
|
|
|
- GetItemQueryIterator<Knowledge>(queryText: sql, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey(pk) }))
|
|
|
- {
|
|
|
- old = item;
|
|
|
- break;
|
|
|
- }
|
|
|
-
|
|
|
- //同步知识块。
|
|
|
-
|
|
|
- if (old != null)
|
|
|
+ var dimension = artSetting.dimensions.FindAll(x => x.type.Intersect(period.type).Any());
|
|
|
+ var bindIds = period.subjects.Where(s => !string.IsNullOrWhiteSpace(s.bindId)).Select(x => x.bindId);
|
|
|
+ //该学段未同步学科的。
|
|
|
+ var unBindIds = dimension.Where(z => !string.IsNullOrWhiteSpace(z.subjectBind)).Select(x => x.subjectBind).ToHashSet().Except(bindIds);
|
|
|
+ if (unBindIds.Any())
|
|
|
+ {
|
|
|
+ //尝试寻找同名学科且没有设置bindId的
|
|
|
+ foreach (var unBindId in unBindIds)
|
|
|
{
|
|
|
- bool change = false;
|
|
|
- //如果之前的是1 来源于区级,后面因区级删除,应该还原为0。
|
|
|
- var oldBlocks = old.blocks.Select(x => x.name).ToHashSet() ;
|
|
|
- var dimBlocks = dim.SelectMany(d => d.blocks);
|
|
|
- //增加的
|
|
|
- var addBlocks = dimBlocks.Except(oldBlocks);
|
|
|
- //减少的
|
|
|
- var cutBlocks = oldBlocks.Except(dimBlocks);
|
|
|
- foreach (var add in addBlocks) {
|
|
|
- old.blocks.Add(new Block { name = add,source = 1 });
|
|
|
- change = true;
|
|
|
- }
|
|
|
- //减少的还原为0
|
|
|
- if (cutBlocks.Any()) {
|
|
|
- old.blocks.ForEach(ob => {
|
|
|
- if (cutBlocks.Contains(ob.name))
|
|
|
+ var subjects = artSetting.dimensions.FindAll(d => !string.IsNullOrWhiteSpace(d.subjectBind) && !string.IsNullOrWhiteSpace(d.subject) && d.subjectBind.Equals(unBindId))?.Select(m => m.subject);
|
|
|
+ if (subjects != null)
|
|
|
+ {
|
|
|
+ foreach (var subject in subjects)
|
|
|
+ {
|
|
|
+ //获取同名学科,且没绑定的
|
|
|
+ var sub = period.subjects.FindAll(sub => sub.name.Equals(subject) && string.IsNullOrWhiteSpace(sub.bindId));
|
|
|
+ if (sub.IsNotEmpty())
|
|
|
{
|
|
|
- ob.source = 0;
|
|
|
- change=true;
|
|
|
+ sub[0].bindId = unBindId;
|
|
|
}
|
|
|
- });
|
|
|
- }
|
|
|
- if (change) {
|
|
|
- await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.School).ReplaceItemAsync(old, old.id, new PartitionKey(old.code));
|
|
|
+ else
|
|
|
+ {
|
|
|
+ period.subjects.Add(new Subject { id = Guid.NewGuid().ToString(), name = subject, bindId = unBindId, type = 1 });
|
|
|
+
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
- else
|
|
|
+ }
|
|
|
+ var period_subjects = period.subjects.Where(s => !string.IsNullOrWhiteSpace(s.bindId));
|
|
|
+ foreach (var subject in period_subjects)
|
|
|
+ {
|
|
|
+ var dim = dimension.Where(x => x.subjectBind.Equals(subject.bindId));
|
|
|
+ if (dim.Any())
|
|
|
{
|
|
|
- var blocks = dim.SelectMany(x => x.blocks).Select(bs=> new Block { name=bs,source=-1 });
|
|
|
- if (blocks.Any()) {
|
|
|
- var _new = new Knowledge
|
|
|
+ Knowledge old = null;
|
|
|
+ string sql = $"select value(c) from c where c.periodId = '{period.id}'";
|
|
|
+ string pk = $"Knowledge-{school.id}-{subject.id}";
|
|
|
+ await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "School").
|
|
|
+ GetItemQueryIterator<Knowledge>(queryText: sql, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey(pk) }))
|
|
|
+ {
|
|
|
+ old = item;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+ //同步知识块。
|
|
|
+
|
|
|
+ if (old != null)
|
|
|
+ {
|
|
|
+ bool change = false;
|
|
|
+ //如果之前的是1 来源于区级,后面因区级删除,应该还原为0。
|
|
|
+ var oldBlocks = old.blocks.Select(x => x.name).ToHashSet();
|
|
|
+ var dimBlocks = dim.SelectMany(d => d.blocks);
|
|
|
+ //增加的
|
|
|
+ var addBlocks = dimBlocks.Except(oldBlocks);
|
|
|
+ //减少的
|
|
|
+ var cutBlocks = oldBlocks.Except(dimBlocks);
|
|
|
+ foreach (var add in addBlocks)
|
|
|
+ {
|
|
|
+ old.blocks.Add(new Block { name = add, source = 1 });
|
|
|
+ change = true;
|
|
|
+ }
|
|
|
+ //减少的还原为0
|
|
|
+ if (cutBlocks.Any())
|
|
|
{
|
|
|
- id = Guid.NewGuid().ToString(),
|
|
|
- pk = "Knowledge",
|
|
|
- code = pk,
|
|
|
- owner = school.id,
|
|
|
- periodId = period.id,
|
|
|
- subjectId = subject.id,
|
|
|
- blocks = blocks.ToList()
|
|
|
- };
|
|
|
- await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.School).CreateItemAsync(_new, new PartitionKey(_new.code));
|
|
|
+ old.blocks.ForEach(ob => {
|
|
|
+ if (cutBlocks.Contains(ob.name))
|
|
|
+ {
|
|
|
+ ob.source = 0;
|
|
|
+ change = true;
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ if (change)
|
|
|
+ {
|
|
|
+ await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.School).ReplaceItemAsync(old, old.id, new PartitionKey(old.code));
|
|
|
+ }
|
|
|
+ var count = new { pcount = old.points != null ? old.points.Count : 0, bcount = old.blocks != null ? old.blocks.Count : 0 };
|
|
|
+ //处理知识点,知识块计数问题
|
|
|
+ await _azureRedis.GetRedisClient(8).HashSetAsync($"Knowledge:Count:{old.owner}-{old.subjectId}", old.periodId, count.ToJsonString());
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ var blocks = dim.SelectMany(x => x.blocks).Select(bs => new Block { name = bs, source = -1 });
|
|
|
+ if (blocks.Any())
|
|
|
+ {
|
|
|
+ var _new = new Knowledge
|
|
|
+ {
|
|
|
+ id = Guid.NewGuid().ToString(),
|
|
|
+ pk = "Knowledge",
|
|
|
+ code = pk,
|
|
|
+ owner = school.id,
|
|
|
+ periodId = period.id,
|
|
|
+ subjectId = subject.id,
|
|
|
+ blocks = blocks.ToList()
|
|
|
+ };
|
|
|
+ await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.School).CreateItemAsync(_new, new PartitionKey(_new.code));
|
|
|
+ var count = new { pcount = _new.points != null ? _new.points.Count : 0, bcount = _new.blocks != null ? _new.blocks.Count : 0 };
|
|
|
+ //处理知识点,知识块计数问题
|
|
|
+ await _azureRedis.GetRedisClient(8).HashSetAsync($"Knowledge:Count:{_new.owner}-{_new.subjectId}", _new.periodId, count.ToJsonString());
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+ await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.School).ReplaceItemAsync(school, school.id, new PartitionKey(school.code));
|
|
|
}
|
|
|
- await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.School).ReplaceItemAsync(school , school.id, new PartitionKey(school.code));
|
|
|
+ return response;
|
|
|
+ } catch (Exception ex) {
|
|
|
+ await _dingDing.SendBotMsg($"area-artsetting-change,{ex.Message}\n{ex.StackTrace}", GroupNames.醍摩豆服務運維群組);
|
|
|
}
|
|
|
return response;
|
|
|
}
|