find(); if ($consumption['status'] != 1 || strtotime($consumption['start_time']) > time() || strtotime($consumption['end_time']) <= time()) { throw new \Exception('当前活动已结束'); } $model = StoreActivityUser::where('user_id', $userId)->where('value', $couponId)->where('activity_id', $activityId)->find(); if (empty($model)) { $model = new StoreActivityUser(); $model->user_id = $userId; $model->value = $couponId; $model->activity_id = $activityId; $model->create_time = time(); $model->status = 1; if (!$model->save()) { throw new \Exception('保存出错'); } } $validModel = StoreActivityUser::where('user_id', $userId)->where('activity_id', $activityId)->where('status', 1)->find(); if ($validModel['value'] != $couponId) { $consumption = StoreConsumption::where('coupon_id', $validModel['value'])->find(); if ($consumption['type'] == StoreConsumption::TYPE_PULL_CONSUMPTION) { $groupOrders = StoreActivityOrder::whereRaw('user_id=:user_id or spread_id=:spread_id', ['user_id' => $userId, 'spread_id' => $userId]) ->where('activity_id', $activityId) ->where('status', StoreActivityOrder::STATUS_VALID) ->count(); if ($groupOrders > 0) { $model->status = 0; if (!$model->save()) { throw new \Exception('保存出错'); } throw new \Exception('您正在参与活动,暂时不支持切换'); } } $validModel->status = 0; if (!$validModel->save()) { throw new \Exception('保存出错'); } $model->status = 1; if (!$model->save()) { throw new \Exception('保存出错'); } } } /** * 获取用户选择的消费金类型 * @param int $userId * @param int $activityId * @return int */ public function getValue(int $userId, int $activityId = 1) { $data = StoreActivityUser::where('user_id', $userId)->where('activity_id', $activityId)->where('status', 1)->find(); return $data->value ?? 0; } public function getOne(int $userId, int $activityId = 1) { return StoreActivityUser::where('user_id', $userId)->where('activity_id', $activityId)->where('status', 1)->find(); } /** * 获取用户参与活动的状态 * @param int $userId 用户id * @param int $activityId 活动id * @return array * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function status(int $userId, int $activityId) { $target = 0; $userInfo = []; $activityUser = StoreActivityUser::where('user_id', $userId) ->where('activity_id', $activityId) ->where('status', 1) ->find(); if (empty($activityUser)) { return ['target' => $target, 'allow_receive' => false, 'user_info' => $userInfo]; } $consumption = StoreConsumption::where('coupon_id', $activityUser['value']) ->where('type', StoreConsumption::TYPE_PULL_CONSUMPTION) ->find(); $unReceiveConsumption = StoreConsumptionUser::where('uid', $userId)->where('status', StoreConsumptionUser::STATUS_UN_RECEIVE)->value('order_id_set'); if (!empty($unReceiveConsumption)) { $groupOrderIds = explode(',', $unReceiveConsumption); $groupOrderId = $groupOrderIds[0]; $myOrderWhere = ['group_order_id' => $groupOrderId]; } else { $myOrderWhere = ['status' => StoreActivityOrder::STATUS_VALID]; } $myOrder = StoreActivityOrder::where('user_id', $userId) ->where('activity_id', $activityId) ->where($myOrderWhere) ->find(); if (empty($myOrder) || empty($myOrder['pay_price'])) { return ['target' => $target, 'allow_receive' => false, 'user_info' => $userInfo]; } $userInfo = User::where('spread_uid', $userId)->field('uid,nickname,avatar')->select()->toArray(); $storeConsumptionUserDao = new StoreConsumptionUserDao(); $scope = $storeConsumptionUserDao->getScope($consumption, $myOrder['pay_price']); // 订单有效金额为实付金额+红包金额 $orderValidAmount = bcadd($myOrder['pay_price'], $myOrder['red_pack'], 2); $orderValidAmount = min($orderValidAmount, $storeConsumptionUserDao->maxAmount); $orderQuery = StoreActivityOrder::where('spread_id', $userId) ->whereIn('user_id', array_column($userInfo, 'uid')) ->where('activity_id', $activityId) ->where('is_first_order', StoreActivityOrder::IS_FIRST_ORDER) ->where('pay_price', '>=', $orderValidAmount); if (!empty($groupOrderIds)) { unset($groupOrderIds[0]); $orderQuery->whereIn('group_order_id', $groupOrderIds); } else { $orderQuery->where('status', StoreActivityOrder::STATUS_VALID); } $orders = $orderQuery->select()->toArray(); $orders = reset_index($orders, 'user_id'); foreach ($userInfo as &$user) { $user['target_amount'] = $orderValidAmount; $user['is_finish'] = isset($orders[$user['uid']]) ? 1 : 0; } return ['target' => $scope['num'], 'allow_receive' => count($orders) >= $scope['num'], 'user_info' => $userInfo]; } /** * 领取消费金 * @param int $userId * @return array * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function receive(int $userId) { $consumption = StoreConsumption::where('type', StoreConsumption::TYPE_PULL_CONSUMPTION) ->where('status', StoreConsumption::STATUS_ENABLE) ->find(); $userConsumption = StoreConsumptionUser::where('uid', $userId) ->where('coupon_id', $consumption['coupon_id']) ->where('status', StoreConsumptionUser::STATUS_UN_RECEIVE) ->find(); if (empty($userConsumption)) { throw new \Exception('您没有可领取的红包'); } $userConsumption->status = StoreConsumptionUser::STATUS_UNUSED; $userConsumption->start_time = date('Y-m-d H:i:s', time() + 7 * 86400); $userConsumption->end_time = '2026-01-15 23:59:59'; if (!$userConsumption->save()) { throw new \Exception('领取出错,请稍后重试'); } return ['amount' => $userConsumption['coupon_price'], 'end_time' => $userConsumption['end_time']]; } /** * 红包获取记录 * @param int $userId * @param int $type * @param int $page * @param int $limit * @return array * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function record(int $userId, int $type, int $page, int $limit) { $totalAmount = StoreConsumptionUser::where('uid', $userId) ->whereIn('type', $type) ->where('status', StoreConsumptionUser::STATUS_UNUSED) ->sum('balance'); $query = UserBill::field('link_id,create_time,number coupon_price,mark') ->where('uid', $userId) ->where('category', 'red_pack') ->where('type', "red_pack_{$type}"); $count = $query->count(); $record = $query->page($page)->limit($limit)->select()->toArray(); foreach ($record as &$item) { $item['order_amount'] = 0; if (mb_strpos($item['mark'], '订单金额:') !== false) { $item['order_amount'] = mb_substr($item['mark'], mb_strpos($item['mark'], '订单金额:') + 5); } unset($item['mark']); } return ['total_amount' => $totalAmount, 'count' => $count, 'record' => $record]; } /** * 红包余额统计 * @param int $userId * @return array */ public function total(int $userId) { $totalAmount = StoreConsumptionUser::where('uid', $userId) ->whereIn('type', [StoreConsumptionUser::TYPE_ONE, StoreConsumptionUser::TYPE_TWO]) ->where('status', StoreConsumptionUser::STATUS_UNUSED) ->field('SUM(balance) as total_amount,type') ->group('type') ->select()->toArray(); $totalAmount = reset_index($totalAmount, 'type'); $result = [ [ 'type' => 1, 'total_amount' => 0.00 ], [ 'type' => 2, 'total_amount' => 0.00 ] ]; foreach ($result as &$item) { if (isset($totalAmount[$item['type']])) { $item['total_amount'] = $totalAmount[$item['type']]['total_amount']; } $item['type_cn'] = StoreConsumptionUser::TYPE_MAP[$item['type']]; } return $result; } }