From 668e274ab23b6e7699614a7087d96ae86788171a Mon Sep 17 00:00:00 2001 From: zayac Date: Tue, 30 Jan 2024 17:57:30 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=AD=98=E6=AC=BE=E4=B8=8D?= =?UTF-8?q?=E6=8F=90=E7=A4=BA=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/entity/pay_record.py | 79 ++++++++++++++++++++++++++-------------- src/ui/app.py | 1 + 2 files changed, 52 insertions(+), 28 deletions(-) diff --git a/src/entity/pay_record.py b/src/entity/pay_record.py index f5e4dd0..657c12b 100644 --- a/src/entity/pay_record.py +++ b/src/entity/pay_record.py @@ -11,7 +11,7 @@ from src.core.util import get_curr_day, get_first_day_month from src.entity.account import Account from src.entity.member import (MemberList, async_get_member_detail_by_name, get_member_by_name, get_member_list) -from src.entity.user import User, get_user_by_telegram_id +from src.entity.user import User, get_user_by_telegram_id, get_user_by_username_and_password @dataclass @@ -51,37 +51,53 @@ def get_pay_record(account: Account): return [PayRecord(**item) for item in api_response.data['list']] +from concurrent.futures import ThreadPoolExecutor, as_completed +from datetime import datetime + + def get_latest_deposit_user(account: Account, count: int): logger.info(f'Getting latest deposit user for account: {account.name} and count: {count}') pay_record_list = get_pay_record(account) - # 提取所有用户名 - names = [] - seen = set() + + # 提取所有用户名,并确保在指定时间内 now = datetime.now() - for item in pay_record_list: - name = item.name - if name not in seen and util.get_difference(item.createdAt, now) < 7200: - names.append(name) - seen.add(name) - logger.debug(f"获取到{len(names)}个成功存款人数") - # 开启多线程 根据用户名查询所有数据 - with ThreadPoolExecutor(max_workers=len(names)) as executor: - futures = [executor.submit(get_member_by_name, account, name) for name in names] - try: - results = [future.result() for future in futures] - except Exception as e: - logger.debug(f'查询失败:{e}') - sorted_members = sorted(results, key=lambda member_detail: member_detail.get_first_pay_datetime(), reverse=True) - # 截取前n个 - details = sorted_members[:count] - record_dict = {record.createdAt: record.scoreAmount for record in pay_record_list} - member_details = [] - for detail in details: - if detail.firstPayAt in record_dict: - detail.deposit = record_dict[detail.firstPayAt] - member_details.append(detail) - logger.info(f'Finished getting latest deposit user for account: {account.name} and count: {count}') - return member_details + unique_names_within_time = { + item.name for item in pay_record_list if util.get_difference(item.createdAt, now) < 7200 + } + + logger.debug(f"获取到{len(unique_names_within_time)}个成功存款人数") + + # 开启多线程根据用户名查询所有数据 + results = [] + with ThreadPoolExecutor(max_workers=min(len(unique_names_within_time), 20)) as executor: # 限制最大工作线程数 + futures = [executor.submit(get_member_by_name, account, name) for name in unique_names_within_time] + for future in as_completed(futures): + try: + result = future.result() + results.append(result) + except Exception as e: + logger.debug(f'查询失败:{e}') + + # 筛选出有有效结果的成员,并按首次付款时间降序排序 + valid_results = [result for result in results if result is not None] + sorted_members = sorted(valid_results, key=lambda member_detail: member_detail.get_first_pay_datetime(), + reverse=True) + + # 截取前n个 + selected_members = sorted_members[:count] + + # 构建时间和金额的映射字典 + record_dict = {record.createdAt: record.scoreAmount for record in pay_record_list} + + member_details = [] + for detail in selected_members: + # 检查 detail.firstPayAt 是否在 record_dict 中,避免 KeyError + if detail.firstPayAt in record_dict: + detail.deposit = record_dict[detail.firstPayAt] + member_details.append(detail) + + logger.info(f'Finished getting latest deposit user for account: {account.name} and count: {count}') + return member_details # 根据用户查询最新存款信息 @@ -227,3 +243,10 @@ def get_pay_failed_by_telegram_id(telegram_id: int) -> Optional[str]: logger.info(text) return text + + +if __name__ == '__main__': + deposit_results = '\n'.join( + [f"用户: `{member.name}`, 首存金额: *{member.deposit}*" for member in + get_latest_deposit_user(get_user_by_username_and_password('zayac', 'password').accounts[0], 1)]) + print(deposit_results) diff --git a/src/ui/app.py b/src/ui/app.py index 74d632c..0cb0a37 100644 --- a/src/ui/app.py +++ b/src/ui/app.py @@ -549,3 +549,4 @@ class Application(QMainWindow): def copy_to_clipboard(text): clipboard = QApplication.clipboard() clipboard.setText(text) +