Browse Source

更新学生

CrazyIter 5 years ago
parent
commit
75966492b4

+ 2 - 2
TEAMModelOS.SDK/Module/AzureCosmosDBV3/AzureCosmosDBV3Repository.cs

@@ -992,10 +992,10 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDBV3
             return response.Resource;
         }
 
-        public async Task<T> FindById<T>(string id) where T : ID
+        public async Task<T> FindById<T>(string id, bool cache = true) where T : ID
         {
             CosmosModelInfo container = await InitializeCollection<T>();
-            if (container.cache && RedisHelper.Instance != null)
+            if (container.cache && RedisHelper.Instance != null  && cache==true)
             {
 
                 return await RedisHelper.CacheShellAsync(CacheCosmosPrefix + container.container.Id, id, timeoutSeconds, () => { return FindByIdAsSql<T>(id); });

+ 8 - 1
TEAMModelOS.SDK/Module/AzureCosmosDBV3/IAzureCosmosDBV3Repository.cs

@@ -79,7 +79,14 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDBV3
         Task<List<dynamic>> FindByDict(string CollectionName, Dictionary<string, object> dict, string partitionKey = null, List<string> propertys = null);
         Task<List<dynamic>> FindCountByDict(string CollectionName, Dictionary<string, object> dict, string partitionKey = null);
         Task<Dictionary<string,CosmosModelInfo>> InitializeDatabase();
-        Task<T>FindById<T>(string id) where T : ID;
+        /// <summary>
+        /// 根据ID获取  是否从cache获取  默认是
+        /// </summary>
+        /// <typeparam name="T"></typeparam>
+        /// <param name="id"></param>
+        /// <param name="cache"></param>
+        /// <returns></returns>
+        Task<T>FindById<T>(string id,bool cache=true) where T : ID;
         Task<List<T>> FindByIds<T>(List<string> ids) where T : ID;
         Task<dynamic> FindById(string CollectionName, string id);
         Task<List<dynamic>> FindByIds (string CollectionName, List<string> ids);

+ 68 - 76
TEAMModelOS/Controllers/Core/StudentController.cs

@@ -16,6 +16,7 @@ using TEAMModelOS.SDK.Helper.Security.TmdCrypt;
 using TEAMModelOS.SDK.Module.AzureCosmosDBV3;
 using TEAMModelOS.SDK.Helper.Common.ValidateHelper;
 using TEAMModelOS.SDK.Context.Exception;
+using OpenXmlPowerTools;
 
 namespace TEAMModelOS.Controllers.Syllabus
 {
@@ -40,18 +41,22 @@ namespace TEAMModelOS.Controllers.Syllabus
                 request.@params.password.value = TmdCrypt.Encrypt(request.@params.password.value);
                 request.@params.password.isSet = true;
             }
-            //Classroom classroom = new Classroom
-            //{
-            //    classroomCode = request.@params.classroom.classroomCode,
-            //    gradeCode = request.@params.classroom.gradeCode,
-            //    periodCode = request.@params.classroom.periodCode,
-            //    scopeCode = request.@params.classroom.scopeCode,
-            //};
-            request.@params.id = request.@params.studentId.Replace("#","-");
-            //request.@params.classroom = classroom;
+            request.@params.id = request.@params.studentId.Replace("#", "-");
+            ///假如更新了班级则先获取更新之前的班级
+            var olStudent= await azureCosmosDBRepository.FindById<Student>(request.@params.studentId,false);
+            if (olStudent != null && !olStudent.classroomCode.Equals(request.@params.classroomCode) ) {
+                ClassroomStudent classroomstu=await azureCosmosDBRepository.FindById<ClassroomStudent>(olStudent.classroomCode);
+                if (classroomstu != null) {
+                    if (classroomstu.studentId.Contains(request.@params.studentId)) {
+                        classroomstu.studentId.Remove(request.@params.studentId);
+                        await azureCosmosDBRepository.SaveOrUpdate(classroomstu);
+                    }
+                }
+            }
+           
             Student data = await azureCosmosDBRepository.SaveOrUpdate<Student>(request.@params);
             ///更新学生关系表
-            ClassroomStudent classroomStudent = await azureCosmosDBRepository.FindById<ClassroomStudent>(data.classroomCode);
+            ClassroomStudent classroomStudent = await azureCosmosDBRepository.FindById<ClassroomStudent>(data.classroomCode,false);
             if (classroomStudent == null)
             {
                 classroomStudent = new ClassroomStudent { id = data.classroomCode, scopeCode = data.schoolCode, studentId = new List<string> { data.studentId } };
@@ -60,10 +65,11 @@ namespace TEAMModelOS.Controllers.Syllabus
                 if (!classroomStudent.studentId.Contains(data.studentId))
                 {
                     classroomStudent.studentId.Add(data.studentId);
+                    await azureCosmosDBRepository.SaveOrUpdate(classroomStudent);
                 }
                // classroomStudent.studentId.UnionWith(new List<string> { data.studentId });
             }
-            await azureCosmosDBRepository.SaveOrUpdate(classroomStudent);
+           
             return builder.Data(data).build();
         }
 
@@ -88,7 +94,35 @@ namespace TEAMModelOS.Controllers.Syllabus
         {
             JsonRPCResponseBuilder builder = JsonRPCResponseBuilder.custom();
 
-            if (ValidateHelper.IsValid(request.@params)) {
+            if (ValidateHelper.IsValid(request.@params) && request.@params.IsNotEmpty()) {
+
+                ///假如更新了班级则先获取更新之前的班级
+                string[] ids = request.@params.Select(x=>x.studentId).ToArray();
+                List<Student> oldStudent = await azureCosmosDBRepository.FindByDict<Student>(new Dictionary<string, object>() { { "studentId", ids } });
+                if (oldStudent.IsNotEmpty()) {
+                    foreach (var old in oldStudent.GroupBy(x=>x.classroomCode))
+                    {
+                        List<string> idsold = new List<string>();
+                        foreach (var oldstu in old) {
+                            Student student = request.@params.Where(x => x.studentId == oldstu.studentId).FirstOrDefault();
+                            if (student != null)
+                            {
+                                if (!student.classroomCode.Equals(oldstu.classroomCode))
+                                {
+                                    idsold.Add(student.studentId);
+                                }
+                            }
+                        }
+                        ClassroomStudent classroomStudent = await azureCosmosDBRepository.FindById<ClassroomStudent>(old.Key, false);
+                        if (classroomStudent != null) {
+                            idsold.ForEach(x => {
+                                classroomStudent.studentId.Remove(x);
+                            });
+                            await azureCosmosDBRepository.SaveOrUpdate(classroomStudent);
+                        }
+                    }
+                }
+
                 long createDate = DateTimeOffset.UtcNow.Ticks;
                 request.@params.ForEach(
                     x => {
@@ -107,7 +141,7 @@ namespace TEAMModelOS.Controllers.Syllabus
                 foreach (var classroom in students.GroupBy(x => x.classroomCode).ToList())
                 {
 
-                    ClassroomStudent classroomStudent = await azureCosmosDBRepository.FindById<ClassroomStudent>(classroom.Key);
+                    ClassroomStudent classroomStudent = await azureCosmosDBRepository.FindById<ClassroomStudent>(classroom.Key,false);
                     if (classroomStudent == null)
                     {
                         classroomStudent = new ClassroomStudent { id = classroom.Key,studentId= new List<string>()};
@@ -123,52 +157,9 @@ namespace TEAMModelOS.Controllers.Syllabus
                     }
                     classroomStudents.Add(classroomStudent);
                 }
-                 await  azureCosmosDBRepository.SaveOrUpdateAll(classroomStudents);
+                await  azureCosmosDBRepository.SaveOrUpdateAll(classroomStudents);
                 builder.Data(students);
             }
-            //Dictionary<string, List<Student>> dictInfo = new Dictionary<string, List<Student>>();
-            //foreach (IGrouping<string, Student> group in request.@params.GroupBy(c => c.classroomCode))
-            //{
-            //    dictInfo.Add(group.Key, group.ToList());
-            //}
-            //List<Student> students = new List<Student>();
-            //foreach (string key in dictInfo.Keys)
-            //{
-            //    List<Classroom> classrooms = await azureCosmosDBRepository.FindByDict<Classroom>(new Dictionary<string, object> { { "classroomCode", key } } );
-            //    if (classrooms.IsNotEmpty())
-            //    {
-            //        long createDate = DateTimeOffset.UtcNow.Ticks;
-            //        dictInfo.TryGetValue(key, out List<Student> sts);
-            //        sts.ForEach(x => {
-            //            Student student = new Student
-            //            {
-            //                schoolCode = classrooms[0].scopeCode,
-            //                id = x.studentId.Replace("#","-"),
-            //             //   TEAMModelId = x.studentId,
-            //               // virtualId = x.studentId,
-            //                createDate = createDate,
-            //                name = x.name,
-            //                //seatNo = x.seatNo,
-            //                studentId = x.studentId
-            //            };
-            //            //设置密码 isSet 是否加密 如果加密则不会再次加密
-            //            if (!x.password.isSet) {
-            //                student.password.value = TmdCrypt.Encrypt(x.password.value);
-            //                student.password.isSet = true;
-            //            }
-            //            //Classroom classroom = new Classroom
-            //            //{
-            //            //    classroomCode = x.classroom.classroomCode,
-            //            //    gradeCode = classrooms[0].gradeCode,
-            //            //    periodCode = classrooms[0].periodCode,
-            //            //    scopeCode = classrooms[0].scopeCode,
-            //            //};
-            //            //student.classroom = classroom;
-            //            students.Add(student);
-            //        });
-            //        await azureCosmosDBRepository.SaveOrUpdateAll(students);
-            //    }
-            //}
             return builder.build();
         }
 
@@ -182,9 +173,24 @@ namespace TEAMModelOS.Controllers.Syllabus
                 request.@params.password.value = TmdCrypt.Encrypt(request.@params.password.value);
                 request.@params.password.isSet = true;
             }
+            ///假如更新了班级则先获取更新之前的班级 并移除相关联的
+            var olStudent = await azureCosmosDBRepository.FindById<Student>(request.@params.studentId, false);
+            if (olStudent != null && !olStudent.classroomCode.Equals(request.@params.classroomCode))
+            {
+                ClassroomStudent classroomstu = await azureCosmosDBRepository.FindById<ClassroomStudent>(olStudent.classroomCode);
+                if (classroomstu != null)
+                {
+                    if (classroomstu.studentId.Contains(request.@params.studentId))
+                    {
+                        classroomstu.studentId.Remove(request.@params.studentId);
+                        await azureCosmosDBRepository.SaveOrUpdate(classroomstu);
+                    }
+                }
+            }
+
             Student data = await azureCosmosDBRepository.SaveOrUpdate<Student>(request.@params);
             ///更新学生关系表
-            ClassroomStudent classroomStudent = await azureCosmosDBRepository.FindById<ClassroomStudent>(data.classroomCode);
+            ClassroomStudent classroomStudent = await azureCosmosDBRepository.FindById<ClassroomStudent>(data.classroomCode,false);
             if (classroomStudent == null)
             {
                 classroomStudent = new ClassroomStudent { id = data.classroomCode, scopeCode = data.schoolCode, studentId = new List<string> { data.studentId } };
@@ -199,20 +205,7 @@ namespace TEAMModelOS.Controllers.Syllabus
             await azureCosmosDBRepository.SaveOrUpdate(classroomStudent);
             return builder.Data(data).build();
         }
-        //[HttpPost("UpdateAllStudent")]
-        //public async Task<BaseJosnRPCResponse> UpdateAllStudent(JosnRPCRequest<Dictionary<string, Dictionary<string, object>>> request)
-        //{
-        //    JsonRPCResponseBuilder builder = JsonRPCResponseBuilder.custom();
-        //    bool find = request.@params.TryGetValue("find", out Dictionary<string, object> findObj);
-        //    bool update = request.@params.TryGetValue("update", out Dictionary<string, object> updateObj);
-        //    List<Student> data = null ;
-        //    if (find && update)
-        //    {
-        //        data = await azureCosmosDBRepository.FindByDict<Student>(findObj);
-        //        data = await azureCosmosDBRepository.UpdateAll<Student>(findObj, updateObj);
-        //    }
-        //    return builder.Data(data).build();
-        //}
+     
         /// <summary>
         /// 根据ID删除
         /// </summary>
@@ -224,8 +217,8 @@ namespace TEAMModelOS.Controllers.Syllabus
             JsonRPCResponseBuilder builder = JsonRPCResponseBuilder.custom();
             IdPk data = await azureCosmosDBRepository.DeleteAsync<Student>(request.@params.id, request.@params.schoolCode);
             ///更新学生关系表
-            Student student= await azureCosmosDBRepository.FindById<Student>(request.@params.id);
-            ClassroomStudent classroomStudent = await azureCosmosDBRepository.FindById<ClassroomStudent>(student.classroomCode);
+            Student student= await azureCosmosDBRepository.FindById<Student>(request.@params.id,false);
+            ClassroomStudent classroomStudent = await azureCosmosDBRepository.FindById<ClassroomStudent>(student.classroomCode,false);
             if (classroomStudent != null)
             {
                 classroomStudent.studentId.Remove(student.studentId);
@@ -248,8 +241,7 @@ namespace TEAMModelOS.Controllers.Syllabus
            // List<ClassroomStudent> classroomStudents = new List<ClassroomStudent>();
             foreach (var classroom in students.GroupBy(x => x.classroomCode).ToList())
             {
-
-                ClassroomStudent classroomStudent = await azureCosmosDBRepository.FindById<ClassroomStudent>(classroom.Key);
+                ClassroomStudent classroomStudent = await azureCosmosDBRepository.FindById<ClassroomStudent>(classroom.Key,false);
                 if (classroomStudent != null)
                 {
                     foreach (var classroomStudent1 in classroom)