ChainOrderCheckEvent.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. namespace common\events\data\api;
  3. use common\components\BaseEvent;
  4. use common\enums\EventNameEnum;
  5. use common\logic\order\BaseOrderForm;
  6. use common\models\user\User;
  7. use Exception;
  8. /**
  9. * 供应链订单售前检测
  10. * @package common\events\data\backend
  11. */
  12. final class ChainOrderCheckEvent extends BaseEvent
  13. {
  14. /**
  15. * 触发事件名称(不可修改)
  16. *
  17. * @var string
  18. */
  19. public $name = EventNameEnum::CHAIN_ORDER_CHECK;
  20. /**
  21. * @var User|array
  22. */
  23. protected $user;
  24. /**
  25. * @var BaseOrderForm
  26. */
  27. protected $form;
  28. /**
  29. * @var boolean
  30. */
  31. protected $isNewRecord = true;
  32. /**
  33. * @var boolean
  34. */
  35. protected $isError = false;
  36. /**
  37. * @var string
  38. */
  39. protected $errorMsg;
  40. /**
  41. * @param integer $mall_id
  42. * @param array $list
  43. * @param array $user
  44. */
  45. public function __construct(int $mall_id, BaseOrderForm $form, bool $is_new_record, $user = [])
  46. {
  47. parent::__construct($mall_id);
  48. $this->form = $form;
  49. $this->isNewRecord = $is_new_record;
  50. $this->user = $user;
  51. }
  52. /**
  53. * @var BaseOrderForm
  54. */
  55. public function getForm()
  56. {
  57. return $this->form;
  58. }
  59. /**
  60. * @param BaseOrderForm $form
  61. * @return void
  62. */
  63. public function setForm(BaseOrderForm $form)
  64. {
  65. $this->form = $form;
  66. }
  67. /**
  68. * @return BaseOrderForm|boolean
  69. */
  70. public function getResultForm()
  71. {
  72. if ($this->isError) return false;
  73. return $this->form;
  74. }
  75. /**
  76. * @return boolean
  77. */
  78. public function isNewRecord()
  79. {
  80. return $this->isNewRecord;
  81. }
  82. /**
  83. * @param string $msg
  84. * @return void
  85. */
  86. public function setErrorMsg(string $msg)
  87. {
  88. $this->isError = true;
  89. $this->errorMsg = $msg;
  90. }
  91. /**
  92. * @return string
  93. */
  94. public function getErrorMsg()
  95. {
  96. return $this->errorMsg;
  97. }
  98. /**
  99. * @param string $source
  100. * @return boolean
  101. */
  102. public function isCurrentSource(string $source): bool
  103. {
  104. return $this->getGoodsSource() == $source;
  105. }
  106. /**
  107. * 获取商品的来源类型
  108. *
  109. * @return string
  110. */
  111. public function getGoodsSource()
  112. {
  113. $sources = array_unique(array_column(array_column($this->getForm()->sku, 'goods'), 'goods_source'));
  114. if (count($sources) > 1) throw new Exception('商品来源不一致');
  115. return reset($sources);
  116. }
  117. }