commit 8eb49b676d421c3cb3c3d87f192611e7cc4c6ea8 Author: admin <45275340@qq.com> Date: Fri Nov 6 05:52:23 2026 +0800 Initial commit: 公示系统集合 Flask 项目 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..91afacb --- /dev/null +++ b/.gitignore @@ -0,0 +1,28 @@ +# Python +__pycache__/ +*.pyc +*.pyo +*.egg-info/ +dist/ +build/ + +# 虚拟环境 +venv/ +.env/ +env/ +.venv/ + +# IDE +.idea/ +.vscode/ +*.swp + +# OS +.DS_Store +Thumbs.db + +# Trae 临时文件 +.trae/ + +# 日志 +*.log diff --git a/app.py b/app.py new file mode 100644 index 0000000..174f550 --- /dev/null +++ b/app.py @@ -0,0 +1,83 @@ +from flask import Flask, abort, jsonify, render_template, request + +from config import APP_CONFIG, PROJECTS +from query_service import query_project_stats, safe_date + + +app = Flask(__name__) + + +def get_project_or_404(project_key: str): + project = PROJECTS.get(project_key) + if not project: + abort(404) + return project + + +@app.route("/") +def home(): + user_uuid = str(request.args.get("uuid", "") or "").strip() + return render_template( + "home.html", + app_name=APP_CONFIG["app_name"], + projects=PROJECTS, + user_uuid=user_uuid, + ) + + +@app.route("/project/") +def project_page(project_key: str): + project = get_project_or_404(project_key) + user_uuid = str(request.args.get("uuid", "") or "").strip() + today = safe_date(request.args.get("date", "")) + return render_template( + "project.html", + project_key=project_key, + project=project, + user_uuid=user_uuid, + today=today, + app_name=APP_CONFIG["app_name"], + ) + + +@app.route("/api/project//stats") +def api_project_stats(project_key: str): + project = get_project_or_404(project_key) + date_str = safe_date(request.args.get("date", "")) + user_uuid = str(request.args.get("uuid", "") or "").strip() + result = query_project_stats(project, date_str, user_uuid) + if not result.get("success"): + return jsonify( + { + "success": False, + "project_key": project_key, + "date": date_str, + "uuid": user_uuid, + "error": result.get("error") or "查询失败", + } + ), 500 + return jsonify( + { + "success": True, + "configured": bool(result.get("configured")), + "message": str(result.get("message") or ""), + "project_key": project_key, + "project_title": project.get("title", ""), + "date": date_str, + "uuid": user_uuid, + "total": len(result["rows"]), + "sum_count": result["sum_count"], + "has_current_user": result["has_current_user"], + "breakdowns": result.get("breakdowns") or [], + "rows": result["rows"], + } + ) + + +if __name__ == "__main__": + print(f"访问地址: http://127.0.0.1:{APP_CONFIG['port']}") + app.run( + host=APP_CONFIG["host"], + port=APP_CONFIG["port"], + debug=APP_CONFIG["debug"], + ) diff --git a/config.py b/config.py new file mode 100644 index 0000000..8e0329b --- /dev/null +++ b/config.py @@ -0,0 +1,152 @@ +DATABASES = { + "wfj_old": { + "DB_HOST": "183.6.7.91", + "DB_PORT": 33016, + "DB_USER": "root", + "DB_PASSWORD": "Aliyun@7646!", + "DB_NAME": "travel_project", + }, + "wfj_pyui": { + "DB_HOST": "183.6.7.91", + "DB_PORT": 6890, + "DB_USER": "root", + "DB_PASSWORD": "Aliyun@7646!", + "DB_NAME": "project_pdd", + }, + "project_menus": { + "DB_HOST": "192.168.5.2", + "DB_PORT": 3307, + "DB_USER": "root", + "DB_PASSWORD": "Aliyun@7646!", + "DB_NAME": "project_menus", + }, + "wfj_h5": { + "DB_HOST": "192.168.5.5", + "DB_PORT": 3306, + "DB_USER": "root", + "DB_PASSWORD": "Aliyun@7646!", + "DB_NAME": "elm_meishi", + }, +} + + +PROJECTS = { + "travel_task_board": { + "title": "出行任务公示", + "subtitle": "按日期统计出行任务完成情况,需使用日期选择器筛选指定日期数据。", + "db_key": "wfj_old", + "table_name": "target_input", + "query_sql": "SELECT workid, COUNT(1) AS cnt FROM target_input WHERE updated_at LIKE %s AND hl_car IS NOT NULL AND gd_car IS NOT NULL GROUP BY workid ORDER BY cnt DESC", + "status": "已配置", + "theme": "teal", + "enable_date_filter": True, + "enable_uuid_filter": True, + }, + "elm_task_1": { + "title": "饿了么1任务", + "subtitle": "按日期统计饿了么1任务完成情况,数据来源于 elm_meishi.ele_project_input。", + "db_key": "wfj_h5", + "table_name": "elm_meishi.ele_project_input", + "query_sql": "SELECT workid, COUNT(`index`) AS cnt FROM elm_meishi.ele_project_input WHERE created_at >= %s AND created_at < %s AND is_ok IN (3, 4) GROUP BY workid ORDER BY COUNT(`index`) DESC", + "status": "已配置", + "theme": "emerald", + "enable_date_filter": True, + "date_filter_mode": "day_range", + "enable_uuid_filter": True, + }, + "elm_task_2": { + "title": "饿了么2_小程序", + "subtitle": "按日期统计饿了么2_小程序任务完成情况,数据来源于 elm_meishi.target_ele_work2。", + "db_key": "wfj_old", + "table_name": "elm_meishi.target_ele_work2", + "query_sql": "SELECT workid, COUNT(`index_id`) AS cnt FROM elm_meishi.target_ele_work2 WHERE create_time > %s AND workid IS NOT NULL AND (`len_head` <> '206' AND len_body <> 206) GROUP BY workid ORDER BY COUNT(`index_id`) DESC", + "status": "已配置", + "theme": "amber", + "enable_date_filter": True, + "date_filter_mode": "raw", + "enable_uuid_filter": True, + }, + "elm_task_2_h5": { + "title": "饿了么2_h5", + "subtitle": "按日期统计饿了么2_h5任务完成情况,数据来源于 elm_meishi.target_ele_work_h5(192.168.5.5:3306)。", + "db_key": "wfj_h5", + "table_name": "elm_meishi.target_ele_work_h5", + "query_sql": "SELECT workid, COUNT(`index_id`) AS cnt FROM elm_meishi.target_ele_work_h5 WHERE create_time > %s AND workid IS NOT NULL AND len_head > 1000 AND len_body > 1000 AND len_operate > 1000 GROUP BY workid ORDER BY COUNT(`index_id`) DESC", + "status": "已配置", + "theme": "orange", + "enable_date_filter": True, + "date_filter_mode": "raw", + "enable_uuid_filter": True, + }, + "pinxixi_task": { + "title": "拼嘻嘻任务", + "subtitle": "按日期统计拼嘻嘻任务完成情况,数据来源于 return_share_new。", + "db_key": "wfj_pyui", + "table_name": "return_share_new", + "query_sql": "SELECT workid, COUNT(id) AS cnt FROM return_share_new WHERE create_time LIKE %s GROUP BY workid ORDER BY COUNT(id) DESC", + "status": "已配置", + "theme": "blue", + "enable_date_filter": True, + "enable_uuid_filter": True, + }, + "dewu_shared": { + "title": "得物分享公示", + "subtitle": "按日期统计得物分享完成情况,数据来源于 project_pdd.dewu_shared 表,c_time 匹配所选日期。", + "db_key": "wfj_pyui", + "table_name": "dewu_shared", + "query_sql": "SELECT user_uuid, COUNT(1) AS cnt FROM dewu_shared WHERE id > '393' AND c_time LIKE %s GROUP BY user_uuid ORDER BY COUNT(1) DESC", + "status": "已配置", + "theme": "red", + "enable_date_filter": True, + "enable_uuid_filter": True, + "identifier_field": "user_uuid", + }, + "model_task_board": { + "title": "模型任务公示", + "subtitle": "展示模型任务账号池统计结果,每次直接按固定 SQL 查询,无需按日期筛选。", + "db_key": "wfj_pyui", + "table_name": "sketchfab_account_pool", + "query_sql": "select account_name, count(api_token) as total FROM sketchfab_account_pool GROUP BY account_name ORDER BY total DESC", + "status": "已配置", + "theme": "violet", + "enable_date_filter": False, + "enable_uuid_filter": True, + "identifier_field": "account_name", + "count_field": "total", + "identifier_label": "账号名称", + "summary_total_label": "账号数", + "summary_count_label": "Token 总量", + "empty_text": "当前暂无模型任务公示数据", + }, + "meishi_task_3": { + "title": "美食3任务", + "subtitle": "按日期统计美食3任务完成情况,数据来源于 project_menus.shop_list,合并 OK=1 与 OK=99 展示。", + "db_key": "project_menus", + "table_name": "shop_list", + "query_sql_list": [ + { + "key": "ok_1", + "label": "OK=1", + "sql": "SELECT workid, COUNT(`index`) AS cnt FROM shop_list WHERE updated_at > %s AND is_ok = 1 GROUP BY workid", + }, + { + "key": "ok_99", + "label": "OK=99", + "sql": "SELECT workid, COUNT(`index`) AS cnt FROM shop_list WHERE updated_at > %s AND is_ok = 99 GROUP BY workid", + }, + ], + "status": "已配置", + "theme": "cyan", + "enable_date_filter": True, + "date_filter_mode": "raw", + "enable_uuid_filter": True, + }, +} + + +APP_CONFIG = { + "app_name": "公示系统集合", + "host": "0.0.0.0", + "port": 9180, + "debug": False, +} diff --git a/query_service.py b/query_service.py new file mode 100644 index 0000000..130ddca --- /dev/null +++ b/query_service.py @@ -0,0 +1,165 @@ +from datetime import date, datetime, timedelta + +import pymysql + +from config import DATABASES + + +def safe_date(date_str: str) -> str: + value = str(date_str or "").strip() + if len(value) == 10 and value[4] == "-" and value[7] == "-": + return value + return date.today().isoformat() + + +def next_date(date_str: str) -> str: + value = datetime.strptime(date_str, "%Y-%m-%d").date() + return (value + timedelta(days=1)).isoformat() + + +def get_db_connection(db_key: str): + db_conf = DATABASES.get(db_key) or {} + db_name = str(db_conf.get("DB_NAME") or "").strip() + if not db_name: + raise ValueError(f"数据库 {db_key} 未配置 DB_NAME") + return pymysql.connect( + host=db_conf["DB_HOST"], + port=int(db_conf["DB_PORT"]), + user=db_conf["DB_USER"], + password=db_conf["DB_PASSWORD"], + database=db_name, + charset="utf8mb4", + cursorclass=pymysql.cursors.DictCursor, + ) + + +def query_project_stats(project_config: dict, date_str: str, user_uuid: str): + query_sql = str(project_config.get("query_sql") or "").strip() + query_sql_list = project_config.get("query_sql_list") or [] + db_key = str(project_config.get("db_key") or "").strip() + enable_date_filter = bool(project_config.get("enable_date_filter", True)) + date_filter_mode = str(project_config.get("date_filter_mode") or "like").strip().lower() + enable_uuid_filter = bool(project_config.get("enable_uuid_filter", True)) + identifier_field = str(project_config.get("identifier_field") or "workid").strip() + count_field = str(project_config.get("count_field") or "count").strip() + if not query_sql and not query_sql_list: + return { + "success": True, + "configured": False, + "message": "当前项目尚未配置查询 SQL,请先补充配置。", + "rows": [], + "sum_count": 0, + "has_current_user": False, + "breakdowns": [], + } + + conn = None + try: + conn = get_db_connection(db_key) + with conn.cursor() as cur: + if enable_date_filter: + if date_filter_mode == "raw": + date_param = date_str + elif date_filter_mode == "day_range": + date_param = (date_str, next_date(date_str)) + else: + date_param = f"%{date_str}%" + else: + date_param = None + + if query_sql_list: + breakdowns = [ + { + "key": str(item.get("key") or "").strip(), + "label": str(item.get("label") or item.get("key") or "").strip(), + } + for item in query_sql_list + if str(item.get("sql") or "").strip() + ] + merged = {} + for item in query_sql_list: + sql = str(item.get("sql") or "").strip() + key = str(item.get("key") or "").strip() + if not sql: + continue + if date_param is None: + cur.execute(sql) + elif date_filter_mode == "day_range": + cur.execute(sql, date_param) + else: + cur.execute(sql, (date_param,)) + for row in cur.fetchall() or []: + identifier_value = str(row.get(identifier_field) or row.get("workid") or "") + if not identifier_value: + continue + count_val = int(row.get(count_field) or row.get("cnt") or row.get("count") or 0) + merged.setdefault(identifier_value, {})[key] = count_val + + mapped_rows = [] + for identifier_value, parts in merged.items(): + is_current_user = bool(enable_uuid_filter and user_uuid) and identifier_value == user_uuid + item = { + "workid": identifier_value, + "count": sum(parts.values()), + "is_current_user": is_current_user, + } + for breakdown in breakdowns: + item[f"count_{breakdown['key']}"] = parts.get(breakdown["key"], 0) + mapped_rows.append(item) + mapped_rows.sort(key=lambda x: x["count"], reverse=True) + return { + "success": True, + "configured": True, + "message": "", + "rows": mapped_rows, + "sum_count": sum(item["count"] for item in mapped_rows), + "has_current_user": any(item["is_current_user"] for item in mapped_rows), + "breakdowns": breakdowns, + } + + if date_param is None: + cur.execute(query_sql) + elif date_filter_mode == "day_range": + cur.execute(query_sql, date_param) + else: + cur.execute(query_sql, (date_param,)) + rows = cur.fetchall() or [] + + mapped_rows = [] + for row in rows: + identifier_value = str(row.get(identifier_field) or row.get("workid") or "") + count_val = int(row.get(count_field) or row.get("count") or row.get("cnt") or 0) + is_current_user = bool(enable_uuid_filter and user_uuid) and identifier_value == user_uuid + mapped_rows.append( + { + "workid": identifier_value, + "count": count_val, + "is_current_user": is_current_user, + } + ) + + return { + "success": True, + "configured": True, + "message": "", + "rows": mapped_rows, + "sum_count": sum(item["count"] for item in mapped_rows), + "has_current_user": any(item["is_current_user"] for item in mapped_rows), + "breakdowns": [], + } + except Exception as exc: + return { + "success": False, + "configured": True, + "error": str(exc), + "rows": [], + "sum_count": 0, + "has_current_user": False, + "breakdowns": [], + } + finally: + try: + if conn: + conn.close() + except Exception: + pass diff --git a/start_app.bat b/start_app.bat new file mode 100644 index 0000000..2bdb8f7 --- /dev/null +++ b/start_app.bat @@ -0,0 +1,6 @@ +@echo off +title gongshi_task_board +setlocal +cd /d "%~dp0" +python app.py +endlocal diff --git a/templates/home.html b/templates/home.html new file mode 100644 index 0000000..9c3e014 --- /dev/null +++ b/templates/home.html @@ -0,0 +1,141 @@ + + + + + + {{ app_name }} + + + +
+
+
TASK PUBLIC BOARD
+

