using System.Text.RegularExpressions;
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.Message;
using RoBot.Start.Service.Dto;
namespace RoBot.Start.Cmd
{
///
/// 坊市上架命令
///
public class QueryGoodsUpShopPriceCmd
{
public static async Task Execute(MessageChain chain)
{
var systemConfig = GlobalConfig.ConfigSetting;
if (chain.Count == 3 &&
chain.FirstOrDefault(e => e is ForwardEntity) is ForwardEntity forward &&
chain.FirstOrDefault(e => e is MentionEntity) is MentionEntity mention &&
chain.FirstOrDefault(e => e is TextEntity) is TextEntity text)
{
if (forward.TargetUin == (uint)systemConfig.XiaoXiaoQQ && mention.Uin == (uint)systemConfig.BotQQ && (text.Text.Contains("查上架") || text.Text.Contains("查价格")))
{
var list = forward.Chain.Where(o => o is TextEntity);
if (!list.Any()) return false;
List convertList = list.Cast().ToList();
convertList = convertList.Where(o => string.IsNullOrWhiteSpace(o.Text) is false)
.ToList();
if (convertList.Count > 0)
{
string desc = convertList[0].Text;
if (desc.Contains("拥有药材") || (desc.Contains("名字") && desc.Contains("拥有数量")))
{
_ = Herbal(desc);
}
}
}
}
return await Task.FromResult(true);
}
///
/// 计算药材
///
///
private static async Task Herbal(string text)
{
List results = [];
string currentGrade = "";
var lines = text.Split(new[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
foreach (var line in lines)
{
if (line.StartsWith("品级:"))
{
currentGrade = line.Substring("品级:".Length).Trim();
}
else if (line.StartsWith("名字:"))
{
var name = line.Substring("名字:".Length).Trim();
// 找到下一行的数量
var index = Array.IndexOf(lines, line);
if (index + 1 < lines.Length && lines[index + 1].StartsWith("拥有数量:"))
{
var quantityLine = lines[index + 1];
var match = Regex.Match(quantityLine, @"拥有数量:(\d+)");
if (match.Success)
{
var quantity = match.Groups[1].Value;
results.Add(new() { Name = name, Num = Convert.ToInt32(quantity) });
}
}
}
}
string msg = "";
decimal totalPrice = 0;
decimal fee = 0;
// 打印结果
foreach (var item in results)
{
var current = RedisHelper.Client.HGet(RedisPrefix.GoodsKey, item.Name);
if (current is not null)
{
int num = item.Num.Value > 10 ? 10 : item.Num.Value;
decimal nicePrice = current.Price - 100000;
totalPrice += Convert.ToDecimal(nicePrice * num);
fee += Math.Round(Utils.CalculateFee(nicePrice) * num, 0);
msg += $"确认坊市上架{item.Name} {(int)current.Price - 100000} {num}\n";
}
}
if (string.IsNullOrWhiteSpace(msg) is false)
{
msg += "\r\n";
msg += "使用前请先@小小查看坊市药材,过完一遍以获取最新价格\r\n当前价格为坊市价格-10w\r\n";
msg += $"总价值约:{Utils.FormatNumberToChineseUnit(totalPrice)}\r\n";
msg += $"手续费约:{Utils.FormatNumberToChineseUnit(fee)}\r\n";
msg += $"到账约:{Utils.FormatNumberToChineseUnit(totalPrice - fee)}";
var systemConfig = GlobalConfig.ConfigSetting;
var bot = GlobalConfig.BotContext;
await bot.SendMsg((uint)systemConfig.GroupQQ, msg);
}
return await Task.FromResult(true);
}
}
}