#!/usr/bin/env python3 # -*- coding: utf-8 -*- """Customer create API""" import json, uuid, time result = {'widgettype': 'Message', 'options': {'title': 'Error', 'message': 'Invalid request', 'type': 'error'}} try: customer_name = params_kw.get('customer_name', '').strip() customer_type = params_kw.get('customer_type', '').strip() owner_id = params_kw.get('owner_id', '').strip() if not customer_name: result['options'] = {'title': 'Error', 'message': '客户名称不能为空', 'type': 'error'} elif not customer_type: result['options'] = {'title': 'Error', 'message': '客户类型不能为空', 'type': 'error'} elif not owner_id: result['options'] = {'title': 'Error', 'message': '负责人不能为空', 'type': 'error'} else: dbname = get_module_dbname('customer_management') new_id = str(uuid.uuid4()).replace('-', '') now = time.strftime('%Y-%m-%d %H:%M:%S') phone = params_kw.get('phone', '').strip() email = params_kw.get('email', '').strip() tax_id = params_kw.get('tax_id', '').strip() industry = params_kw.get('industry', '').strip() customer_level = params_kw.get('customer_level', 'potential').strip() address = params_kw.get('address', '').strip() region = params_kw.get('region', '').strip() status = params_kw.get('status', 'active').strip() async with DBPools().sqlorContext(dbname) as sor: await sor.sqlExe("""INSERT INTO customers (id, customer_name, customer_type, phone, email, tax_id, industry, customer_level, address, owner_id, region, status, created_at, updated_at) VALUES (${id}$, ${customer_name}$, ${customer_type}$, ${phone}$, ${email}$, ${tax_id}$, ${industry}$, ${customer_level}$, ${address}$, ${owner_id}$, ${region}$, ${status}$, ${created_at}$, ${updated_at}$)""", { 'id': new_id, 'customer_name': customer_name, 'customer_type': customer_type, 'phone': phone, 'email': email, 'tax_id': tax_id, 'industry': industry, 'customer_level': customer_level, 'address': address, 'owner_id': owner_id, 'region': region, 'status': status, 'created_at': now, 'updated_at': now }) result = {'widgettype': 'Message', 'options': {'title': 'Success', 'message': '客户创建成功', 'type': 'success'}} except Exception as e: result['options'] = {'title': 'Error', 'message': '创建失败: ' + str(e), 'type': 'error'} return json.dumps(result, ensure_ascii=False)