|
|
@@ -4,8 +4,7 @@ namespace App\Service;
|
|
|
|
|
|
use App\Jobs\ProcessOssTask;
|
|
|
use App\Model\CustomFieldDefinitions;
|
|
|
-use App\Model\SysMenu;
|
|
|
-use App\Model\SysModules;
|
|
|
+use App\Model\CustomFieldValue;
|
|
|
use App\Model\SysOssTasks;
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
|
|
@@ -170,7 +169,7 @@ class CustomFieldSettingService extends Service
|
|
|
return [true, $data];
|
|
|
}
|
|
|
|
|
|
- public static function syncCustomFieldData($id, $data, $user)
|
|
|
+ public static function syncCustomFieldData1($id, $data, $user)
|
|
|
{
|
|
|
$top_depart_id = $user['top_depart_id'];
|
|
|
if (empty($data['menu_id'])) return [true, ''];
|
|
|
@@ -259,4 +258,91 @@ class CustomFieldSettingService extends Service
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ public static function syncCustomFieldData($modelId, $tableName, $data, $user)
|
|
|
+ {
|
|
|
+ if(empty($tableName)) return [false, '表名不存在'];
|
|
|
+
|
|
|
+ $top_depart_id = $user['top_depart_id'];
|
|
|
+ $customFields = $data['custom_fields'] ?? [];
|
|
|
+ $time = time();
|
|
|
+ $pendingTasks = [];
|
|
|
+
|
|
|
+ // 1. 预校验:过滤掉空标签后,判断剩下的标签是否重复
|
|
|
+ $labels = array_filter(array_column($customFields, 'field_label'));
|
|
|
+ if (count($labels) !== count(array_unique($labels))) return [false, '自定义字段名称不能重复'];
|
|
|
+
|
|
|
+ try {
|
|
|
+ // 2. 获取数据库现有的旧数据(仅限未删除的)
|
|
|
+ $oldValues = CustomFieldValue::where('top_depart_id', $top_depart_id)
|
|
|
+ ->where('table_name', $tableName)
|
|
|
+ ->where('model_id', $modelId)
|
|
|
+ ->where('del_time', 0)
|
|
|
+ ->get()
|
|
|
+ ->keyBy('id');
|
|
|
+
|
|
|
+ $submittedIds = [];
|
|
|
+
|
|
|
+ // 3. 处理前端提交的数据
|
|
|
+ foreach ($customFields as $field) {
|
|
|
+ $fId = (int)($field['id'] ?? 0);
|
|
|
+ $label = trim((string)($field['field_label'] ?? ''));
|
|
|
+ $newVal = (string)($field['field_value'] ?? '');
|
|
|
+ $type = (int)($field['field_type'] ?? 1);
|
|
|
+
|
|
|
+ if ($label === '') continue; // 标签为空的直接跳过,不保存
|
|
|
+
|
|
|
+ // --- OSS 附件逻辑 ---
|
|
|
+ if ($type === 2) {
|
|
|
+ $oldVal = isset($oldValues[$fId]) ? $oldValues[$fId]->field_value : '';
|
|
|
+ if ($newVal !== $oldVal) {
|
|
|
+ if (!empty($oldVal)) $pendingTasks[] = ['type' => SysOssTasks::type_one, 'url' => $oldVal, 'top_depart_id' => $top_depart_id, 'crt_time' => $time];
|
|
|
+ if (!empty($newVal)) $pendingTasks[] = ['type' => SysOssTasks::type_two, 'url' => $newVal, 'top_depart_id' => $top_depart_id, 'crt_time' => $time];
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // --- 保存/更新 ---
|
|
|
+ // 如果前端传了 ID,就按 ID 改;没传 ID,就新增(updateOrInsert 在 id 为 0 时会执行插入)
|
|
|
+ $saveData = [
|
|
|
+ 'top_depart_id' => $top_depart_id,
|
|
|
+ 'table_name' => $tableName,
|
|
|
+ 'model_id' => $modelId,
|
|
|
+ 'field_label' => $label,
|
|
|
+ 'field_value' => $newVal,
|
|
|
+ 'field_type' => $type,
|
|
|
+ ];
|
|
|
+
|
|
|
+ if ($fId > 0) {
|
|
|
+ CustomFieldValue::where('id', $fId)->update($saveData);
|
|
|
+ $submittedIds[] = $fId;
|
|
|
+ } else {
|
|
|
+ $saveData['crt_time'] = $time;
|
|
|
+ $newId = CustomFieldValue::insertGetId($saveData);
|
|
|
+ $submittedIds[] = $newId;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 4. 清理:如果旧 ID 不在本次提交的 ID 列表中,说明被删除了
|
|
|
+ foreach ($oldValues as $oldId => $oldRow) {
|
|
|
+ if (!in_array($oldId, $submittedIds)) {
|
|
|
+ // 如果删掉的是附件,加个删除任务
|
|
|
+ if ($oldRow->field_type == 2 && !empty($oldRow->field_value)) {
|
|
|
+ $pendingTasks[] = ['type' => SysOssTasks::type_one, 'url' => $oldRow->field_value, 'top_depart_id' => $top_depart_id, 'crt_time' => $time];
|
|
|
+ }
|
|
|
+ CustomFieldValue::where('id', $oldId)->update(['del_time' => $time]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 5. 触发 OSS 任务
|
|
|
+ if (!empty($pendingTasks)) {
|
|
|
+ SysOssTasks::insert($pendingTasks);
|
|
|
+ ProcessOssTask::dispatch()->onQueue(SysOssTasks::job);
|
|
|
+ }
|
|
|
+
|
|
|
+ } catch (\Throwable $exception) {
|
|
|
+ return [false, $exception->getMessage()];
|
|
|
+ }
|
|
|
+
|
|
|
+ return [true, ''];
|
|
|
+ }
|
|
|
}
|