255 lines
9.0 KiB
PHP
255 lines
9.0 KiB
PHP
<?php
|
|
|
|
namespace App\Tasks;
|
|
|
|
use App\Models\Transaction;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Support\Collection;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class UpdateMassive5Task
|
|
{
|
|
public function handle(): void
|
|
{
|
|
Transaction::query()
|
|
->select([
|
|
'id',
|
|
'date',
|
|
'value',
|
|
])
|
|
->where('id', '>', 0)
|
|
->chunkById(1000, function ($transactions) {
|
|
|
|
$values = [];
|
|
/** @var Collection $transactions */
|
|
foreach ($transactions as $transaction) {
|
|
$transaction->date = Carbon::now()->subDays(random_int(1, 3));
|
|
$transaction->value = 10 + random_int(0, 10);
|
|
|
|
// $values[] = [
|
|
// 'primary_key' => $transaction->id,
|
|
// 'columns' => [
|
|
// 'date' => "'$transaction->date'::timestamp",
|
|
// 'value' => $transaction->value,
|
|
// ]
|
|
// ];
|
|
|
|
$values[] = [
|
|
'id' => $transaction->id,
|
|
'date' => $transaction->date,
|
|
'value' => $transaction->value,
|
|
];
|
|
}
|
|
|
|
// $values = [
|
|
// [
|
|
// 'id' => 1,
|
|
// 'date' => Carbon::now(),
|
|
// 'value' => 10,
|
|
// ],
|
|
// [
|
|
// 'id' => 2,
|
|
// 'date' => '2023-01-02',
|
|
// 'value' => 20,
|
|
//// 'value' => DB::raw('t.value + 1'),
|
|
// ]
|
|
// ];
|
|
//
|
|
// $values = [
|
|
//// [
|
|
//// 'id' => DB::raw('1::int8'),
|
|
//// 'date' => DB::raw('\'' . Carbon::now()->format('Y-m-d H:i:s') . '\'::timestamp'),
|
|
//// 'value' => DB::raw('10::numeric'),
|
|
//// ],
|
|
// [
|
|
// 'id' => 2,
|
|
// 'date' => '2023-01-02',
|
|
// 'value' => 20,
|
|
//// 'value' => DB::raw('t.value + 1'),
|
|
// ]
|
|
// ];
|
|
|
|
|
|
DB::enableQueryLog();
|
|
|
|
// DB::table('transactions')
|
|
// ->insert($values);
|
|
|
|
$values = [
|
|
[
|
|
'id' => 1,
|
|
'value' => 20,
|
|
// 'day' => 2,
|
|
],
|
|
[
|
|
'id' => 2,
|
|
'value' => 30,
|
|
// 'day' => 5,
|
|
],
|
|
[
|
|
'value' => 30,
|
|
'id' => 3,
|
|
// 'day' => 3,
|
|
|
|
],
|
|
];
|
|
|
|
|
|
DB::table('transactions as t')
|
|
->joinFrom($values, 'm', DB::raw('m.id::bigint'), '=', 't.id')
|
|
->updateFrom([
|
|
'value' => DB::raw('m.value::decimal'),
|
|
]);
|
|
|
|
|
|
DB::table('transactions as t')
|
|
->massUpdate([
|
|
[
|
|
'id' => 1,
|
|
'value' => DB::raw('20::float'),
|
|
'date' => '2023-01-01',
|
|
],
|
|
[
|
|
'id' => 2,
|
|
'value' => 30,
|
|
'date' => '2023-01-01',
|
|
],
|
|
[
|
|
'id' => 1,
|
|
'value' => 30,
|
|
'date' => '2023-01-01',
|
|
],
|
|
], 'id');
|
|
|
|
|
|
// $properties = array_keys($values[0]);
|
|
// $x = [];
|
|
// $propertiesCount = count($properties);
|
|
// foreach ($values as $item) {
|
|
// foreach ($properties as $property) {
|
|
// if (count(array_keys($item)) !== $propertiesCount) {
|
|
// throw new \Exception('a quantidade de propriedades é diferente');
|
|
// }
|
|
//
|
|
// $x [] = $item[$property];
|
|
// }
|
|
// }
|
|
//
|
|
//// dd($x);
|
|
//// DB::table('transactions as t')
|
|
//// ->massUpdate($values, $uniqueKeys = ['id', 'value'], );
|
|
//
|
|
// DB::table('transaction as t')
|
|
// ->whereIn('id', [1,2,3])
|
|
// ->update([
|
|
// 'value'=> DB::raw('value + (case id when 1 then 2.0 when 2 then 5.0 when 3 then 2.32 end)')
|
|
// ]);
|
|
|
|
|
|
// $table = DB::raw('(values (?, ?), (?, ?), (?, ?)) as "m" (' . implode(', ', $properties) . ')');
|
|
|
|
DB::enableQueryLog();
|
|
DB::table('transactions as t')
|
|
// ->join($table, DB::raw('m.id::bigint'), '=', 't.id')
|
|
// ->addBinding($x)
|
|
->joinFrom($values, 'm', DB::raw('m.id::bigint'), '=', 't.id')
|
|
// ->where('t.date', '>', Carbon::now()->subDays(5))
|
|
->updateFrom([
|
|
// "value" => DB::raw('t.value + "m"."value"::decimal'),
|
|
// "date" => DB::raw('(t.date::timestamp + (interval \'1\' day * m.day::int))'),
|
|
"value" => DB::raw('m.value::decimal'),
|
|
]);
|
|
|
|
dd(DB::getQueryLog());
|
|
dd('aa');
|
|
|
|
// t.value = m.value + 1;
|
|
// t.value = m.value + 1;
|
|
|
|
|
|
DB::table('transactions as t')
|
|
->joinFrom($values, 'm', DB::raw('m.id::bigint'), '=', 't.id')
|
|
->updateFrom([
|
|
'value' => DB::raw('m.value::decimal'),
|
|
]);
|
|
dd(DB::getQueryLog());
|
|
|
|
$values = [
|
|
[
|
|
'id' => 1,
|
|
'date' => Carbon::now(),
|
|
'value' => 20,
|
|
],
|
|
[
|
|
'id' => 2,
|
|
'date' => '2023-01-02',
|
|
'value' => 30,
|
|
],
|
|
];
|
|
|
|
|
|
DB::table('transactions as t')
|
|
->joinFrom($values, 'm', DB::raw('m.id::bigint'), '=', 't.id')
|
|
->updateFrom([
|
|
'value' => DB::raw('(m.value::decimal + 1)::decimal'),
|
|
'date' => DB::raw('m.date::timestamp'),
|
|
'type' => 2,
|
|
'status' => DB::raw('case t.id when 1 then \'paid\' else t.status end'),
|
|
]);
|
|
|
|
|
|
// DB::table('transactions as t')
|
|
// ->join(DB::raw('(values (1,\'2019-01-15 10:00:00\',10), (2,\'2019-01-15 10:00:00\', 20) ) as mu (id, value, date)'), 'mu.id', '=', 't.id')
|
|
// ->updateFrom([
|
|
// 'value' => DB::raw('(mu.value::decimal + 1)::decimal'),
|
|
// 'date' => DB::raw('mu.date::timestamp'),
|
|
// 'type' => 2,
|
|
// 'status' => DB::raw('case t.id when 1 then \'paid\' else t.status end'),
|
|
// ]);
|
|
|
|
dd(DB::getQueryLog());
|
|
dd('aa');
|
|
// DB::table('transactions as t')
|
|
// ->massUpdateWithFrom(
|
|
// values: [
|
|
// [
|
|
// 'id' => 1,
|
|
// 'date' => Carbon::now(),
|
|
// 'value' => 10,
|
|
// ]
|
|
// ],
|
|
//// valuesAlias: 'mu',
|
|
//// updateColumns: [
|
|
//// 'date' => 'mu.date::timestamp',
|
|
//// 'value' => 'mu.value + 1',
|
|
//// 'type' => '2',
|
|
//// 'fee' => '(t.value / mu.value) * 0.1',
|
|
//// ]
|
|
// );
|
|
|
|
// $queryLog = DB::getQueryLog();
|
|
//
|
|
// $sql = $queryLog[1]['query'];
|
|
// $bindings = $queryLog[1]['bindings'];
|
|
//
|
|
// foreach ($bindings as $i => $binding) {
|
|
// if ($binding instanceof \DateTimeInterface) {
|
|
// $bindings[$i] = $binding->format('\'Y-m-d H:i:s\'');
|
|
// } else {
|
|
// $bindings[$i] = "'$binding'";
|
|
// }
|
|
// }
|
|
//
|
|
// $sql = vsprintf(str_replace('?', '%s', $sql), $bindings);
|
|
//
|
|
// dd($sql);
|
|
|
|
//
|
|
// dd('aa');
|
|
// $updateMassive2 = new UpdateMassive5();
|
|
// $updateMassive2->apply('transactions', 'id', $values);
|
|
});
|
|
}
|
|
|
|
}
|