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 xx = json["user_id"]?.ToString();
if (xx != App.Configuration["QQConfig:XiaoXiaoRobotQQ"])
{
return false;
}
string groupMsg = null;
var keywords = new[]
{
"道友的灵田还不能收取",
"道友本次采集成果",
"道友成功收获药材",
"道友的灵田灵气未满"
};
var messageArray = JArray.Parse(json["message"].ToString());
groupMsg = messageArray
.Where(o => o["type"]?.ToString() == "text")
.Select(o => o["data"]?["text"]?.ToString())
.FirstOrDefault(text =>
!string.IsNullOrWhiteSpace(text) &&
keywords.Any(k => text.Contains(k))
);
if (string.IsNullOrWhiteSpace(groupMsg)) return false;
List goods = new();
if (groupMsg.Contains("道友的灵田还不能收取")|| groupMsg.Contains("道友的灵田灵气未满"))
{
double ts = ConvertLastTime(groupMsg);
if (ts > 0)
{
ts += 0.01;
var atData = JArray.Parse(json["message"].ToString()).FirstOrDefault(o => o["type"].ToString() == "at");
if (atData is not null)
{
string at = atData["data"]["qq"].ToString();
ScheduleNextNotify(groupId, at, ts);
GroupSendMessageRequest request = new()
{
GroupId = groupId,
Message = new()
{
new MessageItem()
{
Type = "text",
Data = new()
{
Text = $"灵田结算预计在 【{DateTime.Now.AddHours(ts):yyyy-MM-dd HH:mm:ss}】 提醒您收取"
}
},
new MessageItem()
{
Type = "at",
Data = new()
{
QQ = at
}
}
}
};
await RobotAPI.SendGroupText(request);
}
}
}
else 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 = "reply",
Data = new()
{
Id = json["message_id"].ToString()
}
},
new MessageItem()
{
Type = "text",
Data = new()
{
Text = msg
}
}
}
};
await RobotAPI.SendGroupText(request);
}
var atData = JArray.Parse(json["message"].ToString()).FirstOrDefault(o => o["type"].ToString() == "at");
if (atData is not null)
{
ScheduleNextNotify(groupId, atData["data"]["qq"].ToString(), 47.01);
}
}
}
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 double ConvertLastTime(string text)
{
Match match = Regex.Match(text, @"([\d\.]+)\s*小时");
double hours = 0;
if (match.Success)
{
hours = double.Parse(match.Groups[1].Value);
}
else
{
}
return hours;
}
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, double hours)
{
DateTime now = DateTime.Now.AddHours(hours);
TimeSpan delay = TimeSpan.FromHours(hours + 0.09);
Log.Information($@"{DateTime.Now:yyyy-MM-dd HH:mm:ss} {userId} 触发下次灵田结算通知 结算时间:{now} ");
RedisHelper.Client.Set($"{RedisPrefix.LingTianKey}{groupId}:{userId}", new TaskDetailInfo()
{
GroupId = groupId,
UserId = userId,
TriggerTime = now
}, delay);
}
}
}