BalanceMergeController.php 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. namespace console\controllers;
  3. use biz\ghs\classes\GhsClass as BizGhsClass;
  4. use bizGhs\custom\classes\AccountMoneyClass;
  5. use bizGhs\custom\classes\CustomClass;
  6. use yii\console\Controller;
  7. /**
  8. * 挂账并入余额 — 控制台补漏(休眠账户批量处理)
  9. *
  10. * 【用途】处理长期未触发「开单/充值/结账」等入口、仍未 merge 的历史账户。
  11. * 【与线上一致】内部调用 AccountMoneyClass::ensureCustomMoneyReady / ensureGhsMoneyReady。
  12. *
  13. * 用法:php yii balance-merge/run 或 --dryRun=1 仅预览
  14. */
  15. class BalanceMergeController extends Controller
  16. {
  17. /** 1=只打印将处理的 id 与金额,不写库 */
  18. public $dryRun = 0;
  19. public function options($actionID)
  20. {
  21. return array_merge(parent::options($actionID), ['dryRun']);
  22. }
  23. /**
  24. * 【用途】执行或预览补漏:先客户表 xhGhsCustom,再关系表 xhGhs。
  25. */
  26. public function actionRun()
  27. {
  28. $dry = (int)$this->dryRun === 1;
  29. echo $dry ? "【预览模式】\n" : "【执行合并】\n";
  30. $customCount = $this->mergeCustomRows($dry);
  31. $ghsCount = $this->mergeGhsRows($dry);
  32. echo "完成:客户 {$customCount} 条,供货商关系 {$ghsCount} 条,dryRun=" . ($dry ? '1' : '0') . PHP_EOL;
  33. }
  34. /**
  35. * 【用途】扫 xhGhsCustom:debtAmount>0 且(有 balanceMerged 列时)balanceMerged=0,逐条 ensureCustomMoneyReady。
  36. */
  37. protected function mergeCustomRows($dry)
  38. {
  39. $count = 0;
  40. $where = ['debtAmount>' => 0];
  41. if ($this->columnExists('xhGhsCustom', 'balanceMerged')) {
  42. $where['balanceMerged'] = 0;
  43. }
  44. $list = CustomClass::getAllByCondition($where, null, '*', null, true);
  45. foreach ($list as $custom) {
  46. if ($dry) {
  47. echo "[客户] id={$custom->id} debtAmount={$custom->debtAmount} balance={$custom->balance}" . PHP_EOL;
  48. $count++;
  49. continue;
  50. }
  51. AccountMoneyClass::ensureCustomMoneyReady($custom, true);
  52. $count++;
  53. }
  54. return $count;
  55. }
  56. /**
  57. * 【用途】扫 xhGhs:条件同上,逐条 ensureGhsMoneyReady(不自动带 custom,休眠 ghs 单独处理)。
  58. */
  59. protected function mergeGhsRows($dry)
  60. {
  61. $count = 0;
  62. $where = ['debtAmount>' => 0];
  63. if ($this->columnExists('xhGhs', 'balanceMerged')) {
  64. $where['balanceMerged'] = 0;
  65. }
  66. $list = BizGhsClass::getAllByCondition($where, null, '*', null, true);
  67. foreach ($list as $ghs) {
  68. if ($dry) {
  69. echo "[关系] id={$ghs->id} debtAmount={$ghs->debtAmount} balance={$ghs->balance}" . PHP_EOL;
  70. $count++;
  71. continue;
  72. }
  73. AccountMoneyClass::ensureGhsMoneyReady($ghs, true);
  74. $count++;
  75. }
  76. return $count;
  77. }
  78. /**
  79. * 【用途】判断迁移 SQL 是否已执行,避免 where balanceMerged 报错。
  80. */
  81. protected function columnExists($table, $column)
  82. {
  83. $db = \Yii::$app->db;
  84. $schema = $db->getTableSchema($table, true);
  85. return $schema && isset($schema->columns[$column]);
  86. }
  87. }