BaseOrderInitData.php 1.5 KB

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