From f3b15713c2e3b50479342769dcc13252ae4e1c6d Mon Sep 17 00:00:00 2001 From: xing Date: Sun, 26 May 2024 22:15:55 +0800 Subject: [PATCH] abstract batch update to trait --- app/Models/Base.php | 33 ++----------------------------- app/helpers/batchUpdate.php | 39 +++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 31 deletions(-) create mode 100644 app/helpers/batchUpdate.php diff --git a/app/Models/Base.php b/app/Models/Base.php index 00a26a2..833a953 100644 --- a/app/Models/Base.php +++ b/app/Models/Base.php @@ -2,8 +2,8 @@ namespace App\Models; +use App\helpers\batchUpdate; use Illuminate\Database\Eloquent\Model; -use Illuminate\Support\Facades\DB; /** * @method static int update(array $values) @@ -16,36 +16,7 @@ */ class Base extends Model { + use batchUpdate; public $timestamps = true; - public static function updateBatch($multipleData = []): int - { - $tableName = (new static())->getTable(); // 表名 - $firstRow = current($multipleData); - $updateColumn = array_keys($firstRow); - // 默认以id为条件更新,如果没有ID则以第一个字段为条件 - $referenceColumn = isset($firstRow['id']) ? 'id' : current($updateColumn); - unset($updateColumn[0]); - // 拼接sql语句 - $updateSql = "UPDATE " . $tableName . " SET "; - $sets = []; - $bindings = []; - foreach ($updateColumn as $uColumn) { - $setSql = "`" . $uColumn . "` = CASE "; - foreach ($multipleData as $data) { - $setSql .= "WHEN `" . $referenceColumn . "` = ? THEN ? "; - $bindings[] = $data[$referenceColumn]; - $bindings[] = $data[$uColumn]; - } - $setSql .= "ELSE `" . $uColumn . "` END "; - $sets[] = $setSql; - } - $updateSql .= implode(', ', $sets); - $whereIn = array_column($multipleData, $referenceColumn); - $bindings = array_merge($bindings, $whereIn); - $whereIn = rtrim(str_repeat('?,', count($whereIn)), ','); - $updateSql = rtrim($updateSql, ", ") . " WHERE `" . $referenceColumn . "` IN (" . $whereIn . ")"; - // 传入预处理sql语句和对应绑定数据 - return DB::update($updateSql, $bindings); - } } diff --git a/app/helpers/batchUpdate.php b/app/helpers/batchUpdate.php new file mode 100644 index 0000000..f8e589b --- /dev/null +++ b/app/helpers/batchUpdate.php @@ -0,0 +1,39 @@ +getTable(); // 表名 + $firstRow = current($multipleData); + $updateColumn = array_keys($firstRow); + // 默认以id为条件更新,如果没有ID则以第一个字段为条件 + $referenceColumn = isset($firstRow['id']) ? 'id' : current($updateColumn); + unset($updateColumn[0]); + // 拼接sql语句 + $updateSql = "UPDATE " . $tableName . " SET "; + $sets = []; + $bindings = []; + foreach ($updateColumn as $uColumn) { + $setSql = "`" . $uColumn . "` = CASE "; + foreach ($multipleData as $data) { + $setSql .= "WHEN `" . $referenceColumn . "` = ? THEN ? "; + $bindings[] = $data[$referenceColumn]; + $bindings[] = $data[$uColumn]; + } + $setSql .= "ELSE `" . $uColumn . "` END "; + $sets[] = $setSql; + } + $updateSql .= implode(', ', $sets); + $whereIn = array_column($multipleData, $referenceColumn); + $bindings = array_merge($bindings, $whereIn); + $whereIn = rtrim(str_repeat('?,', count($whereIn)), ','); + $updateSql = rtrim($updateSql, ", ") . " WHERE `" . $referenceColumn . "` IN (" . $whereIn . ")"; + // 传入预处理sql语句和对应绑定数据 + return DB::update($updateSql, $bindings); + } +}