Просмотр исходного кода

統測活動 預報名轉正式報名邏輯

jeff 2 месяцев назад
Родитель
Сommit
dcba95ca4d

+ 153 - 0
TEAMModelOS.SDK/Models/Service/JointService.cs

@@ -833,6 +833,159 @@ namespace TEAMModelOS.SDK.Models.Service
             return result;
         }
 
+        /// <summary>
+        /// 將活動預報名轉為正式報名
+        /// </summary>
+        /// <param name="client"></param>
+        /// <param name="jointEventId"></param>
+        /// <param name="jointGroupId"></param>
+        /// <param name="creatorId"></param>
+        /// <returns></returns>
+        public static async Task<List<JointEventGroupDb>> JointCoursePreToRegularAsync(CosmosClient client, string jointEventId, string jointGroupId, string creatorId)
+        {
+            long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
+            List<JointEventGroupDb> result = new List<JointEventGroupDb>();
+            //取得本活動所有預報名
+            List<string> groupIds = new List<string>();
+            List<JointEventGroupDb> jointCourseListPre = new List<JointEventGroupDb>();
+            StringBuilder stringBuilder = new($"SELECT * FROM c WHERE c.jointEventId = '{jointEventId}' ");
+            if (!string.IsNullOrWhiteSpace(jointGroupId))
+            {
+                stringBuilder.Append($" AND c.jointGroupId = '{jointGroupId}' ");
+            }
+            if (!string.IsNullOrWhiteSpace(creatorId))
+            {
+                stringBuilder.Append($" AND c.creatorId = '{creatorId}' ");
+            }
+            StringBuilder stringBuilderPre = new (stringBuilder.ToString());
+            stringBuilderPre.Append($" AND c.type = 'pre' ");
+            await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryStreamIteratorSql(queryText: stringBuilderPre.ToString(), requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("JointCourse") }))
+            {
+                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())
+                    {
+                        JointEventGroupDb jcourse = obj.ToObject<JointEventGroupDb>();
+                        jointCourseListPre.Add(jcourse);
+                        //取得groupList ID
+                        foreach (JointEventGroupCourse course in jcourse.courseLists)
+                        {
+                            foreach (JointEventGroupCourseGroup group in course.groupLists)
+                            {
+                                if(!groupIds.Contains(group.id))
+                                    groupIds.Add(group.id);
+                            }
+                        }
+                    }
+                }
+            }
+            //取得所有班級成員
+            List<RGroupList> groupList = new List<RGroupList>();
+            string gids = string.Join(",", groupIds.Select(x => $"'{x}'"));
+            StringBuilder stringBuilderGrp = new($"SELECT * FROM c WHERE c.id IN ({gids}) ");
+            await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryStreamIteratorSql(queryText: stringBuilderGrp.ToString(), requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("GroupList") }))
+            {
+                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())
+                    {
+                        RGroupList gp = obj.ToObject<RGroupList>();
+                        groupList.Add(gp);
+                    }
+                }
+            }
+            //刪除本活動所有正式報名
+            List<JointEventGroupDb> jointCourseListRegular = new List<JointEventGroupDb>();
+            StringBuilder stringBuilderRegular = new StringBuilder(stringBuilder.ToString());
+            stringBuilderRegular.Append($" AND c.type = 'regular' ");
+            await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryStreamIteratorSql(queryText: stringBuilderRegular.ToString(), requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("JointCourse") }))
+            {
+                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())
+                    {
+                        JointEventGroupDb jcourse = obj.ToObject<JointEventGroupDb>();
+                        jointCourseListRegular.Add(jcourse);
+                    }
+                }
+            }
+            if (jointCourseListRegular.Count > 0)
+            {
+                foreach (JointEventGroupDb jcRegular in jointCourseListRegular)
+                {
+                    await client.GetContainer(Constant.TEAMModelOS, "Teacher").DeleteItemAsync<JointEventGroupDb>(jcRegular.id, new PartitionKey("JointCourse"));
+                }
+            }
+            //生成本活動所有正式報名:有班級成員者才記入
+            List<JointEventGroupDb> jointCourseListRegularCrt = new List<JointEventGroupDb>();
+            foreach (JointEventGroupDb jcPre in jointCourseListPre)
+            {
+                foreach (JointEventGroupCourse jcPreCourse in jcPre.courseLists)
+                {
+                    foreach (JointEventGroupCourseGroup jcPreGroup in jcPreCourse.groupLists)
+                    {
+                        string groupId = jcPreGroup.id;
+                        RGroupList groupInfo = groupList.FirstOrDefault(g => g.id.Equals(groupId));
+                        if (groupInfo != null && groupInfo.members.Count > 0)
+                        {
+                            ///正式報名 JointCourse本體生成
+                            JointEventGroupDb jcRegularNow = jointCourseListRegularCrt.FirstOrDefault(j => j.jointEventId.Equals(jcPre.jointEventId) && j.jointGroupId.Equals(jcPre.jointGroupId) && j.creatorId.Equals(jcPre.creatorId));
+                            if (jcRegularNow == null)
+                            {
+                                JointEventGroupDb jcRegularNew = new JointEventGroupDb();
+                                jcRegularNew.id = Guid.NewGuid().ToString();
+                                jcRegularNew.code = "JointCourse";
+                                jcRegularNew.pk = "JointCourse";
+                                jcRegularNew.jointEventId = jcPre.jointEventId;
+                                jcRegularNew.jointGroupId = jcPre.jointGroupId;
+                                jcRegularNew.createTime = now;
+                                jcRegularNew.scope = "private";
+                                jcRegularNew.type = "regular";
+                                jcRegularNew.creatorId = jcPre.creatorId;
+                                jcRegularNew.creatorName = jcPre.creatorName;
+                                jcRegularNew.creatorEmail = jcPre.creatorEmail;
+                                jcRegularNew.schoolId = jcPre.schoolId;
+                                jcRegularNew.schoolName = jcPre.schoolName;
+                                jointCourseListRegularCrt.Add(jcRegularNew);
+                                jcRegularNow = jointCourseListRegularCrt.FirstOrDefault(j => j.jointEventId.Equals(jcPre.jointEventId) && j.jointGroupId.Equals(jcPre.jointGroupId) && j.creatorId.Equals(jcPre.creatorId) && j.type.Equals("regular") && j.scope.Equals("private"));
+                            }
+                            ///courseLists.row 生成
+                            JointEventGroupCourse jcRegularCourseNow = jcRegularNow.courseLists.FirstOrDefault(c => c.courseId.Equals(jcPreCourse.courseId));
+                            if (jcRegularCourseNow == null)
+                            {
+                                JointEventGroupCourse jcRegularCourseNew = new JointEventGroupBase.JointEventGroupCourse();
+                                jcRegularCourseNew.courseId = jcPreCourse.courseId;
+                                jcRegularCourseNew.courseName = jcPreCourse.courseName;
+                                jcRegularNow.courseLists.Add(jcRegularCourseNew);
+                                jcRegularCourseNow = jcRegularNow.courseLists.FirstOrDefault(c => c.courseId.Equals(jcPreCourse.courseId));
+                            }
+                            ///groupLists.row 生成
+                            JointEventGroupCourseGroup jcRegularCourseGroupNow = jcRegularCourseNow.groupLists.FirstOrDefault(g => g.id.Equals(jcPreGroup.id));
+                            if (jcRegularCourseGroupNow == null)
+                            {
+                                JointEventGroupCourseGroup jcRegularCourseGroupNew = new JointEventGroupBase.JointEventGroupCourseGroup();
+                                jcRegularCourseGroupNew.id = groupInfo.id;
+                                jcRegularCourseGroupNew.name = groupInfo.name;
+                                jcRegularCourseNow.groupLists.Add(jcRegularCourseGroupNew);
+                                jcRegularCourseGroupNow = jcRegularCourseNow.groupLists.FirstOrDefault(g => g.id.Equals(jcPreGroup.id));
+                            }
+                        }
+                    }
+                }
+            }
+            //計算各活動階段進行狀況
+            foreach (JointEventGroupDb jointCourse in jointCourseListRegularCrt)
+            {
+                string creator = jointCourse.creatorId;
+                JointEventGroupDb jointCourseCrt = await JointService.CalJointCourseGroupScheduleStatusAsync(client, jointEventId, jointGroupId, creator, jointCourse, null);
+                await client.GetContainer(Constant.TEAMModelOS, Constant.Teacher).CreateItemAsync<JointEventGroupDb>(jointCourseCrt, new PartitionKey(jointCourseCrt.code));
+                result.Add(jointCourseCrt);
+            }
+            return result;
+        }
 
         /// <summary>
         /// 計算決賽通過的老師課程名單用中間model

