#!/usr/bin/env python3 import json, time from appPublic.uniqueID import getID result = {'widgettype': 'Message', 'options': {'title': 'Error', 'message': 'Invalid', 'type': 'error'}} try: user_id = await get_user() org_id = (await get_userorgid()) or '0' now = time.strftime('%Y-%m-%d %H:%M:%S') dbname = get_module_dbname('product_management') data = dict(params_kw) data['id'] = getID() data['org_id'] = org_id data['created_by'] = user_id data['created_at'] = now data['updated_at'] = now if 'status' not in data: data['status'] = '1' if 'sort_order' not in data: data['sort_order'] = '0' if 'price_type' not in data: data['price_type'] = '1' if 'price' not in data: data['price'] = '0.00' if 'currency' not in data: data['currency'] = 'CNY' # Verify category belongs to current org if data.get('category_id'): async with DBPools().sqlorContext(dbname) as sor: cat_check = await sor.sqlExe( "SELECT id, product_type FROM product_category WHERE id = ${category_id}$ AND org_id = ${org_id}$", {'category_id': data['category_id'], 'org_id': org_id} ) if not cat_check: raise ValueError('类别不存在或不属于当前机构') # Auto-fill product_type from category if not data.get('product_type'): data['product_type'] = cat_check[0].get('product_type', '') async with DBPools().sqlorContext(dbname) as sor: await sor.C('product', data) 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)