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.

101 lines
2.2 KiB

<?php
namespace App\Models;
class VisitAudit extends SoftDeletesModel
{
protected $table = 'visit_audits';
protected $casts = [
'status' => 'integer',
'level' => 'boolean',
];
// 审核状态常量
const STATUS_PENDING = 0; // 待审核
const STATUS_APPROVED = 1; // 通过
const STATUS_REJECTED = 2; // 驳回
/**
* 关联访问申请
*/
public function visit()
{
return $this->belongsTo(Visit::class, 'visit_id');
}
/**
* 关联审核人员
*/
public function auditAdmin()
{
return $this->belongsTo(Admin::class, 'audit_admin_id');
}
/**
* 获取审核状态文本
*/
public function getStatusTextAttribute()
{
$statuses = [
self::STATUS_PENDING => '待审核',
self::STATUS_APPROVED => '通过',
self::STATUS_REJECTED => '驳回',
];
return $statuses[$this->status] ?? '未知';
}
/**
* 获取审核等级文本
*/
public function getLevelTextAttribute()
{
return $this->level ? '高级审核' : '普通审核';
}
/**
* 检查是否可以审核
*/
public function canAudit($adminId)
{
// 检查审核权限
if ($this->audit_admin_id != $adminId) {
return false;
}
// 检查状态
if ($this->status != self::STATUS_PENDING) {
return false;
}
return true;
}
/**
* 执行审核
*/
public function performAudit($status, $reason = null, $adminId = null)
{
if (!$this->canAudit($adminId)) {
throw new \Exception('无权限审核或状态不允许');
}
$this->status = $status;
$this->reason = $reason;
$this->save();
// 更新访问申请状态
$visit = $this->visit;
if ($visit) {
if ($status == self::STATUS_APPROVED) {
$visit->audit_status = Visit::AUDIT_STATUS_APPROVED;
} elseif ($status == self::STATUS_REJECTED) {
$visit->audit_status = Visit::AUDIT_STATUS_REJECTED;
}
$visit->save();
}
return $this;
}
}