You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

365 lines
14 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
namespace App\Http\Controllers\Admin;
use App\Helpers\ResponseCode;
use App\Models\CustomForm;
use App\Models\CustomFormField;
use App\Models\CustomFormRelation;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Str;
/**
* 自定义表单
*/
class CustomFormController extends CommonController
{
/**
* @OA\Get(
* path="/api/admin/custom-form/index",
* tags={"自定义表单"},
* summary="列表",
* description="",
* @OA\Parameter(name="page_size", in="query", @OA\Schema(type="string"), required=false, description="每页显示的条数"),
* @OA\Parameter(name="page", in="query", @OA\Schema(type="string"), required=false, description="页码"),
* @OA\Parameter(name="sort_name", in="query", @OA\Schema(type="string"), required=false, description="排序字段名字"),
* @OA\Parameter(name="sort_type", in="query", @OA\Schema(type="string"), required=false, description="排序类型"),
* @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"),
* @OA\Response(
* response="200",
* description="暂无"
* )
* )
*/
public function index()
{
$all = request()->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_fieldlink_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('克隆成功');
}
}