Basics.php 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2024 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\api\service\order\source\checkout;
  13. use app\common\service\BaseService;
  14. use app\api\model\User as UserModel;
  15. use app\api\service\Order as OrderService;
  16. /**
  17. * 订单结算台扩展基类
  18. * Class Basics
  19. * @package app\api\service\order\source\checkout
  20. */
  21. abstract class Basics extends BaseService
  22. {
  23. /* @var UserModel $user 当前用户信息 */
  24. protected UserModel $user;
  25. // 订单结算商品列表
  26. protected iterable $goodsList;
  27. /**
  28. * 构造方法
  29. * Checkout constructor.
  30. * @param UserModel $user
  31. * @param iterable $goodsList
  32. */
  33. public function __construct(UserModel $user, iterable $goodsList)
  34. {
  35. parent::__construct();
  36. $this->user = $user;
  37. $this->goodsList = $goodsList;
  38. }
  39. /**
  40. * 验证商品列表
  41. * @return mixed
  42. */
  43. abstract public function validateGoodsList();
  44. /**
  45. * 验证商品限购
  46. * @param int $orderSource 商品来源
  47. * @return bool
  48. */
  49. public function validateRestrict(int $orderSource): bool
  50. {
  51. foreach ($this->goodsList as $goods) {
  52. // 不限购
  53. if (!$goods['is_restrict']) return true;
  54. // 拼团商品单次限购数量
  55. if ($goods['total_num'] > $goods['restrict_single']) {
  56. $this->error = "很抱歉,该商品限购{$goods['restrict_single']}件,请修改购买数量";
  57. return false;
  58. }
  59. // 获取用户已下单的件数(未取消 订单来源)
  60. $alreadyBuyNum = OrderService::getGoodsBuyNum($this->user['user_id'], $goods['goods_id'], $orderSource);
  61. // 情况1: 已购买0件, 实际想购买5件
  62. if ($alreadyBuyNum == 0 && $goods['total_num'] > $goods['restrict_total']) {
  63. $this->error = "很抱歉,该商品限购{$goods['restrict_total']}件,请修改购买数量";
  64. return false;
  65. }
  66. // 情况2: 已购买3件, 实际想购买1件
  67. if ($alreadyBuyNum >= $goods['restrict_total']) {
  68. $this->error = "很抱歉,该商品限购{$goods['restrict_total']}件,您当前已下单{$alreadyBuyNum}件,无法购买";
  69. return false;
  70. }
  71. // 情况3: 已购买2件, 实际想购买2件
  72. if (($alreadyBuyNum + $goods['total_num']) > $goods['restrict_total']) {
  73. $diffNum = ($alreadyBuyNum + $goods['total_num']) - $goods['restrict_total'];
  74. $this->error = "很抱歉,该商品限购{$goods['restrict_total']}件,您最多能再购买{$diffNum}件";
  75. return false;
  76. }
  77. }
  78. return true;
  79. }
  80. }