|
|
|
@ -9,6 +9,9 @@ use App\Model\CouponUserRecType; |
|
|
|
use App\Model\CouponRec; |
|
|
|
use App\Constants\LogLabel; |
|
|
|
use App\Commons\Log; |
|
|
|
use Hyperf\Utils\ApplicationContext; |
|
|
|
use App\TaskWorker\SSDBTask; |
|
|
|
use App\Constants\SsdbKeysPrefix; |
|
|
|
use Exception; |
|
|
|
|
|
|
|
class CouponRebateService implements CouponRebateServiceInterface |
|
|
|
@ -40,7 +43,7 @@ class CouponRebateService implements CouponRebateServiceInterface |
|
|
|
'status' => 1, |
|
|
|
'coupon_text' => '继续努力~' |
|
|
|
]; |
|
|
|
|
|
|
|
|
|
|
|
// 错误日志记录
|
|
|
|
$errorData = [ |
|
|
|
'coupon_ids' =>$ids, |
|
|
|
@ -50,12 +53,17 @@ class CouponRebateService implements CouponRebateServiceInterface |
|
|
|
'phone' =>$phone |
|
|
|
]; |
|
|
|
|
|
|
|
Db::transaction( function() use ($ids,$receiveType,$userId,$sendUserId,$phone,$now,&$result,&$errorData) { |
|
|
|
$receiveSsdb = []; |
|
|
|
|
|
|
|
try{ |
|
|
|
Db::transaction( function() use ($ids,$receiveType,$userId,$sendUserId,$phone,$now,&$result,&$errorData,&$receiveSsdb) { |
|
|
|
|
|
|
|
try{ |
|
|
|
$success = []; |
|
|
|
$fail = []; |
|
|
|
|
|
|
|
//读写锁,完全控制,性能低
|
|
|
|
$coupons = Coupon::whereIn('id', $ids)->lockForUpdate() |
|
|
|
->select('id','title','status','inventory','inventory_use','start_time','end_time','full_amount','discounts') |
|
|
|
->select('id','title','status','inventory','inventory_use','start_time','end_time','full_amount','discounts','active_type') |
|
|
|
->get(); |
|
|
|
|
|
|
|
foreach($coupons as $coupon){ |
|
|
|
@ -70,6 +78,8 @@ class CouponRebateService implements CouponRebateServiceInterface |
|
|
|
$coupon->start_time <= $now |
|
|
|
&& |
|
|
|
$coupon->end_time >= $now |
|
|
|
&& |
|
|
|
$coupon->active_type == 2 |
|
|
|
) |
|
|
|
{ |
|
|
|
|
|
|
|
@ -107,10 +117,12 @@ class CouponRebateService implements CouponRebateServiceInterface |
|
|
|
$couponReceive->phone = $phone; |
|
|
|
|
|
|
|
if ( $couponReceive->save() && $coupon->save() ) { |
|
|
|
$result['status'] = 0; |
|
|
|
$result['coupon_text'] = '恭喜您领取成功!'; |
|
|
|
$success[] = $coupon; |
|
|
|
$receiveSsdb[] = $coupon->id;; |
|
|
|
} |
|
|
|
}else{ |
|
|
|
$fail[] = $coupon; |
|
|
|
$receiveSsdb[] = $coupon->id; |
|
|
|
$result['status'] = 2; |
|
|
|
$result['coupon_text'] = '您已领取!赶快去下单吧~'; |
|
|
|
|
|
|
|
@ -121,6 +133,7 @@ class CouponRebateService implements CouponRebateServiceInterface |
|
|
|
); |
|
|
|
} |
|
|
|
}else{ |
|
|
|
$fail[] = $coupon; |
|
|
|
$errorData['remain_receive_number'] = $coupon->inventory - $coupon->inventory_use; |
|
|
|
$errorData['one_receive_number'] = $couponReceiveType->one_receive_number; |
|
|
|
$errorData['msg'] = '优惠券剩余数量不足'; |
|
|
|
@ -130,6 +143,7 @@ class CouponRebateService implements CouponRebateServiceInterface |
|
|
|
); |
|
|
|
} |
|
|
|
}else{ |
|
|
|
$fail[] = $coupon; |
|
|
|
$errorData['msg'] = '优惠券已经过期或者被禁用或者已领完'; |
|
|
|
$this->log->event( |
|
|
|
LogLabel::COUPON_LOG, |
|
|
|
@ -137,16 +151,33 @@ class CouponRebateService implements CouponRebateServiceInterface |
|
|
|
); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} catch (Exception $e){ |
|
|
|
$errorData['msg'] = $e->getMessage(); |
|
|
|
$result['data'] = [ |
|
|
|
'success' => $success, |
|
|
|
'fail' => $fail |
|
|
|
]; |
|
|
|
if(count($success) > 0){ |
|
|
|
$result['status'] = 0; |
|
|
|
$result['coupon_text'] = '恭喜您领取成功!'; |
|
|
|
} |
|
|
|
}); |
|
|
|
} catch (Exception $e){ |
|
|
|
$errorData['msg'] = $e->getMessage(); |
|
|
|
$this->log->event( |
|
|
|
LogLabel::COUPON_LOG, |
|
|
|
$errorData |
|
|
|
); |
|
|
|
} |
|
|
|
if(count($receiveSsdb) > 0){ |
|
|
|
// 记录到ssdb
|
|
|
|
$ssdb = ApplicationContext::getContainer()->get(SSDBTask::class); |
|
|
|
if(false === $ssdb->exec('hset',SsdbKeysPrefix::COUPON_REBATE_RECEIVE.$userId,$receiveSsdb)){ |
|
|
|
$errorData['msg'] = '记录领取优惠券到ssdb失败'; |
|
|
|
$this->log->event( |
|
|
|
LogLabel::COUPON_LOG, |
|
|
|
$errorData |
|
|
|
); |
|
|
|
} |
|
|
|
}); |
|
|
|
|
|
|
|
); |
|
|
|
}; |
|
|
|
} |
|
|
|
return $result; |
|
|
|
} |
|
|
|
|
|
|
|
|