Przeglądaj źródła

[BI]訊息推送-Email(途中)

jeff 3 miesięcy temu
rodzic
commit
bd9b71bc5e

+ 2 - 2
TEAMModelBI/ClientApp/src/view/systemConfig/NewMsg/index.vue

@@ -156,8 +156,8 @@
     // 定义 active 变量并设定初始值
     const active = ref(0);
 
-    // 批量或個別:  BatchType, SingleType
-    const selectionType = ref('BatchType');
+    // 批量或個別:  single:個別 multi:批次
+    const selectionType = ref('multi');
     provide("selectionType", selectionType);
 
     // 通知類型

+ 4 - 4
TEAMModelBI/ClientApp/src/view/systemConfig/NewMsg/new_msg_Type.vue

@@ -21,8 +21,8 @@
             </template>
 
             <el-radio-group v-model="selectionType" style="margin-top: 20px;">
-                <el-radio label="BatchType">批量挑選</el-radio>
-                <el-radio label="SingleType" disabled>個別挑選</el-radio>
+                <el-radio label="multi">批量挑選</el-radio>
+                <el-radio label="single" disabled>個別挑選</el-radio>
                 
             </el-radio-group>
 
@@ -68,9 +68,9 @@
     const goNextStep = () => {
         //emit('update-active', 1);  // 通知父组件更新 active 的值
 
-        if (selectionType.value === 'BatchType') {
+        if (selectionType.value === 'multi') {
             emit('update-active', 1);  // 通知父组件更新 active 的值
-        } else if (selectionType.value === 'SingleType') {
+        } else if (selectionType.value === 'single') {
             emit('update-active', 2);  // 通知父组件更新 active 的值
         }
 

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

@@ -38,6 +38,7 @@ using System.Text.RegularExpressions;
 using MathNet.Numerics.Distributions;
 using System.Drawing.Drawing2D;
 using Microsoft.Azure.Amqp.Framing;
+using TEAMModelOS.SDK.DI.IPIP;
 
 
 namespace TEAMModelBI.Controllers.BICommon
@@ -1279,7 +1280,9 @@ namespace TEAMModelBI.Controllers.BICommon
             //     ]
             //}
             #endregion
+            var cosmosClientIes = _azureCosmos.GetCosmosClient();
             var cosmosClientCsv2 = _azureCosmos.GetCosmosClient(name: "CoreServiceV2");
+            var (_tmdId, _tmdName, _, _, _, _) = HttpJwtAnalysis.JwtXAuthBI(HttpContext.GetXAuth("AuthToken"), _option);
 
             List<string> tmids_area = await GetTmidByAreaId(target.area);
             List<string> tmids_geo = await GetTmidByGeo(target.geo);
@@ -1294,6 +1297,10 @@ namespace TEAMModelBI.Controllers.BICommon
             tmids = tmids.Union(tmids_school).ToList();
             tmids = tmids.Union(tmids_direct).ToList();
             tmids = tmids.Distinct().ToList();  //唯一化
+#if DEBUG //測試模式時限制TMID帳號,正式站佈署時不生效
+            List<string> filterTmid = new List<string>() { "1522758684", "1595321354" };
+            tmids = tmids.Intersect(filterTmid).ToList();
+#endif
 
             //取得TMID資料
             List<IdInfo> tmidInfos = new List<IdInfo>();
@@ -1324,7 +1331,18 @@ namespace TEAMModelBI.Controllers.BICommon
                 List<string> tmIds = tmidInfos.Select(i => i.id).ToList();
                 if(send.Equals(0)) //立即寄送
                 {
+                    //訊息寄送
                     HttpResponseMessage response = CallPushNotifyApi(tmIds, title, body, sender, hubName, template, data, eventId, eventName);
+                    //寄送訊息DB記入
+                    if(response.IsSuccessStatusCode)
+                    {
+                        BINotice bINotice = TransMsgRequestToBINotice("notify", method, subject, title, body, sender, hubName, target, tmIds, data);
+                        bINotice.id = Guid.NewGuid().ToString();
+                        bINotice.createId = _tmdId;
+                        bINotice.sendTime = (send.Equals(0)) ? DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() : send; //先處理"及時寄送","定時寄送"待處理
+                        await cosmosClientIes.GetContainer(Constant.TEAMModelOS, "Common").CreateItemAsync<BINotice>(bINotice, new PartitionKey("BINotice"));
+                    }
+
                     var result = new { status = response.StatusCode, content = await response.Content.ReadAsStringAsync() };
                     return result;
                 }
@@ -1336,13 +1354,28 @@ namespace TEAMModelBI.Controllers.BICommon
             }
             else if(type.Equals("mail")) //Email
             {
+                string defaultTemplate = "d-f1c5abd8247f4be79ceaecdd327e9a68";
                 List<string> tmIds = tmidInfos.Where(i => !string.IsNullOrWhiteSpace(i.mail)).Select(i => i.id).ToList();
                 //呼叫Email API [未]
                 ///模板設定
                 if(_option.Location.Contains("Global"))
                 {
-                    if (string.IsNullOrWhiteSpace(template)) template = "d-f1c5abd8247f4be79ceaecdd327e9a68"; //若未指定模板ID,用預設模板
+                    if (string.IsNullOrWhiteSpace(template))
+                        template = defaultTemplate; //若未指定模板ID,用預設模板
                 }
+
+                //Email寄送
+                HttpResponseMessage response = CallPushNotifyApi(tmIds, title, body, sender, hubName, template, data, eventId, eventName);
+                //寄送訊息DB記入
+                if (response.IsSuccessStatusCode)
+                {
+                    BINotice bINotice = TransMsgRequestToBINotice("notify", method, subject, title, body, sender, hubName, target, tmIds, data);
+                    bINotice.id = Guid.NewGuid().ToString();
+                    bINotice.createId = _tmdId;
+                    bINotice.sendTime = (send.Equals(0)) ? DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() : send; //先處理"及時寄送","定時寄送"待處理
+                    await cosmosClientIes.GetContainer(Constant.TEAMModelOS, "Common").CreateItemAsync<BINotice>(bINotice, new PartitionKey("BINotice"));
+                }
+
                 var result = new { };
                 return result;
             }
@@ -1558,10 +1591,6 @@ namespace TEAMModelBI.Controllers.BICommon
             NotifyData notify = new NotifyData();
             notify.hubName = hubName;
             notify.sender = sender;
-//#if DEBUG //測試模式時限制TMID帳號,正式站佈署時不生效
-//            List<string> filterTmid = new List<string>() { "1522758684", "1595321354" };
-//            tmIds = tmIds.Intersect(filterTmid).ToList();
-//#endif
             notify.tags = tmIds.Select(s => $"{s}_{sender}").ToList();
             notify.title = title;
             notify.body = body;
@@ -1583,15 +1612,153 @@ namespace TEAMModelBI.Controllers.BICommon
             return responseMessage;
         }
 
