|
|
<?php
|
|
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
|
|
use App\Exports\BaseFormExport;
|
|
|
use App\Helpers\ResponseCode;
|
|
|
use App\Http\Requests\BaseFormRequest;
|
|
|
use App\Models\Admin;
|
|
|
use App\Models\BaseForm;
|
|
|
use App\Models\CustomForm;
|
|
|
use App\Models\CustomFormField;
|
|
|
use App\Models\CustomFormFieldUpdate;
|
|
|
use App\Repositories\BaseFormExtendRepository;
|
|
|
use App\Repositories\BaseFormRepository;
|
|
|
use Illuminate\Http\Request;
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
use Illuminate\Support\Facades\Validator;
|
|
|
use Maatwebsite\Excel\Facades\Excel;
|
|
|
use Rap2hpoutre\FastExcel\FastExcel;
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 自定义表单增删改查操作
|
|
|
*/
|
|
|
class BaseFormController extends CommonController
|
|
|
{
|
|
|
|
|
|
/**
|
|
|
* @OA\Get(
|
|
|
* path="/api/admin/base-form/index",
|
|
|
* tags={"自定义表单增删改查操作"},
|
|
|
* summary="列表",
|
|
|
* description="",
|
|
|
* @OA\Parameter(name="table_name", in="query", @OA\Schema(type="string"), required=false, description="表名"),
|
|
|
* @OA\Parameter(name="is_export", in="query", @OA\Schema(type="string"), required=false, description="是否导出0否1是"),
|
|
|
* @OA\Parameter(name="export_fields", in="query", @OA\Schema(type="string"), required=false, 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="filter", 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="is_auth", in="query", @OA\Schema(type="string"), required=false, description="是否鉴权 0否1是"),
|
|
|
* @OA\Parameter(name="show_relation", 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(BaseFormRepository $repository)
|
|
|
{
|
|
|
$all = \request()->all();
|
|
|
$list = $repository->buildWith($all['show_relation'] ?? [])->buildSeacher($all['filter'] ?? [])
|
|
|
->where(function ($query) use ($all) {
|
|
|
// 权限
|
|
|
if (isset($all['is_auth']) && !empty($all['is_auth'])) {
|
|
|
$user = $this->getUser();
|
|
|
$departmentIds = Admin::roleAllowAdminIds($user);
|
|
|
$query->where(function ($qry) use ($departmentIds, $user) {
|
|
|
$qry->whereIn('department_id', $departmentIds)->orWhere('admin_id', $user->id);
|
|
|
});
|
|
|
}
|
|
|
})->orderBy($all['sort_name'] ?? 'id', $all['sort_type'] ?? 'desc');
|
|
|
if (isset($all['is_export']) && !empty($all['is_export'])) {
|
|
|
$list = $list->get();
|
|
|
$export_fields = $all['export_fields'] ?? [];
|
|
|
// 导出文件名字
|
|
|
$filename = (new CustomForm())->hasTable($all['table_name'])->name;
|
|
|
return Excel::download(new BaseFormExport($export_fields, $list), $filename . date('YmdHis') . '.xlsx');
|
|
|
} else {
|
|
|
// 输出
|
|
|
$list = $list->paginate($all['page_size'] ?? 20);
|
|
|
}
|
|
|
return $this->success($list);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* @OA\Get(
|
|
|
* path="/api/admin/base-form/show",
|
|
|
* tags={"自定义表单增删改查操作"},
|
|
|
* summary="详情",
|
|
|
* description="",
|
|
|
* @OA\Parameter(name="table_name", in="query", @OA\Schema(type="string"), required=false, 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(BaseFormRepository $repository)
|
|
|
{
|
|
|
$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 = $repository->buildWith()->find($all['id']);
|
|
|
// 获取所有一对多json附加数据
|
|
|
$detail = $repository->getJsonData($detail);
|
|
|
return $this->success($detail);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* @OA\Post(
|
|
|
* path="/api/admin/base-form/save",
|
|
|
* tags={"自定义表单增删改查操作"},
|
|
|
* summary="更新",
|
|
|
* description="",
|
|
|
* @OA\Parameter(name="table_name", in="query", @OA\Schema(type="string"), required=false, description="表名"),
|
|
|
* @OA\Parameter(name="id", in="query", @OA\Schema(type="int"), required=true, description="Id(存在更新,不存在新增)"),
|
|
|
* @OA\Parameter(name="字段名_relation", 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 save(BaseFormRequest $request, BaseFormRepository $repository)
|
|
|
{
|
|
|
$all = $request->all();
|
|
|
DB::beginTransaction();
|
|
|
try {
|
|
|
if (isset($all['id'])) {
|
|
|
$model = BaseForm::find($all['id']);
|
|
|
} else {
|
|
|
$model = new BaseForm();
|
|
|
$all['admin_id'] = $this->getUserId();
|
|
|
$all['department_id'] = $this->getUser()->department_id;
|
|
|
}
|
|
|
// 修改前原始数据
|
|
|
$original = $model->getOriginal();
|
|
|
// 保存前额外前置操作
|
|
|
$allAfter = BaseFormExtendRepository::saveBefore($all);
|
|
|
$model->fill($allAfter);
|
|
|
$model->save();
|
|
|
DB::commit();
|
|
|
$model = $model->find($model->id);
|
|
|
// 更新模型关系
|
|
|
$repository->updateRelation($all, $model);
|
|
|
// 保存后额外后置操作
|
|
|
BaseFormExtendRepository::saveAfter($all, $model);
|
|
|
// 字段修改记录
|
|
|
$repository->saveLogs($this->getUser(), $all['table_name'], $original, $model);
|
|
|
return $this->success('更新成功');
|
|
|
} catch (\Exception $exception) {
|
|
|
DB::rollBack();
|
|
|
return $this->fail([$exception->getCode(), $exception->getMessage()]);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* @OA\Get(
|
|
|
* path="/api/admin/base-form/destroy",
|
|
|
* tags={"自定义表单增删改查操作"},
|
|
|
* summary="删除",
|
|
|
* description="",
|
|
|
* @OA\Parameter(name="table_name", in="query", @OA\Schema(type="string"), required=false, description="表名"),
|
|
|
* @OA\Parameter(name="id", in="query", @OA\Schema(type="string"), required=true, description="id"),
|
|
|
* @OA\Parameter(name="destroy_relation", 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 destroy(BaseFormRepository $repository)
|
|
|
{
|
|
|
$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())]);
|
|
|
}
|
|
|
$form = $repository->find($all['id']);
|
|
|
if (isset($all['destroy_relation']) && !empty($all['destroy_relation'])) {
|
|
|
// 删除关联关系
|
|
|
foreach ($all['destroy_relation'] as $item) {
|
|
|
$form->$item()->delete();
|
|
|
}
|
|
|
}
|
|
|
$form->delete();
|
|
|
// 字段修改记录
|
|
|
$repository->saveLogs($this->getUser(), $all['table_name'], 'delete', $form);
|
|
|
return $this->success('删除成功');
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* @OA\Post(
|
|
|
* path="/api/admin/base-form/excel-show",
|
|
|
* tags={"自定义表单增删改查操作"},
|
|
|
* summary="导入预览",
|
|
|
* description="",
|
|
|
* @OA\Parameter(name="table_name", in="query", @OA\Schema(type="string"), required=false, description="表名"),
|
|
|
* @OA\Parameter(name="file", 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 excelShow(Request $request)
|
|
|
{
|
|
|
$file = $request->file('file');
|
|
|
$table_name = $request->get('table_name');
|
|
|
//判断文件是否有效
|
|
|
if (!($request->hasFile('file') && $file->isValid())) {
|
|
|
return $this->fail([ResponseCode::ERROR_BUSINESS, '文件不存在或无效']);
|
|
|
}
|
|
|
//获取文件大小
|
|
|
$img_size = floor($file->getSize() / 1024);
|
|
|
if ($img_size >= 10 * 1024) {
|
|
|
return $this->fail([ResponseCode::ERROR_BUSINESS, '文件必须小于10M']);
|
|
|
}
|
|
|
//过滤文件后缀
|
|
|
$ext = $file->getClientOriginalExtension();
|
|
|
if (!in_array($ext, ['xls', 'xlsx', 'csv'])) {
|
|
|
return $this->fail([ResponseCode::ERROR_BUSINESS, '仅支持xls/xlsx/csv格式']);
|
|
|
}
|
|
|
$tempFile = $file->getRealPath();
|
|
|
$dataArray = (new FastExcel)->import($tempFile)->toArray();
|
|
|
// 数据过滤,只能导入数据表有的字段
|
|
|
$baseForm = new BaseForm();
|
|
|
$customFormField = new CustomFormField();
|
|
|
$rowTableFieldByComment = $customFormField->getRowTableFieldsByComment($table_name);
|
|
|
$list = [];
|
|
|
foreach ($dataArray as $key => $value) {
|
|
|
foreach ($rowTableFieldByComment as $k => $v) {
|
|
|
if (isset($value[$v])) {
|
|
|
$list[$key][$k] = $value[$v];
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
return $this->success($list);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* @OA\Post(
|
|
|
* path="/api/admin/base-form/import",
|
|
|
* tags={"自定义表单增删改查操作"},
|
|
|
* summary="导入",
|
|
|
* description="",
|
|
|
* @OA\Parameter(name="table_name", in="query", @OA\Schema(type="string"), required=false, description="表名"),
|
|
|
* @OA\Parameter(name="data", 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 import(BaseFormRepository $repository)
|
|
|
{
|
|
|
$all = \request()->all();
|
|
|
$messages = [
|
|
|
'data.required' => '数据必填',
|
|
|
];
|
|
|
$validator = Validator::make($all, [
|
|
|
'data' => 'required',
|
|
|
], $messages);
|
|
|
if ($validator->fails()) {
|
|
|
return $this->fail([ResponseCode::ERROR_PARAMETER, implode(',', $validator->errors()->all())]);
|
|
|
}
|
|
|
$total = count($all['data']);
|
|
|
$fail = 0;
|
|
|
$err = [];
|
|
|
foreach ($all['data'] as $item) {
|
|
|
try {
|
|
|
// 数据字典转换
|
|
|
$item = $repository->fieldToParameter($item);
|
|
|
BaseForm::create($item);
|
|
|
} catch (\Exception $e) {
|
|
|
$fail += 1;
|
|
|
$err[] = $e->getMessage();
|
|
|
}
|
|
|
}
|
|
|
return $this->success(['total' => $total, 'fail' => $fail, 'err' => $err]);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* @OA\Get(
|
|
|
* path="/api/admin/base-form/logs",
|
|
|
* tags={"自定义表单增删改查操作"},
|
|
|
* summary="更新记录",
|
|
|
* description="",
|
|
|
* @OA\Parameter(name="table_name", in="query", @OA\Schema(type="string"), required=false, description="表名"),
|
|
|
* @OA\Parameter(name="table_id", in="query", @OA\Schema(type="string"), required=false, description="记录id"),
|
|
|
* @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="filter", 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 logs()
|
|
|
{
|
|
|
$all = \request()->all();
|
|
|
$list = CustomFormFieldUpdate::with('admin', 'department')->where(function ($query) use ($all) {
|
|
|
if (isset($all['table_name'])) {
|
|
|
$query->where('table_name', $all['table_name']);
|
|
|
}
|
|
|
if (isset($all['table_id'])) {
|
|
|
$query->where('table_id', $all['table_id']);
|
|
|
}
|
|
|
})->orderBy($all['sort_name'] ?? 'id', $all['sort_type'] ?? 'desc')
|
|
|
->paginate($all['page_size'] ?? 20);;
|
|
|
return $this->success($list);
|
|
|
}
|
|
|
|
|
|
}
|