GroupBuyLimit.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. namespace addons\GroupBuy\common\logic\order\limit;
  3. use addons\GroupBuy\common\enums\GroupBuyEnum;
  4. use addons\GroupBuy\common\models\GroupBuyActive;
  5. use addons\GroupBuy\common\models\GroupBuyOrder;
  6. use Exception;
  7. use yii\helpers\Json;
  8. class GroupBuyLimit extends BaseLimit
  9. {
  10. public function execute( $form)
  11. {
  12. try {
  13. } catch (Exception $e) {
  14. return false;
  15. }
  16. }
  17. /**
  18. * @desc:购买等级验证
  19. * @Author: hua
  20. * @Date: 2021/12/15
  21. * @Time: 10:17
  22. * @Copyright: copyright (c) 2021 广东七件事集团
  23. * @param $form
  24. * @throws Exception
  25. */
  26. public static function compute($form)
  27. {
  28. $user = $form->user;
  29. $time = time();
  30. $data = is_array($form->data) ? $form->data : Json::decode($form->data, true);
  31. $group_buy_active = GroupBuyActive::findOne(['id' => $data['active_id'], 'mall_id' => $form->mall_id]);
  32. if (empty($group_buy_active)) throw new \Exception('活动不存在');
  33. if ($time < $group_buy_active['start_at']) throw new \Exception('活动还没开始');
  34. if ($time > $group_buy_active['end_at']) throw new \Exception('活动已过期');
  35. if (!empty($data['open_group_id'])) {
  36. //参与别人的团,团员身份
  37. $user_level_limit = $group_buy_active['user_level_limit'];
  38. if ($user_level_limit != 'all' && !in_array($user['level'], explode(',', $user_level_limit))) {
  39. throw new \Exception('不是指定的团员等级');
  40. }
  41. // $res = GroupBuyOrder::findOne([
  42. // 'group_buy_active_id' => $group_buy_active['id'],
  43. // 'mall_id' => $form['mall_id'],
  44. // 'is_commander' => 0,
  45. // 'user_id' => $form['user_id'],
  46. // 'order_status' => GroupBuyEnum::GROUP_SUCCESS
  47. // ]);
  48. // if ($res) throw new \Exception('您已参与过该拼团活动');
  49. $open_order = GroupBuyOrder::findOne(['id' => $data['open_group_id'], 'mall_id' => $form['mall_id']]);
  50. if ($open_order->group_buy_active_id != $data['active_id']) throw new \Exception('该团不合法');
  51. if ($open_order->order_status != GroupBuyEnum::JOINING) throw new \Exception('该团无效');
  52. $effective_time = $group_buy_active['effective_time'] * 60;
  53. if ($time - $open_order['created_at'] > $effective_time) throw new \Exception('拼团已结束');
  54. $counts = GroupBuyOrder::find()->where(['open_group_id' => $data['open_group_id'], 'mall_id' => $form['mall_id'], 'order_status' => GroupBuyEnum::JOINING])->count();
  55. $surplus_size = $group_buy_active['group_size'] - $counts;
  56. // if ($group_buy_active['is_virtual'] && $counts == $group_buy_active['gt_group_size']) throw new \Exception('该团已满员');
  57. if ($surplus_size <= 0) throw new \Exception('该团已满员');
  58. $open_order = GroupBuyOrder::findOne(['open_group_id' => $data['open_group_id'], 'order_status' => [GroupBuyEnum::NOT_PAY, GroupBuyEnum::JOINING], 'user_id' => $form->user_id, 'mall_id' => $form['mall_id']]);
  59. if ($open_order){
  60. if($open_order->order_status==GroupBuyEnum::NOT_PAY)throw new \Exception('您已参团,还未支付,请到拼团订单付款');
  61. if($open_order->order_status==GroupBuyEnum::OPEN_GROUP)throw new \Exception('您已参与该拼团,请耐心等级开团结果');
  62. }
  63. } else {
  64. //自己发起拼团,团长身份
  65. $commander_level_limit = $group_buy_active['commander_level_limit'];
  66. if ($commander_level_limit != 'all' && !in_array($user['level'], explode(',', $commander_level_limit))) {
  67. throw new \Exception('不是指定的团长等级');
  68. }
  69. }
  70. if (!empty($group_buy_active['limit_buy_num']) && $data['num'] > $group_buy_active['limit_buy_num']) throw new \Exception('每单限购' . $group_buy_active['limit_buy_num']);
  71. }
  72. }