using Furion.Logging;
using NapCatRobotClient.Core.RobotAPI.Dto.Request;
namespace NapCatRobotClient.Service.Group.TextProcess
{
///
/// 灵田结算
///
public class LingTianProcess
{
///
/// 处理群消息
///
///
///
public static async Task ProcessGroupRequest(string groupId, string message)
{
try
{
JObject json = JObject.Parse(message);
string groupMsg = JArray.Parse(json["message"].ToString()).FirstOrDefault(o => o["type"].ToString() == "text"
&& !string.IsNullOrWhiteSpace(o["data"]["text"].ToString()) && (o["data"]["text"].ToString().Contains("道友本次采集成果") || o["data"]["text"].ToString().Contains("道友成功收获药材")))?["data"]?["text"]?.ToString();
if (string.IsNullOrWhiteSpace(groupMsg)) return false;
List goods = new();
if (groupMsg.Contains("道友本次采集成果"))
{
goods = await NewCmd(groupMsg);
}
else if (groupMsg.Contains("道友成功收获药材"))
{
goods = await OldCmd(groupMsg);
}
if (goods.Count > 0)
{
string msg = "";
decimal totalPrice = 0;
decimal fee = 0;
// 打印结果
foreach (var good in goods)
{
var current = RedisHelper.Client.HGet(RedisPrefix.GoodsKey, good.Name);
if (current is not null)
{
int num = good.Num.Value > 10 ? 10 : good.Num.Value;
decimal nicePrice = current.Price - 100000;
totalPrice += Convert.ToDecimal(nicePrice * num);
fee += Math.Round(Utils.CalculateFee(nicePrice) * num, 0);
}
}
if (totalPrice > 0)
{
msg = $"恭喜道友成功收取{goods.Sum(o => o.Num)}株药材\r\n";
msg += $"总价值约:{Utils.FormatNumberToChineseUnit(totalPrice)}\r\n";
msg += $"手续费约:{Utils.FormatNumberToChineseUnit(fee)}\r\n";
msg += $"到账约:{Utils.FormatNumberToChineseUnit(totalPrice - fee)}";
GroupSendMessageRequest request = new()
{
GroupId = groupId,
Message = new()
{
new MessageItem()
{
Type = "text",
Data = new()
{
Text = msg
}
},
new MessageItem()
{
Type = "reply",
Data = new()
{
Id = json["message_id"].ToString()
}
}
}
};
await RobotAPI.SendGroupText(request);
}
var atData = JArray.Parse(message).FirstOrDefault(o => o["type"].ToString() == "at");
if (atData is not null)
{
ScheduleNextNotify(groupId, atData["data"]["qq"].ToString());
}
}
}
catch (Exception ex)
{
Log.Error($@"{DateTime.Now:yyyy-MM-dd HH:mm:ss} 灵田结算查价格 发生异常,异常信息:{ex.Message},异常堆栈:{ex.StackTrace}", true);
}
return await Task.FromResult(true);
}
private static async Task> OldCmd(string message)
{
List goodsInfos = new();
var result = new List<(string Name, int Count)>();
var regex = new Regex(@"道友成功收获药材:(.+?) (\d+) 个!");
foreach (Match match in regex.Matches(message))
{
string name = match.Groups[1].Value;
int count = int.Parse(match.Groups[2].Value);
goodsInfos.Add(new() { Name = name, Num = count });
}
return await Task.FromResult(goodsInfos);
}
private static async Task> NewCmd(string message)
{
List goodsInfos = new();
var result = new List<(string Name, int Count)>();
var regex = new Regex(@"收获药材:(.+?) (\d+) 个!");
foreach (Match match in regex.Matches(message))
{
string name = match.Groups[1].Value;
int count = int.Parse(match.Groups[2].Value);
goodsInfos.Add(new() { Name = name, Num = count });
}
return await Task.FromResult(goodsInfos);
}
private static void ScheduleNextNotify(string groupId, string userId)
{
TimeSpan delay = TimeSpan.FromHours(47.01);
Log.Information($@"{DateTime.Now:yyyy-MM-dd HH:mm:ss} {userId} 触发下次灵田结算通知 ");
_ = Task.Run(async () =>
{
await Task.Delay(delay);
GroupSendMessageRequest request = new()
{
GroupId = groupId,
Message = new()
{
new MessageItem()
{
Type = "text",
Data = new()
{
Text = $"\r\n【灵田结算通知】\r\n该结算奖励了!"
}
},
new MessageItem()
{
Type = "at",
Data = new()
{
QQ = userId
}
}
}
};
await RobotAPI.SendGroupText(request);
});
}
}
}