using Furion.Logging;
using NapCatRobotClient.Core.RobotAPI.Dto.Request;
namespace NapCatRobotClient.Service.Group.TextProcess
{
///
/// 坊市上架命令
///
public class GoodsUpShopProcess
{
public static async Task ProcessGroupRequest(string groupId, string message)
{
try
{
JObject json = JObject.Parse(message);
var messageArray = JArray.Parse(json["message"].ToString());
string groupMsg = messageArray.LastOrDefault(o => o["type"].ToString() == "text"
&& !string.IsNullOrWhiteSpace(o["data"]["text"].ToString()))?["data"]?["text"]?.ToString();
if (string.IsNullOrWhiteSpace(groupMsg)) return false;
// 小小药材背包
List keyWord = new() { "拥有药材", "坊市数据" };
if (keyWord.All(k => groupMsg.Contains(k)))
{
RedisHelper.Client.Set(json["message_id"].ToString(), groupMsg, 60 * 10);
}
// 用户查询上架
else if (groupMsg.Contains("查价格") || groupMsg.Contains("查上架") && messageArray.Any(o => o["type"].ToString() == "reply"))
{
string replyId = messageArray.FirstOrDefault(o => o["type"].ToString() == "reply")["data"]["id"].ToString();
var goodsStr = RedisHelper.Client.Get(replyId);
if (string.IsNullOrEmpty(goodsStr) is false)
{
bool upCmd = groupMsg.Contains("-f");
_ = Herbal(groupId, goodsStr, upCmd);
}
else
{
GroupSendMessageRequest request = new()
{
GroupId = groupId,
Message = new()
{
new MessageItem()
{
Type = "text",
Data = new()
{
Text = "没有查询到缓存,请重新艾特小小查药材背包"
}
},
}
};
await RobotAPI.SendGroupText(request);
}
}
}
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 async Task Herbal(string groupId, string text, bool upCmd)
{
List results = [];
var lines = text.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
foreach (var line in lines)
{
var content = line.Trim();
// 跳过无用行
if (content.StartsWith("鬼谷子的药材背包") ||
content.StartsWith("☆拥有药材☆") ||
content.StartsWith("第") ||
content.StartsWith("下一页"))
{
continue;
}
// 品级标题,例如:☆五品药材☆
if (Regex.IsMatch(content, @"^☆.+品药材☆$"))
{
// 如果以后需要品级,可以在这里记录
// currentLevel = ...
continue;
}
// 药材行,例如:天灵果 - 数量:4 炼金 | 坊市数据
var match = Regex.Match(content, @"^(.*?)\s*-\s*数量:(\d+)");
if (match.Success)
{
results.Add(new GoodsInfo
{
Name = match.Groups[1].Value.Trim(),
Num = int.Parse(match.Groups[2].Value)
});
}
}
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);
if (upCmd)
{
msg += $"确认坊市上架{item.Name} {(int)current.Price - 100000} {num}|";
}
else
{
msg += $"确认坊市上架{item.Name} {(int)current.Price - 100000} {num}\n";
}
}
}
if (string.IsNullOrWhiteSpace(msg) is false)
{
if (!upCmd)
{
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)}";
}
if (msg[^1] == '|') msg = msg[..^1];
GroupSendMessageRequest request = new()
{
GroupId = groupId,
Message = new()
{
new MessageItem()
{
Type = "text",
Data = new()
{
Text = msg
}
},
}
};
await RobotAPI.SendGroupText(request);
}
return await Task.FromResult(true);
}
}
}