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

270 lines
8.5 KiB

10 months ago
<?php
namespace app\api\model\Server;
10 months ago
use app\api\model\Setting as SettingModel;
10 months ago
use app\api\service\User as UserService;
10 months ago
use app\common\enum\order\PayStatus;
use app\common\enum\order\PayStatus as PayStatusEnum;
10 months ago
use app\common\enum\ServerEnum;
10 months ago
use app\common\model\server\Order;
use cores\exception\BaseException;
10 months ago
use think\db\exception\DataNotFoundException;
10 months ago
use think\db\exception\DbException;
10 months ago
use think\db\exception\ModelNotFoundException;
10 months ago
use think\Model;
10 months ago
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
*/
10 months ago
public function orderList($where, int $listRows = 15): Paginator
10 months ago
{
// 当前用户ID
$userId = UserService::getCurrentLoginUserId();
10 months ago
//判断当前用户身份
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);
}
10 months ago
}
10 months ago
/**
* @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();
}
}
10 months ago
/**
10 months ago
* 获取用户订单详情(仅订单记录)
* @param int $orderId
* @param array $with
* @param bool $onlyCurrentUser 只查询当前登录用户的记录
* @return array|Order|null
10 months ago
* @throws BaseException
10 months ago
*/
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
10 months ago
* @throws DbException
10 months ago
* @throws ModelNotFoundException
10 months ago
* @author: wanghousheng
*/
10 months ago
public static function getUnpaidOrderDetail(int $orderId): array
10 months ago
{
10 months ago
// 获取订单详情
$orderInfo = static::getDetail($orderId);
// 验证订单状态
10 months ago
if ($orderInfo['order_status'] != ServerEnum::APPLYPAY || $orderInfo['pay_status'] == PayStatus::SUCCESS) {
10 months ago
throwError('当前订单状态不允许支付');
}
// 未支付订单的过期时间
$orderCloseTime = SettingModel::getOrderCloseTime() * 60 * 60;
// 订单超时截止时间
10 months ago
$expirationTime = strtotime($orderInfo['create_time']) + $orderCloseTime;
10 months ago
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),
];
10 months ago
}
10 months ago
/**
* 获取当前用户服务订单数量
* @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();
// 查询数据
$query = $this;
// 非店长不带入user_id查询
if (!UserService::isStore()) {
$query->where('user_id', '=', $userId);
}
return $query->where($dataTypeFilter)
->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;
}
10 months ago
}