2024-05-02 15:24:13 -07:00

354 lines
16 KiB
C#

using Church.Net.DAL.EF;
using Church.Net.Entity;
using Church.Net.Utility;
using LineMessaging;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WebAPI.Logics;
using WebAPI.Services.Interfaces;
namespace WebAPI.Services.AutoReplyCommands
{
public class ArHappinessGroupTask : IAutoReplyCommand
{
private PastoralDomain pastoralDomain;
public ArHappinessGroupTask(
PastoralDomainLogic logic)
{
this.logic = logic;
}
private static readonly string[] COMMANDS = { "分工", "分工#" };
private static readonly DomainType[] GROUPS = {
DomainType.HappinessGroup,
};
private readonly PastoralDomainLogic logic;
public string Description => "顯示幸福小組分工表 #代表周數 9顯示所有分工";
public IEnumerable<string> Commands => COMMANDS;
public IEnumerable<DomainType> SupportGroups => GROUPS;
public string ReplyTextMessage => null;
public string ReplyJsonMessage => jsonMessage;
private string jsonMessage;
private IEnumerable<ILineMessage> lineMessages;
public IEnumerable<ILineMessage> LineMessage => lineMessages;
public void Initialize(PastoralDomain pastoralDomain = null)
{
this.pastoralDomain = pastoralDomain;
}
private int weekSeq = 0;
private HappinessWeek week = null;
public bool Enabled(PastoralDomain pastoralDomain = null, string command = null)
{
if (pastoralDomain != null && command.IndexOf("分工")>-1)
{
this.pastoralDomain = pastoralDomain;
command = command.Replace("分工", "");
if (command.Length > 0)
{
weekSeq = int.Parse(command);
}
logic.GetHappinessGroupInfo(pastoralDomain);
if (weekSeq > 0)
{
week = pastoralDomain.HappinessWeeks.Where(w => w.SEQ == weekSeq).FirstOrDefault();
}
else
{
week = pastoralDomain.HappinessWeeks.Where(w => w.Date >= DateTime.UtcNow).FirstOrDefault();
weekSeq = week.SEQ;
}
if (week != null)
{
PrepareLineMessage();
}
else
{
PrepareAllTaskMessage();
}
return true;
};
return false;
}
private void PrepareAllTaskMessage()
{
this.lineMessages = null;
StringBuilder messageContent = new StringBuilder();
messageContent.AppendLine("{ ");
messageContent.AppendLine(" \"altText\": \"幸福小組 8 週分工\", ");
messageContent.AppendLine(" \"type\": \"flex\", ");
messageContent.AppendLine(" \"contents\": { ");
messageContent.AppendLine(" \"type\": \"carousel\", ");
messageContent.AppendLine(" \"contents\": [ ");
foreach (var week in pastoralDomain.HappinessWeeks.OrderBy(w => w.SEQ))
{
messageContent.AppendLine("{ ");
messageContent.AppendLine(" \"type\": \"bubble\", ");
messageContent.AppendLine(" \"header\": { ");
messageContent.AppendLine(" \"type\": \"box\", ");
messageContent.AppendLine(" \"layout\": \"vertical\", ");
messageContent.AppendLine(" \"contents\": [ ");
messageContent.AppendLine(" { ");
messageContent.AppendLine(" \"type\": \"text\", ");
messageContent.AppendLine($" \"text\": \"W{week.SEQ} {week.Topic} 分工表\", ");
messageContent.AppendLine(" \"flex\": 0, ");
messageContent.AppendLine(" \"size\": \"lg\", ");
messageContent.AppendLine(" \"weight\": \"bold\", ");
messageContent.AppendLine(" \"wrap\": false, ");
messageContent.AppendLine(" \"align\": \"center\" ");
messageContent.AppendLine(" } ");
messageContent.AppendLine(" ], ");
messageContent.AppendLine(" \"paddingAll\": \"sm\" ");
messageContent.AppendLine(" }, ");
messageContent.AppendLine(" \"hero\": { ");
messageContent.AppendLine(" \"type\": \"box\", ");
messageContent.AppendLine(" \"layout\": \"vertical\", ");
messageContent.AppendLine(" \"contents\": [ ");
messageContent.AppendLine(" { ");
messageContent.AppendLine(" \"type\": \"image\", ");
messageContent.AppendLine($" \"url\": \"https://happiness.tours/assets/images/HappinessGroup/week0{week.SEQ}.jpg\", ");
messageContent.AppendLine(" \"size\": \"full\", ");
messageContent.AppendLine(" \"aspectRatio\": \"1.5:1\" ");
messageContent.AppendLine(" } ");
messageContent.AppendLine(" ] ");
messageContent.AppendLine(" }, ");
messageContent.AppendLine(" \"body\": { ");
messageContent.AppendLine(" \"type\": \"box\", ");
messageContent.AppendLine(" \"layout\": \"vertical\", ");
messageContent.AppendLine(" \"contents\": [ ");
messageContent.AppendLine(" { ");
messageContent.AppendLine(" \"type\": \"text\", ");
messageContent.AppendLine(" \"text\": \"分工明細\", ");
messageContent.AppendLine(" \"flex\": 0, ");
messageContent.AppendLine(" \"size\": \"md\", ");
messageContent.AppendLine(" \"offsetBottom\": \"md\", ");
messageContent.AppendLine(" \"weight\": \"bold\", ");
messageContent.AppendLine(" \"wrap\": false ");
messageContent.AppendLine(" }, ");
foreach (var task in week.Tasks)
{
messageContent.AppendLine(" { ");
messageContent.AppendLine(" \"type\": \"box\", ");
messageContent.AppendLine(" \"layout\": \"baseline\", ");
messageContent.AppendLine(" \"contents\": [ ");
messageContent.AppendLine(" { ");
messageContent.AppendLine(" \"type\": \"text\", ");
messageContent.AppendLine($" \"text\": \"{task.Type.EnumToDescriptionString()}\", ");
messageContent.AppendLine(" \"flex\": 3, ");
messageContent.AppendLine(" \"size\": \"sm\", ");
messageContent.AppendLine(" \"weight\": \"regular\", ");
messageContent.AppendLine(" \"color\": \"#aaaaaa\", ");
messageContent.AppendLine(" \"wrap\": false ");
messageContent.AppendLine(" }, ");
messageContent.AppendLine(" { ");
messageContent.AppendLine(" \"type\": \"text\", ");
messageContent.AppendLine($" \"text\": \"{task.Tasker} \", ");
messageContent.AppendLine(" \"flex\": 3, ");
messageContent.AppendLine(" \"size\": \"sm\", ");
messageContent.AppendLine(" \"weight\": \"regular\", ");
messageContent.AppendLine(" \"color\": \"#666666\", ");
messageContent.AppendLine(" \"wrap\": true ");
messageContent.AppendLine(" }, ");
messageContent.AppendLine(" { ");
messageContent.AppendLine(" \"type\": \"text\", ");
messageContent.AppendLine($" \"text\": \"內容:{task.Content}\", ");
messageContent.AppendLine(" \"flex\": 7, ");
messageContent.AppendLine(" \"size\": \"sm\", ");
messageContent.AppendLine(" \"weight\": \"regular\", ");
messageContent.AppendLine(" \"color\": \"#666666\", ");
messageContent.AppendLine(" \"wrap\": true ");
messageContent.AppendLine(" } ");
messageContent.AppendLine(" ] ");
messageContent.AppendLine(" }, ");
}
messageContent.AppendLine(" { ");
messageContent.AppendLine(" \"type\": \"separator\", ");
messageContent.AppendLine(" \"margin\": \"md\" ");
messageContent.AppendLine(" } ");
messageContent.AppendLine(" ], ");
messageContent.AppendLine(" \"paddingBottom\": \"none\" ");
messageContent.AppendLine(" }, ");
messageContent.AppendLine(" \"footer\": { ");
messageContent.AppendLine(" \"type\": \"box\", ");
messageContent.AppendLine(" \"layout\": \"vertical\", ");
messageContent.AppendLine(" \"contents\": [ ");
messageContent.AppendLine(" { ");
messageContent.AppendLine(" \"type\": \"button\", ");
messageContent.AppendLine(" \"action\": { ");
messageContent.AppendLine(" \"type\": \"uri\", ");
messageContent.AppendLine(" \"label\": \"管理分工\", ");
messageContent.AppendLine($" \"uri\": \"https://happiness.tours/myapp/happinessWeeks/395BBPTU4NG3F?openExternalBrowser={week.SEQ}\" ");
messageContent.AppendLine(" }, ");
messageContent.AppendLine(" \"style\": \"link\", ");
messageContent.AppendLine(" \"height\": \"md\" ");
messageContent.AppendLine(" } ");
messageContent.AppendLine(" ], ");
messageContent.AppendLine(" \"paddingAll\": \"none\" ");
messageContent.AppendLine(" } ");
if (week.SEQ < 8)
{
messageContent.AppendLine(" }, ");
}
else
{
messageContent.AppendLine(" } ");
}
}
messageContent.AppendLine(" ] ");
messageContent.AppendLine(" } ");
messageContent.AppendLine(" } ");
this.jsonMessage = messageContent.ToString();
}
private void PrepareLineMessage()
{
var random = new Random();
List<ILineMessage> list = new List<ILineMessage>();
string title = $"W{weekSeq} {week.Topic} 分工表";
string imageUrl = $"https://happiness.tours/assets/images/HappinessGroup/week0{weekSeq}.jpg";
var flexMessage = new LineFlexMessage();
flexMessage.AltText = title;
#region Header
var headerContent = flexMessage.Contents.InitHeader();
headerContent.Add(
new LineFlexText(title)
{
Size = FlexObjectSize.lg,
Weight = FlexObjectTextWeidht.Bold,
Align = "center"
});
#endregion
#region Hero
flexMessage.Contents.InitHero()
.Add(
new LineFlexImage(imageUrl)
{
Size = FlexObjectSize.full,
AspectRatio = "1.5:1"
}
);
#endregion
#region Body
var bodyContent = flexMessage.Contents.InitBody();
TimeSpan ts = week.Date - DateTime.Now;
Console.WriteLine("No. of Minutes (Difference) = {0}", ts.TotalMinutes);
bodyContent.Add(
new LineFlexText($"再過 {ts.TotalMinutes.ToString("N0")} 分鐘,就是萬眾期待的幸福小組啦!!!")
{
Size = FlexObjectSize.md,
Weight = FlexObjectTextWeidht.Regular,
OffsetBottom = FlexObjectSize.xxl,
Wrap = true
});
//bodyContent.Add(
// new LineFlexText($"{_event.Time.ToString("MM/dd HH:mm tt")} 準時開飯唷!!")
// {
// Size = FlexObjectSize.md,
// Weight = FlexObjectTextWeidht.Regular,
// OffsetBottom = FlexObjectSize.xl,
// Wrap = true
// });
bodyContent.Add(
new LineFlexText("分工明細")
{
Size = FlexObjectSize.md,
Weight = FlexObjectTextWeidht.Bold,
OffsetBottom = FlexObjectSize.md
});
//$"目前暫無禱告事項唷, 趕快來新增代禱事項吧!!"
List<LineFlexBox> comments = new List<LineFlexBox>();
foreach (var a in week.Tasks)
{
var name = string.IsNullOrEmpty(a.Tasker) ? "N/A" : a.Tasker;// logic.GetMemberFirstNameById(a.MemberId);
var baseLineBox = new LineFlexBox()
{
Layout = FlexObjectBoxLayout.Baseline,
};
baseLineBox.Contents.Add(
new LineFlexText(a.Type.EnumToDescriptionString())
{
Size = FlexObjectSize.sm,
Color = "#aaaaaa",
Flex = 3
});
baseLineBox.Contents.Add(
new LineFlexText(
$"{name}"
)
{
Size = FlexObjectSize.sm,
Color = "#666666",
Flex = 3,
Wrap = true
});
baseLineBox.Contents.Add(
new LineFlexText(
$"內容:{a.Content}"
)
{
Size = FlexObjectSize.sm,
Color = "#666666",
Flex = 7,
Wrap = true
});
bodyContent.Add(baseLineBox);
}
bodyContent.Add(new LineFlexSeparator());
#endregion
#region Footer
flexMessage.Contents.InitFooter()
.Add(
new LineFlexButton()
{
Action = new UriAction()
{
Uri = $"https://happiness.tours/myapp/happinessWeeks/{pastoralDomain.Id}",
Label = "管理分工"
}
}
);
#endregion
list.Insert(0, flexMessage);
this.lineMessages = list;
}
}
}