| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- <?php
- namespace console\controllers;
- use biz\ghs\classes\GhsClass as BizGhsClass;
- use bizGhs\custom\classes\AccountMoneyClass;
- use bizGhs\custom\classes\CustomClass;
- use yii\console\Controller;
- /**
- * 挂账并入余额 — 控制台补漏(休眠账户批量处理)
- *
- * 【用途】处理长期未触发「开单/充值/结账」等入口、仍未 merge 的历史账户。
- * 内部调用 AccountMoneyClass::mergeCustomMoneyForScript / mergeGhsMoneyForScript(线上业务不再自动合并)。
- *
- * 用法:php yii balance-merge/run 或 --dryRun=1 仅预览
- */
- class BalanceMergeController extends Controller
- {
- /** 1=只打印将处理的 id 与金额,不写库 */
- public $dryRun = 0;
- public function options($actionID)
- {
- return array_merge(parent::options($actionID), ['dryRun']);
- }
- /**
- * 【用途】执行或预览补漏:先客户表 xhGhsCustom,再关系表 xhGhs。
- */
- public function actionRun()
- {
- $dry = (int)$this->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,逐条脚本合并。
- */
- 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::mergeCustomMoneyForScript($custom, true);
- $count++;
- }
- return $count;
- }
- /**
- * 【用途】扫 xhGhs:条件同上,逐条 mergeGhsMoneyForScript(不自动带 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::mergeGhsMoneyForScript($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]);
- }
- }
|