Sfoglia il codice sorgente

Merge branch 'develop' of http://52.130.252.100:10000/TEAMMODEL/TEAMModelOS into develop

CrazyIter_Bin 1 anno fa
parent
commit
a73d855c7f

+ 6 - 7
TEAMModelBI/Controllers/BITmid/TmidController.cs

@@ -187,21 +187,20 @@ namespace TEAMModelBI.Controllers.BITmid
                             if (string.IsNullOrWhiteSpace(tmidStics.mobile)) tmidStics.mobile = GenDataMask(doc.GetProperty("mobile").GetString(), "mobile");
                             if (string.IsNullOrWhiteSpace(tmidStics.mail)) tmidStics.mail = GenDataMask(doc.GetProperty("mail").GetString(), "mail");
                             string country = doc.GetProperty("country").GetString();
-                            tmidStics.country = (regionData.country.ContainsKey(country)) ? regionData.country[country].name : country;
-                            tmidStics.country = tmidStics.country.Replace("地區", "").Replace("地区", "");
                             string province = doc.GetProperty("province").GetString();
                             string city = doc.GetProperty("city").GetString();
                             string district = string.Empty;
-                            if (country.Equals("TW"))
+                            if (!string.IsNullOrWhiteSpace(country) && country.Equals("TW"))
                             {
                                 district = city;
                                 city = province;
                                 province = string.Empty;
                             }
-                            tmidStics.province = (regionData.country.ContainsKey(country) && regionData.province.ContainsKey(country) && regionData.province[country].ContainsKey(province)) ? regionData.province[country][province].name : province;
-                            if (country.Equals("TW"))
-                                tmidStics.city = (regionData.city.ContainsKey(country) && regionData.city[country]["tw"].ContainsKey(city)) ? regionData.city[country]["tw"][city].name : city;
-                            else if(country.Equals("CN"))
+                            tmidStics.country = (!string.IsNullOrWhiteSpace(country) && regionData.country.ContainsKey(country)) ? regionData.country[country].name.Replace("地區", "").Replace("地区", "") : country;
+                            tmidStics.province = (!string.IsNullOrWhiteSpace(country) && regionData.country.ContainsKey(country) && !string.IsNullOrWhiteSpace(province) && regionData.province.ContainsKey(country) && regionData.province[country].ContainsKey(province)) ? regionData.province[country][province].name : province;
+                            if (!string.IsNullOrWhiteSpace(country) && country.Equals("TW"))
+                                tmidStics.city = (regionData.city.ContainsKey(country) && !string.IsNullOrWhiteSpace(city) && regionData.city[country]["tw"].ContainsKey(city)) ? regionData.city[country]["tw"][city].name : city;
+                            else if(!string.IsNullOrWhiteSpace(country) && country.Equals("CN"))
                                 tmidStics.city = (regionData.city.ContainsKey(country) && regionData.city[country].ContainsKey(province) && regionData.city[country][province].ContainsKey(city)) ? regionData.city[country][province][city].name : city;
                             tmidStics.dist = (!string.IsNullOrWhiteSpace(district) && country.Equals("TW") && regionData.city[country]["tw"].ContainsKey(city) && regionData.dist[country]["tw"][city].ContainsKey(district)) ? regionData.dist[country]["tw"][city][district].name : district;
                             tmidStics.schoolCode = (doc.TryGetProperty("schoolCode", out JsonElement schCode)) ? schCode.GetString() : string.Empty;

+ 7 - 0
TEAMModelOS.FunctionV4/CosmosDB/TriggerExam.cs

@@ -324,6 +324,10 @@ namespace TEAMModelOS.FunctionV4
                                                 ids.Add((member.id, member.code));
                                             }
                                             //ids = members.Where(c => c.id.Equals(cla)).SelectMany(m => m.members).Select(g => g.id).ToList();
