using Lagrange.Core.Message;
using Lagrange.Core.Message.Entity;
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;
namespace RoBot.Start.Cmd
{
///
/// 查看物品信息命令 价格/数据更新时间
///
public class QueryGoodsPriceCmd
{
public static async Task Execute(MessageChain chain)
{
try
{
var systemConfig = GlobalConfig.ConfigSetting;
var bot = GlobalConfig.BotContext;
if (chain.Count == 2 &&
chain.FirstOrDefault(e => e is MentionEntity) is MentionEntity mention &&
chain.FirstOrDefault(e => e is TextEntity) is TextEntity text)
{
// 判断是否@了机器人
if (mention.Uin == systemConfig.BotQQ)
{
string inputText = text.Text?.Trim();
// 判断是否包含“查价格”
if (!string.IsNullOrEmpty(inputText) && inputText.Contains("查价格"))
{
// 提取物品名称
var goodsName = inputText
.Split(' ', StringSplitOptions.RemoveEmptyEntries)
.FirstOrDefault(word => word != "查价格")
?.Replace("查价格", "")?.Trim();
if (!string.IsNullOrWhiteSpace(goodsName))
{
var goodsInfo = RedisHelper.Client.HGet(RedisPrefix.GoodsKey, goodsName);
if (goodsInfo is not null)
{
await bot.SendMsg((uint)systemConfig.GroupQQ,
$"物品:{goodsInfo.Name}\n价格:{goodsInfo.ShowPriceDesc}\n收录时间:{goodsInfo.LastUpdateTime:yyyy-MM-dd HH:mm:ss}");
}
else
{
await bot.SendMsg((uint)systemConfig.GroupQQ, "未收录");
}
}
else
{
await bot.SendMsg((uint)systemConfig.GroupQQ, "请输入要查询的物品名称,例如:查价格 七星草");
}
}
}
}
}
catch (Exception ex)
{
Logs.Write($@"{DateTime.Now:yyyy-MM-dd HH:mm:ss} 查看物品信息命令 发生异常,异常信息:{ex.Message},异常堆栈:{ex.StackTrace}");
}
return await Task.FromResult(true);
}
}
}