-        //BI訊息推送DB記入
-        private async Task<BINotice> CrtBiNoticeData(BINotice bINotice)
+        private HttpResponseMessage CallSendMailApi(List<string> tmIds, string subject, string title, string body, string template, string data, string eventId = "", string eventName = "")
         {
-            var cosmosClientIes = _azureCosmos.GetCosmosClient(); //IES
-            bINotice.id = Guid.NewGuid().ToString();
-            BINotice result = await cosmosClientIes.GetContainer(Constant.TEAMModelOS, "Common").CreateItemAsync<BINotice>(bINotice, new PartitionKey("BINotice"));
+            //{
+            //    "sub":"a",
+            //    "title":"嘿嘿",
+            //    "body":"見鬼拉",
+            //    "image":"https://corestorageservice.blob.core.windows.net/public-marketing/S__43540491.png"
+
+            //}
+        }
+
+        /// <summary>
+        /// 取得訊息寄送紀錄
+        /// </summary>
+        /// <param name="msgType"></param>
+        /// <param name="selType"></param>
+        /// <param name="theme">標題(title)</param>
+        /// <param name="content">內文</param>
+        /// <param name="source">發送源(HiTeach、IES、Sokrates、Auth、Event)</param>
+        /// <returns></returns>
+        [HttpPost("get-notice-history")]
+#if !DEBUG
+        [AuthToken(Roles = "admin")]
+#endif
+        public async Task<List<BINotice>> GetBINoticeHistory(JsonElement jsonElement)
+        {
+            string msgType = (jsonElement.TryGetProperty("msgType", out JsonElement _msgType)) ? _msgType.ToString() : string.Empty; //發送類型 mail:郵件、notify:端外、sms:簡訊
+            string selType = (jsonElement.TryGetProperty("selType", out JsonElement _selType)) ? _selType.ToString() : string.Empty; //挑選方式 single:個別 multi:批次
+            string theme = (jsonElement.TryGetProperty("theme", out JsonElement _theme)) ? _theme.ToString() : string.Empty; //標題(title)
+            string content = (jsonElement.TryGetProperty("content", out JsonElement _content)) ? _content.ToString() : string.Empty; //內文
+            string source = (jsonElement.TryGetProperty("source", out JsonElement _source)) ? _source.ToString() : string.Empty; //发送消息来源 HiTeach、IES、Sokrates、Auth、Event
+            string createId = (jsonElement.TryGetProperty("createId", out JsonElement _createId)) ? _createId.ToString() : string.Empty;
+            Geo geo = (jsonElement.TryGetProperty("geo", out JsonElement _geo)) ? _geo.ToObject<Geo>() : null;
+            List<string> unitType = (jsonElement.TryGetProperty("unitType", out JsonElement _unitType)) ? _unitType.ToObject<List<string>>() : null;
+            List<string> school = (jsonElement.TryGetProperty("school", out JsonElement _school)) ? _school.ToObject<List<string>>() : null;
+            List<string> crowdIds = (jsonElement.TryGetProperty("crowdIds", out JsonElement _crowdIds)) ? _crowdIds.ToObject<List<string>>() : null; //寄送對象(tmid列表)
+            long sendTimeFrom = (jsonElement.TryGetProperty("sendTimeFrom", out JsonElement _sendTimeFrom)) ?  _sendTimeFrom.GetInt64() : 0;
+            long sendTimeTo = (jsonElement.TryGetProperty("sendTimeTo", out JsonElement _sendTimeTo)) ? _sendTimeTo.GetInt64() : 0;
+
+            var cosmosClientIes = _azureCosmos.GetCosmosClient();
+            List<BINotice> result = new List<BINotice> ();
+            string sqlWhere = string.Empty;
+            if(!string.IsNullOrWhiteSpace(msgType)) 
+                sqlWhere += $" AND c.msgType = '{msgType}' ";
+            if (!string.IsNullOrWhiteSpace(selType)) 
+                sqlWhere += $" AND c.selType = '{selType}' ";
+            if (!string.IsNullOrWhiteSpace(theme))
+                sqlWhere += $" AND CONTAINS(c.theme, '{theme}') ";
+            if (!string.IsNullOrWhiteSpace(content))
+                sqlWhere += $" AND CONTAINS(c.content, '{content}') ";
+            if (!string.IsNullOrWhiteSpace(source))
+                sqlWhere += $" AND c.source = '{source}' ";
+            if (geo != null)
+            {
+                if(geo.type.Equals("tmid"))
+                    sqlWhere += $" AND cs.mode = 'tmidGeo' ";
+                else if(geo.type.Equals("school"))
+                    sqlWhere += $" AND cs.mode = 'schGeo' ";
+                if (!string.IsNullOrWhiteSpace(geo.countryId))
+                    sqlWhere += $" AND cs.countryId = '{geo.countryId}' ";
+                if (!string.IsNullOrWhiteSpace(geo.provinceId))
+                    sqlWhere += $" AND cs.provinceId = '{geo.provinceId}' ";
+                if (!string.IsNullOrWhiteSpace(geo.cityId))
+                    sqlWhere += $" AND cs.cityId = '{geo.cityId}' ";
+                if (!string.IsNullOrWhiteSpace(geo.distId))
+                    sqlWhere += $" AND cs.distId = '{geo.distId}' ";
+            }
+            if (school != null && school.Count > 0)
+                sqlWhere += $" AND ARRAY_CONTAINS({JsonSerializer.Serialize(school)}, cs.school) ";
+            if (crowdIds != null && crowdIds.Count > 0)
+                sqlWhere += $" AND EXISTS( SELECT VALUE 1 FROM crowdId IN c.crowdIds WHERE ARRAY_CONTAINS({JsonSerializer.Serialize(crowdIds)}, crowdId) ) ";
+            if (unitType != null && unitType.Count > 0)
+                sqlWhere += $" AND ARRAY_CONTAINS({JsonSerializer.Serialize(unitType)}, cs.unitType) ";
+            if (sendTimeFrom > 0)
+                sqlWhere += $" AND c.sendTime >= {sendTimeFrom} ";
+            if (sendTimeTo > 0)
+                sqlWhere += $" AND c.sendTime <= {sendTimeTo} ";
+            string sql = $"SELECT c.msgType, c.selType, c.hubName, c.subject, c.template, c.search, c.data, c.type, c.theme, c.content, c.crowd, c.crowdIds, c.createId, c.sendTime, c.createTime, c.source FROM c JOIN cs IN c.search WHERE 1=1 {sqlWhere}";
+            await foreach (var item in cosmosClientIes.GetContainer(Constant.TEAMModelOS, "Common").GetItemQueryIteratorSql<BINotice>(queryText: sql, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"BINotice") }))
+            {
+                result.Add(item);
+            }
             return result;
         }
 
