using Azure.Cosmos;
using Microsoft.Azure.WebJobs;
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using TEAMModelOS.SDK.DI;
using TEAMModelOS.SDK.Extension;
using TEAMModelOS.SDK;
using TEAMModelOS.SDK.Models;
using TEAMModelOS.SDK.Models.Cosmos.Common;
namespace TEAMModelFunction
{
public class CourseServiceBus
{
private readonly AzureCosmosFactory _azureCosmos;
private readonly DingDing _dingDing;
public CourseServiceBus(AzureCosmosFactory azureCosmos, DingDing dingDing)
{
_azureCosmos = azureCosmos;
_dingDing = dingDing;
}
///
/// 完善课程变更
///
///
/// CourseChange
/////
///
///
[FunctionName("Course")]
public async Task Course([ServiceBusTrigger("%Azure:ServiceBus:ActiveTask%", "course", Connection = "Azure:ServiceBus:ConnectionString")] string msg)
{
var client = _azureCosmos.GetCosmosClient();
try
{
//await _dingDing.SendBotMsg($"ServiceBus,CourseChange:{msg}", GroupNames.醍摩豆服務運維群組);
var jsonMsg = JsonDocument.Parse(msg);
CourseChange courseChange = msg.ToObject();
if (courseChange == null) {
return;
}
foreach (var cls in courseChange.addClass) {
(List addTmdidsCls, List addStudentsCls,List classLists) = await TriggerStuActivity.GetStuList(client, _dingDing,new List { cls}, courseChange.school);
foreach (var stu in addStudentsCls) {
try
{
ItemResponse stuCourse = await client.GetContainer("TEAMModelOS", "Student").ReadItemAsync(courseChange.id, new PartitionKey($"StuCourse-{courseChange.school}-{stu.id}"));
if (!stuCourse.Value.classId.Contains(cls)) {
stuCourse.Value.classId.Add(cls);
}
await client.GetContainer("TEAMModelOS", "Student").ReplaceItemAsync(stuCourse, courseChange.id, new PartitionKey($"StuCourse-{courseChange.school}-{stu.id}"));
}
catch (CosmosException ex)
{
if (ex.Response.Status == 404)
{
var course = new StuCourse
{
id = courseChange.id,
scode = courseChange.code,
name = courseChange.name,
code = $"StuCourse-{courseChange.school}-{stu.id}",
scope = courseChange.scope,
school = courseChange.school,
creatorId = courseChange.creatorId,
classId= new List { cls},
pk = "StuCourse"
};
await client.GetContainer("TEAMModelOS", "Student").CreateItemAsync(course, new PartitionKey(course.code));
}
}
}
foreach (var tmd in addTmdidsCls)
{
try
{
ItemResponse stuCourse = await client.GetContainer("TEAMModelOS", "Teacher").ReadItemAsync(courseChange.id, new PartitionKey($"StuCourse-{tmd.id}"));
if (!stuCourse.Value.classId.Contains(cls))
{
stuCourse.Value.classId.Add(cls);
}
await client.GetContainer("TEAMModelOS", "Teacher").ReplaceItemAsync(stuCourse, courseChange.id, new PartitionKey($"StuCourse-{tmd.id}"));
}
catch (CosmosException ex)
{
if (ex.Response.Status == 404)
{
var course = new StuCourse
{
id = courseChange.id,
scode = courseChange.code,
name = courseChange.name,
code = $"StuCourse-{tmd.id}",
scope = courseChange.scope,
school = courseChange.school,
creatorId = courseChange.creatorId,
classId = new List { cls },
pk = "StuCourse"
};
await client.GetContainer("TEAMModelOS", "Teacher").CreateItemAsync(course, new PartitionKey(course.code));
}
}
}
}
foreach (var list in courseChange.addList)
{
(List addTmdidsCls, List addStudentsCls,List classLists) = await TriggerStuActivity.GetStuList(client, _dingDing, new List { list }, courseChange.school);
foreach (var stu in addStudentsCls)
{
try
{
ItemResponse stuCourse = await client.GetContainer("TEAMModelOS", "Student").ReadItemAsync(courseChange.id, new PartitionKey($"StuCourse-{stu.schoolId}-{stu.id}"));
if (!stuCourse.Value.stulist.Contains(list))
{
stuCourse.Value.stulist.Add(list);
await client.GetContainer("TEAMModelOS", "Student").ReplaceItemAsync(stuCourse, courseChange.id, new PartitionKey($"StuCourse-{stu.schoolId}-{stu.id}"));
}
}
catch (CosmosException ex)
{
if (ex.Response.Status == 404)
{
var course = new StuCourse
{
id = courseChange.id,
scode = courseChange.code,
name = courseChange.name,
code = $"StuCourse-{courseChange.school}-{stu.id}",
scope = courseChange.scope,
school = courseChange.school,
creatorId = courseChange.creatorId,
stulist = new List { list },
pk = "StuCourse"
};
await client.GetContainer("TEAMModelOS", "Student").CreateItemAsync(course, new PartitionKey(course.code));
}
}
}
foreach (var tmd in addTmdidsCls)
{
try
{
ItemResponse stuCourse = await client.GetContainer("TEAMModelOS", "Teacher").ReadItemAsync(courseChange.id, new PartitionKey($"StuCourse-{tmd.id}"));
if (!stuCourse.Value.stulist.Contains(list))
{
stuCourse.Value.stulist.Add(list);
await client.GetContainer("TEAMModelOS", "Teacher").ReplaceItemAsync(stuCourse, courseChange.id, new PartitionKey($"StuCourse-{tmd.id}"));
}
}
catch (CosmosException ex)
{
if (ex.Response.Status == 404)
{
var course = new StuCourse
{
id = courseChange.id,
scode = courseChange.code,
name = courseChange.name,
code = $"StuCourse-{tmd.id}",
scope = courseChange.scope,
school = courseChange.school,
creatorId = courseChange.creatorId,
stulist = new List { list },
pk = "StuCourse"
};
await client.GetContainer("TEAMModelOS", "Teacher").CreateItemAsync(course, new PartitionKey(course.code));
}
}
}
}
foreach (var delCls in courseChange.delClass) {
(List delTmdidsCls, List delStudentsCls,List classLists) = await TriggerStuActivity.GetStuList(client, _dingDing, new List { delCls }, courseChange.school);
foreach (var stu in delStudentsCls)
{
try
{
ItemResponse stuCourse = await client.GetContainer("TEAMModelOS", "Student").ReadItemAsync(courseChange.id, new PartitionKey($"StuCourse-{courseChange.school}-{stu.id}"));
if (stuCourse.Value.classId.Contains(delCls))
{
stuCourse.Value.classId.Remove(delCls);
}
if (!stuCourse.Value.classId.IsNotEmpty() && !stuCourse.Value.stulist.IsNotEmpty())
{
//当两个列表都不存在时则直接删除
await client.GetContainer("TEAMModelOS", "Student").DeleteItemAsync( courseChange.id, new PartitionKey($"StuCourse-{courseChange.school}-{stu.id}"));
}
else {
await client.GetContainer("TEAMModelOS", "Student").ReplaceItemAsync(stuCourse, courseChange.id, new PartitionKey($"StuCourse-{courseChange.school}-{stu.id}"));
}
}
catch (CosmosException ex)
{
}
}
foreach (var tmd in delTmdidsCls)
{
try
{
ItemResponse stuCourse = await client.GetContainer("TEAMModelOS", "Teacher").ReadItemAsync(courseChange.id, new PartitionKey($"StuCourse-{tmd.id}"));
if (stuCourse.Value.classId.Contains(delCls))
{
stuCourse.Value.classId.Remove(delCls);
}
if (!stuCourse.Value.classId.IsNotEmpty() && !stuCourse.Value.stulist.IsNotEmpty())
{
//当两个列表都不存在时则直接删除
await client.GetContainer("TEAMModelOS", "Teacher").DeleteItemAsync(courseChange.id, new PartitionKey($"StuCourse-{tmd.id}"));
}
else
{
await client.GetContainer("TEAMModelOS", "Teacher").ReplaceItemAsync(stuCourse, courseChange.id, new PartitionKey($"StuCourse-{tmd.id}"));
}
}
catch (CosmosException ex)
{
}
}
}
foreach (var delList in courseChange.delList)
{
(List delTmdidsCls, List delStudentsCls, List classLists) = await TriggerStuActivity.GetStuList(client, _dingDing, new List { delList }, courseChange.school);
foreach (var stu in delStudentsCls)
{
try
{
ItemResponse stuCourse = await client.GetContainer("TEAMModelOS", "Student").ReadItemAsync(courseChange.id, new PartitionKey($"StuCourse-{courseChange.school}-{stu.id}"));
if (stuCourse.Value.stulist.Contains(delList))
{
stuCourse.Value.stulist.Remove(delList);
}
if (!stuCourse.Value.classId.IsNotEmpty() && !stuCourse.Value.stulist.IsNotEmpty())
{
//当两个列表都不存在时则直接删除
await client.GetContainer("TEAMModelOS", "Student").DeleteItemAsync(courseChange.id, new PartitionKey($"StuCourse-{courseChange.school}-{stu.id}"));
}
else
{
await client.GetContainer("TEAMModelOS", "Student").ReplaceItemAsync(stuCourse, courseChange.id, new PartitionKey($"StuCourse-{courseChange.school}-{stu.id}"));
}
}
catch (CosmosException ex)
{
}
}
foreach (var tmd in delTmdidsCls)
{
try
{
ItemResponse stuCourse = await client.GetContainer("TEAMModelOS", "Teacher").ReadItemAsync(courseChange.id, new PartitionKey($"StuCourse-{tmd.id}"));
if (stuCourse.Value.stulist.Contains(delList))
{
stuCourse.Value.stulist.Remove(delList);
}
if (!stuCourse.Value.classId.IsNotEmpty() && !stuCourse.Value.stulist.IsNotEmpty())
{
//当两个列表都不存在时则直接删除
await client.GetContainer("TEAMModelOS", "Teacher").DeleteItemAsync(courseChange.id, new PartitionKey($"StuCourse-{tmd.id}"));
}
else
{
await client.GetContainer("TEAMModelOS", "Teacher").ReplaceItemAsync(stuCourse, courseChange.id, new PartitionKey($"StuCourse-{tmd.id}"));
}
}
catch (CosmosException ex)
{
}
}
}
////根据新增名单获取 新增的学生id 及timdid
//(List addTmdids, List addStudents) = await TriggerStuActivity.GetStuList(client, _dingDing, courseChange.addList, courseChange.school);
////根据删除名单获取 新增的学生id 及timdid
//(List delTmdids, List delStudents) = await TriggerStuActivity.GetStuList(client, _dingDing, courseChange.delList, courseChange.school);
//foreach (var addStu in addStudents)
//{
// var course = new StuCourse
// {
// id = courseChange.id,
// scode = courseChange.code,
// name = courseChange.name,
// code = $"StuCourse-{courseChange.school}-{addStu.id}",
// scope = courseChange.scope,
// school = courseChange.school,
// creatorId = courseChange.creatorId,
// pk = "StuCourse"
// };
// await client.GetContainer("TEAMModelOS", "Student").UpsertItemAsync(course, new PartitionKey(course.code));
//}
//foreach (var addTmd in addTmdids)
//{
// var course = new StuCourse
// {
// id = courseChange.id,
// scode = courseChange.code,
// name = courseChange.name,
// code = $"StuCourse-{addTmd}",
// scope = courseChange.scope,
// //school = courseChange.school,
// creatorId = courseChange.creatorId,
// pk = "StuCourse"
// };
// await client.GetContainer("TEAMModelOS", "Teacher").UpsertItemAsync(course, new PartitionKey(course.code));
//}
//foreach (var delStu in delStudents)
//{
// await client.GetContainer("TEAMModelOS", "Student").DeleteItemStreamAsync(courseChange.id, new PartitionKey($"Course-{courseChange.school}-{delStu.id}"));
//}
//foreach (var delTmd in delTmdids)
//{
// await client.GetContainer("TEAMModelOS", "Teacher").DeleteItemStreamAsync(courseChange.id, new PartitionKey($"Course-{delTmd}"));
//}
}
catch (Exception ex)
{
await _dingDing.SendBotMsg($"CourseServiceBus-Course\n{ex.Message}{ex.StackTrace}", GroupNames.成都开发測試群組);
}
}
}
}