|
|
<?php
|
|
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
|
|
use App\Http\Requests\VisitRequest;
|
|
|
use App\Models\Visit;
|
|
|
use App\Models\VisitArea;
|
|
|
use App\Models\VisitTime;
|
|
|
use App\Models\Blacklist;
|
|
|
use App\Models\Study;
|
|
|
use App\Models\VisitLog;
|
|
|
use App\Models\VisitAudit;
|
|
|
use App\Models\GateLog;
|
|
|
use App\Models\Config;
|
|
|
use App\Helpers\ResponseCode;
|
|
|
use App\Exports\BaseFormExport;
|
|
|
use Illuminate\Http\Request;
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
use Illuminate\Support\Facades\Validator;
|
|
|
use Maatwebsite\Excel\Facades\Excel;
|
|
|
|
|
|
/**
|
|
|
* @OA\Tag(
|
|
|
* name="访客预约管理",
|
|
|
* description="访客预约登记相关接口"
|
|
|
* )
|
|
|
*/
|
|
|
class VisitController extends BaseController
|
|
|
{
|
|
|
public function __construct()
|
|
|
{
|
|
|
parent::__construct(new Visit());
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* @OA\Get(
|
|
|
* path="/api/admin/visits/index",
|
|
|
* tags={"访客预约管理"},
|
|
|
* summary="访客预约登记列表",
|
|
|
* 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="filter", in="query", @OA\Schema(type="string"), required=false, description="查询条件。数组"),
|
|
|
* @OA\Parameter(name="show_relation", 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="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();
|
|
|
$filter = $all['filter'] ?? [];
|
|
|
$show_relation = $all['show_relation'] ?? [];
|
|
|
$page_size = $all['page_size'] ?? 10;
|
|
|
$page = $all['page'] ?? 1;
|
|
|
$sort_name = $all['sort_name'] ?? 'id';
|
|
|
$sort_type = $all['sort_type'] ?? 'desc';
|
|
|
|
|
|
$query = $this->model->query();
|
|
|
|
|
|
// 应用过滤条件
|
|
|
if (!empty($filter)) {
|
|
|
$query = $this->applyFilters($query, $filter);
|
|
|
}
|
|
|
|
|
|
// 应用排序
|
|
|
$query->orderBy($sort_name, $sort_type);
|
|
|
|
|
|
// 加载关联关系
|
|
|
if (!empty($show_relation)) {
|
|
|
$query->with($show_relation);
|
|
|
}
|
|
|
|
|
|
// 分页
|
|
|
$result = $query->paginate($page_size, ['*'], 'page', $page);
|
|
|
|
|
|
return $this->success($result);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* @OA\Get(
|
|
|
* path="/api/admin/visits/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();
|
|
|
$detail = $this->model->with([
|
|
|
'visitArea',
|
|
|
'visitTime',
|
|
|
'admin',
|
|
|
'department',
|
|
|
'visitAudits',
|
|
|
'logs',
|
|
|
'acceptAdminSign'
|
|
|
])->find($all['id']);
|
|
|
|
|
|
return $this->success($detail);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* @OA\Post(
|
|
|
* path="/api/admin/visits/save",
|
|
|
* tags={"访客预约管理"},
|
|
|
* summary="新增/更新访客预约登记",
|
|
|
* description="创建或更新访客预约登记",
|
|
|
* @OA\Parameter(name="id", in="query", @OA\Schema(type="string"), required=false, description="访客预约登记ID,更新时必填"),
|
|
|
* @OA\Parameter(name="date", in="query", @OA\Schema(type="string"), required=true, description="到访日期"),
|
|
|
* @OA\Parameter(name="visit_time_id", in="query", @OA\Schema(type="string"), required=true, description="访问时段ID"),
|
|
|
* @OA\Parameter(name="visit_area_id", in="query", @OA\Schema(type="string"), required=true, description="访问区域ID"),
|
|
|
* @OA\Parameter(name="reason", 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="mobile", in="query", @OA\Schema(type="string"), required=true, description="手机号"),
|
|
|
* @OA\Parameter(name="credent", in="query", @OA\Schema(type="string"), required=true, description="证件类型"),
|
|
|
* @OA\Parameter(name="idcard", in="query", @OA\Schema(type="string"), required=false, description="证件号码"),
|
|
|
* @OA\Parameter(name="company_name", in="query", @OA\Schema(type="string"), required=true, description="单位名称"),
|
|
|
* @OA\Parameter(name="type", in="query", @OA\Schema(type="string"), required=true, description="类型"),
|
|
|
* @OA\Parameter(name="need_meal", in="query", @OA\Schema(type="boolean"), required=false, description="是否需要用餐:0否1是"),
|
|
|
* @OA\Parameter(name="meal_type", in="query", @OA\Schema(type="integer"), required=false, description="用餐类型:1早餐2午餐3晚餐4全天"),
|
|
|
* @OA\Parameter(name="meal_count", in="query", @OA\Schema(type="integer"), required=false, description="用餐人数"),
|
|
|
* @OA\Parameter(name="meal_remark", in="query", @OA\Schema(type="string"), required=false, description="用餐备注"),
|
|
|
* @OA\Parameter(name="meal_preferences", in="query", @OA\Schema(type="array", @OA\Items(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 save()
|
|
|
{
|
|
|
$formRequest = app(VisitRequest::class);
|
|
|
DB::beginTransaction();
|
|
|
try {
|
|
|
$all = request()->all();
|
|
|
$validator = Validator::make($all, $formRequest->rules(), $formRequest->messages(), method_exists($formRequest, 'attributes') ? $formRequest->attributes() : []);
|
|
|
if ($validator->fails()) {
|
|
|
return $this->fail([ResponseCode::ERROR_PARAMETER, implode(',', $validator->errors()->all())]);
|
|
|
}
|
|
|
$data = $validator->validated();
|
|
|
|
|
|
// 检查黑名单
|
|
|
if (Config::isBlacklistCheckEnabled()) {
|
|
|
$blacklist = Blacklist::checkVisitor($data['mobile'], $data['idcard'] ?? null);
|
|
|
if ($blacklist && (!isset($all['id']) || $blacklist->id != $all['id'])) {
|
|
|
return $this->fail([ResponseCode::ERROR_BUSINESS, __('visit.blacklist_check_failed')]);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if (isset($all['id'])) {
|
|
|
// 更新
|
|
|
$visit = $this->model->find($all['id']);
|
|
|
if (!$visit) {
|
|
|
return $this->fail([ResponseCode::ERROR_BUSINESS, __('visit.not_found')]);
|
|
|
}
|
|
|
$visit->update($data);
|
|
|
VisitLog::log($visit->id, VisitLog::TYPE_UPDATE, __('visit.updated_successfully'), $this->getUserId());
|
|
|
$message = __('visit.updated_successfully');
|
|
|
} else {
|
|
|
// 新增
|
|
|
$data['code'] = Visit::generateCode();
|
|
|
$data['admin_id'] = $this->getUserId();
|
|
|
$data['department_id'] = $this->getUser()->department_id;
|
|
|
$data['audit_status'] = Config::isStudyRequired() ? Visit::AUDIT_STATUS_PENDING_STUDY : Visit::AUDIT_STATUS_PENDING;
|
|
|
$visit = $this->model->create($data);
|
|
|
VisitLog::logCreate($visit->id, __('visit.created_successfully'), $data['user_id'] ?? null);
|
|
|
$message = __('visit.created_successfully');
|
|
|
}
|
|
|
|
|
|
DB::commit();
|
|
|
return $this->success($visit);
|
|
|
} catch (\Exception $exception) {
|
|
|
DB::rollBack();
|
|
|
return $this->fail([$exception->getCode(), $exception->getMessage()]);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* @OA\Get(
|
|
|
* path="/api/admin/visits/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();
|
|
|
$visit = $this->model->find($all['id']);
|
|
|
if (!$visit) {
|
|
|
return $this->fail([ResponseCode::ERROR_BUSINESS, __('visit.not_found')]);
|
|
|
}
|
|
|
|
|
|
DB::beginTransaction();
|
|
|
try {
|
|
|
// 记录日志
|
|
|
VisitLog::log($visit->id, VisitLog::TYPE_CANCEL, __('visit.deleted_successfully'), $this->getUserId());
|
|
|
|
|
|
$visit->delete();
|
|
|
|
|
|
DB::commit();
|
|
|
return $this->success(__('visit.deleted_successfully'));
|
|
|
} catch (\Exception $exception) {
|
|
|
DB::rollBack();
|
|
|
return $this->fail([$exception->getCode(), $exception->getMessage()]);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 应用过滤条件
|
|
|
*/
|
|
|
private function applyFilters($query, $filter)
|
|
|
{
|
|
|
foreach ($filter as $key => $value) {
|
|
|
if (is_array($value)) {
|
|
|
$query->whereIn($key, $value);
|
|
|
} else {
|
|
|
$query->where($key, $value);
|
|
|
}
|
|
|
}
|
|
|
return $query;
|
|
|
}
|
|
|
} |