dryRun === 1; echo $dry ? "【预览模式】\n" : "【执行合并】\n"; $customCount = $this->mergeCustomRows($dry); $ghsCount = $this->mergeGhsRows($dry); echo "完成:客户 {$customCount} 条,供货商关系 {$ghsCount} 条,dryRun=" . ($dry ? '1' : '0') . PHP_EOL; } /** * 【用途】扫 xhGhsCustom:debtAmount>0 且(有 balanceMerged 列时)balanceMerged=0,逐条 ensureCustomMoneyReady。 */ protected function mergeCustomRows($dry) { $count = 0; $where = ['debtAmount>' => 0]; if ($this->columnExists('xhGhsCustom', 'balanceMerged')) { $where['balanceMerged'] = 0; } $list = CustomClass::getAllByCondition($where, null, '*', null, true); foreach ($list as $custom) { if ($dry) { echo "[客户] id={$custom->id} debtAmount={$custom->debtAmount} balance={$custom->balance}" . PHP_EOL; $count++; continue; } AccountMoneyClass::ensureCustomMoneyReady($custom, true); $count++; } return $count; } /** * 【用途】扫 xhGhs:条件同上,逐条 ensureGhsMoneyReady(不自动带 custom,休眠 ghs 单独处理)。 */ protected function mergeGhsRows($dry) { $count = 0; $where = ['debtAmount>' => 0]; if ($this->columnExists('xhGhs', 'balanceMerged')) { $where['balanceMerged'] = 0; } $list = BizGhsClass::getAllByCondition($where, null, '*', null, true); foreach ($list as $ghs) { if ($dry) { echo "[关系] id={$ghs->id} debtAmount={$ghs->debtAmount} balance={$ghs->balance}" . PHP_EOL; $count++; continue; } AccountMoneyClass::ensureGhsMoneyReady($ghs, true); $count++; } return $count; } /** * 【用途】判断迁移 SQL 是否已执行,避免 where balanceMerged 报错。 */ protected function columnExists($table, $column) { $db = \Yii::$app->db; $schema = $db->getTableSchema($table, true); return $schema && isset($schema->columns[$column]); } }