#!/usr/bin/env python3 # -*- coding: utf-8 -*- """Opportunity predictions create API""" import json, time from appPublic.uniqueID import getID result = {'widgettype': 'Message', 'options': {'title': 'Error', 'message': 'Invalid request', 'type': 'error'}} try: opportunity_id = params_kw.get('opportunity_id', '').strip() predicted_amount = params_kw.get('predicted_amount', '0').strip() confidence_level = params_kw.get('confidence_level', '0').strip() prediction_date = params_kw.get('prediction_date', '').strip() if not opportunity_id: result['options'] = {'title': 'Error', 'message': '请填写必填字段', 'type': 'error'} else: dbname = get_module_dbname('opportunity_management') new_id = getID() now = time.strftime('%Y-%m-%d %H:%M:%S') if not prediction_date: prediction_date = time.strftime('%Y-%m-%d') async with DBPools().sqlorContext(dbname) as sor: await sor.sqlExe("""INSERT INTO opportunity_predictions (id, opportunity_id, predicted_amount, confidence_level, prediction_date, created_at) VALUES (${id}$, ${opportunity_id}$, ${predicted_amount}$, ${confidence_level}$, ${prediction_date}$, ${created_at}$)""", { 'id': new_id, 'opportunity_id': opportunity_id, 'predicted_amount': float(predicted_amount) if predicted_amount else 0.0, 'confidence_level': float(confidence_level) if confidence_level else 0.0, 'prediction_date': prediction_date, 'created_at': now }) 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)