with(['image', 'user']) ->where($where) ->where('dealer_id', '=', $userId) ->where('is_delete', '=', 0) ->order(['create_time' => 'desc']) ->paginate($listRows); } elseif (UserService::isStore()) { // 店主 return $this->with(['image', 'user', 'dealer']) ->where($where) ->where('is_delete', '=', 0) ->order(['create_time' => 'desc']) ->paginate($listRows); } else { return $this->with(['image', 'dealer']) ->where($where) ->where('user_id', '=', $userId) ->where('is_delete', '=', 0) ->order(['create_time' => 'desc']) ->paginate($listRows); } } /** * @notes: * @param $where * @return bool * @throws BaseException * @author: wanghousheng */ public function confirmSuccess($where): bool { // 当前用户ID $userId = UserService::getCurrentLoginUserId(); if (!UserService::isDealerEngineer() || !UserService::isStore()) { return false; } if (UserService::isDealerEngineer()) { $where = array_merge($where, ['dealer_id' => $userId]); } //分销商工程师 $order_id = $this->where($where) ->where(['order_status' => ServerEnum::APPLYSERVER]) ->where('is_delete', '=', 0) ->value('order_id'); if (!empty($order_id)) { $this->where(['id' => $order_id])->save(['order_status' => ServerEnum::COMPLETED]); return true; } return false; } /** * @notes:订单详情 * @param $where * @return ServerOrder|array|mixed|Model|null * @throws BaseException * @throws DataNotFoundException * @throws DbException * @throws ModelNotFoundException * @author: wanghousheng */ public function info($where) { // 当前用户ID $userId = UserService::getCurrentLoginUserId(); //判断当前用户身份 if (UserService::isDealerEngineer()) { //分销商工程师 return $this->with(['image', 'user']) ->where($where) ->where('dealer_id', '=', $userId) ->where('is_delete', '=', 0) ->find(); } elseif (UserService::isStore()) { // 店主 return $this->with(['image', 'user', 'dealer']) ->where($where) ->where('is_delete', '=', 0) ->find(); } else { return $this->with(['image', 'dealer']) ->where($where) ->where('user_id', '=', $userId) ->where('is_delete', '=', 0) ->find(); } } /** * 获取用户订单详情(仅订单记录) * @param int $orderId * @param array $with * @param bool $onlyCurrentUser 只查询当前登录用户的记录 * @return array|Order|null * @throws BaseException */ public static function getDetail(int $orderId, array $with = [], bool $onlyCurrentUser = true) { // 查询条件 $where = ['order_id' => $orderId]; $onlyCurrentUser && $where['user_id'] = UserService::getCurrentLoginUserId(); // 查询订单记录 $order = static::detail($where, $with); empty($order) && throwError('订单不存在'); return $order; } /** * 待支付订单详情 * @param string $orderNo 订单号 * @return null|static */ public static function getPayDetail(string $orderNo): ?Order { $where = ['order_no' => $orderNo, 'is_delete' => 0]; return self::detail($where, ['user']); } /** * @notes:获取未支付的订单详情(用于订单支付) * @param int $orderId 订单ID * @return array * @throws BaseException * @throws DataNotFoundException * @throws DbException * @throws ModelNotFoundException * @author: wanghousheng */ public static function getUnpaidOrderDetail(int $orderId): array { // 获取订单详情 $orderInfo = static::getDetail($orderId); // 验证订单状态 if ($orderInfo['order_status'] != ServerEnum::APPLYPAY || $orderInfo['pay_status'] == PayStatus::SUCCESS) { throwError('当前订单状态不允许支付'); } // 未支付订单的过期时间 $orderCloseTime = SettingModel::getOrderCloseTime() * 60 * 60; // 订单超时截止时间 $expirationTime = strtotime($orderInfo['create_time']) + $orderCloseTime; if ($orderCloseTime > 0 && $expirationTime <= time()) { throwError('当前订单支付已超时,请重新下单'); } // 仅返回需要的数据 return [ 'orderId' => $orderInfo['order_id'], 'order_no' => $orderInfo['order_no'], 'pay_price' => $orderInfo['pay_price'], 'pay_status' => $orderInfo['pay_status'], 'order_status' => $orderInfo['order_status'], 'create_time' => $orderInfo['create_time'], 'showExpiration' => $orderCloseTime > 0, 'expirationTime' => format_time($expirationTime), ]; } /** * 获取当前用户服务订单数量 * @param string $dataType 订单类型 (all全部 confirm待确认 service待服务 payment待支付 complete 已完成) * @return int * @throws BaseException */ public function getCount(string $dataType = 'all'): int { // 设置订单类型条件 $dataTypeFilter = $this->getFilterDataType($dataType); // 当前用户ID $userId = UserService::getCurrentLoginUserId(); //判断当前用户身份 if (UserService::isDealerEngineer()) { //分销商工程师 return $this->where($dataTypeFilter) ->where('dealer_id', '=', $userId) ->where('is_delete', '=', 0) ->count(); } elseif (UserService::isStore()) { // 店主 return $this->where($dataTypeFilter) ->where('is_delete', '=', 0) ->count(); } else { return $this->where($dataTypeFilter) ->where('user_id', '=', $userId) ->where('is_delete', '=', 0) ->count(); } } /** * 设置订单类型条件 * @param string $dataType * @return array */ private function getFilterDataType(string $dataType): array { // 筛选条件 $filter = []; // 订单数据类型 switch ($dataType) { case 'all': break; //待确认 case 'confirm': $filter[] = ['pay_status', '=', PayStatusEnum::SUCCESS]; $filter[] = ['order_status', '=', ServerEnum::APPLYDISPATCH]; break; //待服务 case 'service': $filter[] = ['pay_status', '=', PayStatusEnum::SUCCESS]; $filter[] = ['order_status', '=', ServerEnum::APPLYSERVER]; break; //待支付 case 'payment': $filter = [ ['pay_status', '=', PayStatusEnum::PENDING], ['order_status', '=', ServerEnum::APPLYPAY] ]; break; //已完成 case 'complete': $filter = [ ['order_status', '=', ServerEnum::COMPLETED] ]; break; } return $filter; } }