2026_06_04_120000_add_no_pay_channels.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. use Illuminate\Database\Migrations\Migration;
  3. use Illuminate\Support\Facades\DB;
  4. use Illuminate\Support\Facades\Schema;
  5. return new class extends Migration {
  6. private const CHANNELS = ['NOpay12', 'NOpay13', 'NOwithdraw'];
  7. public function up()
  8. {
  9. if (Schema::hasTable('recharge_channel')) {
  10. $this->upsertChannel(1, 'NO快捷充值-扫码支付', 'NOpay12', 0, 10, 49999);
  11. $this->upsertChannel(1, 'NO快捷充值-余额支付', 'NOpay13', 0, 10, 49999);
  12. $this->upsertChannel(2, 'NO快捷提现', 'NOwithdraw', 0, 10, 49999);
  13. }
  14. if (Schema::hasTable('recharge_channel_group')) {
  15. $this->appendGroupTypes('recharge_type', ['NOpay12', 'NOpay13']);
  16. $this->appendGroupTypes('withdraw_type', ['NOwithdraw']);
  17. }
  18. }
  19. public function down()
  20. {
  21. if (Schema::hasTable('recharge_channel')) {
  22. DB::table('recharge_channel')->whereIn('type', self::CHANNELS)->delete();
  23. }
  24. if (Schema::hasTable('recharge_channel_group')) {
  25. $this->removeGroupTypes('recharge_type', ['NOpay12', 'NOpay13']);
  26. $this->removeGroupTypes('withdraw_type', ['NOwithdraw']);
  27. }
  28. }
  29. private function upsertChannel(int $dataType, string $name, string $type, float $rate, int $min, int $max): void
  30. {
  31. $row = compact('name', 'type', 'rate', 'min', 'max');
  32. $row['data_type'] = $dataType;
  33. foreach (['key' => $type, 'fixed' => '', 'status' => 1, 'sort' => 98, 'from' => 1] as $column => $value) {
  34. if (Schema::hasColumn('recharge_channel', $column)) {
  35. $row[$column] = $value;
  36. }
  37. }
  38. DB::table('recharge_channel')->updateOrInsert(
  39. ['data_type' => $dataType, 'type' => $type],
  40. $row
  41. );
  42. }
  43. private function appendGroupTypes(string $column, array $newTypes): void
  44. {
  45. if (!Schema::hasColumn('recharge_channel_group', $column)) {
  46. return;
  47. }
  48. DB::table('recharge_channel_group')->orderBy('id')->chunkById(100, function ($groups) use ($column, $newTypes) {
  49. foreach ($groups as $group) {
  50. $types = array_values(array_filter(explode(',', (string)$group->{$column})));
  51. DB::table('recharge_channel_group')->where('id', $group->id)->update([
  52. $column => implode(',', array_values(array_unique(array_merge($types, $newTypes)))),
  53. ]);
  54. }
  55. });
  56. }
  57. private function removeGroupTypes(string $column, array $removeTypes): void
  58. {
  59. if (!Schema::hasColumn('recharge_channel_group', $column)) {
  60. return;
  61. }
  62. DB::table('recharge_channel_group')->orderBy('id')->chunkById(100, function ($groups) use ($column, $removeTypes) {
  63. foreach ($groups as $group) {
  64. $types = array_values(array_diff(array_filter(explode(',', (string)$group->{$column})), $removeTypes));
  65. DB::table('recharge_channel_group')->where('id', $group->id)->update([
  66. $column => implode(',', $types),
  67. ]);
  68. }
  69. });
  70. }
  71. };