PreviewHandler.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. namespace addons\TinyShop\common\components;
  3. use yii\web\UnprocessableEntityHttpException;
  4. use addons\TinyShop\common\forms\PreviewForm;
  5. use addons\TinyShop\common\enums\ShippingTypeEnum;
  6. use addons\TinyShop\common\components\delivery\LogisticsDelivery;
  7. use addons\TinyShop\common\components\delivery\PickupDelivery;
  8. use addons\TinyShop\common\components\delivery\LocalDelivery;
  9. use addons\TinyShop\common\traits\AutoCalculatePriceTrait;
  10. use addons\TinyShop\common\components\delivery\ToStoreDelivery;
  11. /**
  12. * Class PreviewHandler
  13. * @package addons\TinyShop\common\components
  14. * @author jianyan74 <751393839@qq.com>
  15. */
  16. class PreviewHandler
  17. {
  18. use AutoCalculatePriceTrait;
  19. /**
  20. * @var PreviewHandler
  21. */
  22. private $_handlers;
  23. /**
  24. * @var array
  25. */
  26. private $_names = [];
  27. /**
  28. * 配送类型
  29. *
  30. * @var array
  31. */
  32. private $_delivery = [
  33. ShippingTypeEnum::LOGISTICS => LogisticsDelivery::class, // 物流配送
  34. ShippingTypeEnum::PICKUP => PickupDelivery::class, // 自提
  35. ShippingTypeEnum::LOCAL_DISTRIBUTION => LocalDelivery::class, // 同城配送
  36. ShippingTypeEnum::TO_STORE => ToStoreDelivery::class, // 到店付款
  37. ];
  38. /**
  39. * PreviewHandler constructor.
  40. * @param $handlers
  41. */
  42. public function __construct($handlers)
  43. {
  44. $this->_handlers = $handlers;
  45. }
  46. /**
  47. * 运行
  48. *
  49. * @param PreviewForm $form
  50. * @param bool $isNewRecord 创建记录
  51. * @return PreviewForm|mixed
  52. */
  53. public function start(PreviewForm $form, $isNewRecord = false)
  54. {
  55. // 判断配送方式
  56. if ($isNewRecord == true && empty($form->shipping_type)) {
  57. throw new UnprocessableEntityHttpException('请选择配送方式');
  58. }
  59. /** @var PreviewInterface $delivery 配送类型 */
  60. $delivery = new $this->_delivery[$form->shipping_type];
  61. $delivery->isNewRecord = $isNewRecord;
  62. $form = $delivery->execute($form);
  63. $this->_names[] = $delivery::getName();
  64. foreach ($this->_handlers as $handler) {
  65. /** @var PreviewInterface $class */
  66. $class = new $handler();
  67. $class->isNewRecord = $isNewRecord;
  68. if ($this->reject($class->rejectNames())) {
  69. // 自动计算价格
  70. $form = $class->execute($this->calculatePrice($form));
  71. // 判断是否执行成功并加入已执行列表
  72. if ($class->status == true) {
  73. $this->_names[] = $class::getName();
  74. }
  75. }
  76. }
  77. return $form;
  78. }
  79. /**
  80. * 获取已执行的营销名称
  81. *
  82. * @return array
  83. */
  84. public function executedNames(): array
  85. {
  86. return $this->_names;
  87. }
  88. /**
  89. * 互斥
  90. *
  91. * @param array $names
  92. * @return bool
  93. */
  94. protected function reject(array $names)
  95. {
  96. if (empty($names)) {
  97. return true;
  98. }
  99. foreach ($this->_names as $name) {
  100. if (in_array($name, $names)) {
  101. return false;
  102. }
  103. }
  104. return true;
  105. }
  106. }
粤ICP备19079148号