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.

270 lines
6.9 KiB

<?php
namespace App\Models;
class GateLog extends SoftDeletesModel
{
protected $table = 'gate_logs';
protected $casts = [
'person_no' => 'array',
'car_no' => 'array',
'created_at' => 'datetime',
'updated_at' => 'datetime',
];
// 扫码类型常量
const TYPE_ENTER = 'enter'; // 进厂
const TYPE_LEAVE = 'leave'; // 离厂
const TYPE_CHECK = 'check'; // 检查
/**
* 关联管理员
*/
public function admin()
{
return $this->belongsTo(Admin::class, 'admin_id');
}
/**
* 关联访问申请
*/
public function visit()
{
return $this->belongsTo(Visit::class, 'code', 'code');
}
/**
* 记录门岗扫码
*/
public static function logScan($code, $type, $remark = null, $adminId = null, $personNo = null, $carNo = null)
{
return self::create([
'code' => $code,
'admin_id' => $adminId,
'remark' => $remark,
'person_no' => $personNo,
'car_no' => $carNo,
'created_at' => now(),
'updated_at' => now(),
]);
}
/**
* 记录进厂扫码
*/
public static function logEnter($code, $remark = null, $adminId = null, $personNo = null, $carNo = null)
{
return self::logScan($code, self::TYPE_ENTER, $remark, $adminId, $personNo, $carNo);
}
/**
* 记录离厂扫码
*/
public static function logLeave($code, $remark = null, $adminId = null, $personNo = null, $carNo = null)
{
return self::logScan($code, self::TYPE_LEAVE, $remark, $adminId, $personNo, $carNo);
}
/**
* 记录检查扫码
*/
public static function logCheck($code, $remark = null, $adminId = null, $personNo = null, $carNo = null)
{
return self::logScan($code, self::TYPE_CHECK, $remark, $adminId, $personNo, $carNo);
}
/**
* 获取扫码类型文本
*/
public function getTypeTextAttribute()
{
$types = [
self::TYPE_ENTER => '进厂',
self::TYPE_LEAVE => '离厂',
self::TYPE_CHECK => '检查',
];
return $types[$this->remark] ?? '未知';
}
/**
* 获取操作人信息
*/
public function getOperatorAttribute()
{
return $this->admin ? $this->admin->name : '系统';
}
/**
* 获取人牌信息
*/
public function getPersonNosAttribute()
{
if (!$this->person_no) {
return [];
}
return is_array($this->person_no) ? $this->person_no : [$this->person_no];
}
/**
* 获取车牌信息
*/
public function getCarNosAttribute()
{
if (!$this->car_no) {
return [];
}
return is_array($this->car_no) ? $this->car_no : [$this->car_no];
}
/**
* 检查二维码是否有效
*/
public function isValidCode()
{
return $this->visit && $this->visit->audit_status == Visit::AUDIT_STATUS_APPROVED;
}
/**
* 获取门岗记录统计
*/
public static function getStatistics($startDate = null, $endDate = null)
{
$query = self::query();
if ($startDate) {
$query->where('created_at', '>=', $startDate);
}
if ($endDate) {
$query->where('created_at', '<=', $endDate);
}
$total = $query->count();
$today = self::whereDate('created_at', today())->count();
$thisWeek = self::whereBetween('created_at', [
now()->startOfWeek(),
now()->endOfWeek()
])->count();
$thisMonth = self::whereMonth('created_at', now()->month)->count();
// 按类型统计
$enterCount = self::where('remark', self::TYPE_ENTER)->count();
$leaveCount = self::where('remark', self::TYPE_LEAVE)->count();
$checkCount = self::where('remark', self::TYPE_CHECK)->count();
return [
'total' => $total,
'today' => $today,
'this_week' => $thisWeek,
'this_month' => $thisMonth,
'enter_count' => $enterCount,
'leave_count' => $leaveCount,
'check_count' => $checkCount,
];
}
/**
* 获取访问申请的门岗记录
*/
public static function getVisitGateLogs($visitId)
{
$visit = Visit::find($visitId);
if (!$visit || !$visit->code) {
return collect();
}
return self::where('code', $visit->code)
->with(['admin'])
->orderBy('created_at', 'desc')
->get();
}
/**
* 获取今日门岗记录
*/
public static function getTodayLogs()
{
return self::whereDate('created_at', today())
->with(['admin', 'visit'])
->orderBy('created_at', 'desc')
->get();
}
/**
* 获取未离厂的记录
*/
public static function getNotLeftLogs()
{
$enterLogs = self::where('remark', self::TYPE_ENTER)
->whereDate('created_at', today())
->get();
$leaveLogs = self::where('remark', self::TYPE_LEAVE)
->whereDate('created_at', today())
->pluck('code')
->toArray();
return $enterLogs->filter(function ($log) use ($leaveLogs) {
return !in_array($log->code, $leaveLogs);
});
}
/**
* 检查访问申请是否已进厂
*/
public static function hasEntered($code)
{
return self::where('code', $code)
->where('remark', self::TYPE_ENTER)
->whereDate('created_at', today())
->exists();
}
/**
* 检查访问申请是否已离厂
*/
public static function hasLeft($code)
{
return self::where('code', $code)
->where('remark', self::TYPE_LEAVE)
->whereDate('created_at', today())
->exists();
}
/**
* 获取门岗记录详情
*/
public function getDetails()
{
return [
'id' => $this->id,
'code' => $this->code,
'type' => $this->getTypeTextAttribute(),
'operator' => $this->getOperatorAttribute(),
'remark' => $this->remark,
'person_nos' => $this->getPersonNosAttribute(),
'car_nos' => $this->getCarNosAttribute(),
'created_at' => $this->created_at->format('Y-m-d H:i:s'),
'visit_info' => $this->visit ? [
'id' => $this->visit->id,
'name' => $this->visit->name,
'mobile' => $this->visit->mobile,
'company_name' => $this->visit->company_name,
'type' => $this->visit->getTypeTextAttribute(),
] : null,
];
}
/**
* 清理过期记录
*/
public static function cleanExpiredLogs($days = 90)
{
$expiredDate = now()->subDays($days);
return self::where('created_at', '<', $expiredDate)->delete();
}
}