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.

109 lines
4.5 KiB
C#

using System.Text.RegularExpressions;
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.Service.Dto;
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());
}
}
}
}
catch (Exception ex)
{
Logs.Write($@"{DateTime.Now:yyyy-MM-dd HH:mm:ss} 灵田结算查价格 发生异常,异常信息:{ex.Message},异常堆栈:{ex.StackTrace}");
}
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);
}
}
}