BackendOrderOperationsManager.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2021/2/19
  6. * Time: 16:27
  7. */
  8. namespace app\backend\modules\order\services;
  9. use app\backend\modules\order\common\type\OrderTypeFactory;
  10. use app\common\models\Order;
  11. use app\frontend\modules\order\operations\OrderOperationInterface;
  12. class BackendOrderOperationsManager
  13. {
  14. protected $order;
  15. public function __construct(Order $order)
  16. {
  17. $this->order = $order;
  18. }
  19. protected function getOrder()
  20. {
  21. return $this->order;
  22. }
  23. /**
  24. * @return array
  25. * @throws \app\common\exceptions\AppException
  26. */
  27. public function getOperations()
  28. {
  29. $operationsSettings = $this->getCurrentOperations();
  30. $operations = array_map(function ($operationName) {
  31. /**
  32. * @var OrderOperationInterface $operation
  33. */
  34. $operation = new $operationName($this->getOrder());
  35. if (!$operation->enable()) {
  36. return null;
  37. }
  38. $result['name'] = $operation->getName();
  39. $result['value'] = $operation->getValue();
  40. $result['api'] = $operation->getApi();
  41. $result['type'] = $operation->getType();
  42. return $result;
  43. }, $operationsSettings);
  44. $operations = array_filter($operations);
  45. return array_values($operations) ?: [];
  46. }
  47. protected function getCurrentOperations()
  48. {
  49. $method_name = $this->getStatusMethod($this->getOrder()->status);
  50. return $this->$method_name();
  51. }
  52. protected function getOperationsSetting()
  53. {
  54. $settings = $this->getBasicOperations();
  55. return $settings[$this->getStatusCode($this->getOrder()->status)];
  56. }
  57. //0 待支付
  58. protected function waitPayOperations()
  59. {
  60. return [
  61. \app\backend\modules\order\operations\Pay::class,
  62. ];
  63. }
  64. //1 待发货
  65. protected function waitSendOperations()
  66. {
  67. return [
  68. \app\backend\modules\order\operations\Send::class,
  69. \app\backend\modules\order\operations\SeparateSend::class,
  70. ];
  71. }
  72. //2 待收货
  73. protected function waitReceiveOperations()
  74. {
  75. return [
  76. \app\backend\modules\order\operations\SeparateSend::class,
  77. \app\backend\modules\order\operations\Receive::class,
  78. \app\backend\modules\order\operations\CancelSend::class,
  79. ];
  80. }
  81. //3 已完成
  82. protected function completeOperations()
  83. {
  84. return [];
  85. }
  86. // -1 已关闭
  87. protected function closeOperations()
  88. {
  89. return [];
  90. }
  91. protected function getStatusMethod($status)
  92. {
  93. $methodName = [
  94. 0 => 'waitPayOperations',
  95. 1 => 'waitSendOperations',
  96. 2 => 'waitReceiveOperations',
  97. 3 => 'completeOperations',
  98. -1 => 'closeOperations'
  99. ];
  100. return $methodName[$status];
  101. }
  102. protected function getStatusCode($status)
  103. {
  104. $defaults = [0 => 'waitPay', 1 => 'waitSend', 2 => 'waitReceive', 3 => 'complete', -1 => 'close'];
  105. return $defaults[$status];
  106. }
  107. protected function getBasicOperations()
  108. {
  109. return [
  110. 'waitPay' => [
  111. \app\backend\modules\order\operations\Pay::class,
  112. ],
  113. 'waitSend' => [
  114. \app\backend\modules\order\operations\Send::class,
  115. \app\backend\modules\order\operations\SeparateSend::class,
  116. ],
  117. 'waitReceive' => [
  118. \app\backend\modules\order\operations\SeparateSend::class,
  119. \app\backend\modules\order\operations\Receive::class,
  120. \app\backend\modules\order\operations\CancelSend::class,
  121. ],
  122. 'complete' => [],
  123. 'close' => [],
  124. ];
  125. }
  126. }