migrate_water_commission.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. declare(strict_types=1);
  3. /** Create the immutable attribution snapshot for commissions on future paid water orders. */
  4. require dirname(__DIR__) . '/vendor/autoload.php';
  5. \Dotenv\Dotenv::createUnsafeMutable(dirname(__DIR__))->load();
  6. \support\App::loadAllConfig(['route']);
  7. use Illuminate\Database\Schema\Blueprint;
  8. use plugin\saiadmin\app\model\water\WaterCommission;
  9. use plugin\saiadmin\app\model\water\WaterCommissionSetting;
  10. if (!in_array('--apply', $argv, true)) {
  11. echo "Plan: create commission snapshots and editable platform rates; existing orders are not backfilled.\n";
  12. exit;
  13. }
  14. $model = new WaterCommission();
  15. $schema = $model->getConnection()->getSchemaBuilder();
  16. if (!$schema->hasTable($model->getTable())) {
  17. $schema->create($model->getTable(), function (Blueprint $table): void {
  18. $table->engine = 'InnoDB';
  19. $table->bigIncrements('id');
  20. $table->unsignedBigInteger('order_id');
  21. $table->unsignedBigInteger('enterprise_id');
  22. $table->string('enterprise_name', 120);
  23. $table->unsignedBigInteger('agent_id');
  24. $table->unsignedTinyInteger('agent_level');
  25. $table->unsignedSmallInteger('rate_basis_points');
  26. $table->unsignedBigInteger('base_amount_cents');
  27. $table->unsignedBigInteger('amount_cents');
  28. $table->string('status', 20)->default('pending');
  29. $table->dateTime('created_at');
  30. $table->dateTime('settled_at')->nullable();
  31. $table->unique(['order_id', 'agent_id']);
  32. $table->index(['agent_id', 'status', 'id']);
  33. $table->index(['enterprise_id', 'id']);
  34. $table->comment('供水订单成交时的代理与比例快照;未结算不得提现');
  35. });
  36. }
  37. $connection = $model->getConnection();
  38. $status = $connection->selectOne("SHOW TABLE STATUS LIKE 'sa_water_commission'");
  39. if ($status && strcasecmp((string) $status->Engine, 'InnoDB') !== 0) {
  40. $connection->statement('ALTER TABLE sa_water_commission ENGINE=InnoDB');
  41. }
  42. $settings = new WaterCommissionSetting();
  43. if (!$schema->hasTable($settings->getTable())) {
  44. $schema->create($settings->getTable(), function (Blueprint $table): void {
  45. $table->engine = 'InnoDB';
  46. $table->unsignedInteger('id')->primary();
  47. $table->unsignedSmallInteger('level1_basis_points');
  48. $table->unsignedSmallInteger('level2_basis_points');
  49. $table->unsignedSmallInteger('level3_basis_points');
  50. $table->unsignedInteger('revision')->default(1);
  51. $table->unsignedBigInteger('updated_by')->default(0);
  52. $table->dateTime('updated_at')->nullable();
  53. });
  54. }
  55. $settingsStatus = $connection->selectOne("SHOW TABLE STATUS LIKE 'sa_water_commission_setting'");
  56. if ($settingsStatus && strcasecmp((string) $settingsStatus->Engine, 'InnoDB') !== 0) {
  57. $connection->statement('ALTER TABLE sa_water_commission_setting ENGINE=InnoDB');
  58. }
  59. WaterCommissionSetting::query()->insertOrIgnore([
  60. 'id' => 1,
  61. 'level1_basis_points' => 1000,
  62. 'level2_basis_points' => 500,
  63. 'level3_basis_points' => 200,
  64. 'revision' => 1,
  65. ]);
  66. echo "Applied: commission snapshots and editable rates are ready; existing orders were not changed.\n";