#!/usr/bin/env python3 # -*- coding: utf-8 -*- """Opportunity predictions update API""" import json, time result = {'widgettype': 'Message', 'options': {'title': 'Error', 'message': 'Invalid request', 'type': 'error'}} try: row_id = params_kw.get('id', '').strip() predicted_amount = params_kw.get('predicted_amount', '0').strip() confidence_level = params_kw.get('confidence_level', '0').strip() actual_amount = params_kw.get('actual_amount', '').strip() deviation_rate = params_kw.get('deviation_rate', '').strip() if not row_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') async with DBPools().sqlorContext(dbname) as sor: await sor.sqlExe("""UPDATE opportunity_predictions SET predicted_amount=${predicted_amount}$, confidence_level=${confidence_level}$, actual_amount=${actual_amount}$, deviation_rate=${deviation_rate}$ WHERE id=${id}$""", { 'id': row_id, 'predicted_amount': float(predicted_amount) if predicted_amount else 0.0, 'confidence_level': float(confidence_level) if confidence_level else 0.0, 'actual_amount': float(actual_amount) if actual_amount else None, 'deviation_rate': float(deviation_rate) if deviation_rate else None, }) 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)