You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
yanzong/app/api/model/Server/ServerOrder.php

206 lines
6.5 KiB

<?php
namespace app\api\model\Server;
use app\api\model\Setting as SettingModel;
use app\api\service\User as UserService;
use app\common\enum\order\PayStatus;
use app\common\enum\ServerEnum;
use app\common\model\server\Order;
use cores\exception\BaseException;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\Model;
use think\Paginator;
class ServerOrder extends Order
{
/**
* 隐藏字段
* @var array
*/
protected $hidden = [
'store_id',
'create_time',
'update_time',
'trade_id',
'server_image_id',
'is_delete',
];
/**
* @notes:用户服务订单列表
* @param $where
* @param int $listRows
* @return Paginator
* @throws BaseException
* @throws DbException
* @author: wanghousheng
*/
public function orderList($where, int $listRows = 15): Paginator
{
// 当前用户ID
$userId = UserService::getCurrentLoginUserId();
//判断当前用户身份
if (UserService::isDealerEngineer()) {
//分销商工程师
return $this->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),
];
}
}