From 719c854fb97d17767fe40c7dddbfe1be9ae4430a Mon Sep 17 00:00:00 2001 From: Ching L Date: Fri, 9 Jan 2026 11:56:48 +0800 Subject: [PATCH] feat(sso): add merchant bonus config update tool - Add function to update merchant Product Bonus Coupon configuration - Support modifying bonus mappings and validity periods - Add interactive menu option for configuration updates --- sso/sso_script.py | 93 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) diff --git a/sso/sso_script.py b/sso/sso_script.py index b1d0e2b..0d5bb17 100644 --- a/sso/sso_script.py +++ b/sso/sso_script.py @@ -301,6 +301,96 @@ def query_xdp_device_info(): filter_xdp_device_info(keyword) +def update_merchant_config_bonus(): + """修改商户配置中的 bonus 和有效期""" + import json + print("\n=== 修改商户 Product Bonus Coupon 配置 ===") + try: + merchant = canton.models.Merchant.objects.get(id=1489) + print(f"商户名称: {merchant.name}") + + merchant_config = canton.models.MerchantConfig.objects.filter( + merchant=merchant, + status=utils.const.CANTON_MERCHANT_CONFIG_STATUS_ACTIVE, + config_type=utils.const.CANTON_MERCHANT_CONFIG_TYPE_PRODUCT_BONUS_COUPON).last() + + if not merchant_config: + print("未找到有效的 Product Bonus Coupon 配置") + return + + component = merchant_config.component[0] + + print("\n当前配置:") + print(f" bonus: {component.get('bonus', {})}") + valid_from = component.get('valid_from') + valid_until = component.get('valid_until') + if valid_from: + print(f" valid_from: {valid_from} ({datetime.datetime.fromtimestamp(valid_from)})") + if valid_until: + print(f" valid_until: {valid_until} ({datetime.datetime.fromtimestamp(valid_until)})") + + print("\n请选择要修改的内容:") + print("1. 修改 bonus") + print("2. 修改 valid_from 和 valid_until") + print("3. 修改全部") + modify_choice = input("请选择 (1/2/3): ").strip() + + if modify_choice in ['1', '3']: + print("\n当前 bonus:", component.get('bonus', {})) + print("请输入新的 bonus (JSON 格式):") + print("示例: {\"13365\": [3341, 3343], \"13410\": [3341, 3343]}") + bonus_input = input("新 bonus: ").strip() + if bonus_input: + new_bonus = json.loads(bonus_input) + component['bonus'] = new_bonus + print(f"bonus 已更新为: {new_bonus}") + + if modify_choice in ['2', '3']: + print("\n请输入新的有效期:") + print("格式: YYYY-MM-DD HH:MM:SS 或时间戳") + + valid_from_input = input("valid_from (留空保持不变): ").strip() + if valid_from_input: + if valid_from_input.isdigit(): + component['valid_from'] = int(valid_from_input) + else: + dt = datetime.datetime.strptime(valid_from_input, "%Y-%m-%d %H:%M:%S") + component['valid_from'] = int(dt.timestamp()) + print(f"valid_from 已更新为: {component['valid_from']} ({datetime.datetime.fromtimestamp(component['valid_from'])})") + + valid_until_input = input("valid_until (留空保持不变): ").strip() + if valid_until_input: + if valid_until_input.isdigit(): + component['valid_until'] = int(valid_until_input) + else: + dt = datetime.datetime.strptime(valid_until_input, "%Y-%m-%d %H:%M:%S") + component['valid_until'] = int(dt.timestamp()) + print(f"valid_until 已更新为: {component['valid_until']} ({datetime.datetime.fromtimestamp(component['valid_until'])})") + + merchant_config.component[0] = component + + print("\n更新后的配置:") + print(f" bonus: {component.get('bonus', {})}") + print(f" valid_from: {component.get('valid_from')} ({datetime.datetime.fromtimestamp(component.get('valid_from', 0))})") + print(f" valid_until: {component.get('valid_until')} ({datetime.datetime.fromtimestamp(component.get('valid_until', 0))})") + + confirm = input("\n确认保存? (y/n): ").strip().lower() + if confirm == 'y': + merchant_config.save() + print("配置已保存!") + else: + print("已取消保存") + + except canton.models.Merchant.DoesNotExist: + print("未找到指定的商户") + except json.JSONDecodeError as e: + print(f"JSON 解析错误: {e}") + except ValueError as e: + print(f"输入错误: {e}") + except Exception as e: + print(f"执行出错: {e}") + + def show_menu(): """显示功能菜单""" print("\n" + "="*50) @@ -311,6 +401,7 @@ def show_menu(): print("2. 批次绑定商品") print("3. 批次更新商品") print("4. 查询 XDP 设备信息") + print("5. 修改商户 Product Bonus Coupon 配置") print("0. 退出") print("="*50) @@ -331,6 +422,8 @@ def main(): batch_update_product() elif choice == '4': query_xdp_device_info() + elif choice == '5': + update_merchant_config_bonus() elif choice == '0': print("\n退出脚本...") break