ShippingValidateEvent.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. namespace common\events\order;
  3. use common\components\BaseEvent;
  4. use common\enums\EventNameEnum;
  5. class ShippingValidateEvent extends BaseEvent
  6. {
  7. /**
  8. * 触发事件名称(不可修改)
  9. *
  10. * @var string
  11. */
  12. public $name = EventNameEnum::SHIPPING_VALIDATE;
  13. /**
  14. * 不允许发货的订单id["id" => '原因']
  15. *
  16. * @var array $notAllowedShippingMap
  17. */
  18. private $notAllowedShippingMap = [];
  19. /**
  20. * 需要验证的订单id
  21. *
  22. * @var array $orderIds
  23. */
  24. private $orderIds = [];
  25. public function __construct(int $mall_id)
  26. {
  27. $this->mall_id = $mall_id;
  28. }
  29. /**
  30. * 获取待检查的订单id
  31. *
  32. * @param bool $excludeCheckedIds 如果订单id已经不允许发货的,下一个监听者是否还返回此订单id
  33. *
  34. * @return array
  35. */
  36. public function getOrderIds(bool $excludeCheckedIds = true): array
  37. {
  38. if (!$excludeCheckedIds) {
  39. return $this->orderIds;
  40. }
  41. return array_diff($this->orderIds, array_keys($this->notAllowedShippingMap));
  42. }
  43. /**
  44. * @param array $orderIds
  45. *
  46. * @return void
  47. */
  48. public function setOrderIds(array $orderIds)
  49. {
  50. $this->orderIds = $orderIds;
  51. }
  52. /**
  53. * @param int $orderId
  54. * @param string $reason
  55. *
  56. * @return void
  57. */
  58. public function setNotAllow(int $orderId, string $reason)
  59. {
  60. if (isset($this->notAllowedShippingMap[$orderId])) {
  61. return;
  62. }
  63. $this->notAllowedShippingMap[$orderId] = $reason;
  64. }
  65. /**
  66. * 返回是否允许
  67. *
  68. * @param int $orderId
  69. *
  70. * @return array
  71. */
  72. public function getIsAllow(int $orderId): array
  73. {
  74. if (isset($this->notAllowedShippingMap[$orderId])) {
  75. return [
  76. 'isAllow' => false,
  77. 'reason' => $this->notAllowedShippingMap[$orderId]
  78. ];
  79. }
  80. return [
  81. 'isAllow' => true,
  82. 'reason' => ''
  83. ];
  84. }
  85. }