2026_05_29_120000_add_jd_pay_channel.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. public function up()
  7. {
  8. if (Schema::hasTable('recharge_channel')) {
  9. $this->upsertRechargeChannel(1, 'JD钱包', 0, 100, 49999);
  10. $this->upsertRechargeChannel(2, 'JD钱包', 0.002, 100, 49999);
  11. }
  12. if (Schema::hasTable('recharge_channel_group')) {
  13. $this->appendGroupType('recharge_type');
  14. $this->appendGroupType('withdraw_type');
  15. }
  16. }
  17. public function down()
  18. {
  19. if (Schema::hasTable('recharge_channel')) {
  20. DB::table('recharge_channel')->where('type', 'JDpay')->delete();
  21. }
  22. if (Schema::hasTable('recharge_channel_group')) {
  23. $this->removeGroupType('recharge_type');
  24. $this->removeGroupType('withdraw_type');
  25. }
  26. }
  27. private function upsertRechargeChannel(int $dataType, string $name, float $rate, int $min, int $max): void
  28. {
  29. $row = [
  30. 'data_type' => $dataType,
  31. 'name' => $name,
  32. 'type' => 'JDpay',
  33. 'rate' => $rate,
  34. 'min' => $min,
  35. 'max' => $max,
  36. ];
  37. foreach (['key' => 'JDpay', 'fixed' => '', 'status' => 1, 'sort' => 99, 'from' => 1] as $column => $value) {
  38. if (Schema::hasColumn('recharge_channel', $column)) {
  39. $row[$column] = $value;
  40. }
  41. }
  42. DB::table('recharge_channel')->updateOrInsert(
  43. ['data_type' => $dataType, 'type' => 'JDpay'],
  44. $row
  45. );
  46. }
  47. private function appendGroupType(string $column): void
  48. {
  49. if (!Schema::hasColumn('recharge_channel_group', $column)) {
  50. return;
  51. }
  52. DB::table('recharge_channel_group')->orderBy('id')->chunkById(100, function ($groups) use ($column) {
  53. foreach ($groups as $group) {
  54. $types = array_values(array_filter(explode(',', (string)$group->{$column})));
  55. if (!in_array('JDpay', $types, true)) {
  56. $types[] = 'JDpay';
  57. DB::table('recharge_channel_group')->where('id', $group->id)->update([
  58. $column => implode(',', $types),
  59. ]);
  60. }
  61. }
  62. });
  63. }
  64. private function removeGroupType(string $column): void
  65. {
  66. if (!Schema::hasColumn('recharge_channel_group', $column)) {
  67. return;
  68. }
  69. DB::table('recharge_channel_group')->orderBy('id')->chunkById(100, function ($groups) use ($column) {
  70. foreach ($groups as $group) {
  71. $types = array_values(array_diff(array_filter(explode(',', (string)$group->{$column})), ['JDpay']));
  72. DB::table('recharge_channel_group')->where('id', $group->id)->update([
  73. $column => implode(',', $types),
  74. ]);
  75. }
  76. });
  77. }
  78. };