morphToMany( Flow::class, 'model', (new Process())->getTable(), 'model_id', 'flow_id' )->withTimestamps(); } /** * @param Flow $flow * @return array */ private function processAttribute(Flow $flow) { return ['uid' => \YunShop::app()->getMemberId(), 'flow_id' => $flow->id, 'model_id' => $this->id, 'model_type' => $this->getMorphClass(), 'uniacid' => \YunShop::app()->uniacid ]; } /** * @param Flow $flow * @throws \Exception */ public function addProcess(Flow $flow) { if ($this->currentProcess()->code == $flow->code) { throw new AppException("已存在未完成的{$this->currentProcess()->name}流程,无法继续添加"); } $this->currentProcess = $this->createProcess($flow); $this->currentProcess; } /** * @param Flow $flow * @return Process * @throws \Exception */ protected function createProcess(Flow $flow) { $process = new Process($this->processAttribute($flow)); $process->initStatus(); return $process; } /** * @return HasMany */ public function process() { return $this->hasMany(Process::class, 'model_id')->where('model_type', $this->getMorphClass()); } /** * 当前的流程 * @return Process */ public function currentProcess() { if (!isset($this->currentProcess)) { if ($this->process->isEmpty()) { return null; } $this->currentProcess = $this->process->where('state', 'processing')->first(); } return $this->currentProcess; } }