upsertChannel(1, '808充值', 'ZIMUpay', 0, 10, 49999); $this->upsertChannel(2, '808币提现-钱包地址', 'ZIMUwithdraw', 0, 10, 49999); $this->upsertChannel(2, '808人民币代付', 'ZIMUcash', 0, 10, 49999); } if (Schema::hasTable('recharge_channel_group')) { $this->appendGroupTypes('recharge_type', ['ZIMUpay']); $this->appendGroupTypes('withdraw_type', ['ZIMUwithdraw', 'ZIMUcash']); } } public function down() { if (Schema::hasTable('recharge_channel')) { DB::table('recharge_channel')->whereIn('type', self::CHANNELS)->delete(); } if (Schema::hasTable('recharge_channel_group')) { $this->removeGroupTypes('recharge_type', ['ZIMUpay']); $this->removeGroupTypes('withdraw_type', ['ZIMUwithdraw', 'ZIMUcash']); } } private function upsertChannel(int $dataType, string $name, string $type, float $rate, int $min, int $max): void { $row = compact('name', 'type', 'rate', 'min', 'max'); $row['data_type'] = $dataType; foreach (['key' => $type, 'fixed' => '', 'status' => 1, 'sort' => 97, 'from' => 1] as $column => $value) { if (Schema::hasColumn('recharge_channel', $column)) { $row[$column] = $value; } } DB::table('recharge_channel')->updateOrInsert( ['data_type' => $dataType, 'type' => $type], $row ); } private function appendGroupTypes(string $column, array $newTypes): void { if (!Schema::hasColumn('recharge_channel_group', $column)) { return; } DB::table('recharge_channel_group')->orderBy('id')->chunkById(100, function ($groups) use ($column, $newTypes) { foreach ($groups as $group) { $types = array_values(array_filter(explode(',', (string)$group->{$column}))); DB::table('recharge_channel_group')->where('id', $group->id)->update([ $column => implode(',', array_values(array_unique(array_merge($types, $newTypes)))), ]); } }); } private function removeGroupTypes(string $column, array $removeTypes): void { if (!Schema::hasColumn('recharge_channel_group', $column)) { return; } DB::table('recharge_channel_group')->orderBy('id')->chunkById(100, function ($groups) use ($column, $removeTypes) { foreach ($groups as $group) { $types = array_values(array_diff(array_filter(explode(',', (string)$group->{$column})), $removeTypes)); DB::table('recharge_channel_group')->where('id', $group->id)->update([ $column => implode(',', $types), ]); } }); } };