+ 30 - 13
TEAMModelOS/Controllers/Teacher/JointEventController.cs

@@ -562,7 +562,7 @@ namespace TEAMModelOS.Controllers.Common
                 StringBuilder stringBuilder = new($"SELECT * FROM c WHERE 1=1 ");
                 string jointEventId = (request.TryGetProperty("jointEventId", out JsonElement _jointEventId)) ? _jointEventId.ToString() : string.Empty;
                 string jointGroupId = (request.TryGetProperty("jointGroupId", out JsonElement _jointGroupId)) ? _jointGroupId.ToString() : string.Empty;
-                string type = (request.TryGetProperty("type", out JsonElement _type)) ? _type.ToString() : "regular"; //預設取得報名名單
+                string type = (request.TryGetProperty("type", out JsonElement _type)) ? _type.ToString() : "pre"; //regular:正式報名名單 pre:預報名名單 ※預設:"pre"
                 bool getFinalCourseFlg = (request.TryGetProperty("getFinal", out JsonElement _getFinal)) ? _getFinal.GetBoolean() : false; //是否要取得可晉級名單
                 if (string.IsNullOrWhiteSpace(jointEventId)) return BadRequest();
                 //取得活動
@@ -582,11 +582,7 @@ namespace TEAMModelOS.Controllers.Common
                 {
                     stringBuilder.Append($" AND c.creatorId = '{creatorId}' ");
                 }
