You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

155 lines
6.4 KiB
C#

using Furion.Logging;
using NapCatRobotClient.Core.RobotAPI.Dto.Request;
namespace NapCatRobotClient.Service.Group.TextProcess
{
/// <summary>
/// 坊市上架命令
/// </summary>
public class GoodsUpShopProcess
{
public static async Task<bool> ProcessGroupRequest(string groupId, string message)
{
try
{
JObject json = JObject.Parse(message);
var messageArray = JArray.Parse(json["message"].ToString());
string groupMsg = messageArray.FirstOrDefault(o => o["type"].ToString() == "text"
&& !string.IsNullOrWhiteSpace(o["data"]["text"].ToString()))?["data"]?["text"]?.ToString();
if (string.IsNullOrWhiteSpace(groupMsg)) return false;
// 小小药材背包
List<string> 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<bool> Herbal(string groupId, string text, bool upCmd)
{
List<GoodsInfo> results = [];
var lines = text.Split(new[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
foreach (var line in lines)
{
if (line.StartsWith("品级:"))
{
_ = 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<GoodsInfo>(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);
}
}
}