HasProcessTrait.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. include APPPATH.'/models/Model_process.php';
  3. trait HasProcessTrait
  4. {
  5. /**
  6. * @var Process
  7. */
  8. protected $currentProcess;
  9. /**
  10. * @param Flow $flow
  11. * @return array
  12. */
  13. private function processAttribute($flow,$order)
  14. {
  15. return [
  16. 'flow_id' => $flow['id'],
  17. 'order_id' =>$order['id'] ,
  18. 'state'=>Model_Process::STATUS_PROCESSING,
  19. 'created_at'=>time(),
  20. 'updated_at'=>time(),
  21. ];
  22. }
  23. /**
  24. * @param Flow $flow
  25. * @return Process
  26. * @throws \Exception
  27. */
  28. protected function createProcess($flow,$order)
  29. {
  30. $attr=$this->processAttribute($flow,$order);
  31. $process = new Model_Process();
  32. $processId=$process->insert($attr);
  33. if($processId){
  34. $process->read($processId);
  35. $process->initStatus($flow);
  36. return true;
  37. }
  38. return false;
  39. return $process;
  40. }
  41. /**
  42. * @return HasMany
  43. */
  44. public function process($orderid)
  45. {
  46. $process = new Model_Process();
  47. return $process->find('order_id='.$orderid);
  48. return $this->hasMany(Process::class, 'model_id')->where('model_type', $this->getMorphClass());
  49. }
  50. /**
  51. * 当前的流程
  52. * @return Process
  53. */
  54. public function currentProcess()
  55. {
  56. if (!isset($this->currentProcess)) {
  57. if ($this->process->isEmpty()) {
  58. return null;
  59. }
  60. $this->currentProcess = $this->process->where('state', 'processing')->first();
  61. }
  62. return $this->currentProcess;
  63. }
  64. }