-                if(type.Equals("regular"))
-                {
-                    stringBuilder.Append($" AND (c.type = 'regular' OR NOT IS_DEFINED(c.type) OR IS_NULL(c.type)) ");
-                }
-                else
+                if(!string.IsNullOrWhiteSpace(type))
                 {
                     stringBuilder.Append($" AND c.type = '{type}' ");
                 }
@@ -697,7 +693,7 @@ namespace TEAMModelOS.Controllers.Common
                 string provinceName = (request.TryGetProperty("provinceName", out JsonElement _provinceName)) ? _provinceName.ToString() : string.Empty;
                 string cityId = (request.TryGetProperty("cityId", out JsonElement _cityId)) ? _cityId.ToString() : string.Empty;
                 string cityName = (request.TryGetProperty("cityName", out JsonElement _cityName)) ? _cityName.ToString() : string.Empty;
-                string joinType = (request.TryGetProperty("joinType", out JsonElement _joinType)) ? _joinType.ToString() : "pre";
+                string joinType = (request.TryGetProperty("type", out JsonElement _joinType)) ? _joinType.ToString() : "pre"; //regular:正式報名名單 pre:預報名名單 ※預設:"pre"
                 string scope = (request.TryGetProperty("scope", out JsonElement _scope)) ? _scope.ToString() : string.Empty;
                 List<JointEventGroupBase.JointEventGroupCourse> courseLists = (request.TryGetProperty("courseLists", out JsonElement _courseLists)) ? _courseLists.ToObject<List<JointEventGroupBase.JointEventGroupCourse>>() : null;
                 if (string.IsNullOrWhiteSpace(jointEventId) || string.IsNullOrWhiteSpace(jointGroupId) || string.IsNullOrWhiteSpace(creatorId) || courseLists == null || string.IsNullOrWhiteSpace(scope))
@@ -706,7 +702,7 @@ namespace TEAMModelOS.Controllers.Common
                 }
                 //取得活動報名資料
                 JointEventGroupDb jointCourse = new JointEventGroupDb();
