opportunity_management/wwwroot/api/opportunities_update.dspy
yumoqing 2547fad996 sync: local modifications to opportunity_management
- Updated core.py, init.py, opportunity_core.py, mysql.ddl.sql
- Updated UI: base.ui, opportunity_management.ui
- Added: opportunity.json, opportunity_stage_history.json
- Added API files: opportunities CRUD, sales_stages_list, check_tables
- Added UI/DSPY: opportunity_edit, opportunity_list, sales_stages_list
2026-04-28 18:54:47 +08:00

40 lines
1.7 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Opportunity update API"""
import json, time
result = {'widgettype': 'Message', 'options': {'title': 'Error', 'message': 'Invalid request', 'type': 'error'}}
try:
record_id = params_kw.get('id', '').strip()
if not record_id:
result['options'] = {'title': 'Error', 'message': '记录ID不能为空', 'type': 'error'}
else:
dbname = get_module_dbname('opportunity_management')
now = time.strftime('%Y-%m-%d %H:%M:%S')
fields = ['customer_id', 'customer_name', 'opportunity_name', 'estimated_amount', 'current_stage', 'expected_close_date', 'owner_id', 'owner_name', 'region', 'description', 'probability', 'predicted_revenue', 'status', 'source_type']
set_parts = []
params = {'id': record_id}
for f in fields:
val = params_kw.get(f, None)
if val is not None:
set_parts.append(f"{f} = %({f})s")
params[f] = val
set_parts.append("updated_at = ${updated_at}$")
params['updated_at'] = now
if len(set_parts) <= 1:
result['options'] = {'title': 'Error', 'message': '没有可更新的字段', 'type': 'error'}
else:
sql = f"UPDATE opportunities SET {', '.join(set_parts)} WHERE id = ${id}$"
async with DBPools().sqlorContext(dbname) as sor:
await sor.sqlExe(sql, params)
result = {'widgettype': 'Message', 'options': {'title': 'Success', 'message': '更新成功', 'type': 'success'}}
except Exception as e:
result['options'] = {'title': 'Error', 'message': f'更新失败: {str(e)}', 'type': 'error'}
return json.dumps(result, ensure_ascii=False)