Compare commits

...

1 Commits

Author SHA1 Message Date
f898de515a fix : place order coupon 2025-08-06 10:32:52 +08:00
3 changed files with 50 additions and 3 deletions

View File

@@ -16,6 +16,7 @@ use App\Model\CouponTemplate;
use App\Model\UserCoupon; use App\Model\UserCoupon;
use App\Service\Api\BaseService; use App\Service\Api\BaseService;
use App\Service\ServiceTrait\Admin\Catering\PrintTrait; use App\Service\ServiceTrait\Admin\Catering\PrintTrait;
use App\Service\ServiceTrait\Api\CouponTrait;
use App\Service\ServiceTrait\Api\OrderTrait; use App\Service\ServiceTrait\Api\OrderTrait;
use App\Service\ServiceTrait\Common\CycleTrait; use App\Service\ServiceTrait\Common\CycleTrait;
use Hyperf\Di\Annotation\Inject; use Hyperf\Di\Annotation\Inject;
@@ -24,7 +25,7 @@ use Psr\Container\NotFoundExceptionInterface;
abstract class BaseOrderService extends BaseService abstract class BaseOrderService extends BaseService
{ {
use CycleTrait,OrderTrait,PrintTrait; use CycleTrait,OrderTrait,PrintTrait,CouponTrait;
/** /**
* @var int 周期id * @var int 周期id
@@ -100,7 +101,7 @@ abstract class BaseOrderService extends BaseService
* 优惠券id * 优惠券id
* @var int * @var int
*/ */
protected int $couponId; protected int $couponId = 0;
/** /**
* 地点id * 地点id

View File

@@ -121,6 +121,7 @@ class PlaceOrderService extends BaseOrderService
* @return void * @return void
* @throws ContainerExceptionInterface * @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface * @throws NotFoundExceptionInterface
* @throws ErrException
*/ */
private function placeOrder(): void private function placeOrder(): void
{ {
@@ -133,12 +134,16 @@ class PlaceOrderService extends BaseOrderService
$this->insertOrderGoods(); $this->insertOrderGoods();
$this->couponLock();
Db::commit(); Db::commit();
} catch (Exception $e){ } catch (Exception $e){
echo $e->getMessage(); echo $e->getMessage();
//回滚数据库 和 缓存 //回滚数据库 和 缓存
Db::rollBack(); Db::rollBack();
$this->rollbackStock(); $this->rollbackStock();
// $this->rollbackCouponLock();
//意外抛出 //意外抛出
throw new ErrException($e->getMessage()); throw new ErrException($e->getMessage());
} }
@@ -207,7 +212,7 @@ class PlaceOrderService extends BaseOrderService
$orderInsertModel->is_refund_all = OrderCode::REFUND_NULL; $orderInsertModel->is_refund_all = OrderCode::REFUND_NULL;
$orderInsertModel->order_json = json_encode($this->orderRes); $orderInsertModel->order_json = json_encode($this->orderRes);
if (!$orderInsertModel->save()) throw new Exception('下单失败'); if (!$orderInsertModel->save()) throw new Exception('下单失败-01');
$this->orderId = $orderInsertModel->id; $this->orderId = $orderInsertModel->id;
} }

View File

@@ -4,6 +4,7 @@ namespace App\Service\ServiceTrait\Api;
use App\Constants\Common\CouponCode; use App\Constants\Common\CouponCode;
use App\Constants\Common\OrderCode; use App\Constants\Common\OrderCode;
use App\Exception\ErrException;
use App\Extend\DateUtil; use App\Extend\DateUtil;
use App\Model\UserCoupon; use App\Model\UserCoupon;
use Exception; use Exception;
@@ -40,6 +41,46 @@ trait CouponTrait
if (!$couponInfo->save()) throw new Exception('CancelOrderConsumer:error:couponStatusUpdateError:'.json_encode($orderInfo->toArray())); if (!$couponInfo->save()) throw new Exception('CancelOrderConsumer:error:couponStatusUpdateError:'.json_encode($orderInfo->toArray()));
} }
/**
* @return void
* @throws Exception
*/
protected function couponLock(): void
{
if ($this->couponId <= 0) return;
$couponInfo = $this->userCouponModel->where('id', $this->couponId)->where('user_id',$this->userId)->first();
if (empty($couponInfo)) return;
$couponInfo->status = CouponCode::COUPON_STATUS_USED;
$couponInfo->use_time = date('Y-m-d H:i:s');
if (date('Y-m-d H:i:s') > $couponInfo->validity_end_time) throw new Exception('优惠券已过期');
if (!$couponInfo->save()) throw new Exception('couponLock:error:couponLockUpdateError:'.json_encode($couponInfo->toArray()));
}
/**
* @return void
* @throws ErrException
*/
protected function rollbackCouponLock(): void
{
if ($this->couponId <= 0) return;
$couponInfo = $this->userCouponModel->where('id', $this->couponId)->where('user_id',$this->userId)->first();
if (empty($couponInfo)) return;
if ($couponInfo->status != CouponCode::COUPON_STATUS_USED) return;
$couponInfo->status = CouponCode::COUPON_STATUS_UNUSED;
$couponInfo->use_time = '1970-01-01 00:00:00';
if (!$couponInfo->save()) throw new ErrException('系统错误-rollback_coupon失败');
}
/** /**
* @param int $type * @param int $type
* @param string $value * @param string $value