feat: 优化实体加载,添加分页和搜索功能
Coze-Commit-Type: user Coze-User-ID: 3722323274763196 Coze-Conversation-ID: 9894087
This commit is contained in:
81
app.py
81
app.py
@@ -259,28 +259,81 @@ def move_config(config_id):
|
||||
|
||||
@app.route('/api/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':
|
||||
# 获取企业列表
|
||||
companies = execute_query(
|
||||
'SELECT id AS company_id, company_name FROM t_company WHERE company_state = "1" ORDER BY company_name'
|
||||
)
|
||||
return jsonify({'success': True, 'data': companies})
|
||||
if search:
|
||||
companies = execute_query(
|
||||
'SELECT id AS company_id, company_name FROM t_company WHERE company_state = "1" 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 = "1" AND company_name LIKE %s',
|
||||
(f'%{search}%',)
|
||||
)
|
||||
else:
|
||||
companies = execute_query(
|
||||
'SELECT id AS company_id, company_name FROM t_company WHERE company_state = "1" 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 = "1"'
|
||||
)
|
||||
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':
|
||||
# 获取场站列表
|
||||
stations = execute_query(
|
||||
'SELECT id AS station_id, station_name FROM t_station ORDER BY station_name'
|
||||
)
|
||||
return jsonify({'success': True, 'data': stations})
|
||||
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:
|
||||
# 获取用户列表
|
||||
users = execute_query(
|
||||
'SELECT id AS user_id, user_name FROM t_user ORDER BY user_name'
|
||||
)
|
||||
return jsonify({'success': True, 'data': users})
|
||||
# 获取用户列表(支持分页和搜索)
|
||||
if search:
|
||||
users = execute_query(
|
||||
'SELECT id AS user_id, user_name FROM t_user WHERE user_name LIKE %s ORDER BY user_name LIMIT %s OFFSET %s',
|
||||
(f'%{search}%', page_size, offset)
|
||||
)
|
||||
total_result = execute_query(
|
||||
'SELECT COUNT(*) as total FROM t_user WHERE user_name LIKE %s',
|
||||
(f'%{search}%',)
|
||||
)
|
||||
else:
|
||||
users = execute_query(
|
||||
'SELECT id AS user_id, user_name 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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user