From 7d989e3d6f4349ce6c977202d9b6f7889ca8837d Mon Sep 17 00:00:00 2001 From: "DESKTOP-GG6FIN9\\Administrator" <15040771@qq.com> Date: Fri, 7 Aug 2020 12:07:40 +0800 Subject: [PATCH] =?UTF-8?q?=E8=BF=94=E5=88=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Service/CouponRebateService.php | 115 ++++++++++++++++++---------- 1 file changed, 73 insertions(+), 42 deletions(-) diff --git a/app/Service/CouponRebateService.php b/app/Service/CouponRebateService.php index be0225a..6641015 100644 --- a/app/Service/CouponRebateService.php +++ b/app/Service/CouponRebateService.php @@ -312,6 +312,18 @@ 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; + //判断该优惠券是否有库存 + $inventory = Db::table('ims_system_coupon_user') + ->where('id',$rebate_id) + ->whereRaw('inventory > inventory_use') + ->exists(); + if(!$inventory){ + return '库存不足'; + } /* 判断优惠券类型是否为转发活动优惠券 */ $coupon = Db::table('ims_system_coupon_user_receive as r') ->leftjoin('ims_system_coupon_user_use as u', 'u.user_receive_id', '=', 'r.id') @@ -322,63 +334,82 @@ class CouponRebateService implements CouponRebateServiceInterface ['r.receive_type', '=', 4], ['u.status', '=', 1], ]) - ->select('r.id', 'r.send_user_id', 'u.system_coupon_id') + ->select('r.id', 'r.user_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; + //是否已返过券 + $exists_coupon_rebate = Db::table('ims_system_coupon_user_receive') + ->where([ + ['system_coupon_user_id' ,'=', $rebate_id], + ['user_id' ,'=', $coupon->send_user_id], + ['receive_type' ,'=', 5], + ['status' ,'=', 0], + ]) + ->exists(); //开启事务 + Db::beginTransaction(); 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 '事务失败'; -// } + if($exists_coupon_rebate){ + var_dump(222); + Db::table('ims_system_coupon_user_receive') + ->where([ + ['user_id' ,'=', $coupon->send_user_id], + ['receive_type' ,'=', 5], + ]) + ->increment('number'); + Db::table('ims_system_coupon_user_receive') + ->where([ + ['user_id' ,'=', $coupon->send_user_id], + ['receive_type' ,'=', 5], + ]) + ->increment('number_remain'); + }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, + 'receive_type' => 5, + 'status' => 0, + 'number' => 1, + 'number_remain' => 1, + 'order_main_id' => $order_id, + 'receive_time' => $nowTime, + 'update_time' => $nowTime, + 'created_at' => $nowTime, + 'updated_at' => $nowTime, + ] + ]); + } + //首次返券更新rebate_type字段 防止重复返券 + Db::table('ims_system_coupon_user_receive') + ->where([ + ['user_id','=',$coupon->user_id], + ['receive_type','=',4], + ]) + ->update(['rebate_type' => 2]); + //更新库存操作 + Db::table('ims_system_coupon_user') + ->where('id', $rebate_id) + ->increment('inventory_use'); // 提交 Db::commit(); } catch (\Exception $e) { // 回滚 Db::rollBack(); - return '返券失败'; + $errorData['order_main_id'] = $order_id; + $errorData['msg'] = '返券失败'; + $this->log->event( + LogLabel::COUPON_LOG, + $errorData + ); } - } else { -// $str1 = 'order_main_id:'.$this->order_id . ',未查询到用户领取优惠券信息['.$coupon.']'; -// $this->couponErrorLog($str1); }