model->getCasts() as $k => $v) { if ($v == 'json') $allJsonFields = array_merge($allJsonFields, [$k => $v]); } // 数据查询 $jsonFields = array_keys($allJsonFields); $customFormFields = CustomFormField::where('custom_form_id', $this->model->customForm->id) ->whereIn('field', $jsonFields)->get()->pluck('edit_input', 'field')->toArray(); foreach ($model->getAttributes() as $key => &$item) { $editInput = $customFormFields[$key] ?? null; if ($editInput == 'files' && !empty($model->$key)) { $newField = $key . '_upload_details'; $model->$newField = Upload::whereIn('id', $model->$key)->get(); } } return $model; } /** * 构建with */ public function buildWith($showRelation = []) { $allWith = $this->model->allRelationFields(); if (!empty($showRelation)) { $allWith = array_intersect($allWith, $showRelation); } $this->model = $this->model->with($allWith); return $this; } /** * 搜索构建 */ public function buildSeacher($conditions) { foreach ($conditions as $condition) { $key = $condition['key'] ?? ''; $op = $condition['op'] ?? ''; $value = $condition['value'] ?? ''; if (empty($key) || empty($op) || empty($value)) { continue; } // 等于 if ($op == 'eq') { $this->model = $this->model->where($key, $value); } // 不等于 if ($op == 'neq') { $this->model = $this->model->where($key, '!=', $value); } // 模糊搜索 if ($op == 'like') { $this->model = $this->model->where($key, 'like', '%' . $value . '%'); } // 否定模糊搜索 if ($op == 'notlike') { $this->model = $this->model->where($key, 'not like', '%' . $value . '%'); } // 范围搜索 if ($op == 'range') { list($from, $to) = explode(',', $value); if (empty($from) || empty($to)) { continue; } $this->model = $this->model->whereBetween($key, [$from, $to]); } } return $this; } /** * 更新关联关系 */ public function updateRelation($all, $model) { $allRelation = $this->model->allRelationFields(true, false); foreach ($allRelation as $item) { if (!isset($all[$item->link_with_name])) { continue; } foreach ($all[$item->link_with_name] as $v) { if (isset($v['id'])) { $linkModel = $model->{$item->link_with_name}()->find($v['id']); $linkModel->fill($v); $linkModel->save(); } else { // 新增 $fillData = $this->filterRequestColumns($v, $item->link_table_name); $fillData[$item->foreign_key] = $model->id; $model->{$item->link_with_name}()->insert($fillData); // $model->{$item->link_with_name}()->create($v); } } } return true; } /** * 创建/更新字段数据记录 */ public function saveLogs($user, $tableName, $original, $model) { $list = []; $fileds = (new CustomFormField())->getRowTableFieldsByComment($tableName); if (empty($original)) { $operate = '新建'; // 新增 foreach ($model->toArray() as $key => $item) { $list[] = ($fileds[$key] ?? $key) . ":" . (is_array($item) ? json_encode($item, JSON_UNESCAPED_UNICODE) : $item); } } elseif ($original == 'delete') { $operate = '删除'; } else { // 更新 $operate = '更新'; $changes = $model->getChanges(); foreach ($changes as $key => $item) { if (in_array($key, ['updated_at'])) continue; if (isset($original[$key])) { $list[] = ($fileds[$key] ?? $key) . ":" . $original[$key] . "=>" . (is_array($item) ? json_encode($item, JSON_UNESCAPED_UNICODE) : $item); } } } return CustomFormFieldUpdate::create([ 'table_name' => $tableName, 'admin_id' => $user->id, 'operate' => $operate, 'table_id' => $model->id, 'department_id' => $user->department_id ?? 0, 'ip' => $_SERVER['REMOTE_ADDR'], 'content' => implode('。', $list), 'requst_data' => json_encode(request()->all(), JSON_UNESCAPED_UNICODE) ]); } /** * filter request columns by fields * @param $request * @param $linkTableName * @return array */ public function filterRequestColumns($request, $linkTableName) { $columns = (new CustomFormField())->getRowTableFieldsByType($linkTableName); $return = []; foreach ($request as $k => $v) { if (!in_array($k, array_keys($columns))) { continue; } if ($k === "password") { if (!$v) { continue; } $v = Hash::make($v); } switch ($columns[$k]) { case "json": $v = json_encode($v, JSON_UNESCAPED_UNICODE); break; default: if (is_array($v)) { if (count($v) == count($v, 1)) { $v = implode(',', $v); } else { $v = json_encode($v, JSON_UNESCAPED_UNICODE); } } } $return[$k] = $v; } return $return; } /** * 字段中如果存在数据字段,需要转换成对应的id */ public function fieldToParameter($item) { $relations = Cache::remember('field_to_parameter_' . $this->model->customForm->id, 300, function () { return CustomFormRelation::where('custom_form_id', $this->model->customForm->id) ->whereNotNull('parameter_id')->pluck('parameter_id', 'local_key')->toArray(); }); if ($relations->isEmpty()) return $item; // 匹配数据 foreach ($item as $key => &$value) { foreach ($relations as $k => $v) { if ($key == $k) { // 匹配上字段是数据字典,查询对应的参数id $value = ParameterDetail::where('parameter_id', $v) ->where('value', $value)->value('id'); } } } return $item; } }