2026-07-09 11:26:23 +08:00
|
|
|
|
"""
|
2026-07-13 15:43:03 +08:00
|
|
|
|
Flask 主应用
|
2026-07-09 11:26:23 +08:00
|
|
|
|
订单日报系统
|
|
|
|
|
|
"""
|
|
|
|
|
|
import os
|
|
|
|
|
|
from datetime import datetime
|
2026-07-13 15:43:03 +08:00
|
|
|
|
from flask import Flask, render_template
|
2026-07-09 11:26:23 +08:00
|
|
|
|
from flask_cors import CORS
|
|
|
|
|
|
from apscheduler.schedulers.background import BackgroundScheduler
|
|
|
|
|
|
import pytz
|
|
|
|
|
|
|
2026-07-13 15:43:03 +08:00
|
|
|
|
from lib.db import execute_query
|
2026-07-09 11:26:23 +08:00
|
|
|
|
from lib.report_generator import generate_daily_report
|
2026-07-13 15:43:03 +08:00
|
|
|
|
from lib.api import register_blueprints
|
2026-07-09 11:26:23 +08:00
|
|
|
|
|
2026-07-13 15:43:03 +08:00
|
|
|
|
# 创建 Flask 应用
|
2026-07-09 11:26:23 +08:00
|
|
|
|
app = Flask(__name__,
|
|
|
|
|
|
template_folder='templates',
|
|
|
|
|
|
static_folder='public')
|
|
|
|
|
|
CORS(app)
|
|
|
|
|
|
|
2026-07-13 15:43:03 +08:00
|
|
|
|
# 确保 public/reports 目录存在
|
2026-07-09 11:26:23 +08:00
|
|
|
|
os.makedirs(os.path.join('public', 'reports'), exist_ok=True)
|
|
|
|
|
|
|
2026-07-13 15:43:03 +08:00
|
|
|
|
# 注册 API 蓝图
|
|
|
|
|
|
register_blueprints(app)
|
|
|
|
|
|
|
2026-07-09 11:26:23 +08:00
|
|
|
|
|
|
|
|
|
|
# ==================== 页面路由 ====================
|
|
|
|
|
|
|
|
|
|
|
|
@app.route('/')
|
|
|
|
|
|
def index():
|
|
|
|
|
|
"""主页"""
|
|
|
|
|
|
return render_template('index.html')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# ==================== 定时任务 ====================
|
|
|
|
|
|
|
|
|
|
|
|
def scheduled_job():
|
2026-07-13 15:43:03 +08:00
|
|
|
|
"""定时任务:每天 8:01 自动生成日报"""
|
2026-07-09 11:26:23 +08:00
|
|
|
|
print(f'[{datetime.now()}] 开始执行定时任务...')
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
# 获取所有启用的配置
|
|
|
|
|
|
configs = execute_query(
|
|
|
|
|
|
'SELECT id, config_name FROM t_daily_report_config WHERE is_active = 1'
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
if not configs:
|
|
|
|
|
|
print('没有启用的配置,跳过生成')
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
print(f'找到 {len(configs)} 个启用的配置')
|
|
|
|
|
|
|
|
|
|
|
|
# 为每个配置生成日报
|
|
|
|
|
|
for config in configs:
|
2026-07-13 15:43:03 +08:00
|
|
|
|
print(f'正在生成:{config["config_name"]}')
|
2026-07-09 11:26:23 +08:00
|
|
|
|
result = generate_daily_report(config['id'])
|
|
|
|
|
|
|
|
|
|
|
|
if result['success']:
|
2026-07-13 15:43:03 +08:00
|
|
|
|
print(f' ✓ 成功:{result["total_orders"]} 条订单,金额:{result["total_amount"]}')
|
2026-07-09 11:26:23 +08:00
|
|
|
|
else:
|
2026-07-13 15:43:03 +08:00
|
|
|
|
print(f' ✗ 失败:{result["message"]}')
|
2026-07-09 11:26:23 +08:00
|
|
|
|
|
|
|
|
|
|
print('定时任务执行完成')
|
|
|
|
|
|
except Exception as e:
|
2026-07-13 15:43:03 +08:00
|
|
|
|
print(f'定时任务执行失败:{e}')
|
2026-07-09 11:26:23 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 启动定时任务
|
|
|
|
|
|
scheduler = BackgroundScheduler(timezone='Asia/Shanghai')
|
|
|
|
|
|
scheduler.add_job(scheduled_job, 'cron', hour=8, minute=1)
|
|
|
|
|
|
scheduler.start()
|
|
|
|
|
|
print('定时任务已启动:每天 08:01 执行')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# ==================== 启动应用 ====================
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2026-07-09 15:00:25 +08:00
|
|
|
|
# 支持沙箱环境和本地环境
|
|
|
|
|
|
port = int(os.environ.get('DEPLOY_RUN_PORT', os.environ.get('PORT', 5000)))
|
2026-07-13 15:43:03 +08:00
|
|
|
|
print(f'启动服务器:http://localhost:{port}')
|
|
|
|
|
|
print(f'数据库:{os.environ.get("DB_HOST", "haoslm2.xicp.net")}:{os.environ.get("DB_PORT", "10216")}')
|
|
|
|
|
|
app.run(host='0.0.0.0', port=port, debug=True)
|