OrderOperation.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Author: 芸众商城 www.yunzshop.com
  5. * Date: 2017/2/28
  6. * Time: 上午11:19
  7. * comment: 订单操作基类
  8. */
  9. namespace app\frontend\modules\order\services\behavior;
  10. use app\common\exceptions\AppException;
  11. use app\common\models\Order;
  12. abstract class OrderOperation extends Order
  13. {
  14. /**
  15. * @var Order
  16. */
  17. protected $order;
  18. /**
  19. * @var array 合法前置状态
  20. */
  21. protected $statusBeforeChange = [];
  22. /**
  23. * @var string 类名的过去式
  24. */
  25. protected $past_tense_class_name;
  26. /**
  27. * @var string 操作名
  28. */
  29. protected $name;
  30. /**
  31. * 获取不带命名空间的类名
  32. * @return mixed
  33. */
  34. private function _getOperationName()
  35. {
  36. $result = explode('\\', static::class);
  37. return end($result);
  38. }
  39. /**
  40. * @return string 类名的过去式
  41. */
  42. protected function _getPastTense()
  43. {
  44. return $this->past_tense_class_name;
  45. }
  46. /**
  47. * @return \app\common\events\order\CreatedOrderEvent
  48. */
  49. protected function getBeforeEvent()
  50. {
  51. $event_name = '\app\common\events\order\Before' . $this->_getOperationName() . 'Event';
  52. return new $event_name($this);
  53. }
  54. /**
  55. * 是否满足操作条件
  56. * @return bool
  57. * @throws AppException
  58. */
  59. private function check()
  60. {
  61. $event = $this->getBeforeEvent();
  62. event($event);
  63. if ($this->refund_id > 0) {
  64. if ($this->hasOneRefundApply->isRefunding()) {
  65. throw new AppException("退款中的订单,无法执行{$this->name}操作");
  66. }
  67. }
  68. if (!in_array($this->status, $this->statusBeforeChange)) {
  69. throw new AppException("ID:{$this->id}订单状态不满足{$this->name}操作");
  70. }
  71. return true;
  72. }
  73. /**
  74. * @throws AppException
  75. */
  76. public function handle(){
  77. $this->check();
  78. }
  79. protected function _fireEvent()
  80. {
  81. $event_name = '\app\common\events\order\After' . $this->_getPastTense() . 'Event';
  82. event(new $event_name($this));
  83. }
  84. }