Flow.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: shenyang
  5. * Date: 2018/6/6
  6. * Time: 下午4:11
  7. */
  8. namespace app\common\models;
  9. use Illuminate\Database\Eloquent\Collection;
  10. /**
  11. * 流程类型
  12. * Class Flow
  13. * @inheritdoc
  14. * @package app\common\models\statusFlow
  15. * @property Collection process
  16. * @property int id
  17. * @property string name
  18. * @property string code
  19. * @property Collection allStatus
  20. */
  21. class Flow extends BaseModel
  22. {
  23. public $table = 'yz_flow';
  24. protected $guarded = ['id'];
  25. /**
  26. * 包含的状态类型
  27. * @return \Illuminate\Database\Eloquent\Relations\HasMany
  28. */
  29. public function allStatus()
  30. {
  31. return $this->hasMany(Status::class,'flow_id')->orderBy('order');
  32. }
  33. /**
  34. * 此状态的下一个状态
  35. * @param Status $status
  36. * @return mixed
  37. */
  38. public function getNextStatus(Status $status)
  39. {
  40. $flowStatus = $this->allStatus->where('id',$status->id)->first();
  41. return $this->allStatus->where('order', '>', $flowStatus->order)->sortBy('order')->first();
  42. }
  43. public function getCloseStatus()
  44. {
  45. return $this->allStatus->where('order', Status::ORDER_CLOSE)->first();
  46. }
  47. public function getCancelStatus()
  48. {
  49. return $this->allStatus->where('order', Status::ORDER_CANCEL)->first();
  50. }
  51. /**
  52. * 获取初始状态
  53. * @return mixed
  54. */
  55. public function getFirstStatus()
  56. {
  57. return $this->allStatus->where('order','>=',0)->sortBy('order')->first();
  58. }
  59. /**
  60. * 获取最终状态
  61. * @return mixed
  62. */
  63. public function getFinalStatus()
  64. {
  65. return $this->allStatus->where('order','>=',0)->sortByDesc('order')->first();
  66. }
  67. /**
  68. * 添加一组状态
  69. * @param $statusCollection
  70. */
  71. public function pushManyStatus($statusCollection)
  72. {
  73. $statusCollection = collect($statusCollection)->map(function ($status) {
  74. return new Status($status);
  75. });
  76. $this->allStatus()->saveMany($statusCollection);
  77. }
  78. public function setManyStatus($statusCollection)
  79. {
  80. $statusCollection = collect($statusCollection)->map(function ($status) {
  81. return new Status($status);
  82. });
  83. $this->allStatus()->saveMany($statusCollection);
  84. }
  85. public function process()
  86. {
  87. return $this->hasMany(Process::class, 'flow_id');
  88. }
  89. }