coupon_id <= 0) return; $couponInfo = $this->userCouponModel->where('id', $orderInfo->coupon_id)->where('user_id',$orderInfo->user_id)->first(); if (empty($couponInfo)) return; $couponInfo->status = CouponCode::COUPON_STATUS_UNUSED; if (date('Y-m-d H:i:s') > $couponInfo->validity_end_time) $couponInfo->status = CouponCode::COUPON_STATUS_EXPIRE; 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 string $value * @return array */ protected function getValidityTime(int $type,string $value): array { switch ($type) { case CouponCode::VALIDITY_TIME_TYPE_CYCLE: $validityValue = json_decode($value,true); return [ 'start_time' => date('Y-m-d H:i:s'), 'end_time' => date('Y-m-d H:i:s', time() + ($validityValue['day_num'] * DateUtil::DAY)) ]; case CouponCode::VALIDITY_TIME_TYPE_FIX: return json_decode($value,true); default: return []; } } }