all(); $list = CustomForm::where(function ($query) use ($all) { })->orderBy($all['sort_name'] ?? 'id', $all['sort_type'] ?? 'desc') ->paginate($all['page_size'] ?? 20); return $this->success($list); } /** * @OA\Get( * path="/api/admin/custom-form/real-table-index", * tags={"自定义表单"}, * summary="获取实体数据表", * description="", * @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"), * @OA\Response( * response="200", * description="暂无" * ) * ) */ public function realTableindex() { $all = request()->all(); $raws = Schema::getAllTables(); $list = []; foreach ($raws as $item) { foreach ($item as $k => $v) { if ($k != 'Table_type') $list[] = $v; } } return $this->success($list); } /** * @OA\Get( * path="/api/admin/custom-form/show", * tags={"自定义表单"}, * summary="详情", * description="", * @OA\Parameter(name="id", in="query", @OA\Schema(type="string"), required=true, description="id"), * @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"), * @OA\Response( * response="200", * description="暂无" * ) * ) */ public function show() { $all = \request()->all(); $messages = [ 'id.required' => 'Id必填', ]; $validator = Validator::make($all, [ 'id' => 'required' ], $messages); if ($validator->fails()) { return $this->fail([ResponseCode::ERROR_PARAMETER, implode(',', $validator->errors()->all())]); } $detail = CustomForm::with('relation', 'fields')->find($all['id']); return $this->success($detail); } /** * @OA\Get( * path="/api/admin/custom-form/real-table-show", * tags={"自定义表单"}, * summary="获取实体表字段详情", * description="", * @OA\Parameter(name="table_name", in="query", @OA\Schema(type="string"), required=true, description="表名"), * @OA\Parameter(name="except", in="query", @OA\Schema(type="string"), required=true, description="排除的字段名数组"), * @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"), * @OA\Response( * response="200", * description="暂无" * ) * ) */ public function realTableShow() { $all = \request()->all(); $messages = [ 'table_name.required' => 'table_name必填', ]; $validator = Validator::make($all, [ 'table_name' => 'required' ], $messages); if ($validator->fails()) { return $this->fail([ResponseCode::ERROR_PARAMETER, implode(',', $validator->errors()->all())]); } $except = request('except', []); $detail = (new CustomFormField())->getRowTableFieldsByComment($all['table_name'], $except); return $this->success($detail); } /** * @OA\Post( * path="/api/admin/custom-form/save", * tags={"自定义表单"}, * summary="更新", * description="", * @OA\Parameter(name="id", in="query", @OA\Schema(type="int"), required=true, description="Id(存在更新,不存在新增)"), * @OA\Parameter(name="table_name", in="query", @OA\Schema(type="string"), required=true, description="表名"), * @OA\Parameter(name="name", in="query", @OA\Schema(type="string"), required=true, description="名字"), * @OA\Parameter(name="relation", in="query", @OA\Schema(type="string"), required=false, description="关联关系二维数组,包含字段:id存在更新,不存在新增,custom_form_field,link_table_name关联表名,link_relation关联关系,local_key本地字段,foreign_key关联表字段"), * @OA\Parameter(name="fields", in="query", @OA\Schema(type="string"), required=false, description="字段详情二维数组,包含字段:id存在更新,不存在新增。和其他字段详见字段更新接口"), * @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"), * @OA\Response( * response="200", * description="暂无" * ) * ) */ public function save() { $all = \request()->all(); $messages = [ 'table_name.required' => '表名必填' ]; $validator = Validator::make($all, [ 'table_name' => 'required' ], $messages); if ($validator->fails()) { return $this->fail([ResponseCode::ERROR_PARAMETER, implode(',', $validator->errors()->all())]); } DB::beginTransaction(); try { if (isset($all['id'])) { $model = CustomForm::find($all['id']); // 判断不能重名 $check = $model->where('table_name', $all['table_name'])->where('id', '!=', $all['id'])->first(); if ($check) return $this->fail([ResponseCode::ERROR_BUSINESS, '表名重复']); } else { $model = new CustomForm(); $all['admin_id'] = $this->getUserId(); $all['department_id'] = $this->getUser()->department_id; // 判断不能重名 $check = $model->hasTable($all['table_name']); if ($check) return $this->fail([ResponseCode::ERROR_BUSINESS, '表名重复']); } // 表名必须是复数 $plural = Str::plural($all['table_name']); if ($plural != $all['table_name']) { return $this->fail([ResponseCode::ERROR_BUSINESS, '请用复数形式命名:' . $plural]); } $model->fill($all); $model->save(); DB::commit(); // 关联关系 if (isset($all['relation']) && !empty($all['relation'])) { foreach ($all['relation'] as $detail) { $detail['link_with_name'] = getFieldWithName($detail); if (isset($detail['id'])) { // 更新 (CustomFormRelation::find($detail['id']))->fill($detail)->save(); } else { // 新增 $model->relation()->create($detail); } } } if (isset($all['fields']) && !empty($all['fields'])) { foreach ($all['fields'] as $detail) { if (isset($detail['id'])) { // 更新 (CustomFormField::find($detail['id']))->fill($detail)->save(); } else { // 新增 $model->fields()->create($detail); } } } return $this->success('更新成功'); } catch (\Exception $exception) { DB::rollBack(); return $this->fail([$exception->getCode(), $exception->getMessage()]); } } /** * @OA\Get( * path="/api/admin/custom-form/destroy", * tags={"自定义表单"}, * summary="删除", * description="", * @OA\Parameter(name="id", in="query", @OA\Schema(type="string"), required=true, description="id"), * @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"), * @OA\Response( * response="200", * description="暂无" * ) * ) */ public function destroy() { $all = \request()->all(); $messages = [ 'id.required' => 'Id必填', ]; $validator = Validator::make($all, [ 'id' => 'required' ], $messages); if ($validator->fails()) { return $this->fail([ResponseCode::ERROR_PARAMETER, implode(',', $validator->errors()->all())]); } $customForm = CustomForm::find($all['id']); CustomForm::where('id', $all['id'])->delete(); CustomFormField::where('custom_form_id', $all['id'])->delete(); CustomFormRelation::where('custom_form_id', $all['id'])->delete(); Schema::drop($customForm->table_name); return $this->success('删除成功'); } /** * @OA\Get( * path="/api/admin/custom-form/relation-destroy", * tags={"自定义表单"}, * summary="关联关系删除", * description="", * @OA\Parameter(name="id", in="query", @OA\Schema(type="string"), required=true, description="关联关系id"), * @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"), * @OA\Response( * response="200", * description="暂无" * ) * ) */ public function relationDestroy() { $all = \request()->all(); $messages = [ 'id.required' => 'Id必填', ]; $validator = Validator::make($all, [ 'id' => 'required' ], $messages); if ($validator->fails()) { return $this->fail([ResponseCode::ERROR_PARAMETER, implode(',', $validator->errors()->all())]); } CustomFormRelation::where('id', $all['id'])->delete(); return $this->success('删除成功'); } /** * @OA\Get( * path="/api/admin/custom-form/update-table", * tags={"自定义表单"}, * summary="数据表同步", * description="", * @OA\Parameter(name="id", in="query", @OA\Schema(type="string"), required=true, description="id"), * @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"), * @OA\Response( * response="200", * description="暂无" * ) * ) */ public function updateTable() { $all = \request()->all(); $messages = [ 'id.required' => 'Id必填', ]; $validator = Validator::make($all, [ 'id' => 'required' ], $messages); if ($validator->fails()) { return $this->fail([ResponseCode::ERROR_PARAMETER, implode(',', $validator->errors()->all())]); } $result = (new CustomForm())->updateTable($all['id']); return $this->success($result); } /** * @OA\Get( * path="/api/admin/custom-form/clone-table", * tags={"自定义表单"}, * summary="克隆表结构", * description="", * @OA\Parameter(name="table_name", in="query", @OA\Schema(type="string"), required=true, description="自定义表单表名"), * @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"), * @OA\Response( * response="200", * description="暂无" * ) * ) */ public function cloneTable() { $all = \request()->all(); $messages = [ 'table_name.required' => '表名必填', ]; $validator = Validator::make($all, [ 'table_name' => 'required' ], $messages); if ($validator->fails()) { return $this->fail([ResponseCode::ERROR_PARAMETER, implode(',', $validator->errors()->all())]); } $customForm = CustomForm::where('table_name', $all['table_name'])->first(); if (empty($customForm)) { return $this->fail([ResponseCode::ERROR_BUSINESS, '表名不存在']); } // 执行克隆表 $model = CustomForm::create([ 'name' => $customForm->name . '_clone', 'table_name' => $customForm->table_name . '_clone', 'admin_id' => $this->getUserId(), 'department_id' => $this->getUser()->department_id, ]); // 克隆字段 $customFormField = CustomFormField::where('custom_form_id', $customForm->id)->get()->toArray(); foreach ($customFormField as $item) { $item['custom_form_id'] = $model->id; CustomFormField::create($item); } // 克隆关联关系 $customFormRelation = CustomFormRelation::where('custom_form_id', $customForm->id)->get()->toArray(); foreach ($customFormRelation as $value) { $value['custom_form_id'] = $model->id; CustomFormRelation::create($value); } return $this->success('克隆成功'); } }