+        //將訊息寄送轉換為訊息寄送DB紀錄架構
+        private BINotice TransMsgRequestToBINotice(string msgType, string method, string subject, string title, string body, string sender, string hubName, SendMessageParam target, List<string> tmids, string data)
+        {
+            List<PickParam> searchParams = new List<PickParam>();
+            ///學區
+            if (target.area.Count > 0)
+            {
+                foreach (string areaId in target.area)
+                {
+                    searchParams.Add(new PickParam() { mode = "area", areaId = areaId });
+                }
+            }
+            ///地理資訊
+            if (target.geo.Count > 0)
+            {
+                foreach (Geo geo in target.geo)
+                {
+                    PickParam pickParam = new PickParam() { mode = (geo.type.Equals("tmid")) ? "tmidGeo" : "schGeo" };
+                    if (!string.IsNullOrWhiteSpace(geo.countryId)) pickParam.countryId = geo.countryId;
+                    if (!string.IsNullOrWhiteSpace(geo.provinceId)) pickParam.provinceId = geo.provinceId;
+                    if (!string.IsNullOrWhiteSpace(geo.cityId)) pickParam.cityId = geo.cityId;
+                    if (!string.IsNullOrWhiteSpace(geo.distId)) pickParam.distId = geo.distId;
+                }
+            }
+            ///教育機構
+            if (target.unit.Count > 0)
+            {
+                foreach (string unitType in target.unit)
+                {
+                    searchParams.Add(new PickParam() { mode = "unit", unitType = unitType });
+                }
+            }
+            ///學校
+            if (target.school.Count > 0)
+            {
+                foreach (string schId in target.school)
+                {
+                    searchParams.Add(new PickParam() { mode = "school", school = schId });
+                }
+            }
+            ///TMID
+            if (target.tmid.Count > 0)
+            {
+                searchParams.Add(new PickParam() { mode = "tmid", tmId = target.tmid });
+            }
+            BINotice bINotice = new BINotice()
+            {
+                msgType = msgType,
+                selType = method,
+                search = searchParams,
+                crowdIds = tmids,
+                theme = title,
+                content = body,
+                subject = subject,
+                createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
+                source = sender,
+                hubName = hubName,
+                data = data
+            };
+            return bINotice;
+        }
+
         private string GetDictionaryKeyByValue(Dictionary<string, string> dic, string value)
         {
             string result = string.Empty;

+ 12 - 12
TEAMModelBI/Controllers/BITest/TestController.cs

@@ -1835,20 +1835,20 @@ namespace TEAMModelBI.Controllers.BITest
             bINoticeM.id = "6ec182ab-ad08-43f4-8cc2-6fdbd985f584";
             bINoticeM.msgType = "email";
             bINoticeM.selType = "mul";
-            bINoticeM.accept = new List<PickParam>();
+            bINoticeM.search = new List<PickParam>();
             ///學區
             PickParam area = new PickParam() {
                 mode = "area",
                 areaId = "02944f32-f534-3397-ea56-e6f1fc6c3714"
             };
-            bINoticeM.accept.Add(area);
+            bINoticeM.search.Add(area);
             ///地理 學校
             PickParam geoSch = new PickParam() {
                 mode = "schGeo",
                 countryId = "TW",
                 cityId = "30"
             };
-            bINoticeM.accept.Add(geoSch);
+            bINoticeM.search.Add(geoSch);
             ///地理 ID
             PickParam geoId = new PickParam()
             {
@@ -1856,21 +1856,21 @@ namespace TEAMModelBI.Controllers.BITest
                 countryId = "TW",
                 cityId = "30"
             };
-            bINoticeM.accept.Add(geoId);
+            bINoticeM.search.Add(geoId);
             ///機構
             PickParam inst = new PickParam()
             {
-                mode = "inst",
+                mode = "unit",
                 unitType = "1"
             };
-            bINoticeM.accept.Add(inst);
+            bINoticeM.search.Add(inst);
             ///學校ID
             PickParam sch = new PickParam()
             {
-                mode = "sch",
-                school = new NoticeSchool() { eduId = "hbgl", shortCode = "hbgl", name = "測試學校" }
+                mode = "school",
+                school = "hbgl"
             };
-            bINoticeM.accept.Add(sch);
+            bINoticeM.search.Add(sch);
             await client.GetContainer(Constant.TEAMModelOS, Constant.Common).UpsertItemAsync(bINoticeM, new PartitionKey($"BINotice"));
 
             //個別
@@ -1918,13 +1918,13 @@ namespace TEAMModelBI.Controllers.BITest
             PickParam schPre1 = new PickParam()
             {
                 mode = "sch",
-                school = new NoticeSchool() { eduId = "hbgl", shortCode = "hbgl", name = "測試學校" }
+                school = "hbgl"
             };
             bINoticeS.search.Add(schPre1);
             PickParam schPre2 = new PickParam()
             {
                 mode = "sch",
-                school = new NoticeSchool() { eduId = "habook", shortCode = "habook", name = "玉山學校" }
+                school = "habook"
             };
             bINoticeS.search.Add(schPre2);
 
@@ -1934,7 +1934,7 @@ namespace TEAMModelBI.Controllers.BITest
             };
             tmidList.tmId.Add("1595321354");
             tmidList.tmId.Add("1522758684");
