积分转账功能,使用用户积分
This commit is contained in:
@@ -7,6 +7,7 @@ import xml.etree.ElementTree as ET
|
|||||||
from wcferry import Wcf
|
from wcferry import Wcf
|
||||||
|
|
||||||
from db.connection import DBConnectionManager
|
from db.connection import DBConnectionManager
|
||||||
|
from db.points_db import PointsDBOperator, PointSource
|
||||||
from plugin_common.message_plugin_interface import MessagePluginInterface
|
from plugin_common.message_plugin_interface import MessagePluginInterface
|
||||||
from plugin_common.plugin_interface import PluginStatus
|
from plugin_common.plugin_interface import PluginStatus
|
||||||
from utils.decorator.plugin_decorators import plugin_stats_decorator
|
from utils.decorator.plugin_decorators import plugin_stats_decorator
|
||||||
@@ -57,6 +58,9 @@ class PointTradePlugin(MessagePluginInterface):
|
|||||||
self.message_util = context.get("message_util")
|
self.message_util = context.get("message_util")
|
||||||
self.gbm = context.get("gbm")
|
self.gbm = context.get("gbm")
|
||||||
self.db_manager = DBConnectionManager.get_instance()
|
self.db_manager = DBConnectionManager.get_instance()
|
||||||
|
|
||||||
|
# 初始化积分数据库操作类
|
||||||
|
self.points_db = PointsDBOperator(self.db_manager)
|
||||||
|
|
||||||
self.db_pool = self.db_manager.mysql_pool
|
self.db_pool = self.db_manager.mysql_pool
|
||||||
|
|
||||||
@@ -136,63 +140,46 @@ class PointTradePlugin(MessagePluginInterface):
|
|||||||
group_id = roomid
|
group_id = roomid
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# 查询发信人的记录
|
# 使用新的积分系统进行转账
|
||||||
sender_result = self._get_user_record(trader_wxid, group_id)
|
success, result = self.points_db.transfer_points(
|
||||||
if not sender_result:
|
trader_wxid, target_wxid, group_id,
|
||||||
wcf.send_text(f"❌打赏失败!\n没有找到你的记录,无法进行打赏!",
|
reward_points, f"积分转账命令执行"
|
||||||
(roomid if roomid else sender), sender)
|
)
|
||||||
return True, "发送者记录不存在"
|
|
||||||
|
if not success:
|
||||||
sender_user_id = sender_result['id']
|
error_msg = result.get("error", "未知错误")
|
||||||
sender_wx_id = sender_result['wx_id']
|
if "积分不足" in error_msg:
|
||||||
sender_wx_nick_name = sender_result['wx_nick_name']
|
current_points = result.get("current_points", 0)
|
||||||
sender_current_points = int(sender_result['points'])
|
wcf.send_text(
|
||||||
|
f"❌转账失败!\n你的积分不足以进行转账!当前积分:{current_points},你需要 {reward_points} 积分。",
|
||||||
# 检查发信人积分是否足够
|
(roomid if roomid else sender), sender)
|
||||||
if sender_current_points < reward_points:
|
else:
|
||||||
wcf.send_text(
|
wcf.send_text(
|
||||||
f"❌打赏失败!\n你的积分不足以进行打赏!当前积分:{sender_current_points},你需要 {reward_points} 积分。",
|
f"❌转账失败!\n{error_msg}",
|
||||||
(roomid if roomid else sender), sender)
|
(roomid if roomid else sender), sender)
|
||||||
return True, "积分不足"
|
return True, f"转账失败: {error_msg}"
|
||||||
|
|
||||||
# 查询被打赏人的记录
|
# 获取转账后的积分信息
|
||||||
recipient_result = self._get_user_record_by_nick(target_wxid, group_id)
|
from_user = result.get("from_user", {})
|
||||||
if not recipient_result:
|
to_user = result.get("to_user", {})
|
||||||
wcf.send_text(
|
|
||||||
f"❌打赏失败!\n接收人[{target_wxid}]无法收取积分",
|
# 获取用户昵称
|
||||||
(roomid if roomid else sender), sender)
|
from_user_info = self._get_user_record(trader_wxid, group_id)
|
||||||
return True, "接收者记录不存在"
|
to_user_info = self._get_user_record(target_wxid, group_id)
|
||||||
|
|
||||||
recipient_user_id = recipient_result['id']
|
from_user_name = from_user_info.get('wx_nick_name', trader_wxid) if from_user_info else trader_wxid
|
||||||
recipient_wx_id = recipient_result['wx_id']
|
to_user_name = to_user_info.get('wx_nick_name', target_wxid) if to_user_info else target_wxid
|
||||||
recipient_wx_nick_name = recipient_result['wx_nick_name']
|
|
||||||
recipient_current_points = int(recipient_result['points'])
|
|
||||||
|
|
||||||
# 使用 SQL 增量更新积分
|
|
||||||
self._update_user_points(sender_user_id, -reward_points, group_id) # 减少发送者积分
|
|
||||||
self._update_user_points(recipient_user_id, reward_points, group_id) # 增加接收者积分
|
|
||||||
|
|
||||||
# 获取更新后的积分值用于显示
|
|
||||||
updated_sender = self._get_user_record(trader_wxid, group_id)
|
|
||||||
updated_recipient = self._get_user_record_by_nick(target_wxid, group_id)
|
|
||||||
new_sender_points = int(updated_sender['points']) if updated_sender else sender_current_points
|
|
||||||
new_recipient_points = int(updated_recipient['points']) if updated_recipient else recipient_current_points
|
|
||||||
|
|
||||||
output = (
|
output = (
|
||||||
f"✅积分转账成功!\n"
|
f"✅积分转账成功!\n"
|
||||||
f"👤{sender_wx_nick_name} 转给 👤{recipient_wx_nick_name} {reward_points} 积分\n"
|
f"👤{from_user_name} 转给 👤{to_user_name} {reward_points} 积分\n"
|
||||||
f"👤{sender_wx_nick_name} 当前积分: {new_sender_points}\n"
|
f"👤{from_user_name} 当前积分: {from_user.get('total_points', 0)}\n"
|
||||||
f"👤{recipient_wx_nick_name} 当前积分: {new_recipient_points}"
|
f"👤{to_user_name} 当前积分: {to_user.get('total_points', 0)}"
|
||||||
)
|
)
|
||||||
|
|
||||||
wcf.send_text(output, (roomid if roomid else sender), sender)
|
wcf.send_text(output, (roomid if roomid else sender), sender)
|
||||||
return True, "转账成功"
|
return True, "转账成功"
|
||||||
|
|
||||||
except mysql.connector.Error as e:
|
|
||||||
self.LOG.error(f"积分交易出错: {e}")
|
|
||||||
wcf.send_text(f"❌积分交易失败!请稍后重试。错误: {str(e)}",
|
|
||||||
(roomid if roomid else sender), sender)
|
|
||||||
return True, f"数据库错误: {str(e)}"
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.LOG.error(f"积分交易出错: {e}")
|
self.LOG.error(f"积分交易出错: {e}")
|
||||||
wcf.send_text(f"❌积分交易失败!请稍后重试。错误: {str(e)}",
|
wcf.send_text(f"❌积分交易失败!请稍后重试。错误: {str(e)}",
|
||||||
|
|||||||
Reference in New Issue
Block a user