You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

132 lines
5.5 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using Lagrange.Core.Common.Interface.Api;
using Lagrange.Core.Message;
using Lagrange.Core.Message.Entity;
using RoBot.Core;
using RoBot.Core.ConstValue;
using RoBot.Core.Helper;
using RoBot.Start.Global;
using RoBot.Start.LogConfig;
using RoBot.Start.Message;
using RoBot.Start.Service.Dto;
using System.Text.RegularExpressions;
namespace RoBot.Start.Cmd
{
/// <summary>
/// 灵田结算查价格
/// </summary>
public class LingTianCmd
{
public static async Task<bool> Execute(MessageChain chain)
{
try
{
var textEntities = chain.Where(o => o is TextEntity).ToList();
List<TextEntity> convertList = textEntities.Cast<TextEntity>().ToList().Where(o => o.Text.Contains("道友本次采集成果") || o.Text.Contains("道友成功收获药材")).ToList();
foreach (var item in convertList)
{
List<GoodsInfo> goods = new();
if (item.Text.Contains("道友本次采集成果"))
{
goods = await NewCmd(item.Text);
}
else if (item.Text.Contains("道友成功收获药材"))
{
goods = await OldCmd(item.Text);
}
if (goods.Count > 0)
{
var systemConfig = GlobalConfig.ConfigSetting;
var bot = GlobalConfig.BotContext;
string msg = "";
decimal totalPrice = 0;
decimal fee = 0;
// 打印结果
foreach (var good in goods)
{
var current = RedisHelper.Client.HGet<GoodsInfo>(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)}";
var groupMessageChain = MessageBuilder.Group((uint)systemConfig.GroupQQ).Forward(chain).Text(msg);
await bot.SendMessage(groupMessageChain.Build());
}
MentionEntity callQQMember = chain.Where(o => o is MentionEntity).Cast<MentionEntity>().FirstOrDefault();
if (callQQMember is not null)
{
uint qqNumber = callQQMember.Uin;
ScheduleNextNotify(qqNumber);
}
}
}
}
catch (Exception ex)
{
Logs.Write($@"{DateTime.Now:yyyy-MM-dd HH:mm:ss} 灵田结算查价格 发生异常,异常信息:{ex.Message},异常堆栈:{ex.StackTrace}", true);
}
return await Task.FromResult(true);
}
private static async Task<List<GoodsInfo>> OldCmd(string message)
{
List<GoodsInfo> 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<List<GoodsInfo>> NewCmd(string message)
{
List<GoodsInfo> 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(uint userId)
{
TimeSpan delay = TimeSpan.FromHours(47.01);
Logs.Write($@"{DateTime.Now:yyyy-MM-dd HH:mm:ss} {userId} 触发下次灵田结算通知 ");
_ = Task.Run(async () =>
{
await Task.Delay(delay);
Dto.ConfigDto.ConfigSetting systemConfig = GlobalConfig.ConfigSetting;
Lagrange.Core.BotContext bot = GlobalConfig.BotContext;
await bot.SendMentionMsg((uint)systemConfig.GroupQQ, userId, $"\r\n【灵田结算通知】\r\n该结算奖励了");
});
}
}
}