status; $pay_end_time = time() - (60 * 15); $saas_user = get_saas_user(); $where = ['is_platform' => 1, 'saas_id' => $saas_user->id]; // if ($this->store_id > 0) { // $user = User::findOne(['binding' => $saas_user->mobile, 'is_delete' => 0, 'store_id' => $this->store_id]); // $user_id = 0; // if ($user) { // $user_id = $user->id; // } // $where = ['store_id' => $this->store_id, 'user_id' => $user_id, 'is_pay' => 1]; // } $query = PtActivityOrder::find()->where($where); switch ($status) { case 1: $query->andWhere(['AND', ['is_pay' => 0], ['<>' ,'trade_status', 1]]); break; case 2: $query->andWhere(['AND', ['is_pay' => 1], ['is_pt_finish' => 0]]); break; case 3: $query->andWhere(['AND', ['is_pt_finish' => 1], ['is_winner' => 1]]); break; case 4: $query->andWhere(['OR', ['AND', ['is_pt_finish' => 1], ['is_winner' => 0]], ['AND', ['<', 'created_at', $pay_end_time], ['is_pay' => 0]], ['is_pt_finish' => 2]]); break; } $query->select("id, pay_price, pt_number, is_pay, is_pt_finish, is_winner, pay_time, trade_status, created_at, store_id")->orderBy('created_at desc'); $list = pagination_make($query); foreach ($list['list'] as &$item) { if (!empty($item['pt_number'])) { //获取开团订单信息 $first_order = PtActivityOrder::findOne($item['pt_number']); $item['pay_time'] = $first_order->pay_time; } $item['surplus_num'] = 0; $item['end_time'] = 0; //商品数据 活动数据 $item['goods_list'] = PtActivityOrderDetail::find()->where(['order_id' => $item['id']]) ->select('id, goods_id, pic, goods_name, attr, num, activity_id, total_price')->asArray()->one(); $pt_number = $item['id']; if ((int)$item['pt_number'] > 0) { $pt_number = $item['pt_number']; } if (!empty($item['goods_list'])) { $pic = $item['goods_list']['pic']; $index = strstr($pic, 'http'); if (!$index) { $pic = 'https:' . $pic; } $item['goods_list']['pic'] = \str_replace('http://', 'https://', $pic); $item['goods_list']['attr'] = json_decode($item['goods_list']['attr'], true); $pt_activity = PtActivity::findOne($item['goods_list']['activity_id']); if ($pt_activity) { //拼团剩余数据 $end_time = (($pt_activity->split_time * 60 * 60) + $item['pay_time']) - time(); $item['end_time'] = ($end_time > 0) ? $end_time : 0; if ((int)$item['is_pt_finish'] !== 0) { $item['end_time'] = 0; } $order_num = PtActivityOrder::find()->where(['is_pay' => 1]) ->andWhere(['OR' , ['id' => $pt_number], ['pt_number' => $pt_number]]) ->select('id')->count(); $item['surplus_num'] = ($pt_activity->party_size - $order_num); $item['party_type'] = $pt_activity['party_type']; $item['surplus_num_goods'] = 0; if($pt_activity['party_type'] == 1){ $goodsCountQuery = PtActivityOrderDetail::find()->alias('pod') ->leftJoin(['po' => PtActivityOrder::tableName()], 'pod.order_id = po.id') ->where(['po.is_pay' => 1, 'pod.is_delete' => 0]) ->andWhere(['OR' , ['po.id' => $pt_number], ['po.pt_number' => $pt_number]]); $goodsCount = $goodsCountQuery->sum('pod.num'); $item['surplus_num_goods'] = $pt_activity['party_goods_count'] - (int)$goodsCount; } } } $item['order_id'] = 0; if ((int)$item['is_pay'] === 0 && (int)$item['trade_status'] !== 1) { //待付款 $item['status'] = 1; $order_form = new OrderForm(); $item['pay_type_list'] = $order_form->getPayTypeList(); $item['createdDate'] = $item['created_at']; } if ((int)$item['is_pay'] === 1 && (int)$item['is_pt_finish'] === 0) { //进行中 $item['status'] = 2; } if ((int)$item['is_pt_finish'] === 1 && (int)$item['is_winner'] === 1) { //已成功 $item['status'] = 3; $order = Order::findOne(['pt_order_id' => $item['id']]); $item['order_id'] = $order->id ?? 0; } if (((int)$item['is_pt_finish'] === 1 && (int)$item['is_winner'] === 0) || ($item['created_at'] < $pay_end_time && (int)$item['is_pay'] === 0) || (int)$item['is_pt_finish'] === 2) { //已失败 $item['status'] = 4; } $item['pt_number'] = (int)$pt_number; $item['is_pt_finish'] = intval($item['is_pt_finish']); unset($item['is_pay'], $item['trade_status'], $item['is_winner'], $item['pay_time']); } return [ 'code' => 0, 'msg' => '', 'data' => [ 'list' => $list['list'] ?? [], 'pageNo' => $list['pageNo'] ?? 0, 'totalCount' => $list['totalCount'] ?? 0 ] ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } /** * 拼团信息 成功 失败 进行中 */ public function orderInfo() { try { $pt_number = $this->pt_number; $pt_order_id = $this->pt_order_id; if (empty($pt_number)) { throw new \Exception("缺少必要参数"); } $order = PtActivityOrder::findOne(['id' => $pt_number, 'is_pay' => 1]); if (empty($order)) { throw new \Exception("未找到对应的拼团数据"); } //商品信息 $order_detail = PtActivityOrderDetail::findOne(['order_id' => $pt_number]); $goods_id = $order_detail->goods_id; $activity_id = $order_detail->activity_id; $pt_goods = PtActivityGoods::findOne(['goods_id' => $goods_id, 'is_delete' => 0, 'activity_id' => $activity_id]); $activity = PtActivity::findOne($activity_id); $party_size = $activity->party_size; $activity_time = ($activity->split_time * 60 * 60); $goods_info = Goods::findOne($pt_goods->goods_id); if (empty($order_detail) || empty($pt_goods) || empty($activity) || empty($goods_info)) { throw new \Exception("拼团数据错误"); } $goods = [ 'name' => $order_detail->goods_name, 'id' => $goods_id, 'party_size' => $party_size, 'activity_id' => $activity_id, 'party_type' => $activity->party_type, 'party_goods_count' => $activity->party_goods_count, 'sale_num' => ($pt_goods->sale_num + $pt_goods->virtual_sales), 'pt_price' => $pt_goods->pt_price, 'price' => $goods_info->price, 'cover_pic' => $goods_info->cover_pic, 'num' => $goods_info->goods_num, 'store_id' => $goods_info->store_id ]; $pt_attr = json_decode($pt_goods->attr, true); $pt_price_arr = array_column($pt_attr, 'pt_price'); $pt_price_arr = array_filter($pt_price_arr); $goods['pt_price'] = sprintf("%.2f", min($pt_price_arr)); //最高省的钱 $diff_price = sprintf("%.2f", $goods_info->price - $goods['pt_price']); $pt_order = PtActivityOrder::find()->alias('o') ->where(['o.is_pay' => 1]) ->leftJoin(['u' => User::tableName()], 'o.user_id = u.id') ->leftJoin(['su' => SaasUser::tableName()], 'u.binding = su.mobile') ->andWhere(['OR', ['o.id' => $pt_number], ['o.pt_number' => $pt_number]]) ->select('su.name, su.avatar, o.id, o.is_winner') ->orderBy('o.pt_number asc')->asArray()->all(); // $winner_order_id = 0; // foreach ($pt_order as $value) { // if ((int)$value['is_winner'] === 1) { // $winner_order_id = $value['id']; // } // } $current_num = count($pt_order); $surplus_num = $activity->party_type == 0 ? ($party_size - $current_num) : 0; $surplus_num_goods = 0; if($activity->party_type == 1){ $goodsCountQuery = PtActivityOrderDetail::find()->alias('pod') ->leftJoin(['po' => PtActivityOrder::tableName()], 'pod.order_id = po.id') ->where(['po.is_pay' => 1, 'pod.is_delete' => 0]) ->andWhere(['OR' , ['po.id' => $pt_number], ['po.pt_number' => $pt_number]]); $goodsCount = $goodsCountQuery->sum('pod.num'); $surplus_num_goods = $activity->party_goods_count - (int)$goodsCount; } $end_time = 0; if ((int)$order->is_pt_finish === 0) { $end_time = ($order->pay_time + $activity_time) - time(); $end_time = $end_time > 0 ? $end_time : 0; } $user_id = 0; $saas_user = get_saas_user(); $user = User::findOne(['binding' => $saas_user->mobile, 'is_delete' => 0, 'store_id' => $activity->store_id]); if ($user) { $user_id = $user->id; } //查自己是否拼团成功 $once_order = PtActivityOrder::find()->where(['is_pay' => 1, 'user_id' => $user_id]) ->andWhere(['OR', ['id' => $pt_number], ['pt_number' => $pt_number]]) ->select('is_winner, is_pt_finish')->asArray()->one(); $status = 0; if (!empty($once_order)) { if ((int)$once_order['is_pt_finish'] === 0) { //进行中 $status = 2; } if ((int)$once_order['is_pt_finish'] === 1 && (int)$once_order['is_winner'] === 1) {//成功 $status = 3; } if (((int)$once_order['is_pt_finish'] === 1 && (int)$once_order['is_winner'] === 0) || $once_order['is_pt_finish'] === 2) {//失败 $status = 4; } } //获取拼团成功人员 $pt_msg = PtActivityOrder::find()->alias('o')->where(['is_winner' => 1]) ->leftJoin(['u' => User::tableName()], 'o.user_id = u.id') ->leftJoin(['su' => SaasUser::tableName()], 'u.binding = su.mobile') ->leftJoin(['od' => PtActivityOrderDetail::tableName()], 'od.order_id = o.id') ->leftJoin(['g' => Goods::tableName()], 'od.goods_id = g.id') ->select('o.total_price, su.avatar, su.name, g.price, o.id')->orderBy('o.created_at desc') ->limit(1) ->asArray()->one(); // foreach ($pt_msg as &$msg) { // $msg['diff_price'] = sprintf("%.2f", ($msg['price'] - $msg['total_price'])); // } $order = Order::findOne(['pt_order_id' => $pt_number]); if (!$pt_msg) { $pt_msg = null; } else { $pt_msg['diff_price'] = sprintf("%.2f", ($pt_msg['price'] - $pt_msg['total_price'])); } $pt_order_id = PtActivityOrder::find()->where(['OR', ['id' => $pt_number], ['pt_number' => $pt_number]]) ->andWhere(['is_winner' => 1, 'saas_id' => get_saas_user_id()])->select('id')->asArray()->one(); $order = Order::findOne(['pt_order_id' => $pt_order_id]); $order = [ 'user_list' => $pt_order, 'diff_price' => $diff_price, 'party_type' => $activity->party_type, 'surplus_num_goods' => $surplus_num_goods, 'surplus_num' => $surplus_num, 'end_time' => $end_time, 'status' => $status, 'order_id' => $order->id ]; return [ 'code' => 0, 'msg' => "获取成功", 'data' => [ 'goods' => $goods, 'order' => $order, 'pt_msg' => $pt_msg ] ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } }