Files
fdesk/app/common/model/ApprovalInstances.php
T

122 lines
2.8 KiB
PHP
Raw Normal View History

2025-09-09 17:30:39 +08:00
<?php
2025-09-18 06:46:15 +00:00
namespace app\common\model;
2025-09-09 17:30:39 +08:00
use app\common\model\StockOrders;
use app\common\model\User;
use support\Model;
class ApprovalInstances extends Model
{
/**
* The connection name for the model.
*
* @var string|null
*/
protected $connection = 'mysql';
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'approval_instances';
/**
* The primary key associated with the table.
*
* @var string
*/
protected $primaryKey = 'id';
/**
* Indicates if the model should be timestamped.
*
* @var bool
*/
public $timestamps = true;
2025-09-18 06:46:15 +00:00
// protected $dateFormat = 'U';
2025-09-09 17:30:39 +08:00
/**
* 允许自动填充模型字段
*/
// protected $fillable = ['id', 'flow_type', 'biz_id', 'biz_type', 'form_data', 'initiator_id', 'status', 'current_step', 'finished_at', 'deadline_at', 'created_at', 'updated_at'];
/**
* 不允许自动填充模型字段
*/
protected $guarded = [];
protected $fillable = [
'flow_type',
'biz_id',
'biz_type',
'form_data',
'initiator_id',
'status',
'current_step',
'finished_at',
'deadline_at',
2025-09-24 11:18:59 +08:00
'round'
2025-09-09 17:30:39 +08:00
];
protected $casts = [
'form_data' => 'array', // 自动转换 JSON 字段为数组
'finished_at' => 'datetime',
'deadline_at' => 'datetime',
'created_at' => 'datetime',
'updated_at' => 'datetime',
];
2025-09-19 07:43:48 +00:00
// ====== ====== ====== 映射业务表 ====== ====== ======
protected const BIZ_CLASS = [
2025-09-23 17:26:01 +08:00
'stock_orders' => \app\common\model\StockOrders::class,
2025-10-11 08:41:11 +00:00
'stock_orders_nullify' => \app\common\model\StockOrders::class,
2025-09-23 17:26:01 +08:00
'purchase_orders' => \app\common\model\PurchaseOrders::class,
2025-09-19 07:43:48 +00:00
];
public function getBizTypeAttribute($value)
{
return self::BIZ_CLASS[$value] ?? $value;
}
public function bizInfo()
{
2025-10-11 08:41:11 +00:00
return $this->morphTo('bizInfo', 'biz_type', 'biz_id')->withTrashed();
2025-09-19 07:43:48 +00:00
}
// ====== ====== ====== 映射表业务 ====== ====== ======
2025-09-18 06:46:15 +00:00
public function records()
2025-09-09 17:30:39 +08:00
{
2025-09-18 06:46:15 +00:00
return $this->hasMany(ApprovalRecords::class, 'instance_id', 'id');
2025-09-09 17:30:39 +08:00
}
public function flows()
{
return $this->belongsTo(ApprovalFlows::class, 'flow_type', 'type');
}
public function initiator()
{
return $this->belongsTo(User::class, 'initiator_id', 'id');
}
2025-09-19 07:43:48 +00:00
// public function bizInfo2()
// {
// return $this->belongsTo(StockOrders::class, 'biz_id', 'id');
// }
2025-09-18 06:46:15 +00:00
/**
* 获取当前步骤的所有审批记录(pending 状态)
*/
public function currentStepRecords()
{
2025-09-19 07:43:48 +00:00
return $this->approvalRecords()->where('step_index', $this->current_step)->where('status', 'pending');
2025-09-09 17:30:39 +08:00
}
}