BaseOrderInitData.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. namespace addons\UpgradeGift\common\logic\order;
  3. use common\enums\PreviewTypeEnum;
  4. use addons\UpgradeGift\common\logic\order\purchase\BuyNowPurchase;
  5. use addons\UpgradeGift\common\logic\order\purchase\CartPurchase;
  6. use addons\UpgradeGift\common\logic\order\purchase\BasePurchase;
  7. use common\traits\ErrorTrait;
  8. use Exception;
  9. /**
  10. * 订单数据初始化逻辑层
  11. * @Author bing
  12. * @DateTime 2021-09-22 18:16:58
  13. * @copyright: Copyright (c) 2020 广东七件事集团
  14. */
  15. abstract class BaseOrderInitData
  16. {
  17. use ErrorTrait;
  18. public $isNewRecord = false; // 是否是新增记录
  19. abstract public function getHandlers();
  20. /**
  21. * 执行订单初始化
  22. * @Author bing
  23. * @DateTime 2021-09-22 20:51:26
  24. * @copyright: Copyright (c) 2020 广东七件事集团
  25. * @param $form
  26. * @param [type] $type
  27. * @return object|bool
  28. */
  29. public function execute($form, $type)
  30. {
  31. try{
  32. $handlers = $this->getHandlers();
  33. if (!isset($handlers[$type])) throw new Exception('下单类型错误');
  34. /** @var BasePurchase $class */
  35. $class = new $handlers[$type]();
  36. $class->isNewRecord = $this->isNewRecord;
  37. $form = $class->execute($form);
  38. if($form === false) throw new Exception($class->getError());
  39. if (!$form->order_goods) throw new Exception('该下单的产品不存在或者已经删除');
  40. $form = $class->afterExecute($form, $class::getType());
  41. if($form === false) throw new Exception($class->getError());
  42. return $form;
  43. } catch (Exception $e) {
  44. $this->setError($e->getMessage());
  45. return false;
  46. }
  47. }
  48. }