62 lines
2.5 KiB
C#
62 lines
2.5 KiB
C#
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);
|
||
}
|
||
}
|
||
}
|