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.
128 lines
5.3 KiB
C#
128 lines
5.3 KiB
C#
using System.Text.RegularExpressions;
|
|
using Lagrange.Core.Common.Interface.Api;
|
|
using Lagrange.Core.Message;
|
|
using Lagrange.Core.Message.Entity;
|
|
using RoBot.Core;
|
|
using RoBot.Core.ConstValue;
|
|
using RoBot.Core.Helper;
|
|
using RoBot.Start.Global;
|
|
using RoBot.Start.LogConfig;
|
|
using RoBot.Start.Service.Dto;
|
|
|
|
namespace RoBot.Start.Cmd
|
|
{
|
|
/// <summary>
|
|
/// 坊市上架命令
|
|
/// </summary>
|
|
public class GoodsUpShopCmd
|
|
{
|
|
public static async Task<bool> Execute(MessageChain chain)
|
|
{
|
|
try
|
|
{
|
|
List<IMessageEntity> forwardEntities = chain.Where(o => o is ForwardEntity).ToList();
|
|
List<IMessageEntity> textEntities = chain.Where(o => o is TextEntity).ToList();
|
|
if (chain.Count is 3 or 5 && forwardEntities.Count > 0 && textEntities.Count > 0)
|
|
{
|
|
bool any = textEntities.Cast<TextEntity>().ToList().Where(o => o.Text.Contains("查上架") || o.Text.Contains("查价格")).Any();
|
|
if (any)
|
|
{
|
|
if (textEntities.Count == 0) return false;
|
|
|
|
ForwardEntity currentF = (ForwardEntity)forwardEntities[0];
|
|
|
|
List<TextEntity> convertList = currentF.Chain.Where(o => o is TextEntity).Cast<TextEntity>().ToList();
|
|
convertList = convertList.Where(o => string.IsNullOrWhiteSpace(o.Text) is false)
|
|
.ToList();
|
|
if (convertList.Count > 0)
|
|
{
|
|
string desc = convertList[0].Text;
|
|
|
|
if (desc.Contains("拥有药材") ||
|
|
desc.Contains("的丹药背包") ||
|
|
(desc.Contains("名字") && desc.Contains("拥有数量"))
|
|
)
|
|
{
|
|
_ = Herbal(chain, desc);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Logs.Write($@"{DateTime.Now:yyyy-MM-dd HH:mm:ss} 坊市上架命令 发生异常,异常信息:{ex.Message},异常堆栈:{ex.StackTrace}");
|
|
|
|
}
|
|
return await Task.FromResult(true);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 计算药材
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
private static async Task<bool> Herbal(MessageChain chain, string text)
|
|
{
|
|
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);
|
|
|
|
msg += $"确认坊市上架{item.Name} {(int)current.Price - 100000} {num}\n";
|
|
}
|
|
}
|
|
if (string.IsNullOrWhiteSpace(msg) is false)
|
|
{
|
|
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)}";
|
|
var systemConfig = GlobalConfig.ConfigSetting;
|
|
var bot = GlobalConfig.BotContext;
|
|
|
|
var groupMessageChain = MessageBuilder.Group((uint)systemConfig.GroupQQ).Forward(chain).Text(msg);
|
|
await bot.SendMessage(groupMessageChain.Build());
|
|
}
|
|
|
|
return await Task.FromResult(true);
|
|
}
|
|
}
|
|
}
|