Browse Source

Merge branch 'coupon_rebate' of ssh://120.24.33.109:11022/hyzjshwo/lanzu_api_hyperf into coupon_rebate

master
liangyuyan 5 years ago
parent
commit
452859fab4
  1. 11
      app/Amqp/Consumer/couponRebateConsumer.php
  2. 15
      app/Controller/CouponRebateController.php
  3. 15
      app/Service/CouponRebate.php
  4. 12
      app/Service/CouponRebateInterface.php
  5. 161
      app/Service/CouponRebateService.php
  6. 2
      app/Service/CouponRebateServiceInterface.php
  7. 2
      config/routes.php

11
app/Amqp/Consumer/couponRebateConsumer.php

@ -8,16 +8,27 @@ use Hyperf\Amqp\Result;
use Hyperf\Amqp\Annotation\Consumer; use Hyperf\Amqp\Annotation\Consumer;
use Hyperf\Amqp\Message\ConsumerMessage; use Hyperf\Amqp\Message\ConsumerMessage;
use PhpAmqpLib\Message\AMQPMessage; use PhpAmqpLib\Message\AMQPMessage;
use App\Service\CouponRebateService;
use App\Service\CouponRebateServiceInterface;
use Hyperf\Di\Annotation\Inject;
/** /**
* @Consumer(exchange="couponRebate", routingKey="couponRebate", queue="couponRebate", name ="couponRebateConsumer", nums=1) * @Consumer(exchange="couponRebate", routingKey="couponRebate", queue="couponRebate", name ="couponRebateConsumer", nums=1)
*/ */
class couponRebateConsumer extends ConsumerMessage class couponRebateConsumer extends ConsumerMessage
{ {
/**
* @Inject
* @var CouponRebateService
*/
protected $CouponRebateService;
public function consumeMessage($data, AMQPMessage $message): string public function consumeMessage($data, AMQPMessage $message): string
{ {
var_dump($data); var_dump($data);
var_dump($message->getBody()); var_dump($message->getBody());
$res = $this->CouponRebateService->couponRebate($data);
var_dump($res);
if (false) { if (false) {
return Result::REQUEUE; return Result::REQUEUE;
} }

15
app/Controller/CouponRebateController.php

@ -26,11 +26,11 @@ use App\Request\CouponRebateTieRequest;
class CouponRebateController extends BaseController class CouponRebateController extends BaseController
{ {
/**
/**
* @Inject * @Inject
* @var CouponRebateService * @var CouponRebateService
*/ */
protected $CouponRebateService;
protected $CouponRebateService;
/** /**
* 用户是否领取过领取优惠券 * 用户是否领取过领取优惠券
@ -51,15 +51,22 @@ class CouponRebateController extends BaseController
return $this->success($res); return $this->success($res);
} }
/** /**
* 用户领取优惠券 * 用户领取优惠券
*/
*/
public function userReceiveCoupon(CouponRebateReceiveRequest $validator) public function userReceiveCoupon(CouponRebateReceiveRequest $validator)
{ {
return $this->success($this->CouponRebateService->userReceiveCoupon($this->request->all())); return $this->success($this->CouponRebateService->userReceiveCoupon($this->request->all()));
} }
public function couponRebate()
{
$order_id = $this->request->input('order_id', 0);
$res = $this->CouponRebateService->couponRebate($order_id);
return $this->success($res);
}
/** /**
* 将优惠券绑定活动 * 将优惠券绑定活动
*/ */

15
app/Service/CouponRebate.php

@ -1,15 +0,0 @@
<?php
namespace App\Service;
use Hyperf\DbConnection\Db;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\Di\Annotation\Inject;
use Hyperf\Paginator\Paginator;
class CouponRebate implements CouponRebateInterface
{
}

12
app/Service/CouponRebateInterface.php

@ -1,12 +0,0 @@
<?php
namespace App\Service;
interface CouponRebateInterface
{
public function isCouponRebate($user_id);
public function getActiveInfo();
}

161
app/Service/CouponRebateService.php

@ -16,7 +16,7 @@ use Exception;
class CouponRebateService implements CouponRebateServiceInterface class CouponRebateService implements CouponRebateServiceInterface
{ {
/**
/**
* @Inject * @Inject
* @var Log * @var Log
*/ */
@ -37,13 +37,13 @@ class CouponRebateService implements CouponRebateServiceInterface
$phone = $params["phone"]; $phone = $params["phone"];
$now = time(); $now = time();
$ids = is_array($ids) ? $ids : explode(',',$ids); $ids = is_array($ids) ? $ids : explode(',',$ids);
// status: 0领取成功 >0领取失败 // status: 0领取成功 >0领取失败
$result = [ $result = [
'status' => 1, 'status' => 1,
'coupon_text' => '继续努力~' 'coupon_text' => '继续努力~'
]; ];
// 错误日志记录 // 错误日志记录
$errorData = [ $errorData = [
'coupon_ids' =>$ids, 'coupon_ids' =>$ids,
@ -54,24 +54,24 @@ class CouponRebateService implements CouponRebateServiceInterface
]; ];
$receiveSsdb = []; $receiveSsdb = [];
try{ try{
Db::transaction( function() use ($ids,$receiveType,$userId,$sendUserId,$phone,$now,&$result,&$errorData,&$receiveSsdb) { Db::transaction( function() use ($ids,$receiveType,$userId,$sendUserId,$phone,$now,&$result,&$errorData,&$receiveSsdb) {
$success = []; $success = [];
$fail = []; $fail = [];
$isr = []; $isr = [];
//读写锁,完全控制,性能低 //读写锁,完全控制,性能低
$coupons = Coupon::whereIn('id', $ids)->lockForUpdate() $coupons = Coupon::whereIn('id', $ids)->lockForUpdate()
->select('id','title','status','inventory','inventory_use','start_time','end_time','full_amount','discounts','active_type')
->get();
->select('id','title','status','inventory','inventory_use','start_time','end_time','full_amount','discounts','active_type')
->get();
foreach($coupons as $coupon){ foreach($coupons as $coupon){
$errorData['coupon_id'] = $coupon->id; $errorData['coupon_id'] = $coupon->id;
//如果优惠卷库存小于等于已领取的数量 或者 未在活动时间内可领取, 否则返回领取失败的优惠券 //如果优惠卷库存小于等于已领取的数量 或者 未在活动时间内可领取, 否则返回领取失败的优惠券
if (
if (
$coupon->status == 1 $coupon->status == 1
&& &&
$coupon->inventory > $coupon->inventory_use $coupon->inventory > $coupon->inventory_use
@ -81,7 +81,7 @@ class CouponRebateService implements CouponRebateServiceInterface
$coupon->end_time >= $now $coupon->end_time >= $now
&& &&
$coupon->active_type == 2 $coupon->active_type == 2
)
)
{ {
// 查询一次能领取的数量 // 查询一次能领取的数量
@ -90,15 +90,15 @@ class CouponRebateService implements CouponRebateServiceInterface
$couponReceiveType->where('receive_type',$receiveType); $couponReceiveType->where('receive_type',$receiveType);
} }
$couponReceiveType = $couponReceiveType->first(); $couponReceiveType = $couponReceiveType->first();
// 优惠券可领取数量 >= 本次领取数量 // 优惠券可领取数量 >= 本次领取数量
if($coupon->inventory - $coupon->inventory_use >= $couponReceiveType->one_receive_number){ if($coupon->inventory - $coupon->inventory_use >= $couponReceiveType->one_receive_number){
// 判断是否领取过 存在记录则领取过 // 判断是否领取过 存在记录则领取过
$isReceive = CouponRec::select('id') $isReceive = CouponRec::select('id')
->where('system_coupon_user_id',$coupon->id)
->where('user_id',$userId)
->exists();
->where('system_coupon_user_id',$coupon->id)
->where('user_id',$userId)
->exists();
if(!$isReceive){ if(!$isReceive){
//记录已领取的数量 //记录已领取的数量
@ -116,7 +116,7 @@ class CouponRebateService implements CouponRebateServiceInterface
$couponReceive->receive_type = $receiveType; $couponReceive->receive_type = $receiveType;
$couponReceive->send_user_id = $sendUserId; $couponReceive->send_user_id = $sendUserId;
$couponReceive->phone = $phone; $couponReceive->phone = $phone;
// if ( $couponReceive->save() && $coupon->save() ) { // if ( $couponReceive->save() && $coupon->save() ) {
// $success[] = $coupon; // $success[] = $coupon;
// $receiveSsdb[] = $coupon->id;; // $receiveSsdb[] = $coupon->id;;
@ -126,12 +126,12 @@ class CouponRebateService implements CouponRebateServiceInterface
$receiveSsdb[] = $coupon->id; $receiveSsdb[] = $coupon->id;
$result['status'] = 2; $result['status'] = 2;
$result['coupon_text'] = '您已领取!赶快去下单吧~'; $result['coupon_text'] = '您已领取!赶快去下单吧~';
$errorData['msg'] = '用户已经领取了优惠券'; $errorData['msg'] = '用户已经领取了优惠券';
$this->log->event( $this->log->event(
LogLabel::COUPON_LOG, LogLabel::COUPON_LOG,
$errorData $errorData
);
);
} }
}else{ }else{
$fail[] = $coupon; $fail[] = $coupon;
@ -166,7 +166,7 @@ class CouponRebateService implements CouponRebateServiceInterface
$this->log->event( $this->log->event(
LogLabel::COUPON_LOG, LogLabel::COUPON_LOG,
$errorData $errorData
);
);
} }
if(count($receiveSsdb) > 0){ if(count($receiveSsdb) > 0){
$saveSsdb = []; $saveSsdb = [];
@ -187,32 +187,38 @@ class CouponRebateService implements CouponRebateServiceInterface
return $result; return $result;
} }
/*
* 判断用户是否已领取过优惠券
* */
public function isCouponRebate($user_id) public function isCouponRebate($user_id)
{ {
$res = Db::table('ims_system_coupon_user as u') $res = Db::table('ims_system_coupon_user as u')
->leftjoin('ims_system_coupon_user_receive as r','u.id','=','r.system_coupon_user_id')
->leftjoin('ims_system_coupon_user_receive as r', 'u.id', '=', 'r.system_coupon_user_id')
->where([ ->where([
['r.user_id','=',$user_id],
['r.receive_type','=',4],
['u.active_type','=',2],
['r.user_id', '=', $user_id],
['r.receive_type', '=', 4],
['u.active_type', '=', 2],
]) ])
->select('r.id') ->select('r.id')
->first(); ->first();
return $res; return $res;
} }
/*
*获取活动信息
*/
public function getActiveInfo() public function getActiveInfo()
{ {
$time = time(); $time = time();
$res = Db::table('ims_system_coupon_user') $res = Db::table('ims_system_coupon_user')
->where([ ->where([
['status','=',1],
['active_type','=',2],
['start_time','<=',$time],
['end_time','>',$time],
['status', '=', 1],
['active_type', '=', 2],
['start_time', '<=', $time],
['end_time', '>', $time],
]) ])
->whereRaw('inventory > inventory_use') ->whereRaw('inventory > inventory_use')
->orderBy('addtime','desc')
->orderBy('addtime', 'desc')
->get(); ->get();
return $res; return $res;
} }
@ -221,47 +227,47 @@ class CouponRebateService implements CouponRebateServiceInterface
* 将优惠券绑定活动 * 将优惠券绑定活动
* 领取优惠券 COUPON_REBATE_FORWARD 可多张 * 领取优惠券 COUPON_REBATE_FORWARD 可多张
* 返还优惠券 COUPON_REBATE_REPAY 只一张 * 返还优惠券 COUPON_REBATE_REPAY 只一张
*/
public function tieCouponActive($couponActivity,$couponForward,$couponRepay)
*/
public function tieCouponActive($couponActivity, $couponForward, $couponRepay)
{ {
$result = [ $result = [
'forward' => true, 'forward' => true,
'repay' => true,
'repay' => true,
]; ];
$ssdb = ApplicationContext::getContainer()->get(SSDBTask::class); $ssdb = ApplicationContext::getContainer()->get(SSDBTask::class);
$rrss1 = $ssdb->exec('hgetall',SsdbKeysPrefix::COUPON_REBATE_FORWARD.$couponActivity);
$rrss2 = $ssdb->exec('get',SsdbKeysPrefix::COUPON_REBATE_REPAY.$couponActivity);
return [$rrss1,$rrss2];
$rrss1 = $ssdb->exec('hgetall', SsdbKeysPrefix::COUPON_REBATE_FORWARD . $couponActivity);
$rrss2 = $ssdb->exec('get', SsdbKeysPrefix::COUPON_REBATE_REPAY . $couponActivity);
return [$rrss1, $rrss2];
// 记录领取类型优惠券 // 记录领取类型优惠券
$forwardData = []; $forwardData = [];
foreach($couponForward as $kForward => $vForward){
foreach ($couponForward as $kForward => $vForward) {
$forwardData[] = $kForward; $forwardData[] = $kForward;
$forwardData[] = $vForward; $forwardData[] = $vForward;
} }
if(false === $ssdb->exec('multi_hset',SsdbKeysPrefix::COUPON_REBATE_FORWARD.$couponActivity,$forwardData)){
if (false === $ssdb->exec('multi_hset', SsdbKeysPrefix::COUPON_REBATE_FORWARD . $couponActivity, $forwardData)) {
$this->log->event( $this->log->event(
LogLabel::COUPON_LOG, LogLabel::COUPON_LOG,
[ [
'coupon_activity' => $couponActivity, 'coupon_activity' => $couponActivity,
'coupon_forward' => $forwardData,
'msg' => '绑定-领取-优惠券到ssdb失败'
'coupon_forward' => $forwardData,
'msg' => '绑定-领取-优惠券到ssdb失败'
] ]
); );
$result['forward'] = false; $result['forward'] = false;
}; };
// 记录返还类型优惠券 // 记录返还类型优惠券
if(false === $ssdb->exec('set',SsdbKeysPrefix::COUPON_REBATE_REPAY.$couponActivity,$couponRepay)){
if (false === $ssdb->exec('set', SsdbKeysPrefix::COUPON_REBATE_REPAY . $couponActivity, $couponRepay)) {
$this->log->event( $this->log->event(
LogLabel::COUPON_LOG, LogLabel::COUPON_LOG,
[ [
'coupon_activity' => $couponActivity, 'coupon_activity' => $couponActivity,
'coupon_Repay' => $couponRepay,
'msg' => '绑定-返还-优惠券到ssdb失败'
'coupon_Repay' => $couponRepay,
'msg' => '绑定-返还-优惠券到ssdb失败'
] ]
); );
$result['repay'] = false; $result['repay'] = false;
@ -269,4 +275,81 @@ class CouponRebateService implements CouponRebateServiceInterface
return $result; return $result;
} }
/*
* 支付成功 返券
*/
public function couponRebate($order_id)
{
/* 判断优惠券类型是否为转发活动优惠券 */
$coupon = Db::table('ims_system_coupon_user_receive as r')
->leftjoin('ims_system_coupon_user_use as u', 'u.user_receive_id', '=', 'r.id')
->where([
['u.order_main_id', '=', $order_id],
['r.send_user_id', '>', 0],
['r.rebate_type', '=', 1],
['r.receive_type', '=', 4],
['u.status', '=', 1],
])
->select('r.id', 'r.send_user_id', 'u.system_coupon_id')
->first();
/* 如果使用的优惠券为转发活动优惠券 则给赠送者返一张优惠券*/
if (isset($coupon->send_user_id) && ($coupon->send_user_id) > 0) {
//判断该优惠券是否有库存
// $inventory = Db::table('system_coupon_user_receive as r');
// if($inventory['inventory'] <= 0){
// return '库存不足';
// }
//return $exist_coupon;
//开启事务
try {
/*
* 如果已有该优惠券 则领取数量 可用数量 自增1
* 否则新增一条返券记录
*/
$nowTime = time();
$res = Db::table('ims_system_coupon_user_receive')->moreIncrementOrInsert(
[
'system_coupon_user_id' => $coupon->system_coupon_id,
'user_id' => $coupon->send_user_id,
'receive_type' => 5,
'status' => 0,
],
[
'order_main_id' => $order_id,
'receive_time' => $nowTime,
'update_time' => $nowTime,
'created_at' => $nowTime,
'updated_at' => $nowTime,
],
[
'number' => 1,
'number_remain' => 1,
]
);
// if ($res) {
// //首次返券更新rebate_type字段 防止重复返券
// Db::table('system_coupon_user_receive')->where('id', $coupon->id)->update(['rebate_type' => 2]);
// //更新库存操作
// Db::table('system_coupon_user')
// ->where('id', $coupon->id)
// ->increment('inventory_use');
// } else {
// Db::rollBack();
// return '事务失败';
// }
// 提交
Db::commit();
} catch (\Exception $e) {
// 回滚
Db::rollBack();
return '返券失败';
}
} else {
// $str1 = 'order_main_id:'.$this->order_id . ',未查询到用户领取优惠券信息['.$coupon.']';
// $this->couponErrorLog($str1);
}
return '返券成功';
}
} }

2
app/Service/CouponRebateServiceInterface.php

@ -15,4 +15,6 @@ interface CouponRebateServiceInterface
public function tieCouponActive($couponActivity,$couponForward,$couponRepay); public function tieCouponActive($couponActivity,$couponForward,$couponRepay);
public function couponRebate($order_id);
} }

2
config/routes.php

@ -30,4 +30,6 @@ Router::addGroup('/v1/',function (){
Router::post('CouponRebate/userReceiveCoupon', 'App\Controller\CouponRebateController@userReceiveCoupon'); Router::post('CouponRebate/userReceiveCoupon', 'App\Controller\CouponRebateController@userReceiveCoupon');
Router::post('CouponRebate/getActiveInfo', 'App\Controller\CouponRebateController@getActiveInfo'); Router::post('CouponRebate/getActiveInfo', 'App\Controller\CouponRebateController@getActiveInfo');
Router::post('CouponRebate/tieCouponActive', 'App\Controller\CouponRebateController@tieCouponActive'); Router::post('CouponRebate/tieCouponActive', 'App\Controller\CouponRebateController@tieCouponActive');
Router::post('CouponRebate/couponRebate', 'App\Controller\CouponRebateController@couponRebate');
}); });
Loading…
Cancel
Save