-            bINoticeS.accept.Add(tmidList);
+            bINoticeS.search.Add(tmidList);
             await client.GetContainer(Constant.TEAMModelOS, Constant.Common).UpsertItemAsync(bINoticeS, new PartitionKey($"BINotice"));
 
             return Ok(new { state = 200 });

+ 17 - 5
TEAMModelOS.SDK/Models/Cosmos/BI/BICommon/Notice.cs

@@ -25,13 +25,25 @@ namespace TEAMModelOS.SDK.Models.Cosmos.BI
         /// </summary>
         public string selType { get; set; }
         /// <summary>
+        /// 訊息中樞
+        /// </summary>
+        public string hubName { get; set; }
+        /// <summary>
+        /// 主旨 ※Email用
+        /// </summary>
+        public string subject { get; set; }
+        /// <summary>
+        /// 模板ID ※Email用
+        /// </summary>
+        public string template { get; set; }
+        /// <summary>
         /// 搜尋條件 ※複數
         /// </summary>
         public List<PickParam> search { get; set; } = new();
         /// <summary>
-        /// 接收名單 ※複數
+        /// 附加資料 ※JSON to string
         /// </summary>
-        public List<PickParam> accept { get; set; } = new();
+        public string data { get; set; }
         #endregion
 
         #region 原有舊通知類型
