deep-risk/backend/scripts/export_to_excel.py
2025-12-14 20:08:27 +08:00

171 lines
6.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python3
"""
将测试数据导出为Excel格式以便通过数据采集模块导入到正式数据库
"""
import json
import pandas as pd
from datetime import datetime, timedelta
import random
from typing import List, Dict
def load_test_data():
"""加载测试数据"""
data_dir = "/Users/liulujian/Documents/code/deeprisk-claude-1/backend/test_data/revenue_test"
with open(f"{data_dir}/streamers.json", 'r', encoding='utf-8') as f:
streamers = json.load(f)
with open(f"{data_dir}/contracts.json", 'r', encoding='utf-8') as f:
contracts = json.load(f)
with open(f"{data_dir}/recharges.json", 'r', encoding='utf-8') as f:
recharges = json.load(f)
with open(f"{data_dir}/tax_declarations.json", 'r', encoding='utf-8') as f:
tax_declarations = json.load(f)
return streamers, contracts, recharges, tax_declarations
def export_streamers_to_excel(streamers: List[Dict]):
"""导出主播信息到Excel"""
data = []
for idx, s in enumerate(streamers):
# 为每个主播生成唯一的身份证号
id_card_no = f"11010119900101{1000 + idx:04d}"
data.append({
"streamer_id": s['streamer_id'],
"real_name": s['streamer_name'],
"stage_name": s['streamer_name'],
"id_card_no": id_card_no,
"phone": "13800138000",
"email": f"{s['streamer_id'].lower()}@example.com",
"platform": "抖音",
"mcn_agency_id": "",
"entry_date": "2024-01-01",
"contract_status": "有效",
"entity_type": "个人",
"tax_region": "北京市",
"bank_account_no": "6222021234567890123",
"bank_name": "中国工商银行",
})
df = pd.DataFrame(data)
output_file = "/Users/liulujian/Documents/code/deeprisk-claude-1/backend/test_data/revenue_test/主播信息导入模板.xlsx"
df.to_excel(output_file, index=False, engine='openpyxl')
print(f"✓ 主播信息已导出到: {output_file}")
print(f"{len(data)} 条记录")
def export_contracts_to_excel(contracts: List[Dict]):
"""导出分成协议到Excel"""
data = []
for c in contracts:
data.append({
"contract_no": c['contract_id'],
"contract_type": "独家",
"streamer_id": c['streamer_id'],
"streamer_name": c['streamer_id'],
"streamer_entity_type": "个人",
"platform_party": c['platform'],
"platform_credit_code": "91110000000000000X",
"revenue_type": "直播打赏",
"platform_ratio": float(c['platform_share_ratio']),
"streamer_ratio": float(c['share_ratio']),
"settlement_cycle": "月结",
"contract_start_date": c['start_date'],
"contract_end_date": c['end_date'],
"contract_status": c['status'],
"remark": "",
})
df = pd.DataFrame(data)
output_file = "/Users/liulujian/Documents/code/deeprisk-claude-1/backend/test_data/revenue_test/分成协议导入模板.xlsx"
df.to_excel(output_file, index=False, engine='openpyxl')
print(f"✓ 分成协议已导出到: {output_file}")
print(f"{len(data)} 条记录")
def export_recharges_to_excel(recharges: List[Dict]):
"""导出充值记录到Excel"""
data = []
for r in recharges:
data.append({
"platform": "抖音",
"recharge_amount": float(r['recharge_amount']),
"recharge_date": r['recharge_date'],
"recharge_type": "现金",
"account_name": r['streamer_id'],
"voucher_no": f"CZ{r['recharge_id']}",
"remark": f"用户{r['streamer_id']}的充值记录",
})
df = pd.DataFrame(data)
output_file = "/Users/liulujian/Documents/code/deeprisk-claude-1/backend/test_data/revenue_test/充值记录导入模板.xlsx"
df.to_excel(output_file, index=False, engine='openpyxl')
print(f"✓ 充值记录已导出到: {output_file}")
print(f"{len(data)} 条记录")
def export_tax_declarations_to_excel(tax_declarations: List[Dict]):
"""导出税务申报到Excel"""
data = []
for t in tax_declarations:
data.append({
"taxpayer_name": t['streamer_id'],
"taxpayer_id": t['tax_no'],
"tax_period": t['declaration_period'],
"declaration_date": t['declaration_date'],
"tax_authority_code": "11010100",
"tax_authority_name": "北京市朝阳区税务局",
"taxpayer_type": "小规模纳税人",
"tax_rate": float(t['tax_rate']),
"sales_revenue": float(t['declared_amount']),
"sales_revenue_taxable": float(t['declared_amount']),
"output_tax": float(t['tax_amount']),
"input_tax": 0.00,
"input_tax_deductible": 0.00,
"tax_payable": float(t['tax_amount']),
"tax_to_pay": float(t['tax_amount']),
"refund_amount": 0.00,
"declaration_status": t['status'],
"is_reconciled": "",
})
df = pd.DataFrame(data)
output_file = "/Users/liulujian/Documents/code/deeprisk-claude-1/backend/test_data/revenue_test/税务申报导入模板.xlsx"
df.to_excel(output_file, index=False, engine='openpyxl')
print(f"✓ 税务申报已导出到: {output_file}")
print(f"{len(data)} 条记录")
def main():
"""主函数"""
print("=" * 80)
print("将测试数据导出为Excel格式")
print("=" * 80)
print()
# 加载测试数据
streamers, contracts, recharges, tax_declarations = load_test_data()
# 导出各类数据
print("\n📤 正在导出数据...")
export_streamers_to_excel(streamers)
export_contracts_to_excel(contracts)
export_recharges_to_excel(recharges)
export_tax_declarations_to_excel(tax_declarations)
print("\n" + "=" * 80)
print("✅ 所有数据导出完成!")
print("=" * 80)
print("\n📋 导出文件列表:")
print(" 1. 主播信息导入模板.xlsx")
print(" 2. 分成协议导入模板.xlsx")
print(" 3. 充值记录导入模板.xlsx")
print(" 4. 税务申报导入模板.xlsx")
print("\n📖 使用说明:")
print(" 1. 通过前端界面 http://localhost:3000/data-import/upload 上传文件")
print(" 2. 选择对应的导入类型streamer/contract/recharge/tax")
print(" 3. 确认导入并等待完成")
print("\n" + "=" * 80)
if __name__ == "__main__":
main()