Browse Source

返券 测试版

master
parent
commit
0dbf1b1b17
  1. 12
      app/Amqp/Consumer/couponRebateConsumer.php
  2. 89
      app/Service/CouponRebateService.php

12
app/Amqp/Consumer/couponRebateConsumer.php

@ -4,16 +4,19 @@ declare(strict_types=1);
namespace App\Amqp\Consumer; namespace App\Amqp\Consumer;
use App\Constants\SsdbKeysPrefix;
use Hyperf\Amqp\Result; 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 Hyperf\Utils\ApplicationContext;
use PhpAmqpLib\Message\AMQPMessage; use PhpAmqpLib\Message\AMQPMessage;
use App\Service\CouponRebateService; use App\Service\CouponRebateService;
use App\Service\CouponRebateServiceInterface; use App\Service\CouponRebateServiceInterface;
use Hyperf\Di\Annotation\Inject; use Hyperf\Di\Annotation\Inject;
/** /**
* @Consumer(exchange="couponRebate", routingKey="couponRebate", queue="couponRebate", name ="couponRebateConsumer", nums=1)
* @Consumer(exchange="couponRebate1", routingKey="couponRebate1", queue="couponRebate1", name ="couponRebateConsumer", nums=1)
*/ */
class couponRebateConsumer extends ConsumerMessage class couponRebateConsumer extends ConsumerMessage
{ {
@ -25,10 +28,11 @@ class couponRebateConsumer extends ConsumerMessage
public function consumeMessage($data, AMQPMessage $message): string public function consumeMessage($data, AMQPMessage $message): string
{ {
var_dump($data);
var_dump($message->getBody());
//var_dump($data);
//ar_dump($rebate_id);
//var_dump($message->getBody());
$res = $this->CouponRebateService->couponRebate($data); $res = $this->CouponRebateService->couponRebate($data);
var_dump($res);
//var_dump($res);
if (false) { if (false) {
return Result::REQUEUE; return Result::REQUEUE;
} }

89
app/Service/CouponRebateService.php

@ -208,8 +208,13 @@ class CouponRebateService implements CouponRebateServiceInterface
* */ * */
public function isCouponRebate($user_id) public function isCouponRebate($user_id)
{ {
//获取SSDB上的活动信息
$ssdb = ApplicationContext::getContainer()->get(SSDBTask::class);
$active = $ssdb->exec('hgetall',SsdbKeysPrefix::COUPON_REBATE_ACTIVITY);
$coupon_ids = explode(',',$active['forward']);
$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')
->whereIn('r.system_coupon_user_id',$coupon_ids)
->where([ ->where([
['r.user_id', '=', $user_id], ['r.user_id', '=', $user_id],
['r.receive_type', '=', 4], ['r.receive_type', '=', 4],
@ -225,8 +230,13 @@ class CouponRebateService implements CouponRebateServiceInterface
*/ */
public function getActiveInfo() public function getActiveInfo()
{ {
//获取SSDB上的活动信息
$ssdb = ApplicationContext::getContainer()->get(SSDBTask::class);
$active = $ssdb->exec('hgetall',SsdbKeysPrefix::COUPON_REBATE_ACTIVITY);
$coupon_ids = explode(',',$active['forward']);
$time = time(); $time = time();
$res = Db::table('ims_system_coupon_user') $res = Db::table('ims_system_coupon_user')
->whereIn('id',$coupon_ids)
->where([ ->where([
['status', '=', 1], ['status', '=', 1],
['active_type', '=', 2], ['active_type', '=', 2],
@ -234,6 +244,7 @@ class CouponRebateService implements CouponRebateServiceInterface
['end_time', '>', $time], ['end_time', '>', $time],
]) ])
->whereRaw('inventory > inventory_use') ->whereRaw('inventory > inventory_use')
->orderBy('weigh', 'desc')
->orderBy('addtime', 'desc') ->orderBy('addtime', 'desc')
->get(); ->get();
return $res; return $res;
@ -313,19 +324,22 @@ class CouponRebateService implements CouponRebateServiceInterface
*/ */
public function couponRebate($order_id) public function couponRebate($order_id)
{ {
//$ssdb = ApplicationContext::getContainer()->get(SSDBTask::class);
// $active_id = $ssdb->exec('get',SsdbKeysPrefix::COUPON_REBATE_ACTIVITY);
// $rebate_id = $ssdb->exec('get',SsdbKeysPrefix::COUPON_REBATE_REPAY.$active_id);
$rebate_id = 56;
//判断该优惠券是否有库存
//获取SSDB上的活动信息
$ssdb = ApplicationContext::getContainer()->get(SSDBTask::class);
$active = $ssdb->execWithoutTask('hgetall',SsdbKeysPrefix::COUPON_REBATE_ACTIVITY);
//判断返券优惠券是否有库存
$inventory = Db::table('ims_system_coupon_user') $inventory = Db::table('ims_system_coupon_user')
->where('id',$rebate_id)
->where('id',$active['repay'])
->whereRaw('inventory > inventory_use') ->whereRaw('inventory > inventory_use')
->exists(); ->exists();
if(!$inventory){ if(!$inventory){
return '库存不足'; return '库存不足';
} }
/* 判断优惠券类型是否为转发活动优惠券 */
//获取活动发放优惠券id
$coupon_ids = explode(',',$active['forward']);
/* 判断被使用的优惠券类型是否为转发活动优惠券 */
$coupon = Db::table('ims_system_coupon_user_receive as r') $coupon = Db::table('ims_system_coupon_user_receive as r')
->leftjoin('ims_system_coupon_user_use as u', 'u.user_receive_id', '=', 'r.id') ->leftjoin('ims_system_coupon_user_use as u', 'u.user_receive_id', '=', 'r.id')
->where([ ->where([
@ -333,49 +347,52 @@ class CouponRebateService implements CouponRebateServiceInterface
['r.send_user_id', '>', 0], ['r.send_user_id', '>', 0],
['r.rebate_type', '=', 1], ['r.rebate_type', '=', 1],
['r.receive_type', '=', 4], ['r.receive_type', '=', 4],
['u.status', '=', 1],
]) ])
->select('r.id', 'r.user_id', 'r.send_user_id', 'u.system_coupon_id')
->whereIn('r.system_coupon_user_id',$coupon_ids)
->select('r.user_id', 'r.send_user_id')
->first(); ->first();
/* 如果使用的优惠券为转发活动优惠券 则给赠送者返一张优惠券*/ /* 如果使用的优惠券为转发活动优惠券 则给赠送者返一张优惠券*/
if (isset($coupon->send_user_id) && ($coupon->send_user_id) > 0) { if (isset($coupon->send_user_id) && ($coupon->send_user_id) > 0) {
//是否已返过券 //是否已返过券
$exists_coupon_rebate = Db::table('ims_system_coupon_user_receive') $exists_coupon_rebate = Db::table('ims_system_coupon_user_receive')
->where([ ->where([
['system_coupon_user_id' ,'=', $rebate_id],
['system_coupon_user_id' ,'=', $active['repay']],
['user_id' ,'=', $coupon->send_user_id], ['user_id' ,'=', $coupon->send_user_id],
['receive_type' ,'=', 5], ['receive_type' ,'=', 5],
['status' ,'=', 0],
]) ])
->exists();
->select('id','status')
->first();
//开启事务 //开启事务
Db::beginTransaction(); Db::beginTransaction();
try { try {
/*
* 如果已有该优惠券 则领取数量 可用数量 自增1
* 否则新增一条返券记录
*/
if($exists_coupon_rebate){
var_dump(222);
//返券
if($exists_coupon_rebate->id){
//如果已有该优惠券 则领取数量 和 可用数量 自增1
Db::table('ims_system_coupon_user_receive') Db::table('ims_system_coupon_user_receive')
->where([ ->where([
['user_id' ,'=', $coupon->send_user_id],
['receive_type' ,'=', 5],
['id' ,'=', $exists_coupon_rebate->id],
]) ])
->increment('number'); ->increment('number');
Db::table('ims_system_coupon_user_receive') Db::table('ims_system_coupon_user_receive')
->where([ ->where([
['user_id' ,'=', $coupon->send_user_id],
['receive_type' ,'=', 5],
['id' ,'=', $exists_coupon_rebate->id],
]) ])
->increment('number_remain'); ->increment('number_remain');
//如果该用户在领取表中 status为已用完状态 2 则改为已用部分 1
if($exists_coupon_rebate->status == 2){
Db::table('ims_system_coupon_user_receive')
->where([
['id' ,'=', $exists_coupon_rebate->id],
])
->update(['status' => 1]);;
}
}else { }else {
var_dump(111111222);
//否则新增一条返券记录
$nowTime = time(); $nowTime = time();
Db::table('ims_system_coupon_user_receive')->insert([ Db::table('ims_system_coupon_user_receive')->insert([
[ [
'user_id' => $coupon->send_user_id, 'user_id' => $coupon->send_user_id,
'system_coupon_user_id' => $rebate_id,
'system_coupon_user_id' => $active['repay'],
'receive_type' => 5, 'receive_type' => 5,
'status' => 0, 'status' => 0,
'number' => 1, 'number' => 1,
@ -394,26 +411,38 @@ class CouponRebateService implements CouponRebateServiceInterface
['user_id','=',$coupon->user_id], ['user_id','=',$coupon->user_id],
['receive_type','=',4], ['receive_type','=',4],
]) ])
->whereIn('system_coupon_user_id',$coupon_ids)
->update(['rebate_type' => 2]); ->update(['rebate_type' => 2]);
//更新库存操作 //更新库存操作
Db::table('ims_system_coupon_user') Db::table('ims_system_coupon_user')
->where('id', $rebate_id)
->where('id', $active['repay']['repay'])
->increment('inventory_use'); ->increment('inventory_use');
// 提交 // 提交
Db::commit(); Db::commit();
} catch (\Exception $e) { } catch (\Exception $e) {
// 回滚 // 回滚
Db::rollBack(); Db::rollBack();
$errorData['order_main_id'] = $order_id;
$errorData['msg'] = '返券失败';
$log_Data = array();
$log_Data['name'] = '返券';
$log_Data['order_main_id'] = $order_id;
$log_Data['msg'] = '返券失败';
$this->log->event( $this->log->event(
LogLabel::COUPON_LOG, LogLabel::COUPON_LOG,
$errorData
$log_Data
); );
} }
} }
$log_Data = array();
$log_Data['name'] = '返券';
$log_Data['user_id'] = $order_id;
$log_Data['send_user_id'] = $coupon->send_user_id;
$log_Data['order_main_id'] = $order_id;
$log_Data['msg'] = '返券成功';
$this->log->event(
LogLabel::COUPON_LOG,
$log_Data
);
return '返券成功'; return '返券成功';
} }
} }
Loading…
Cancel
Save