'array', 'cars' => 'array', 'file' => 'array', 'vehicle_images' => 'array', 'meal_preferences' => 'array', 'date' => 'date', 'start_date' => 'date', 'end_date' => 'date', 'work_start_time' => 'date', 'work_end_time' => 'date', 'credent' => 'boolean', 'long_time' => 'boolean', 'audit_status' => 'integer', 'need_meal' => 'boolean', ]; // 访问类型常量 const TYPE_VISITOR = 1; // 访客 const TYPE_VISITOR_CAR = 2; // 访客车辆 const TYPE_LOGISTICS_CAR = 3; // 物流车辆 // 审核状态常量 const AUDIT_STATUS_PENDING_STUDY = -1; // 待学习 const AUDIT_STATUS_PENDING = 0; // 待审核 const AUDIT_STATUS_APPROVED = 1; // 通过(待进厂) const AUDIT_STATUS_REJECTED = 2; // 驳回 const AUDIT_STATUS_ENTERED = 3; // 已进厂 const AUDIT_STATUS_LEFT = 4; // 已离厂 // 证件类型常量 const CREDENT_ID_CARD = 1; // 身份证 const CREDENT_PASSPORT = 2; // 护照 // 用餐类型常量 const MEAL_TYPE_BREAKFAST = 1; // 早餐 const MEAL_TYPE_LUNCH = 2; // 午餐 const MEAL_TYPE_DINNER = 3; // 晚餐 const MEAL_TYPE_ALL_DAY = 4; // 全天 /** * 关联访问区域 */ public function visitArea() { return $this->belongsTo(VisitArea::class, 'visit_area_id'); } /** * 关联访问时段 */ public function visitTime() { return $this->belongsTo(VisitTime::class, 'visit_time_id'); } /** * 关联接待部门 */ public function acceptDepartment() { return $this->belongsTo(Department::class, 'accpet_department_id'); } public function department() { return $this->belongsTo(Department::class, 'department_id'); } /** * 关联接待人员 */ public function acceptAdmin() { return $this->belongsTo(Admin::class, 'accept_admin_id'); } public function admin() { return $this->belongsTo(Admin::class, 'admin_id'); } /** * 关联收货人 */ public function acceptGoodsAdmin() { return $this->belongsTo(Admin::class, 'accept_goods_admin_id'); } /** * 关联陪同人 */ public function accompany() { return $this->belongsTo(Admin::class, 'accompany_id'); } /** * 关联用户 */ public function user() { return $this->belongsTo(User::class, 'user_id'); } /** * 关联审核记录 */ public function visitAudits() { return $this->hasMany(VisitAudit::class, 'visit_id'); } /** * 关联访问日志 */ public function logs() { return $this->hasMany(VisitLog::class, 'visit_id'); } /** * 关联签字图片(单文件) */ public function acceptAdminSign() { return $this->belongsTo(Upload::class, 'accept_admin_sign'); } /** * 获取附件文件列表(多文件) */ public function getFilesAttribute() { if (!$this->attributes['file']) { return collect([]); } $files = json_decode($this->attributes['file'], true); if (!$files) { return collect([]); } return Upload::whereIn('id', $files)->get(); } /** * 获取货车图片列表(多文件) */ public function getVehicleImagesAttribute() { if (!$this->attributes['vehicle_images']) { return collect([]); } $vehicleImages = json_decode($this->attributes['vehicle_images'], true); if (!$vehicleImages) { return collect([]); } return Upload::whereIn('id', $vehicleImages)->get(); } /** * 获取访问类型文本 */ public function getTypeTextAttribute() { $types = [ self::TYPE_VISITOR => '访客', self::TYPE_VISITOR_CAR => '访客车辆', self::TYPE_LOGISTICS_CAR => '物流车辆', ]; return $types[$this->type] ?? '未知'; } /** * 获取审核状态文本 */ public function getAuditStatusTextAttribute() { $statuses = [ self::AUDIT_STATUS_PENDING_STUDY => '待学习', self::AUDIT_STATUS_PENDING => '待审核', self::AUDIT_STATUS_APPROVED => '通过(待进厂)', self::AUDIT_STATUS_REJECTED => '驳回', self::AUDIT_STATUS_ENTERED => '已进厂', self::AUDIT_STATUS_LEFT => '已离厂', ]; return $statuses[$this->audit_status] ?? '未知'; } /** * 获取证件类型文本 */ public function getCredentTextAttribute() { $credents = [ self::CREDENT_ID_CARD => '身份证', self::CREDENT_PASSPORT => '护照', ]; return $credents[$this->credent] ?? '未知'; } /** * 获取用餐类型文本 */ public function getMealTypeTextAttribute() { $mealTypes = [ self::MEAL_TYPE_BREAKFAST => '早餐', self::MEAL_TYPE_LUNCH => '午餐', self::MEAL_TYPE_DINNER => '晚餐', self::MEAL_TYPE_ALL_DAY => '全天', ]; return $mealTypes[$this->meal_type] ?? '未知'; } /** * 检查是否在黑名单中 */ public function isInBlacklist() { return Blacklist::where('mobile', $this->mobile) ->orWhere('idcard', $this->idcard) ->where('status', 1) ->where(function ($query) { $query->whereNull('start_date') ->orWhere('start_date', '<=', now()); }) ->where(function ($query) { $query->whereNull('end_date') ->orWhere('end_date', '>=', now()); }) ->exists(); } /** * 生成访问编号 */ public static function generateCode() { $prefix = 'V' . date('Ymd'); $lastVisit = self::where('code', 'like', $prefix . '%') ->orderBy('code', 'desc') ->first(); if ($lastVisit) { $lastNumber = intval(substr($lastVisit->code, -4)); $newNumber = $lastNumber + 1; } else { $newNumber = 1; } return $prefix . str_pad($newNumber, 4, '0', STR_PAD_LEFT); } }