@@ -89,7 +101,7 @@ namespace TEAMModelOS.SDK.Models.Cosmos.BI
         /// </summary>
         public long createTime { get; set; }
         /// <summary>
-        /// 发送消息来源, 默认(BI)、IES5、HiTA
+        /// 发送消息来源, HiTeach、IES、Sokrates、Auth、Event
         /// </summary>
         public string source { get; set; } = "IES";
         #endregion
@@ -99,7 +111,7 @@ namespace TEAMModelOS.SDK.Models.Cosmos.BI
     /// </summary>
     public class PickParam
     {
-        public string mode { get; set; } //格式 area:學區 schGeo:學校地理 inst:機構 sch:學校     crtTime:帳號生成時間 tmidGeo:TMID地理 softUse:使用軟體 point:積分範圍 tmid:帳號列表(個別挑選用)
+        public string mode { get; set; } //格式 area:學區 schGeo:學校地理 tmidGeo:TMID地理 unit:機構 school:學校 crtTime:帳號生成時間 softUse:使用軟體 point:積分範圍 tmid:帳號列表(個別挑選用)
         public string areaId { get; set; } //學區ID ※area專有
         public string countryId { get; set; } //國ID ※schGeo、tmidGeo專有
         public string provinceId { get; set; } //省ID ※schGeo、tmidGeo專有
@@ -112,7 +124,7 @@ namespace TEAMModelOS.SDK.Models.Cosmos.BI
         public string softUseMode { get; set; } //軟體使用模式 and or ※softUse專有
         public int pointFrom { get; set; } //積分範圍 起始 ※point專有
         public int pointTo { get; set; } //積分範圍 終止 ※point專有
-        public NoticeSchool school { get; set; } //學校 ※sch專有
+        public string school { get; set; } //學校ID ※sch專有
         public List<string> tmId { get; set; } = new(); //TMID列表 ※tmid專有
     }
     /// <summary>