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.

210 lines
7.4 KiB

11 months ago
<?php
namespace App\Http\Controllers\Admin;
use App\Models\VisitLog;
use App\Models\Visit;
use App\Helpers\ResponseCode;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
/**
* @OA\Tag(
* name="拜访日志管理",
* description="拜访日志相关接口"
* )
*/
class VisitLogController extends BaseController
{
public function __construct()
{
parent::__construct(new VisitLog());
}
/**
* @OA\Get(
* path="/api/admin/visit-logs/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/visit-logs/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(['visit', 'admin'])->find($all['id']);
return $this->success($detail);
}
11 months ago
/**
* @OA\Post(
* path="/api/admin/visit-logs/save",
* tags={"拜访日志管理"},
* summary="保存拜访日志",
* description="创建或更新拜访日志",
* @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"),
* @OA\RequestBody(
* required=true,
* @OA\JsonContent(
* @OA\Property(property="id", type="integer", description="拜访日志ID更新时必填"),
* @OA\Property(property="visit_id", type="integer", description="拜访ID"),
* @OA\Property(property="remark", type="string", description="备注"),
* @OA\Property(property="type", type="integer", description="日志类型1进入2离开3取消")
* )
* ),
* @OA\Response(
* response="200",
* description="成功保存拜访日志"
* )
* )
*/
public function save()
{
$all = request()->all();
// 验证必填字段
if (empty($all['visit_id'])) {
return $this->fail([ResponseCode::ERROR_PARAMETER, __('visit_log.visit_id_required')]);
}
if (empty($all['type'])) {
return $this->fail([ResponseCode::ERROR_PARAMETER, __('visit_log.type_required')]);
}
DB::beginTransaction();
try {
if (!empty($all['id'])) {
// 更新
$visitLog = $this->model->find($all['id']);
if (!$visitLog) {
return $this->fail([ResponseCode::ERROR_BUSINESS, __('visit_log.not_found')]);
}
$visitLog->fill($all);
$visitLog->save();
} else {
// 创建
$all['admin_id'] = auth('admin')->id();
$all['department_id'] = auth('admin')->user()->department_id;
$visitLog = $this->model->create($all);
}
DB::commit();
return $this->success($visitLog);
} catch (\Exception $e) {
DB::rollBack();
return $this->fail([ResponseCode::ERROR_INSIDE, __('visit_log.save_failed') . '' . $e->getMessage()]);
}
}
/**
* @OA\Get(
* path="/api/admin/visit-logs/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();
if (empty($all['id'])) {
return $this->fail([ResponseCode::ERROR_PARAMETER, __('visit_log.id_required')]);
}
DB::beginTransaction();
try {
$visitLog = $this->model->find($all['id']);
if (!$visitLog) {
return $this->fail([ResponseCode::ERROR_BUSINESS, __('visit_log.not_found')]);
}
$visitLog->delete();
DB::commit();
return $this->success(__('visit_log.delete_success'));
} catch (\Exception $e) {
DB::rollBack();
return $this->fail([ResponseCode::ERROR_INSIDE, __('visit_log.delete_failed') . '' . $e->getMessage()]);
}
}
11 months ago
/**
* 应用过滤条件
*/
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;
}
}