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/common/service/server/Order.php

91 lines
3.1 KiB

10 months ago
<?php
declare (strict_types=1);
namespace app\common\service\server;
use app\api\model\dealer\User as DealerUserModel;
use app\common\enum\dealer\DealerUserEnum;
use app\common\enum\order\PayStatus;
use app\common\enum\ServerEnum;
use app\common\model\UserCoupon as UserCouponModel;
use app\common\service\BaseService;
use app\common\service\order\Refund as RefundService;
use cores\exception\BaseException;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
class Order extends BaseService
{
/**
* 生成订单号
* @return string
*/
public static function createOrderNo(): string
{
return date('Ymd') . substr(implode('', array_map('ord', str_split(substr(uniqid(), 7, 13)))), 0, 8);
}
/**
* @notes:用户取消订单
* @param array $order
* @return bool
* @throws BaseException
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
* @author: wanghousheng
*/
public static function cancelOrder(array $order): bool
{
//未完成 未取消
if (!empty($order['order_status']) && $order['order_status'] != ServerEnum::COMPLETED || $order['order_status'] != ServerEnum::CANCELLED) {
if (!empty($order['coupon_id'])) {
// 回退用户优惠券
$order['coupon_id'] > 0 && UserCouponModel::setIsUse($order['coupon_id'], false);
}
//判断是否已支付
if ($order['pay_status'] == PayStatus::SUCCESS) {
if (!(new RefundService)->handle($order)) {
try {
throwError('执行订单退款失败');
} catch (BaseException $e) {
return false;
}
}
}
//更新订单状态
$model = new \app\common\model\server\Order;
$model->where(['order_id' => $order['order_id']])->save(['order_status' => ServerEnum::CANCELLED]);
return true;
}
return false;
}
/**
* @notes:派单
* @param array $order
* @param int $dealerId
* @return bool
* @author: wanghousheng
*/
public static function dispatchOrders(array $order, int $dealerId): bool
{
//判断该订单是否可以指派
if (!empty($order) && $order['order_status'] == ServerEnum::APPLYDISPATCH) {
//判断指派人员是否是分销工程师
$dealerInfo = DealerUserModel::detail($dealerId, []);
if (!empty($dealerInfo)) {
$dealerInfo = $dealerInfo->toArray();
if ($dealerInfo['type'] == DealerUserEnum::ENGINEER) {
$model = new \app\common\model\server\Order;
$up['dealer_id'] = $dealerId;
$up['order_status'] = ServerEnum::APPLYSERVER;
$model->where(['order_id' => $order['order_id']])->save($up);
return true;
}
}
}
return false;
}
}