Browse Source

优惠券返券活动-领取成功记录到ssdb

master
liangyuyan 6 years ago
parent
commit
8e004f23c7
  1. 5
      app/Constants/SsdbKeysPrefix.php
  2. 57
      app/Service/CouponRebateService.php

5
app/Constants/SsdbKeysPrefix.php

@ -16,4 +16,9 @@ class SsdbKeysPrefix extends AbstractConstants
* @Message("Params Token Key Prefix")
*/
const PARAMS_TOKEN = 'params_token_';
/**
* @Message("Coupon rebate Key Prefix")
*/
const COUPON_REBATE_RECEIVE = 'coupon_rebate_receive_';
}

57
app/Service/CouponRebateService.php

@ -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;
}

Loading…
Cancel
Save