result = {'widgettype': 'Message', 'options': {'title': 'Error', 'message': 'Invalid', 'type': 'error'}} try: user_id = await get_user() user_orgid = (await get_userorgid()) or '0' dbname = get_module_dbname('supplychain') data = dict(params_kw) data['id'] = getID() data['resellerid'] = user_orgid data['created_by'] = user_id data['created_at'] = timestampstr() data['updated_at'] = timestampstr() # Handle external supplier: create new org is_external = data.get('is_external', '1') orgid = data.get('orgid') if is_external == '1' and not orgid: supplier_name = data.get('supplier_name') if not supplier_name: result['options'] = {'title': 'Error', 'message': '供应商名称不能为空', 'type': 'error'} return json.dumps(result, ensure_ascii=False) new_orgid = getID() now_org = timestampstr() org_rec = { 'id': new_orgid, 'orgname': supplier_name, 'orgtype': 'supplier', 'status': '1', 'created_by': user_id, 'created_at': now_org, 'updated_at': now_org } db = DBPools() async with db.sqlorContext('sage') as sor_sage: await sor_sage.C('organization', org_rec) data['orgid'] = new_orgid elif is_external == '0' and not orgid: result['options'] = {'title': 'Error', 'message': '内部供应商必须选择所属机构', 'type': 'error'} return json.dumps(result, ensure_ascii=False) async with DBPools().sqlorContext(dbname) as sor: await sor.C('suppliers', 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)