公示系统集成

+

统一承载多个任务公示项目的入口页面。当前先提供导航首页和统一查询骨架,后续新增项目时只需补充数据库配置、表名和 SQL。

+
+ +
+ {% for key, project in projects.items() %} + +
+

{{ project.title }}

+ {{ project.status }} +
+

{{ project.subtitle }}

+
进入统一查询页
+
+ {% endfor %} +
+ +
+ 后续接入新项目时,只需在 config.py 中补充项目配置,并为该项目填写数据库标识、表名和查询 SQL,即可复用统一项目页和统一查询接口。 +
+
+ + diff --git a/templates/project.html b/templates/project.html new file mode 100644 index 0000000..1d09ab2 --- /dev/null +++ b/templates/project.html @@ -0,0 +1,173 @@ + + + + + + {{ project.title }} + + + +
+
+ 返回主页 +

{{ project.title }}

+
{{ project.subtitle }}
+
+
+ + +
+
+ + +
+
+ +
+
+
-
+
+
+ +
+ + + + + + {% if project.get('query_sql_list') %} + {% for b in project.get('query_sql_list') %} + + {% endfor %} + + {% else %} + + {% endif %} + + + + +
序号workid{{ b.get('label') or b.get('key', '') }}总量完成量标记
+ +
+
+ + + +