feat: 完成配置管理增强功能
## 修复问题 ### 1. 企业加载不出来 **问题**:`Unknown column 'company_id' in 'table list'` **原因**:`t_company` 表中字段名是 `id` 而不是 `company_id` **修复**:修改 `/api/entities` API 中的SQL查询,使用正确的字段名 ### 2. 用户加载不出来 **问题**:`Unknown column 'name' in 'field list'` **原因**:`t_user` 表中字段名是 `user_name` 而不是 `name` **修复**:修改SQL查询,使用正确的字段名 `user_name` ## 新增功能 ### 3. 拆分方式增加"按场站" - 在拆分方式下拉框中添加"按场站"选项 - 支持从 `t_station` 表加载场站列表 - 场站显示名称格式:`station_id - name` ### 4. 企业/用户/场站多选功能 - 将选择框改为多选(`multiple` 属性) - 保存时将多个值用逗号分隔存储 - 编辑时自动解析逗号分隔的值并选中对应选项 - 日报生成时支持多选值查询(使用 IN 语句) ### 5. 配置复制功能 - 在配置列表中添加"复制"按钮 - 复制配置时自动添加"_副本"后缀 - 后端API:`POST /api/config/<id>/copy` ### 6. 后端查询优化 - 修改 `report_generator.py` 中的查询逻辑 - 支持多选值查询(逗号分隔的值使用 IN 语句) ## 重启应用测试 ```bash # Ctrl+C 停止 python app.py ``` 然后刷新页面,测试: 1. ✅ 企业/用户/场站列表是否正常加载 2. ✅ 多选功能是否正常工作 3. ✅ 配置复制功能是否正常 4. ✅ 生成日报时多选值是否正确查询 Coze-Commit-Type: user Coze-User-ID: 3722323274763196 Coze-Conversation-ID: 9894087
This commit is contained in:
10
app.py
10
app.py
@@ -259,7 +259,7 @@ def move_config(config_id):
|
||||
|
||||
@app.route('/api/entities', methods=['GET'])
|
||||
def get_entities():
|
||||
"""获取企业或用户列表"""
|
||||
"""获取企业、用户或场站列表"""
|
||||
try:
|
||||
entity_type = request.args.get('type', 'company')
|
||||
|
||||
@@ -269,10 +269,16 @@ def get_entities():
|
||||
'SELECT id AS company_id, company_name FROM t_company WHERE company_state = "1" ORDER BY company_name'
|
||||
)
|
||||
return jsonify({'success': True, 'data': companies})
|
||||
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})
|
||||
else:
|
||||
# 获取用户列表
|
||||
users = execute_query(
|
||||
'SELECT DISTINCT user_id, user_name, order_type FROM t_equipment_charge_order WHERE user_id IS NOT NULL ORDER BY user_name'
|
||||
'SELECT id AS user_id, user_name FROM t_user ORDER BY user_name'
|
||||
)
|
||||
return jsonify({'success': True, 'data': users})
|
||||
except Exception as e:
|
||||
|
||||
@@ -65,18 +65,38 @@ def generate_daily_report(config_id, start_time=None, end_time=None):
|
||||
|
||||
# 构建查询SQL
|
||||
fields_str = ', '.join(selected_fields)
|
||||
sql = f"""
|
||||
SELECT {fields_str}
|
||||
FROM t_equipment_charge_order
|
||||
WHERE state = 3
|
||||
AND report_time >= %s
|
||||
AND report_time < %s
|
||||
AND {config['split_type']} = %s
|
||||
ORDER BY report_time DESC
|
||||
"""
|
||||
|
||||
# 检查是否为多选值(逗号分隔)
|
||||
split_values = [v.strip() for v in config['split_value'].split(',') if v.strip()]
|
||||
|
||||
if len(split_values) > 1:
|
||||
# 多选:使用 IN 查询
|
||||
placeholders = ', '.join(['%s'] * len(split_values))
|
||||
sql = f"""
|
||||
SELECT {fields_str}
|
||||
FROM t_equipment_charge_order
|
||||
WHERE state = 3
|
||||
AND report_time >= %s
|
||||
AND report_time < %s
|
||||
AND {config['split_type']} IN ({placeholders})
|
||||
ORDER BY report_time DESC
|
||||
"""
|
||||
params = [start_time_str, end_time_str] + split_values
|
||||
else:
|
||||
# 单选:使用 = 查询
|
||||
sql = f"""
|
||||
SELECT {fields_str}
|
||||
FROM t_equipment_charge_order
|
||||
WHERE state = 3
|
||||
AND report_time >= %s
|
||||
AND report_time < %s
|
||||
AND {config['split_type']} = %s
|
||||
ORDER BY report_time DESC
|
||||
"""
|
||||
params = [start_time_str, end_time_str, config['split_value']]
|
||||
|
||||
# 执行查询
|
||||
orders = execute_query(sql, (start_time_str, end_time_str, config['split_value']))
|
||||
orders = execute_query(sql, tuple(params))
|
||||
|
||||
print(f"[日报生成] 查询到 {len(orders)} 条订单")
|
||||
|
||||
|
||||
@@ -466,14 +466,16 @@
|
||||
<option value="">请选择拆分方式</option>
|
||||
<option value="company_id">按企业ID</option>
|
||||
<option value="user_id">按用户ID</option>
|
||||
<option value="station_id">按场站ID</option>
|
||||
</select>
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
<label>选择企业/用户 *</label>
|
||||
<select id="split-value" class="form-control">
|
||||
<label>选择企业/用户/场站(可多选) *</label>
|
||||
<select id="split-value" class="form-control" multiple size="5">
|
||||
<option value="">请先选择拆分方式</option>
|
||||
</select>
|
||||
<small class="form-text text-muted">按住 Ctrl 或 Shift 键可多选</small>
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
@@ -641,18 +643,41 @@
|
||||
|
||||
select.innerHTML = '<option value="">加载中...</option>';
|
||||
|
||||
// 根据拆分方式确定实体类型
|
||||
let entityType = '';
|
||||
if (splitType === 'company_id') {
|
||||
entityType = 'company';
|
||||
} else if (splitType === 'user_id') {
|
||||
entityType = 'user';
|
||||
} else if (splitType === 'station_id') {
|
||||
entityType = 'station';
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await fetch(`/api/entities?type=${splitType === 'company_id' ? 'company' : 'user'}`);
|
||||
const response = await fetch(`/api/entities?type=${entityType}`);
|
||||
const result = await response.json();
|
||||
|
||||
if (result.success) {
|
||||
const entities = result.data;
|
||||
select.innerHTML = '<option value="">请选择</option>' +
|
||||
select.innerHTML = '<option value="">请选择(可多选)</option>' +
|
||||
entities.map(e => {
|
||||
const value = splitType === 'company_id' ? e.company_id : e.user_id;
|
||||
const name = splitType === 'company_id' ? e.company_name : e.user_name;
|
||||
const type = e.order_type === 3 ? '(企业用户)' : '(普通用户)';
|
||||
return `<option value="${value}" data-name="${name}">${name} ${splitType === 'user_id' ? type : ''}</option>`;
|
||||
let value = '';
|
||||
let name = '';
|
||||
let typeLabel = '';
|
||||
|
||||
if (splitType === 'company_id') {
|
||||
value = e.id;
|
||||
name = e.name;
|
||||
} else if (splitType === 'user_id') {
|
||||
value = e.id;
|
||||
name = e.name;
|
||||
typeLabel = e.order_type === 3 ? '(企业用户)' : '(普通用户)';
|
||||
} else if (splitType === 'station_id') {
|
||||
value = e.id;
|
||||
name = e.name;
|
||||
}
|
||||
|
||||
return `<option value="${value}" data-name="${name}">${name} ${typeLabel}</option>`;
|
||||
}).join('');
|
||||
}
|
||||
} catch (error) {
|
||||
@@ -665,8 +690,11 @@
|
||||
const configName = document.getElementById('config-name').value;
|
||||
const splitType = document.getElementById('split-type').value;
|
||||
const splitValueSelect = document.getElementById('split-value');
|
||||
const splitValue = splitValueSelect.value;
|
||||
const splitName = splitValueSelect.options[splitValueSelect.selectedIndex].dataset.name || '';
|
||||
|
||||
// 处理多选:获取所有选中的选项
|
||||
const selectedOptions = Array.from(splitValueSelect.selectedOptions).filter(opt => opt.value);
|
||||
const splitValue = selectedOptions.map(opt => opt.value).join(',');
|
||||
const splitName = selectedOptions.map(opt => opt.dataset.name || '').join(',');
|
||||
|
||||
const selectedFields = Array.from(document.querySelectorAll('#fields-list input:checked')).map(cb => cb.value);
|
||||
const sumFields = Array.from(document.querySelectorAll('#sum-fields-list input:checked')).map(cb => cb.value);
|
||||
@@ -735,7 +763,15 @@
|
||||
document.getElementById('split-type').value = config.split_type;
|
||||
|
||||
await loadEntities();
|
||||
document.getElementById('split-value').value = config.split_value;
|
||||
|
||||
// 处理多选值回显:split_value 可能是逗号分隔的多个值
|
||||
const splitValueSelect = document.getElementById('split-value');
|
||||
const splitValues = config.split_value.split(',');
|
||||
|
||||
// 清除所有选中状态
|
||||
Array.from(splitValueSelect.options).forEach(opt => {
|
||||
opt.selected = splitValues.includes(opt.value);
|
||||
});
|
||||
|
||||
await loadFields();
|
||||
renderFields(config.selected_fields, config.sum_fields);
|
||||
|
||||
Reference in New Issue
Block a user