diff --git a/point_trade/main.py b/point_trade/main.py
index 0f3cb01..958b2bf 100644
--- a/point_trade/main.py
+++ b/point_trade/main.py
@@ -33,48 +33,30 @@ class PointTrade:
self.LOG.info(f"[积分交易] 组件初始化完成,指令: {self.command}")
def at_list(self, xml):
- # xml demo内容
- # xml_data = '''
- #
- #
- #
- # 1
- # 1
- # 5
- # V1_tTavqCYr|v1_tTavqCYr
- #
- #
- #
- #
- #
- # 1
- #
- #
- # '''
- # 解析 XML 数据
- # 解析 XML 数据
- root = ET.fromstring(xml)
+ """
+ 解析消息中的 @用户列表
+ :param xml: 消息的 XML 数据
+ :return: @用户的集合
+ """
+ try:
+ root = ET.fromstring(xml)
+ atuserlist_element = root.find('.//atuserlist')
+ atuserlist_content = (atuserlist_element.text if atuserlist_element is not None else '').strip()
- # 查找 元素并提取其文本内容
- atuserlist_element = root.find('.//atuserlist')
- atuserlist_content = (atuserlist_element.text if atuserlist_element is not None else '').strip() # 去除前后的所有空白字符
-
- # 由于 CDATA 内容前后有逗号,我们需要先去除它们,然后再分割字符串
- # 注意:这里我们假设 CDATA 内容只有前后的逗号是多余的,内部没有逗号需要保留
- atuserlist_content_no_commas = atuserlist_content.strip(',') # 实际上这里只会去除前后的逗号,不会去除换行符等其他空白字符
- # 如果需要去除换行符,可以再加一行:
- atuserlist_content_no_commas = atuserlist_content_no_commas.replace('\n', '')
- atuserlist_content_no_commas = re.sub(r'\s+', '', atuserlist_content_no_commas)
- # 但由于您的 CDATA 示例中没有换行符,所以这行通常是不必要的
-
- # 按逗号分割成列表,然后转换成集合
- atuserlist_set = set(atuserlist_content_no_commas.split(','))
-
- # 输出集合
- print("atuserlist 集合:", atuserlist_set)
- return atuserlist_set
+ atuserlist_content_no_commas = atuserlist_content.strip(',')
+ atuserlist_content_no_commas = re.sub(r'\s+', '', atuserlist_content_no_commas)
+ atuserlist_set = set(atuserlist_content_no_commas.split(','))
+ self.LOG.debug(f"解析到的 @用户列表: {atuserlist_set}")
+ return atuserlist_set
+ except ET.ParseError as e:
+ self.LOG.error(f"解析 XML 失败: {e}")
+ return set()
def handle_text(self, message: WxMsg):
+ """
+ 处理文本消息,进行积分交易
+ :param message: 微信消息对象
+ """
if not self.enable:
return
@@ -103,11 +85,11 @@ class PointTrade:
return
reward_points = int(command[1])
-
target_wxid = next(iter(self.at_list(message.xml)))
trader_wxid = message.sender
group_id = message.roomid
- # 查询发信人的记录(获取发信人当前积分)
+
+ # 查询发信人的记录
sender_result = self._get_user_record(trader_wxid, group_id)
if not sender_result:
self.wcf.send_text(f"-----Bot-----\n❌打赏失败!\n没有找到你的记录,无法进行打赏!",
@@ -115,18 +97,17 @@ class PointTrade:
return
sender_user_id, sender_wx_id, sender_wx_nick_name, sender_current_points = sender_result
-
sender_current_points = int(sender_current_points)
- # 如果发信人积分不足
+
+ # 检查发信人积分是否足够
if sender_current_points < reward_points:
self.wcf.send_text(
f"-----Bot-----\n❌打赏失败!\n你的积分不足以进行打赏!当前积分:{sender_current_points},你需要 {reward_points} 积分。",
(message.roomid if message.from_group() else message.sender), message.sender)
return
- # 查询被打赏人的记录
+ # 查询被打赏人的记录
recipient_result = self._get_user_record_by_nick(target_wxid, group_id)
-
if not recipient_result:
self.wcf.send_text(
f"-----Bot-----\n❌打赏失败!\n接收人[{target_wxid}]无法收取积分",
@@ -134,15 +115,22 @@ class PointTrade:
return
recipient_user_id, recipient_wx_id, recipient_wx_nick_name, recipient_current_points = recipient_result
-
recipient_current_points = int(recipient_current_points)
- # 计算发信人和接收者的新积分
- new_sender_points = sender_current_points - reward_points
- new_recipient_points = recipient_current_points + reward_points
- # 更新发信人和接收者的积分
- self._update_user_points(sender_user_id, new_sender_points, group_id)
- self._update_user_points(recipient_user_id, new_recipient_points, group_id)
+ # 使用 SQL 增量更新积分(不再在程序中计算)
+ try:
+ self._update_user_points(sender_user_id, -reward_points, group_id) # 减少发送者积分
+ self._update_user_points(recipient_user_id, reward_points, group_id) # 增加接收者积分
+ except mysql.connector.Error as e:
+ self.wcf.send_text(f"-----Bot-----\n❌积分更新失败!请稍后重试。错误: {str(e)}",
+ (message.roomid if message.from_group() else message.sender), message.sender)
+ return
+
+ # 获取更新后的积分值用于显示
+ 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 = (
f"\n-----Bot-----\n"
@@ -169,40 +157,53 @@ class PointTrade:
:param group_id: 群组ID
:return: 用户记录(id, wx_id, wx_nick_name, points)
"""
- # 连接到数据库
- with self._get_db_connection() as conn:
- with conn.cursor(dictionary=True) as cursor:
- cursor.execute("""
- SELECT id, wx_id, wx_nick_name, points FROM t_sign_record
- WHERE wx_id = %s AND group_id = %s
- """, (wx_id, group_id))
- return cursor.fetchone()
+ try:
+ with self._get_db_connection() as conn:
+ with conn.cursor(dictionary=True) as cursor:
+ cursor.execute("""
+ SELECT id, wx_id, wx_nick_name, points FROM t_sign_record
+ WHERE wx_id = %s AND group_id = %s
+ """, (wx_id, group_id))
+ return cursor.fetchone()
+ except mysql.connector.Error as e:
+ self.LOG.error(f"查询用户记录失败: {e}")
+ return None
def _get_user_record_by_nick(self, wx_id, group_id):
"""
- 根据微信昵称查询用户的记录
- :param wx_id: 用户的微信昵称
+ 根据微信ID查询用户的记录(此处与 _get_user_record 功能相同,可优化)
+ :param wx_id: 用户的微信ID
:param group_id: 群组ID
:return: 用户记录(id, wx_id, wx_nick_name, points)
"""
- with self._get_db_connection() as conn:
- with conn.cursor(dictionary=True) as cursor:
- cursor.execute("""
- SELECT id, wx_id, wx_nick_name, points FROM t_sign_record
- WHERE wx_id = %s AND group_id = %s
- """, (wx_id, group_id))
- return cursor.fetchone()
+ try:
+ with self._get_db_connection() as conn:
+ with conn.cursor(dictionary=True) as cursor:
+ cursor.execute("""
+ SELECT id, wx_id, wx_nick_name, points FROM t_sign_record
+ WHERE wx_id = %s AND group_id = %s
+ """, (wx_id, group_id))
+ return cursor.fetchone()
+ except mysql.connector.Error as e:
+ self.LOG.error(f"查询用户记录失败: {e}")
+ return None
- def _update_user_points(self, user_id, new_points, group_id):
+ def _update_user_points(self, user_id, points_change, group_id):
"""
- 更新用户积分
- :param user_id: 用户ID
- :param new_points: 新的积分数
+ 更新用户积分,使用 SQL 增量调整
+ :param user_id: 用户ID (数据库中的 id 字段)
+ :param points_change: 积分变化量(正数增加,负数减少)
+ :param group_id: 群组ID
"""
- with self._get_db_connection() as conn:
- with conn.cursor(dictionary=True) as cursor:
- cursor.execute("""
- UPDATE t_sign_record
- SET points = %s, update_time = %s
- WHERE wx_id = %s AND group_id = %s
- """, (new_points, datetime.now(), user_id, group_id))
+ try:
+ with self._get_db_connection() as conn:
+ with conn.cursor(dictionary=True) as cursor:
+ cursor.execute("""
+ UPDATE t_sign_record
+ SET points = points + %s, update_time = %s
+ WHERE id = %s AND group_id = %s
+ """, (points_change, datetime.now(), user_id, group_id))
+ conn.commit()
+ except mysql.connector.Error as e:
+ self.LOG.error(f"更新用户积分失败: {e}")
+ raise
\ No newline at end of file