#!/usr/bin/env python3 import json, time from appPublic.uniqueID import getID result = {'widgettype': 'Message', 'options': {'title': 'Error', 'message': 'Invalid', 'type': 'error'}} try: dbname = get_module_dbname('product_management') user_id = await get_user() now = time.strftime('%Y-%m-%d %H:%M:%S') data = dict(params_kw) data['id'] = getID() 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' # If category_id is set but product_table_name is not, look it up from category if data.get('category_id') and not data.get('product_table_name'): sql = """SELECT product_table_name FROM product_category WHERE id = ${category_id}$ AND has_product='1'""" async with DBPools().sqlorContext(dbname) as sor: rows = await sor.sqlExe(sql, {'category_id': data['category_id']}) if rows: data['product_table_name'] = rows[0]['product_table_name'] 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)