OrderInitDataLogic.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. namespace addons\Sudoku\common\logic\order;
  3. use common\enums\PreviewTypeEnum;
  4. use addons\Sudoku\common\logic\order\purchase\BuyNowPurchase;
  5. use common\traits\ErrorTrait;
  6. use Exception;
  7. /**
  8. * 订单数据初始化逻辑层
  9. * @Author ltm
  10. * @DateTime 2021-12-30 14:16:58
  11. * @copyright: Copyright (c) 2020 广东七件事集团
  12. */
  13. class OrderInitDataLogic{
  14. use ErrorTrait;
  15. public $isNewRecord = false; // 是否是新增记录
  16. /**
  17. * 下单方式
  18. *
  19. * @var array
  20. */
  21. protected $handlers = [
  22. PreviewTypeEnum::BUY_NOW => BuyNowPurchase::class, // 立即下单
  23. ];
  24. /**
  25. * @desc:执行订单初始化
  26. * @Author: ltm
  27. * @Date: 2021/12/30
  28. * @Time: 14:14
  29. * @Copyright: copyright (c) 2021 广东七件事集团
  30. * @param OrderFormLogic $form
  31. * @param $type
  32. * @return false|mixed
  33. */
  34. public function execute(OrderFormLogic $form, $type){
  35. try{
  36. if (!isset($this->handlers[$type])) throw new Exception('下单类型错误');
  37. $class = new $this->handlers[$type]();
  38. $class->isNewRecord = $this->isNewRecord;
  39. $form = $class->execute($form);
  40. if($form === false) throw new Exception($class->getError());
  41. if (!$form->order_goods) throw new Exception('无该下单的产品');
  42. $form = $class->afterExecute($form,$class::getType());
  43. if($form === false) throw new Exception($class->getError());
  44. return $form;
  45. }catch(Exception $e){
  46. $this->setError($e->getMessage());
  47. return false;
  48. }
  49. }
  50. }