OrderThrough.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. namespace addons\shopro\service\order;
  3. use app\admin\model\shopro\order\OrderItem;
  4. use app\admin\model\shopro\order\Order;
  5. use addons\shopro\service\StockSale;
  6. use addons\shopro\facade\Activity as ActivityFacade;
  7. class OrderThrough
  8. {
  9. /**
  10. * 商品限购
  11. *
  12. * @param [type] $buyInfo
  13. * @param \Closure $next
  14. * @return void
  15. */
  16. public function limitBuy ($buyInfo, \Closure $next)
  17. {
  18. $user = auth_user();
  19. $goods = $buyInfo['goods'];
  20. $goods_num = $buyInfo['goods_num'] ?? 1;
  21. $activity = $goods['activity'];
  22. if ($activity) {
  23. // 活动限购
  24. $rules = $activity['rules'] ?? [];
  25. $limit_type = 'activity';
  26. $limit_num = (isset($rules['limit_num']) && $rules['limit_num'] > 0) ? $rules['limit_num'] : 0;
  27. } else {
  28. // 普通商品限购
  29. $limit_type = $goods->limit_type;
  30. $limit_num = ($limit_type != 'none' && $goods->limit_num > 0) ? $goods->limit_num : 0;
  31. }
  32. if ($limit_num) { // limit_num = 0; 不限购
  33. // 查询用户老订单,判断本次下单数量,判断是否超过购买限制, 未支付的或者已完成的都算
  34. $buy_num = OrderItem::where('user_id', $user['id'])->where('goods_id', $goods->id)
  35. ->where(function ($query) use ($limit_type, $goods, $activity) {
  36. if ($limit_type == 'daily') {
  37. // 按天限购
  38. $daily_start = strtotime(date('Y-m-d'));
  39. $daily_end = strtotime(date('Y-m-d', (time() + 86400))) - 1;
  40. $query->where('createtime', 'between', [$daily_start, $daily_end]);
  41. } else if ($limit_type == 'activity') {
  42. $query->where('activity_id', $activity['id']); // 这个活动下所有的购买记录
  43. } else {
  44. // all,不加任何条件
  45. }
  46. return $query;
  47. })
  48. ->whereExists(function ($query) use ($goods) {
  49. $order_table_name = (new Order())->getQuery()->getTable();
  50. $query->table($order_table_name)->where('order_id=' . $order_table_name . '.id')
  51. ->whereNotIn('status', [Order::STATUS_CLOSED, Order::STATUS_CANCEL]); // 除了交易关闭,和 取消的订单
  52. })->sum('goods_num');
  53. if (($buy_num + $goods_num) > $limit_num) {
  54. $msg = '该商品' . ($limit_type == 'daily' ? '每日' : ($limit_type == 'activity' ? '活动期间' : '')) . '限购 ' . $limit_num . ' 件';
  55. if ($buy_num < $limit_num) {
  56. $msg .= ',当前还可购买 ' . ($limit_num - $buy_num) . ' 件';
  57. }
  58. error_stop($msg);
  59. }
  60. }
  61. return $next($buyInfo);
  62. }
  63. public function checkStock($buyInfo, \Closure $next)
  64. {
  65. $goods = $buyInfo['goods'];
  66. $activity = $goods['activity'];
  67. if (!$activity) {
  68. $stockSale = new StockSale();
  69. $stockSale->stockLock($buyInfo);
  70. }
  71. return $next($buyInfo);
  72. }
  73. public function activity($buyInfo, \Closure $next)
  74. {
  75. $goods = $buyInfo['goods'];
  76. $activity = $goods['activity'];
  77. if ($activity) {
  78. $buyInfo = ActivityFacade::buy($buyInfo, $activity);
  79. }
  80. return $next($buyInfo);
  81. }
  82. public function through($throughs = [])
  83. {
  84. $throughs = is_array($throughs) ? $throughs : [$throughs];
  85. $pipes = [];
  86. foreach ($throughs as $through) {
  87. if (method_exists($this, $through)) {
  88. $pipes[] = function ($params, \Closure $next) use ($through) {
  89. return $this->{$through}($params, $next);
  90. };
  91. }
  92. }
  93. return $pipes;
  94. }
  95. }