From 0dbf1b1b17da7f1f4380d6938115d10045ca7566 Mon Sep 17 00:00:00 2001 From: "DESKTOP-GG6FIN9\\Administrator" <15040771@qq.com> Date: Fri, 7 Aug 2020 18:19:56 +0800 Subject: [PATCH] =?UTF-8?q?=E8=BF=94=E5=88=B8=20=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Amqp/Consumer/couponRebateConsumer.php | 12 ++- app/Service/CouponRebateService.php | 89 ++++++++++++++-------- 2 files changed, 67 insertions(+), 34 deletions(-) diff --git a/app/Amqp/Consumer/couponRebateConsumer.php b/app/Amqp/Consumer/couponRebateConsumer.php index 475c7b9..e04615e 100644 --- a/app/Amqp/Consumer/couponRebateConsumer.php +++ b/app/Amqp/Consumer/couponRebateConsumer.php @@ -4,16 +4,19 @@ declare(strict_types=1); namespace App\Amqp\Consumer; +use App\Constants\SsdbKeysPrefix; use Hyperf\Amqp\Result; use Hyperf\Amqp\Annotation\Consumer; use Hyperf\Amqp\Message\ConsumerMessage; +use Hyperf\Utils\ApplicationContext; 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="couponRebate1", routingKey="couponRebate1", queue="couponRebate1", name ="couponRebateConsumer", nums=1) */ class couponRebateConsumer extends ConsumerMessage { @@ -25,10 +28,11 @@ class couponRebateConsumer extends ConsumerMessage 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); - var_dump($res); + //var_dump($res); if (false) { return Result::REQUEUE; } diff --git a/app/Service/CouponRebateService.php b/app/Service/CouponRebateService.php index d3474ff..b3db743 100644 --- a/app/Service/CouponRebateService.php +++ b/app/Service/CouponRebateService.php @@ -208,8 +208,13 @@ class CouponRebateService implements CouponRebateServiceInterface * */ 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') ->leftjoin('ims_system_coupon_user_receive as r', 'u.id', '=', 'r.system_coupon_user_id') + ->whereIn('r.system_coupon_user_id',$coupon_ids) ->where([ ['r.user_id', '=', $user_id], ['r.receive_type', '=', 4], @@ -225,8 +230,13 @@ class CouponRebateService implements CouponRebateServiceInterface */ 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(); $res = Db::table('ims_system_coupon_user') + ->whereIn('id',$coupon_ids) ->where([ ['status', '=', 1], ['active_type', '=', 2], @@ -234,6 +244,7 @@ class CouponRebateService implements CouponRebateServiceInterface ['end_time', '>', $time], ]) ->whereRaw('inventory > inventory_use') + ->orderBy('weigh', 'desc') ->orderBy('addtime', 'desc') ->get(); return $res; @@ -313,19 +324,22 @@ class CouponRebateService implements CouponRebateServiceInterface */ 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') - ->where('id',$rebate_id) + ->where('id',$active['repay']) ->whereRaw('inventory > inventory_use') ->exists(); if(!$inventory){ return '库存不足'; } - /* 判断优惠券类型是否为转发活动优惠券 */ + + //获取活动发放优惠券id + $coupon_ids = explode(',',$active['forward']); + + /* 判断被使用的优惠券类型是否为转发活动优惠券 */ $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([ @@ -333,49 +347,52 @@ class CouponRebateService implements CouponRebateServiceInterface ['r.send_user_id', '>', 0], ['r.rebate_type', '=', 1], ['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(); /* 如果使用的优惠券为转发活动优惠券 则给赠送者返一张优惠券*/ if (isset($coupon->send_user_id) && ($coupon->send_user_id) > 0) { //是否已返过券 $exists_coupon_rebate = Db::table('ims_system_coupon_user_receive') ->where([ - ['system_coupon_user_id' ,'=', $rebate_id], + ['system_coupon_user_id' ,'=', $active['repay']], ['user_id' ,'=', $coupon->send_user_id], ['receive_type' ,'=', 5], - ['status' ,'=', 0], ]) - ->exists(); + ->select('id','status') + ->first(); //开启事务 Db::beginTransaction(); try { - /* - * 如果已有该优惠券 则领取数量 和 可用数量 自增1 - * 否则新增一条返券记录 - */ - if($exists_coupon_rebate){ - var_dump(222); + //返券 + if($exists_coupon_rebate->id){ + //如果已有该优惠券 则领取数量 和 可用数量 自增1 Db::table('ims_system_coupon_user_receive') ->where([ - ['user_id' ,'=', $coupon->send_user_id], - ['receive_type' ,'=', 5], + ['id' ,'=', $exists_coupon_rebate->id], ]) ->increment('number'); Db::table('ims_system_coupon_user_receive') ->where([ - ['user_id' ,'=', $coupon->send_user_id], - ['receive_type' ,'=', 5], + ['id' ,'=', $exists_coupon_rebate->id], ]) ->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 { - var_dump(111111222); + //否则新增一条返券记录 $nowTime = time(); Db::table('ims_system_coupon_user_receive')->insert([ [ 'user_id' => $coupon->send_user_id, - 'system_coupon_user_id' => $rebate_id, + 'system_coupon_user_id' => $active['repay'], 'receive_type' => 5, 'status' => 0, 'number' => 1, @@ -394,26 +411,38 @@ class CouponRebateService implements CouponRebateServiceInterface ['user_id','=',$coupon->user_id], ['receive_type','=',4], ]) + ->whereIn('system_coupon_user_id',$coupon_ids) ->update(['rebate_type' => 2]); //更新库存操作 Db::table('ims_system_coupon_user') - ->where('id', $rebate_id) + ->where('id', $active['repay']['repay']) ->increment('inventory_use'); + // 提交 Db::commit(); } catch (\Exception $e) { // 回滚 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( 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 '返券成功'; } } \ No newline at end of file