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.

238 lines
13 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\Models\Admin;
use App\Models\Visit;
use App\Models\VisitLog;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
use App\Helpers\ResponseCode;
use Rap2hpoutre\FastExcel\FastExcel;
/**
* 拜访记录
*/
class VisitController extends CommonController
{
/**
* @OA\Get(
* path="/api/admin/visit/index",
* tags={"拜访记录"},
* summary="列表",
* description="",
* @OA\Parameter(name="keyword", in="query", @OA\Schema(type="string"), required=false, description="关键词"),
* @OA\Parameter(name="audit_status", in="query", @OA\Schema(type="string"), required=false, description="审核状态-1待学习0待审核1通过(待进厂)2驳回3已进厂4已离厂"),
* @OA\Parameter(name="start_date", in="query", @OA\Schema(type="string"), required=false, description="开始日期"),
* @OA\Parameter(name="end_date", 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="is_export", in="query", @OA\Schema(type="string"), required=false, description="是否导出0否1是默认0"),
* @OA\Parameter(name="my_self", in="query", @OA\Schema(type="string"), required=false, description="是否显示自己创建的0否1是默认0"),
* @OA\Parameter(name="my_audit", in="query", @OA\Schema(type="string"), required=false, description="是否显示我审核的记录0否1是默认0"),
* @OA\Parameter(name="my_accept_admin", in="query", @OA\Schema(type="string"), required=false, description="是否显示接待人员是自己的0否1是默认0"),
* @OA\Parameter(name="long_time", in="query", @OA\Schema(type="string"), required=false, description="是否长期访客0否1是"),
* @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 = Visit::with('accompany.department','gateLogs', 'visitTime', 'admin', 'visitArea', 'acceptAdmin.department', 'acceptAdminSignFile', 'acceptGoodsAdmin.department', 'audit.auditAdmin')->where(function ($query) use ($all) {
if (isset($all['keyword'])) {
$query->where('name', 'like', '%' . $all['keyword'] . '%');
}
if (isset($all['audit_status'])) {
$query->where('audit_status', $all['audit_status']);
}
if (isset($all['start_date']) && isset($all['end_date'])) {
$query->whereBetween('date', [$all['start_date'], $all['end_date']]);
}
if (isset($all['my_self']) && !empty($all['my_self'])) {
$query->where('admin_id', $this->getUserId());
}
if (isset($all['my_accept_admin']) && !empty($all['my_accept_admin'])) {
$query->where('accept_admin_id', $this->getUserId());
}
if (isset($all['long_time']) && !empty($all['long_time'])) {
$query->where('long_time', $all['long_time']);
}
if (isset($all['my_audit']) && !empty($all['my_audit'])) {
$query->whereHas('audit', function ($q) {
$q->where('audit_admin_id', $this->getUserId());
});
}
// 权限设置
// $user = $this->getUser();
// $adminIds = Admin::roleAllowAdminIds($user, $departmentIds);
// $query->where(function ($qry) use ($adminIds, $departmentIds, $user) {
// $qry->whereIn('accpet_department_id', $departmentIds)->orWhere(function ($qry) use ($adminIds) {
// $qry->whereIn('accept_admin_id', $adminIds);
// });
// });
})->orderBy($all['sort_name'] ?? 'id', $all['sort_type'] ?? 'desc');
if (isset($all['is_export']) && !empty($all['is_export'])) {
return (new FastExcel($list->limit(5000)->get()->toArray()))->download('访问记录' . date('YmdHis') . '.csv', function ($info) {
return [
'姓名' => $info['name'],
'类型' => $info['type_text'],
'审核状态' => $info['audit_status_text'],
'是否随访' => empty($info['follw_people']) ? '否' : '是',
'预约时间' => $info['date'],
'证件号' => $info['idcard'],
'证件类型' => $info['credent'] == 1 ? '身份证' : '护照',
'单位名称' => $info['company_name'],
'手机号' => $info['mobile'],
'访问时间' => ($info['visit_time']['start_time']) ?? '',
'结束时间' => ($info['visit_time']['end_time']) ?? '',
'创建人' => $info['admin']['name'],
'创建时间' => $info['created_at'],
];
});
} else {
$list = $list->paginate($all['page_size'] ?? 20);
}
return $this->success($list);
}
/**
* @OA\Get(
* path="/api/admin/visit/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 = Visit::with('accompany.department','logs.admin', 'logs.user', 'audit.auditAdmin', 'visitTime', 'acceptAdmin.department', 'acceptAdminSignFile', 'acceptGoodsAdmin.department', 'visitArea', 'audit.auditAdmin')->find($all['id']);
return $this->success($detail);
}
/**
* @OA\Post(
* path="/api/admin/visit/save",
* tags={"拜访记录"},
* summary="更新",
* description="",
* @OA\Parameter(name="id", in="query", @OA\Schema(type="int"), required=true, description="Id(存在更新,不存在新增)"),
* @OA\Parameter(name="admin_id", in="query", @OA\Schema(type="string"), required=false, description="不用填"),
* @OA\Parameter(name="department_id", in="query", @OA\Schema(type="string"), required=false, description="不用填"),
* @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=false, description="时间段id"),
* @OA\Parameter(name="visit_area_id", in="query", @OA\Schema(type="string"), required=false, description="区域id"),
* @OA\Parameter(name="reason", in="query", @OA\Schema(type="string"), required=false, description="事由"),
* @OA\Parameter(name="remark", in="query", @OA\Schema(type="string"), required=false, description="备注"),
* @OA\Parameter(name="name", in="query", @OA\Schema(type="string"), required=false, description="名字"),
* @OA\Parameter(name="mobile", in="query", @OA\Schema(type="string"), required=false, description="电话"),
* @OA\Parameter(name="credent", in="query", @OA\Schema(type="string"), required=false, description="证件类型1身份证2护照"),
* @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=false, description="单位名称"),
* @OA\Parameter(name="follw_people", in="query", @OA\Schema(type="string"), required=false, description="随访人员"),
* @OA\Parameter(name="cars", in="query", @OA\Schema(type="string"), required=false, description="到访车辆"),
* @OA\Parameter(name="start_date", in="query", @OA\Schema(type="string"), required=false, description="开始时间"),
* @OA\Parameter(name="end_date", in="query", @OA\Schema(type="string"), required=false, description="结束时间"),
* @OA\Parameter(name="accpet_department_id", in="query", @OA\Schema(type="string"), required=false, description="接待部门"),
* @OA\Parameter(name="accept_admin_id", in="query", @OA\Schema(type="string"), required=false, description="接待人员"),
* @OA\Parameter(name="plate", in="query", @OA\Schema(type="string"), required=false, description="车牌号"),
* @OA\Parameter(name="type", in="query", @OA\Schema(type="string"), required=false, description="类型1访客2施工3车辆"),
* @OA\Parameter(name="long_time", in="query", @OA\Schema(type="string"), required=false, description="是否长期0否1是"),
* @OA\Parameter(name="accept_admin_sign", in="query", @OA\Schema(type="string"), required=false, description="被访人签字图片id"),
* @OA\Parameter(name="file", in="query", @OA\Schema(type="string"), required=false, description="附件数组"),
* @OA\Parameter(name="accept_goods_admin_id", in="query", @OA\Schema(type="string"), required=false, description="收货人id"),
* @OA\Parameter(name="work_start_time", in="query", @OA\Schema(type="string"), required=false, description="施工开始时间"),
* @OA\Parameter(name="work_end_time", 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 save()
{
$all = \request()->all();
$messages = [
'name.required' => '名称必填'
];
$validator = Validator::make($all, [
'name' => 'required'
], $messages);
if ($validator->fails()) {
return $this->fail([ResponseCode::ERROR_PARAMETER, implode(',', $validator->errors()->all())]);
}
DB::beginTransaction();
try {
if (isset($all['id'])) {
$model = Visit::find($all['id']);
} else {
$model = new Visit();
$all['admin_id'] = $this->getUserId();
$all['department_id'] = $this->getUser()->department_id;
$all['code'] = randStr(6, true);
}
$model->fill($all);
$model->save();
VisitLog::add($this->getUser(), '', $model->id, isset($all['id']) ? '更新拜访记录' : '新增拜访记录');
DB::commit();
return $this->success('更新成功');
} catch (\Exception $exception) {
DB::rollBack();
return $this->fail([$exception->getCode(), $exception->getMessage()]);
}
}
/**
* @OA\Get(
* path="/api/admin/visit/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())]);
}
Visit::where('id', $all['id'])->delete();
return $this->success('删除成功');
}
}