2026_11_05_160000_add_backflow_granted_amounts.php 1.5 KB

1234567891011121314151617181920212223242526272829303132
  1. <?php
  2. use Illuminate\Database\Migrations\Migration;
  3. use Illuminate\Database\Schema\Blueprint;
  4. use Illuminate\Support\Facades\DB;
  5. use Illuminate\Support\Facades\Schema;
  6. /** 回水可多次发放:记录已发放金额/打码量;status 2 = 部分发放(当月提前发放后仍在累计)。 */
  7. return new class extends Migration {
  8. public function up(): void
  9. {
  10. if (!Schema::hasTable('backflow')) return;
  11. Schema::table('backflow', function (Blueprint $t) {
  12. if (!Schema::hasColumn('backflow', 'granted_amount')) $t->decimal('granted_amount', 20, 2)->default(0)->comment('已发放金额');
  13. if (!Schema::hasColumn('backflow', 'granted_need_flow')) $t->decimal('granted_need_flow', 20, 2)->default(0)->comment('已发放部分的打码量');
  14. });
  15. // 已发放的历史记录:发放时的金额即为已发放金额
  16. DB::table('backflow')->where('status', 1)->where('granted_amount', 0)
  17. ->update(['granted_amount' => DB::raw('amount'), 'granted_need_flow' => DB::raw('COALESCE(need_flow, 0)')]);
  18. }
  19. public function down(): void
  20. {
  21. if (!Schema::hasTable('backflow')) return;
  22. DB::table('backflow')->where('status', 2)->update(['status' => 1]);
  23. Schema::table('backflow', function (Blueprint $t) {
  24. foreach (['granted_amount', 'granted_need_flow'] as $column) {
  25. if (Schema::hasColumn('backflow', $column)) $t->dropColumn($column);
  26. }
  27. });
  28. }
  29. };