jeff 4 месяцев назад
Родитель
Сommit
8d4a41172a
1 измененных файлов с 47 добавлено и 10 удалено
  1. 47 10
      TEAMModelBI/Controllers/BICommon/BINoticeController.cs

+ 47 - 10
TEAMModelBI/Controllers/BICommon/BINoticeController.cs

@@ -1112,7 +1112,10 @@ namespace TEAMModelBI.Controllers.BICommon
                     //輸出模式:TMID單位
                     if(output.Equals("tmid"))
                     {
-                        tmidInfos.Add(new AreaInfo() { id = tmId, name = tmName });
+                        if (tmidInfos.FirstOrDefault(t => t.id.Equals(tmId)) == null)
+                        {
+                            tmidInfos.Add(new AreaInfo() { id = tmId, name = tmName });
+                        }
                     }
                     //輸出模式:地理位置單位
                     else {
@@ -1178,10 +1181,10 @@ namespace TEAMModelBI.Controllers.BICommon
                 Dictionary<string, string> schGeoDic = new Dictionary<string, string>(); //key: schoolId val:統計的geoId
                 Dictionary<string, string> schNameDic = new Dictionary<string, string>(); //key: schoolId val:學校名稱
                 Dictionary<string, int> schTeacherCntDic = new Dictionary<string, int>(); //key: schoolId val:學校教師數
+                List<string> teacherCodes = new List<string>();
                 if (!string.IsNullOrWhiteSpace(sqlWhere))
                 {
                     //取得學校ID列表
-                    List<string> teacherCodes = new List<string>();
                     sqlWhere = $" WHERE {sqlWhere}";
                     string sqlSch = $"SELECT c.id, c.name, c.region as country, c.province, c.city, c.dist FROM c {sqlWhere} ";
                     ///實體校
@@ -1277,6 +1280,20 @@ namespace TEAMModelBI.Controllers.BICommon
                 //資料整理2 移除無學校學區
                 var result = geoInfos.Where(a => a.scCnt > 0).ToList();
                 if (groupKey.Equals("name")) groupKey = "school";
+                //資料整理3 取得TMID列表
+                if(output.Equals("tmid"))
+                {
+                    string sqlTch = $"SELECT REPLACE(c.code, 'Teacher-', '') as schoolId, c.id, c.name FROM c WHERE c.pk = 'Teacher' AND c.status = 'join' AND ARRAY_CONTAINS({JsonSerializer.Serialize(teacherCodes)}, c.code)";
+                    await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, Constant.School).GetItemQueryIteratorSql<JsonElement>(queryText: sqlTch, requestOptions: null))
+                    {
+                        string tmid = item.GetProperty("id").ToString();
+                        string name = item.GetProperty("name").ToString();
+                        if(tmidInfos.FirstOrDefault(t => t.id.Equals(tmid)) == null)
+                        {
+                            tmidInfos.Add(new AreaInfo() { id = tmid, name = name });
+                        }
+                    }
+                }
             }
 
             //輸出
@@ -1555,6 +1572,7 @@ namespace TEAMModelBI.Controllers.BICommon
             string body = (jsonElement.TryGetProperty("body", out JsonElement _body)) ? _body.ToString() : string.Empty; //內文
             var dataParam = (jsonElement.TryGetProperty("data", out JsonElement _data)) ? _data : new JsonElement { };
             List<NotifyAction> action = (jsonElement.TryGetProperty("action", out JsonElement _action)) ? _action.ToObject<List<NotifyAction>>() : new List<NotifyAction>();
+            long send = (jsonElement.TryGetProperty("send", out JsonElement _send)) ? _send.GetInt64() : 0; //發送時間 0:立即發送
 
             if (target == null || string.IsNullOrWhiteSpace(type) || string.IsNullOrWhiteSpace(method) || string.IsNullOrWhiteSpace(title) || string.IsNullOrWhiteSpace(body)) return BadRequest();
             string eventKey = "bi-gen-notify";
@@ -1563,12 +1581,12 @@ namespace TEAMModelBI.Controllers.BICommon
             DataInNotify data = new DataInNotify();
             if(dataParam.ValueKind != JsonValueKind.Undefined) data.value = dataParam;
             if (action.Count > 0) data.action = action;
-            var result = await SendMessageCore(target, type, method, title, body, data, eventId, eventName);
+            var result = await SendMessageCore(target, type, method, title, body, data, send, eventId, eventName);
 
             return Ok(new { state = RespondCode.Ok, result });
         }
         ///寄發訊息核心邏輯
-        private async Task<object> SendMessageCore(SendMessageParam target, string type, string method, string title, string body, DataInNotify data, string eventId = "", string eventName = "")
+        private async Task<object> SendMessageCore(SendMessageParam target, string type, string method, string title, string body, DataInNotify data, long send, string eventId = "", string eventName = "")
         {
             #region target 內容例
             //{
@@ -1635,9 +1653,17 @@ namespace TEAMModelBI.Controllers.BICommon
             if (type.Equals("notify")) //端內外通知
             {
                 List<string> tmIds = tmidInfos.Select(i => i.id).ToList();
-                HttpResponseMessage response = CallPushNotifyApi(tmIds, title, body, data, eventId, eventName);
-                var result = new { status = response.StatusCode, content = await response.Content.ReadAsStringAsync() };
-                return result;
+                if(send.Equals(0)) //立即寄送
+                {
+                    HttpResponseMessage response = CallPushNotifyApi(tmIds, title, body, data, eventId, eventName);
+                    var result = new { status = response.StatusCode, content = await response.Content.ReadAsStringAsync() };
+                    return result;
+                }
+                else //定時寄送 [待做]
+                {
+
+                }
+                
             }
             else if(type.Equals("mail")) //Email
             {
@@ -1686,10 +1712,21 @@ namespace TEAMModelBI.Controllers.BICommon
             List<string> tmids = new List<string>();
             foreach (Geo geo in geos)
             {
-                var (geoInfos, _) = await GetDataByGeo("tmid", "tmid", true, geo.countryId, geo.provinceId, geo.cityId);
-                foreach (var geoInfo in geoInfos)
+                if(!string.IsNullOrWhiteSpace(geo.type) && geo.type.Equals("tmid"))
                 {
-                    if (!tmids.Contains(geoInfo.id)) tmids.Add(geoInfo.id);
+                    var (geoInfos, _) = await GetDataByGeo("tmid", "tmid", true, geo.countryId, geo.provinceId, geo.cityId);
+                    foreach (var geoInfo in geoInfos)
+                    {
+                        if (!tmids.Contains(geoInfo.id)) tmids.Add(geoInfo.id);
+                    }
+                }
+                else if(!string.IsNullOrWhiteSpace(geo.type) && geo.type.Equals("school"))
+                {
+                    var (geoInfos, _) = await GetDataByGeo("school", "tmid", true, geo.countryId, geo.provinceId, geo.cityId);
+                    foreach (var geoInfo in geoInfos)
+                    {
+                        if (!tmids.Contains(geoInfo.id)) tmids.Add(geoInfo.id);
+                    }
                 }
             }
             return tmids;