|
@@ -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;
|