result = {'success': False, 'message': ''} action = params_kw.action try: dbname = get_module_dbname('llmage') record_id = params_kw.get('id') if not record_id: result['message'] = '缺少id' elif action not in ('published', 'unpublished'): result['message'] = '无效的状态值' else: async with DBPools().sqlorContext(dbname) as sor: await sor.U('llm', {'id': record_id, 'status': action}) result['success'] = True result['message'] = '上架成功' if action == 'published' else '下架成功' if action == 'published': # 同步必须放在事务块外:块内未提交时另一连接读到旧状态 env = request._run_ns sync_fn = getattr(env, 'sync_llm_product', None) if sync_fn: try: sync_result = await sync_fn(record_id) except Exception as sync_e: sync_result = {'success': False, 'error': str(sync_e)} if not sync_result: result['message'] += ',已同步为产品' elif sync_result.get('success'): result['message'] += ',已同步为产品' else: result['message'] += ',但产品同步失败: ' + str(sync_result.get('error', '未知错误')) elif action == 'unpublished': # 下架时同步下架产品 product_dbname = get_serverenv('get_module_dbname')('product_management') async with DBPools().sqlorContext(product_dbname) as sor: products = await sor.sqlExe( "SELECT id FROM product WHERE resource_ref_id = ${ref_id}$ AND status = '1'", {'ref_id': record_id}) if products: for p in products: await sor.U('product', {'id': p.id, 'status': '0'}) result['message'] += ',已同步下架' + str(len(products)) + '个产品' else: result['message'] += ',未找到关联产品' except Exception as e: result['message'] = str(e) return { "widgettype": "Text", "options": { "otext": result['message'], "i18n": True } }