89 lines
2.4 KiB
PHP
89 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace App\Database;
|
|
|
|
use Illuminate\Database\DatabaseManager;
|
|
use Illuminate\Database\Query\Expression;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class UpdateMassive4
|
|
{
|
|
|
|
/**
|
|
* UPDATE transactions
|
|
* SET updated_at = CASE id
|
|
* WHEN 1 THEN '2019-01-15 10:00:00'::date
|
|
* WHEN 2 THEN '2019-01-16 11:00:00'::date
|
|
* WHEN 3 THEN '2019-01-17 12:00:00'::date
|
|
* END
|
|
* WHERE id IN (1, 2, 3);
|
|
*
|
|
* update transactions set updated_at = '2019-01-15 10:00:00' where id = 1
|
|
* update transactions set updated_at = '2019-01-16 10:00:00' where id = 2
|
|
* update transactions set updated_at = '2019-01-17 10:00:00' where id = 3
|
|
*
|
|
* @param string $table
|
|
* @param string $primaryKeyName
|
|
* @param $config
|
|
*
|
|
* Config:
|
|
* [
|
|
* [
|
|
* "primary_key" => 200576
|
|
* "columns" => array:1 [
|
|
* "transactions_banks.coupon_code" => "2001462387"
|
|
* ]
|
|
* ]
|
|
* ]
|
|
*
|
|
* @return void
|
|
*/
|
|
public function apply(string $table, string $primaryKeyName, $config): void
|
|
{
|
|
/** @var DatabaseManager $db */
|
|
$db = app('db');
|
|
|
|
$primaryKeyValues = [];
|
|
$whenGroup = [];
|
|
|
|
foreach ($config as $configItem) {
|
|
$primaryKey = $configItem['primary_key'];
|
|
|
|
$primaryKeyValues[] = $primaryKey;
|
|
foreach ($configItem['columns'] as $columnName => $columnValue) {
|
|
if (!isset($whenGroup[$columnName])) {
|
|
$whenGroup[$columnName] = [];
|
|
}
|
|
|
|
$whenGroup[$columnName][] = vsprintf('WHEN %s THEN %s', [
|
|
$primaryKey,
|
|
$columnValue
|
|
]);
|
|
}
|
|
}
|
|
$values = [];
|
|
foreach ($whenGroup as $column => $whenArr) {
|
|
/** @var Expression[] $whenArr */
|
|
$case = $db->raw(vsprintf('CASE %s %s END', [$primaryKeyName, implode(' ', $whenArr)]));
|
|
$values[$column] = $case;
|
|
}
|
|
|
|
if (empty($values)) {
|
|
return;
|
|
}
|
|
|
|
// DB::table('t_maquinetas as (,....)')
|
|
// ->join( 'from ()')
|
|
// UPDATE t_maquinetas
|
|
//SET t_terminal_id = sub.id
|
|
// FROM (SELECT id, id_antigo FROM t_terminal WHERE tipo = 1) as sub
|
|
//WHERE sub.id_antigo = t_pos_id AND
|
|
// (t_pdv_id IS NULL);
|
|
|
|
|
|
$db->table($table)
|
|
->whereIn($primaryKeyName, $primaryKeyValues)
|
|
->update($values);
|
|
}
|
|
}
|