setName('shopro:partner:clean') ->addOption('force', 'f', Option::VALUE_NONE, '强制执行,不提示确认') ->addOption('with-wallet-log', 'w', Option::VALUE_NONE, '同时清理分佣与订单相关的佣金钱包流水日志') ->setDescription('清理所有订单、分销分润、日结日志,重置dmepa与汇率回初始值'); } protected function execute(Input $input, Output $output) { $force = (bool) $input->getOption('force'); $withWallet = (bool) $input->getOption('with-wallet-log'); $output->writeln("========================================================"); $output->writeln(" 收益中心交易数据清理与dmepa汇率重置程序"); $output->writeln("========================================================"); if (!$force) { $output->writeln("【警告】此操作将清空所有商城订单、分销记录、分润流水并重置dmepa汇率!"); $output->writeln("如需执行,请增加参数 --force 或 -f,例如:"); $output->writeln("php think shopro:partner:clean --force"); return 1; } $prefix = config('database.prefix') ?: 'fa_'; $output->writeln("当前数据库前缀: [{$prefix}]"); Db::startTrans(); try { // 1. 清理商城订单及关联子表 $orderTables = [ 'shopro_order', 'shopro_order_item', 'shopro_order_action', 'shopro_order_aftersale', 'shopro_order_aftersale_log', 'shopro_order_express', 'shopro_order_invoice', 'shopro_order_item_stock', ]; $output->writeln("\n>>> 1. 正在清理商城订单及交易明细..."); foreach ($orderTables as $tbl) { $fullTable = $prefix . $tbl; if ($this->tableExists($fullTable)) { Db::execute("TRUNCATE TABLE `{$fullTable}`"); $output->writeln(" [OK] 清空表 `{$fullTable}`"); } } // 2. 清理分销与合伙人收益流水表 $rewardTables = [ 'shopro_partner_reward', // 二开收益中心全量分润流水 'shopro_commission_order', // 分销订单关联表 'shopro_commission_reward', // 原分销奖励明细 'shopro_commission_log', // 分销变动日志 ]; $output->writeln("\n>>> 2. 正在清理分销订单与收益分润记录..."); foreach ($rewardTables as $tbl) { $fullTable = $prefix . $tbl; if ($this->tableExists($fullTable)) { Db::execute("TRUNCATE TABLE `{$fullTable}`"); $output->writeln(" [OK] 清空表 `{$fullTable}`"); } } // 3. 清理日结对账、dmepa大盘汇率快照与兑换券释放计划 $dailyTables = [ 'shopro_partner_rate_log', // dmepa大盘汇率历史快照日志(清空后大盘汇率无历史,自动按 0 单重算) 'shopro_partner_daily_report', // 每日日结对账日报表 'shopro_partner_voucher_plan', // 兑换券释放计划表 'shopro_partner_voucher_log', // 兑换券释放明细流水 ]; $output->writeln("\n>>> 3. 正在清理日结对账、汇率快照与兑换券计划..."); foreach ($dailyTables as $tbl) { $fullTable = $prefix . $tbl; if ($this->tableExists($fullTable)) { Db::execute("TRUNCATE TABLE `{$fullTable}`"); $output->writeln(" [OK] 清空表 `{$fullTable}`"); } } // 4. 重置分销商资产状态(dmepa归零、推三返一序号归零、兑换券归零、收益归零) $output->writeln("\n>>> 4. 正在重置分销商(Agent)资产指标..."); $agentTable = $prefix . 'shopro_commission_agent'; if ($this->tableExists($agentTable)) { Db::execute("UPDATE `{$agentTable}` SET `madow` = 0.00000000, `direct_order_seq` = 0, `voucher_total` = 0.00, `voucher_released` = 0.00, `voucher_used` = 0.00, `total_income` = 0.00, `child_order_money_0` = 0.00, `child_order_money_1` = 0.00, `child_order_money_2` = 0.00, `child_order_money_all` = 0.00, `child_order_count_0` = 0, `child_order_count_1` = 0, `child_order_count_2` = 0, `child_order_count_all` = 0, `last_self_order_time` = NULL"); $output->writeln(" [OK] 已将所有分销商持仓dmepa、推三返一序号、兑换券及累计收益重置归零"); } // 5. 重置用户表钱包分销佣金与累计消费额 $output->writeln("\n>>> 5. 正在重置会员用户(User)交易与佣金数据..."); $userTable = $prefix . 'user'; if ($this->tableExists($userTable)) { Db::execute("UPDATE `{$userTable}` SET `commission` = 0.00, `total_consume` = 0.00"); $output->writeln(" [OK] 已将所有会员钱包佣金、累计消费重置归零"); } // 6. 可选清理钱包流水表中佣金相关的记录 if ($withWallet) { $walletLogTable = $prefix . 'shopro_user_wallet_log'; if ($this->tableExists($walletLogTable)) { Db::execute("DELETE FROM `{$walletLogTable}` WHERE `type` = 'commission' OR `event` IN ('reward_income', 'reward_back', 'order_pay', 'order_refund')"); $output->writeln(" [OK] 已清理佣金钱包及订单相关流水日志"); } } Db::commit(); $output->writeln("\n[SUCCESS] 数据库交易与分润记录清理完毕!事务已成功提交。"); } catch (\Exception $e) { Db::rollback(); $output->writeln("\n[FAIL] 清理过程中发生异常,事务已回滚: " . $e->getMessage()); return 1; } // 7. 清理缓存中汇率与收益中心配置 $output->writeln("\n>>> 6. 正在清除dmepa汇率缓存与系统配置缓存..."); cache('partner_madow_current_rate', null); cache('partner_rules', null); if (function_exists('rmdirs')) { @rmdirs(RUNTIME_PATH . 'cache'); @rmdirs(RUNTIME_PATH . 'temp'); } $output->writeln(" [OK] dmepa即时汇率缓存已清理"); // 8. 校验回归初始汇率 $rateService = new \addons\shopro\service\partner\RateService(); $resetRate = $rateService->currentRate(); $resetN = $rateService->countN(); $formattedRate = \addons\shopro\service\partner\IdentityService::formatRate($resetRate); $output->writeln("\n========================================================"); $output->writeln(" 重置后指标核对:"); $output->writeln(" 合格订单总数 N : {$resetN} 单 (应为 0)"); $output->writeln(" 当前大盘汇率 : {$formattedRate} 元/豆 (回归初始基准值 0.00000075)"); $output->writeln("========================================================\n"); return 0; } protected function tableExists($table) { try { $check = Db::query("SHOW TABLES LIKE '{$table}'"); return !empty($check); } catch (\Exception $e) { return false; } } }