From e0dc9963b731d0c004a5e45706834df1bfd72a52 Mon Sep 17 00:00:00 2001 From: yumoqing Date: Fri, 10 Jul 2026 11:16:30 +0800 Subject: [PATCH] feat: sub_distributors_create now creates organization + supply relation --- wwwroot/api/sub_distributors_create.dspy | 51 ++++++++++++++++++++++-- 1 file changed, 48 insertions(+), 3 deletions(-) diff --git a/wwwroot/api/sub_distributors_create.dspy b/wwwroot/api/sub_distributors_create.dspy index b99d754..acd66de 100644 --- a/wwwroot/api/sub_distributors_create.dspy +++ b/wwwroot/api/sub_distributors_create.dspy @@ -6,8 +6,33 @@ try: dbname = get_module_dbname('supplychain') data = dict(params_kw) - data['id'] = getID() - data['resellerid'] = user_orgid + resellerid = data.get('resellerid', user_orgid) + sub_dist_name = data.get('sub_dist_name', '') + + if not sub_dist_name: + return {'widgettype': 'Message', 'options': {'title': 'Error', 'message': '名称不能为空', 'type': 'error'}} + + # Use same ID for org and sub_distributor + dist_id = getID() + + # 1. Create organization record (same ID as sub_distributor) + org_data = { + 'id': dist_id, + 'orgname': sub_dist_name, + 'orgabbr': sub_dist_name, + 'parentid': resellerid, + 'org_type': 'reseller', + 'contactor': data.get('contact_person', ''), + 'contactor_phone': data.get('contact_phone', ''), + 'emailaddress': data.get('contact_email', ''), + 'address': data.get('address', '') + } + async with DBPools().sqlorContext('sage') as sor: + await sor.C('organization', org_data) + + # 2. Create sub_distributors record (same ID) + data['id'] = dist_id + data['resellerid'] = resellerid data['created_by'] = user_id data['created_at'] = timestampstr() data['updated_at'] = timestampstr() @@ -15,7 +40,27 @@ try: async with DBPools().sqlorContext(dbname) as sor: await sor.C('sub_distributors', data) - result = {'widgettype': 'Message', 'options': {'title': 'Success', 'message': '分销商创建成功', 'type': 'success'}} + # 3. Create platform_supply_relations: reseller → sub-distributor + relation_id = getID() + relation_data = { + 'id': relation_id, + 'supplier_org_id': resellerid, + 'buyer_org_id': dist_id, + 'relation_code': f"SUB_{sub_dist_name[:16]}", + 'relation_name': f"供销关系-{sub_dist_name}", + 'relation_type': 'distribution', + 'settlement_mode': 'discount', + 'start_date': datestr(), + 'status': '1', + 'created_by': user_id, + 'created_at': timestampstr(), + 'updated_at': timestampstr() + } + + async with DBPools().sqlorContext(dbname) as sor: + await sor.C('platform_supply_relations', relation_data) + + result = {'widgettype': 'Message', 'options': {'title': 'Success', 'message': '分销商创建成功(已创建机构及供销关系)', 'type': 'success'}} except Exception as e: result['options'] = {'title': 'Error', 'message': '创建失败: ' + str(e), 'type': 'error'}