+                                            List<string> blobs = new();
+                                            if (info.qamode == 2) {
+                                                blobs =  info.papers.Where(c => c.subjectId.Equals(subject.id)).Select(c => c.blob).ToList();
+                                            }
                                             foreach (var (sId, scode) in ids)
                                             {
                                                 result.mark.Add(marks);
@@ -334,6 +338,9 @@ namespace TEAMModelOS.FunctionV4
                                                 result.ans.Add(anses);
                                                 result.sum.Add(0);
                                                 result.status.Add(1);
+                                                if (info.qamode == 2) {
+                                                    result.paper.Add(blobs[new Random().Next(blobs.Count)]);
+                                                }                                              
                                             }
 
                                             //result.progress = info.progress;

+ 1 - 0
TEAMModelOS.SDK/Models/Cosmos/Common/ExamClassResult.cs

@@ -57,6 +57,7 @@ namespace TEAMModelOS.SDK.Models
         public List<int> fphc { get; set; } = new List<int>();
         public List<int> fplc { get; set; } = new List<int>();
         public List<int> fpc { get; set; } = new List<int>();
+        public List<string> paper { get; set; }= new List<string>();
     }
 /*    public class PaperSimple {
         public string id { get; set; }

+ 16 - 10
TEAMModelOS.SDK/Models/Service/StudentService.cs

@@ -219,11 +219,11 @@ namespace TEAMModelOS.SDK
                     subjectSQL = $" and  c.id in ({string.Join(",", subjects.Select(z => $"'{z}'"))}) ";
                 }
 
-                StringBuilder SQL = new StringBuilder($"select {filed} from c  {subjectJoin} where c.pk='Art' {subjectSQL} {groupListSQL} and c.startTime>={stime} and c.startTime <= {etime} ");
+                StringBuilder SQL = new($"select {filed} from c  {subjectJoin} where c.pk='Art' {subjectSQL} {groupListSQL} and c.startTime>={stime} and c.startTime <= {etime} ");
                 //获取学校发布的活动
                 if (userScope.Equals(Constant.ScopeStudent) && !string.IsNullOrWhiteSpace(school))
                 {
-                    var resultSchool = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.Common).GetList<StudentActivity>($"{SQL.ToString()}  and c.school='school' ", $"Art-{school}");
+                    var resultSchool = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.Common).GetList<StudentActivity>($"{SQL}  and c.school='school' ", $"Art-{school}");
                     activities.AddRange(resultSchool.list);
                 }
             }
@@ -1761,10 +1761,10 @@ namespace TEAMModelOS.SDK
             {
                 //TODO : 進階查詢選項調整、部分地方可用並行處理
                 //以學校學生角度去抓資料
-                Dictionary<string, List<(string id, string name, string picture, int year, string no, string periodId, string irs, string imei, string gender, int graduate, List<StudentGuardian> guardians)>> dicClassStuds =
-                    new Dictionary<string, List<(string id, string name, string picture, int year, string no, string periodId, string irs, string imei, string gender, int graduate, List<StudentGuardian> guardians)>>();
-                List<(string id, string name, string picture, int year, string no, string periodId, string irs, string imei, string gender, int graduate, List<StudentGuardian> guardians)> notJoinClassStuds =
-                    new List<(string id, string name, string picture, int year, string no, string periodId, string irs, string imei, string gender, int graduate, List<StudentGuardian> guardians)>();
+                Dictionary<string, List<(string id, string name, string picture, int year, string no, string periodId, string irs, string imei, string gender, int graduate, bool hasEduOpenId, List<StudentGuardian> guardians)>> dicClassStuds =
+                    new Dictionary<string, List<(string id, string name, string picture, int year, string no, string periodId, string irs, string imei, string gender, int graduate, bool hasEduOpenId, List<StudentGuardian> guardians)>>();
+                List<(string id, string name, string picture, int year, string no, string periodId, string irs, string imei, string gender, int graduate, bool hasEduOpenId, List<StudentGuardian> guardians)> notJoinClassStuds =
+                    new List<(string id, string name, string picture, int year, string no, string periodId, string irs, string imei, string gender, int graduate, bool hasEduOpenId, List<StudentGuardian> guardians)>();
 
 
                 List<Imei> imeis = new List<Imei>();
@@ -1859,7 +1859,8 @@ namespace TEAMModelOS.SDK
                                             $"{irs}",
                                             imeiObj?.id,//imei
                                             acc.TryGetProperty("gender", out JsonElement _gender) && _gender.ValueKind.Equals(JsonValueKind.String) ? _gender.GetString() : null,
-                                              acc.TryGetProperty("graduate", out JsonElement _graduate) && _graduate.ValueKind.Equals(JsonValueKind.Number) ? int.Parse($"{_graduate}") : 0,
+                                            acc.TryGetProperty("graduate", out JsonElement _graduate) && _graduate.ValueKind.Equals(JsonValueKind.Number) ? int.Parse($"{_graduate}") : 0,
+                                            acc.TryGetProperty("openId", out JsonElement _openId) && !string.IsNullOrWhiteSpace(_openId.GetString()) ? true : false,
                                             guardians
                                         )
                                     );
@@ -1878,7 +1879,8 @@ namespace TEAMModelOS.SDK
                                                 $"{irs}",
                                                 imeiObj?.id,//imei
                                                 acc.TryGetProperty("gender", out JsonElement _gender) && _gender.ValueKind.Equals(JsonValueKind.String) ? _gender.GetString() : null,
-                                                  acc.TryGetProperty("graduate", out JsonElement _graduate) && _graduate.ValueKind.Equals(JsonValueKind.Number) ? int.Parse($"{_graduate}") : 0,
+                                                acc.TryGetProperty("graduate", out JsonElement _graduate) && _graduate.ValueKind.Equals(JsonValueKind.Number) ? int.Parse($"{_graduate}") : 0,
+                                                acc.TryGetProperty("openId", out JsonElement _openId) && !string.IsNullOrWhiteSpace(_openId.GetString()) ? true : false,
                                                 guardians
                                             )
                                         );
@@ -1886,7 +1888,7 @@ namespace TEAMModelOS.SDK
                                 else
                                 {
                                     dicClassStuds.Add(classId,
-                                            new List<(string id, string name, string picture, int year, string no, string periodId, string irs, string imei, string gender, int graduate, List<StudentGuardian> guardians)>()
+                                            new List<(string id, string name, string picture, int year, string no, string periodId, string irs, string imei, string gender, int graduate, bool hasEduOpenId, List<StudentGuardian> guardians)>()
                                             {
                                                 (
                                                     acc.GetProperty("id").GetString(),
@@ -1897,7 +1899,8 @@ namespace TEAMModelOS.SDK
                                                     $"{irs}"  ,
                                                     imeiObj?.id,//imei
                                                     acc.TryGetProperty("gender", out JsonElement _gender) && _gender.ValueKind.Equals(JsonValueKind.String) ? _gender.GetString() : null,
-                                                      acc.TryGetProperty("graduate", out JsonElement _graduate) && _graduate.ValueKind.Equals(JsonValueKind.Number) ? int.Parse($"{_graduate}"): 0,
+                                                    acc.TryGetProperty("graduate", out JsonElement _graduate) && _graduate.ValueKind.Equals(JsonValueKind.Number) ? int.Parse($"{_graduate}"): 0,
+                                                    acc.TryGetProperty("openId", out JsonElement _openId) && !string.IsNullOrWhiteSpace(_openId.GetString()) ? true : false,
                                                     guardians
                                                 )
                                             }
@@ -1952,6 +1955,7 @@ namespace TEAMModelOS.SDK
                                          imei = o.imei,
                                          gender = o.gender,
                                          graduate = o.graduate,
+                                         hasEduOpenId = o.hasEduOpenId,
                                          guardians = o.guardians,
                                      });
                     ret.AddRange(tmp);
@@ -1975,6 +1979,7 @@ namespace TEAMModelOS.SDK
                         irs = o.irs,
                         imei = o.imei,
                         gender = o.gender,
+                        hasEduOpenId = o.hasEduOpenId,
                         guardians = o.guardians,
                     }));
 
@@ -1999,6 +2004,7 @@ namespace TEAMModelOS.SDK
                         irs = o.irs,
                         imei = imeiObj?.id,
                         gender = o.gender,
+                        hasEduOpenId = (!string.IsNullOrWhiteSpace(o.openId)) ? true : false,
                         guardians = o.guardians,
                     });
                 });

+ 29 - 23
TEAMModelOS/Controllers/Common/ExamController.cs

@@ -3773,7 +3773,7 @@ namespace TEAMModelOS.Controllers
 
             //return Ok(new { record, avaliable });
 
-            Dictionary<string, string> dict = new();
+            Dictionary<string, List<string>> dict = new();
 
 
             await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Student").GetItemQueryStreamIterator(queryText: qry, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"{pk}") }))
@@ -3783,22 +3783,16 @@ namespace TEAMModelOS.Controllers
                 {
                     foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
                     {
-                        if (!dict.ContainsKey(obj.GetProperty("id").ToString()))
-                        {//去重複
-                            dict.Add(obj.GetProperty("id").ToString(), obj.GetProperty("blob").ToString());
-                            //if (!String.IsNullOrWhiteSpace(obj.GetProperty("pId") + ""))
-                            //{
-                            //    if (!dict.ContainsKey(obj.GetProperty("pId").ToString()))
-                            //    {
-
-
-                            //        dict.Add(obj.GetProperty("pId").ToString(), obj.GetProperty("blob").ToString());
-                            //    }
-                            //}
-                            //else
-                            //{
-                            //    dict.Add(obj.GetProperty("id").ToString(), obj.GetProperty("blob").ToString());
-                            //}
+                        if(dict.ContainsKey(obj.GetProperty("id").ToString()))
+                        {
+                            if (!dict[obj.GetProperty("id").ToString()].Contains(obj.GetProperty("blob").ToString()))
+                            {
+                                dict[obj.GetProperty("id").ToString()].Add(obj.GetProperty("blob").ToString());
+                            }
+                        }
+                        else
+                        {
+                            dict.Add(obj.GetProperty("id").ToString(), new List<string> { obj.GetProperty("blob").ToString() });
                         }
                     }
                 }
@@ -3808,16 +3802,27 @@ namespace TEAMModelOS.Controllers
             {
                 if (schCode != null && schCode != "noschoolid")
                 {
-                    if (_azureStorage.GetBlobContainerClient(schCode).GetBlobClient($"{obj.Value}/{obj.Key}.json").Exists())
-                    {// 去除blob不存在項目
-                        avaliable++;
+                    string qid = obj.Key;
+                    List<string> blobs = obj.Value;
+                    foreach(string blob in blobs)
+                    {
+                        if (_azureStorage.GetBlobContainerClient(schCode).GetBlobClient($"{blob}/{qid}.json").Exists())
+                        {// 去除blob不存在項目
+                            avaliable++;
+                        }
                     }
+                    
                 }
                 else if (!string.IsNullOrWhiteSpace(teacherid))
                 {
-                    if (_azureStorage.GetBlobContainerClient(teacherid).GetBlobClient($"{obj.Value}/{obj.Key}.json").Exists())
+                    string qid = obj.Key;
+                    List<string> blobs = obj.Value;
+                    foreach (string blob in blobs)
                     {
-                        avaliable++;
+                        if (_azureStorage.GetBlobContainerClient(teacherid).GetBlobClient($"{blob}/{qid}.json").Exists())
+                        {
+                            avaliable++;
+                        }
                     }
                 }
             }
@@ -4304,7 +4309,8 @@ namespace TEAMModelOS.Controllers
             }
             var error = elements.Select(c => new { 
                 c.items,
-                c.attachments
+                c.attachments,
+                c.qamode
             });
             return Ok(new { error });
         }

+ 87 - 1
TEAMModelOS/Controllers/Student/StudentController.cs

@@ -22,6 +22,7 @@ using Microsoft.AspNetCore.Cryptography.KeyDerivation;
 using Microsoft.AspNetCore.Hosting;
 using Microsoft.AspNetCore.Http;
 using Microsoft.AspNetCore.Mvc;
+using Microsoft.Azure.Amqp.Framing;
 using Microsoft.Extensions.Configuration;
 using Microsoft.Extensions.Hosting;
 using Microsoft.Extensions.Options;
@@ -870,7 +871,7 @@ namespace TEAMModelOS.Controllers
                             curPeriod = school.period.Where(p => p.id.Equals(studata.periodId)).FirstOrDefault();
                         }
                         ExamSimple gradeInfo = new ExamSimple();
-                        if (string.IsNullOrWhiteSpace(curPeriod.id))
+                        if (!string.IsNullOrWhiteSpace(curPeriod.id))
                         {
                             gradeInfo = getGradeInfoByYear(studata.year, curPeriod);
                         }
@@ -993,6 +994,91 @@ namespace TEAMModelOS.Controllers
             }
         }
 
+        /// <summary>
+        /// 學生解綁教育雲ID
+        /// </summary>
+        /// <param name = "request" ></ param >
+        [AllowAnonymous]
+        [HttpPost("rmv-open-stu")]
+        public async Task<IActionResult> rmvOpenidToStudent(JsonElement request)
+        {
+            if (!request.TryGetProperty("school_code", out JsonElement _school_code)) return BadRequest();
+            string schoolCode = _school_code.GetString();
+            if(string.IsNullOrWhiteSpace(schoolCode)) return BadRequest();
+            if (!request.TryGetProperty("stuid", out JsonElement _stuid)) return BadRequest();
+            string stuId = _stuid.GetString();
+            if(string.IsNullOrWhiteSpace(stuId)) return BadRequest();
+            string openType = (request.TryGetProperty("type", out JsonElement _type)) ? _type.GetString().ToLower() : "educloudtwl"; //educloudtwl: 教育雲
+
+            var client = _azureCosmos.GetCosmosClient();
+            var studentClient = client.GetContainer(Constant.TEAMModelOS, "Student");
+            //取得學生基本資料
+            Student student = new Student();
+            try
+            {
+                student = await studentClient.ReadItemAsync<Student>($"{stuId}", new PartitionKey($"Base-{schoolCode}"));
+            }
+            catch (CosmosException ex)
+            {
+                return Ok(new { error = 2, message = "Can not find student data." });
+            }
+            //CSV解綁
+            bool rmvSuccess = false;
+            if (openType.Equals("educloudtwl")) //教育雲
+            {
+                string openId = student.openId;
+                if(string.IsNullOrWhiteSpace(openId)) //學生無OpenID,視為解綁成功
+                {
+                    return Ok(new { error = 0, message = string.Empty });
+                }
+                stuOpenData openData = new stuOpenData();
+                var httpClient = _httpClient.CreateClient();
+                string AccessToken = await getCoreAccessToken();
+                httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", AccessToken);
+                string csv2Domain = _configuration.GetValue<string>("HaBookAuth:CoreAPI");
+                string csv2Url = $"{csv2Domain}/oauth2/EduCloudTWBingManage";
+                Dictionary<string, object> dict = new() {
+                    {   "grant_type", "unbind" },
+                    {   "open_id", openId },
+                    {   "id", $"Base-{schoolCode},{student.id}" }
+                };
+                HttpContent content = new StringContent(dict.ToJsonString(), Encoding.UTF8, "application/json");
+                HttpResponseMessage httpResponse = await httpClient.PostAsync(csv2Url, content);
+                if (httpResponse.StatusCode == HttpStatusCode.OK)
+                {
+                    string responseContent = await httpResponse.Content.ReadAsStringAsync();
+                    if (string.IsNullOrWhiteSpace(responseContent))
+                    {
+                        rmvSuccess = true;
+                    }
+                    else
+                    {
+                        csApiResponse csResult = responseContent.ToObject<csApiResponse>();
+                        if (!string.IsNullOrWhiteSpace(csResult.message))
+                        {
+                            return Ok(new { error = csResult.error, message = csResult.message });
+                        }
+                    }
+                }
+                else
+                {
+                    return Ok(new { error = 1, message = "Can not get opendata from CS." });
+                }
+            }
+            //學生資料變更
+            if(rmvSuccess)
+            {
+                if (openType.Equals("educloudtwl")) //教育雲
+                {
+                    student.openId = null;
+                }
+                //DB更新
+                await studentClient.ReplaceItemAsync(student, student.id);
+                return Ok(new { error = 0, message = string.Empty });
+            }
+            return Ok(new { error = 9, message = "Can not unbind student." });
+        }
+
         //查询学生名单详情
         [ProducesDefaultResponseType]
         //[AuthToken(Roles = "teacher")]