Files
NapCatRobot/NapCatRobotClient/NapCatRobotClient.Service/Group/TextProcess/InertOrUpdateGoodsInfoProcess.cs
T

62 lines
2.5 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using Furion.Logging;
using static Microsoft.Extensions.Logging.EventSource.LoggingEventSource;
namespace NapCatRobotClient.Service.Group.TextProcess
{
/// <summary>
/// 保存或更新物品价格
/// </summary>
public class InertOrUpdateGoodsInfoProcess
{
private static Regex Regex = new Regex(@"价格[:](\d+(?:\.\d+)?[万亿])\s+([^\s]+)");
public static async Task<bool> ProcessGroupRequest(string groupId, string message)
{
try
{
if (message.Contains("不鼓励不保障"))
{
JObject json = JObject.Parse(message);
string jsonMessage = json["message"]?.ToString();
jsonMessage = JArray.Parse(jsonMessage)
.Where(x => x?["type"]?.ToString() == "text")
.Select(x => x?["data"]?["text"]?.ToString())
.FirstOrDefault(t => !string.IsNullOrWhiteSpace(t) && new string[] { "不鼓励不保障" }.All(k => t.Contains(k)));
if (string.IsNullOrWhiteSpace(jsonMessage)) return false;
List<GoodsInfo> results = new();
MatchCollection matches = Regex.Matches(jsonMessage);
foreach (Match match in matches)
{
string price = match.Groups[1].Value;
string name = match.Groups[2].Value.Replace("\u200b", "").Replace("\u200c", "").Replace("\u200d", "").Replace("?", ""); // 去除零宽字符
GoodsInfo gds = new()
{
Name = name,
Price = Utils.ParseChineseNumber(price),
ShowPriceDesc = price,
LastUpdateTime = DateTime.Now
};
results.Add(gds);
}
foreach (GoodsInfo item in results)
{
RedisHelper.Client.HSet(RedisPrefix.GoodsKey, item.Name, item);
}
}
}
catch (Exception ex)
{
Log.Error($@"{DateTime.Now:yyyy-MM-dd HH:mm:ss} 保存或更新物品价格 发生异常,异常信息:{ex.Message},异常堆栈:{ex.StackTrace}", true);
}
return await Task.FromResult(true);
}
}
}