142 lines
4.6 KiB
PHP
Executable File
142 lines
4.6 KiB
PHP
Executable File
<?php
|
||
|
||
declare(strict_types=1);
|
||
/**
|
||
* This file is part of Hyperf.
|
||
*
|
||
* @link https://www.hyperf.io
|
||
* @document https://hyperf.wiki
|
||
* @contact group@hyperf.io
|
||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
|
||
*/
|
||
|
||
namespace App\Job;
|
||
|
||
use App\Model\ColumnConfig;
|
||
use App\Scope\CompanyScope;
|
||
|
||
use Hyperf\Coroutine\Parallel;
|
||
use function Hyperf\Collection\collect;
|
||
use function Hyperf\Config\config;
|
||
|
||
class ColumnConfigJob extends BaseJob
|
||
{
|
||
protected function process(): void
|
||
{
|
||
['user' => $user, 'method' => $method, 'params' => $params] = $this->data;
|
||
|
||
// 提交了保存列配置
|
||
if (!isset($params['save_column'])) return;
|
||
if ($params['save_column'] === false) {
|
||
$this->deleteColumnConfig($user, $method);
|
||
return;
|
||
}
|
||
|
||
// 获取默认配置, 没有配置则不处理
|
||
$configList = config('column_config.' . $method) ?? [];
|
||
if (empty($configList)) return;
|
||
|
||
$submitConfig = $params['column_config'];
|
||
$submittedMap = collect($submitConfig)->keyBy('prop')->toArray();
|
||
|
||
$parallel = new Parallel();
|
||
|
||
foreach ($configList as $key => $value) {
|
||
$parallel->add(function () use ($key, $value, $submittedMap, $user) {
|
||
$value['sort'] = $key + 1;
|
||
|
||
// 存在前端提交的列
|
||
if (isset($submittedMap[$value['prop']])) {
|
||
$merged = array_merge([
|
||
'condition' => 'like',
|
||
'search_type' => 'text',
|
||
'is_search' => 1,
|
||
'sortable' => 0,
|
||
], $value, $submittedMap[$value['prop']]);
|
||
|
||
return $this->prepareConfig($merged, $user);
|
||
}
|
||
|
||
// 否则删除该列配置
|
||
ColumnConfig::query()->withoutGlobalScope(CompanyScope::class)
|
||
->where([
|
||
'prop' => $value['prop'],
|
||
'method' => $this->data['method'],
|
||
'creator_id' => $user['id'],
|
||
])
|
||
->forceDelete();
|
||
|
||
return null;
|
||
});
|
||
}
|
||
|
||
// 执行并获取所有结果
|
||
$results = $parallel->wait();
|
||
|
||
// 过滤非 null 的更新数据
|
||
$updates = array_filter($results);
|
||
|
||
if (!empty($updates)) {
|
||
$this->updateOrCreateColumnConfigs($updates, $method, $user);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 更新或创建列配置.
|
||
*/
|
||
private function updateOrCreateColumnConfigs(array $configs, string $method, array $creator): void
|
||
{
|
||
// 默认配置key值
|
||
$defaultKeys = (new ColumnConfig())->getFillable();
|
||
|
||
// 删除$configs里不存在$defaultKeys里的字段,防止前端传递的额外字段导致更新失败
|
||
$configs = array_map(function ($config) use ($defaultKeys) {
|
||
$newConfig = array_intersect_key($config, array_flip($defaultKeys));
|
||
$newConfig['sort'] = $config['sort'] ?? 0;
|
||
return $newConfig;
|
||
}, $configs);
|
||
|
||
// 添加公共字段
|
||
foreach ($configs as &$config) {
|
||
$config['company_id'] = $creator['company_id'] ?? 0;
|
||
$config['method'] = $method;
|
||
$config['creator_id'] = $creator['id'];
|
||
$config['creator_name'] = $creator['name'];
|
||
$config['created_at'] = $config['updated_at'] = time();
|
||
}
|
||
unset($config);
|
||
|
||
$updateKeys = array_keys($configs[0]);
|
||
// 删除不需要更新的字段
|
||
unset(
|
||
$updateKeys[array_search('creator_name', $updateKeys, true)],
|
||
$updateKeys[array_search('prop', $updateKeys, true)],
|
||
$updateKeys[array_search('method', $updateKeys, true)],
|
||
$updateKeys[array_search('creator_id', $updateKeys, true)],
|
||
);
|
||
|
||
// 批量插入或更新
|
||
ColumnConfig::query()->withoutGlobalScope(CompanyScope::class)
|
||
->upsert($configs, ['prop', 'method', 'creator_id'], array_values($updateKeys));
|
||
}
|
||
|
||
/**
|
||
* 删除列配置.
|
||
*/
|
||
private function deleteColumnConfig(array $user, string $method): void
|
||
{
|
||
ColumnConfig::query()->withoutGlobalScope(CompanyScope::class)
|
||
->where(['method' => $method, 'creator_id' => $user['id']])
|
||
->forceDelete();
|
||
}
|
||
|
||
private function prepareConfig(array $config, array $user): array
|
||
{
|
||
return array_merge($config, [
|
||
'company_id' => $user['company_id'] ?? 0,
|
||
'creator_id' => $user['id'],
|
||
'creator_name' => $user['name'],
|
||
]);
|
||
}
|
||
}
|