-                StringBuilder stringBuilder = new($"SELECT * FROM c WHERE c.jointEventId = '{jointEventId}' AND c.jointGroupId = '{jointGroupId}' AND c.creatorId = '{creatorId}' AND ( IS_DEFINED(c.type) = false OR c.type = 'regular' ) ");
+                StringBuilder stringBuilder = new($"SELECT * FROM c WHERE c.jointEventId = '{jointEventId}' AND c.jointGroupId = '{jointGroupId}' AND c.creatorId = '{creatorId}' AND c.type = '{joinType}' ");
                 await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryStreamIteratorSql(queryText: stringBuilder.ToString(), requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("JointCourse") }))
                 {
                     using var json = await JsonDocument.ParseAsync(item.Content);
@@ -750,7 +746,7 @@ namespace TEAMModelOS.Controllers.Common
                     jointCourse.provinceName = provinceName;
                     jointCourse.cityId = cityId;
                     jointCourse.cityName = cityName;
-                    jointCourse.type = (joinType.Equals("pre")) ? "pre" : "regular"; //regular:教師正式報名名單 pre:預報名名單
+                    jointCourse.type = (joinType.Equals("regular")) ? "regular" : "pre"; //regular:教師正式報名名單 pre:預報名名單 custom:決賽名單
                     //courseLists修正:防止course的重複生成
                     List<JointEventGroupBase.JointEventGroupCourse> courseListsFix = new List<JointEventGroupBase.JointEventGroupCourse>();
                     foreach (JointEventGroupBase.JointEventGroupCourse courseInfo in courseLists)
@@ -780,16 +776,19 @@ namespace TEAMModelOS.Controllers.Common
                         }
                     }
                     jointCourse.courseLists = courseListsFix;
-                    //各Schedule Status計算
-                    jointCourse = await JointService.CalJointCourseGroupScheduleStatusAsync(client, jointEventId, jointGroupId, creatorId, jointCourse, null);
+                    //各Schedule Status計算 ※正式報名才記入
+                    if(jointCourse.type.Equals("regular"))
+                    {
+                        jointCourse = await JointService.CalJointCourseGroupScheduleStatusAsync(client, jointEventId, jointGroupId, creatorId, jointCourse, null);
+                    }
                     //報名班級 DB更新
                     await client.GetContainer(Constant.TEAMModelOS, "Teacher").UpsertItemAsync<JointEventGroupDb>(jointCourse, new PartitionKey("JointCourse"));
                 }
 
                 //取得該老師報名該活動該組別且progress="going"的JointExam並生成Exam
                 List<JointExam> jointExams = new List<JointExam>();
-                ///個人
-                if (jointCourse.scope.Equals("private"))
+                ///個人、正式報名
+                if (jointCourse.scope.Equals("private") && jointCourse.type.Equals("regular"))
                 {
                     StringBuilder sqlJointExam = new($"SELECT * FROM c WHERE c.jointEventId = '{jointCourse.jointEventId}' AND c.jointGroupId = '{jointCourse.jointGroupId}' AND c.progress = 'going' AND c.examType = 'regular' ");
                     await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Common").GetItemQueryStreamIteratorSql(queryText: sqlJointExam.ToString(), requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("JointExam") }))
@@ -848,6 +847,24 @@ namespace TEAMModelOS.Controllers.Common
             return Ok(new { errCode = "", err = "" });
         }
 
+        /// <summary>
+        /// 將預報名轉為正式報名
+        /// </summary>
+        [ProducesDefaultResponseType]
+        [Authorize(Roles = "IES,HTCommunity")]
+        [HttpPost("course/pre-to-regular")]
+        public async Task<IActionResult> PreToRegular(JsonElement request)
+        {
+            var client = _azureCosmos.GetCosmosClient();
+            string jointEventId = (request.TryGetProperty("jointEventId", out JsonElement _jointEventId)) ? _jointEventId.ToString() : string.Empty;
+            string jointGroupId = (request.TryGetProperty("jointGroupId", out JsonElement _jointGroupId)) ? _jointGroupId.ToString() : string.Empty;
+            string creatorId = (request.TryGetProperty("creatorId", out JsonElement _creatorId)) ? _creatorId.ToString() : string.Empty;
+            if (string.IsNullOrWhiteSpace(jointEventId)) return BadRequest();
+
+            var result = await JointService.JointCoursePreToRegularAsync(client, jointEventId, jointGroupId, creatorId);
+            return Ok(result);
+        }
+
         /// <summary>
         /// 取得統測評量
         /// </summary>