Files
ylt_diy/lib/api/entity_api.py
user9994793890 f84aa8a4a9 refactor: 重构项目结构,按功能模块拆分文件
- 后端:将 app.py 拆分为 lib/api/ 目录下的多个蓝图模块
  - config_api.py: 配置管理 API
  - entity_api.py: 实体管理 API
  - field_api.py: 字段列表 API
  - report_api.py: 日报生成和历史记录 API
  - init_api.py: 数据库初始化 API
  - download_api.py: 文件下载 API
- 前端:将 index.html 拆分为多个文件
  - static/css/style.css: CSS 样式
  - static/js/common.js: 通用工具函数
  - static/js/config.js: 配置管理
  - static/js/entity.js: 实体管理
  - static/js/report.js: 日报生成
  - static/js/history.js: 历史记录
- 更新 app.py 使用蓝图注册 API
- 创建 AGENTS.md 项目文档

Coze-Commit-Type: user
Coze-User-ID: 3722323274763196
Coze-Conversation-ID: 9894087
2026-07-13 15:43:03 +08:00

122 lines
5.3 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.

"""
实体管理 API企业、用户、场站
"""
from flask import request, jsonify
from lib.db import execute_query
from . import entity_bp
@entity_bp.route('/entities', methods=['GET'])
def get_entities():
"""获取企业、用户或场站列表(支持分页和搜索)"""
try:
entity_type = request.args.get('type', 'company')
page = int(request.args.get('page', 1))
page_size = int(request.args.get('page_size', 100))
search = request.args.get('search', '')
offset = (page - 1) * page_size
if entity_type == 'company':
# 获取企业列表 (company_state=0 表示启用)
if search:
companies = execute_query(
'SELECT id AS company_id, company_name FROM t_company WHERE company_state = 0 AND company_name LIKE %s ORDER BY company_name LIMIT %s OFFSET %s',
(f'%{search}%', page_size, offset)
)
total_result = execute_query(
'SELECT COUNT(*) as total FROM t_company WHERE company_state = 0 AND company_name LIKE %s',
(f'%{search}%',)
)
else:
companies = execute_query(
'SELECT id AS company_id, company_name FROM t_company WHERE company_state = 0 ORDER BY company_name LIMIT %s OFFSET %s',
(page_size, offset)
)
total_result = execute_query(
'SELECT COUNT(*) as total FROM t_company WHERE company_state = 0'
)
total = total_result[0]['total'] if total_result else 0
return jsonify({'success': True, 'data': companies, 'total': total, 'page': page, 'page_size': page_size})
elif entity_type == 'station':
# 获取场站列表
if search:
stations = execute_query(
'SELECT id AS station_id, station_name FROM t_station WHERE station_name LIKE %s ORDER BY station_name LIMIT %s OFFSET %s',
(f'%{search}%', page_size, offset)
)
total_result = execute_query(
'SELECT COUNT(*) as total FROM t_station WHERE station_name LIKE %s',
(f'%{search}%',)
)
else:
stations = execute_query(
'SELECT id AS station_id, station_name FROM t_station ORDER BY station_name LIMIT %s OFFSET %s',
(page_size, offset)
)
total_result = execute_query(
'SELECT COUNT(*) as total FROM t_station'
)
total = total_result[0]['total'] if total_result else 0
return jsonify({'success': True, 'data': stations, 'total': total, 'page': page, 'page_size': page_size})
else:
# 获取用户列表(支持分页和搜索,搜索支持用户名和手机号)
if search:
users = execute_query(
'SELECT id AS user_id, user_name, phone FROM t_user WHERE user_name LIKE %s OR phone LIKE %s ORDER BY user_name LIMIT %s OFFSET %s',
(f'%{search}%', f'%{search}%', page_size, offset)
)
total_result = execute_query(
'SELECT COUNT(*) as total FROM t_user WHERE user_name LIKE %s OR phone LIKE %s',
(f'%{search}%', f'%{search}%')
)
else:
users = execute_query(
'SELECT id AS user_id, user_name, phone FROM t_user ORDER BY user_name LIMIT %s OFFSET %s',
(page_size, offset)
)
total_result = execute_query(
'SELECT COUNT(*) as total FROM t_user'
)
total = total_result[0]['total'] if total_result else 0
return jsonify({'success': True, 'data': users, 'total': total, 'page': page, 'page_size': page_size})
except Exception as e:
return jsonify({'success': False, 'message': str(e)}), 500
@entity_bp.route('/entities/by_ids', methods=['POST'])
def get_entities_by_ids():
"""根据 ID 列表查询实体信息(用于编辑回显)"""
try:
data = request.json
entity_type = data.get('type', 'company')
ids = data.get('ids', [])
if not ids:
return jsonify({'success': True, 'data': []})
placeholders = ', '.join(['%s'] * len(ids))
if entity_type == 'company':
entities = execute_query(
f'SELECT id AS company_id, company_name FROM t_company WHERE id IN ({placeholders})',
tuple(ids)
)
elif entity_type == 'station':
entities = execute_query(
f'SELECT id AS station_id, station_name FROM t_station WHERE id IN ({placeholders})',
tuple(ids)
)
else:
entities = execute_query(
f'SELECT id AS user_id, user_name FROM t_user WHERE id IN ({placeholders})',
tuple(ids)
)
return jsonify({'success': True, 'data': entities})
except Exception as e:
return jsonify({'success': False, 'message': str(e)}), 500