diff --git a/app/Controller/NotifyController.php b/app/Controller/NotifyController.php index b58bc91..955db3f 100644 --- a/app/Controller/NotifyController.php +++ b/app/Controller/NotifyController.php @@ -13,6 +13,7 @@ use App\Model\Store; use App\Model\StoreAccount; use App\Model\SystemConfig; use App\Model\Users; +use App\Service\CouponRebateServiceInterface; use App\Service\DeviceServiceInterface; use App\Service\FeiePrintServiceInterface; use App\Service\MiniprogramServiceInterface; @@ -61,6 +62,12 @@ class NotifyController extends BaseController */ protected $userService; + /** + * @Inject + * @var CouponRebateServiceInterface + */ + protected $couponRebateService; + public function wxminiOnline() { @@ -175,6 +182,9 @@ class NotifyController extends BaseController $inSalesStatistics = OrderSalesStatistic::query()->insert($statistics); } + // 优惠券返券 + $this->couponRebateService->couponRebateInTask($orderMain->id); + // 喇叭通知,兼容旧音响,MQTT+IOT $res = $this->mqttSpeakerService->speakToStore($orderMain->id); $this->log->event( diff --git a/app/Service/CouponRebateService.php b/app/Service/CouponRebateService.php index 394bf3d..efe3f34 100644 --- a/app/Service/CouponRebateService.php +++ b/app/Service/CouponRebateService.php @@ -379,6 +379,133 @@ class CouponRebateService implements CouponRebateServiceInterface } return true; } + + /* + * 支付成功 返券 + */ + public function couponRebateInTask($order_id) + { + //获取SSDB上的活动信息 + $ssdb = ApplicationContext::getContainer()->get(SSDBTask::class); + $active = $ssdb->exec('hgetall',SsdbKeysPrefix::COUPON_REBATE_ACTIVITY); + //判断返券优惠券是否有库存 + $inventory = Db::table('ims_system_coupon_user') + ->where('id',$active['repay']) + ->whereRaw('inventory > inventory_use') + ->exists(); + if(!$inventory){ + //库存不足 + return false; + } + + //获取活动发放优惠券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([ + ['u.order_main_id', '=', $order_id], + ['r.send_user_id', '>', 0], + ['r.rebate_type', '=', 1], + ['r.receive_type', '=', 4], + ]) + ->whereIn('r.system_coupon_user_id',$coupon_ids) + ->select('r.user_id', 'r.send_user_id') + ->first(); + /* 如果使用的优惠券为转发活动优惠券 + **则给赠送者返一张优惠券 + * **自己给自己转发的券不给返券 + */ + if ($coupon && ($coupon->user_id != $coupon->send_user_id)) { + //是否已返过券 + $exists_coupon_rebate = Db::table('ims_system_coupon_user_receive') + ->where([ + ['system_coupon_user_id' ,'=', $active['repay']], + ['user_id' ,'=', $coupon->send_user_id], + ['receive_type' ,'=', 5], + ]) + ->select('id','status') + ->first(); + //开启事务 + Db::beginTransaction(); + try { + //返券 + if($exists_coupon_rebate){ + //如果已有该优惠券 则领取数量 和 可用数量 自增1 + Db::table('ims_system_coupon_user_receive') + ->where([ + ['id' ,'=', $exists_coupon_rebate->id], + ]) + ->increment('number'); + Db::table('ims_system_coupon_user_receive') + ->where([ + ['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 { + //否则新增一条返券记录 + $nowTime = time(); + Db::table('ims_system_coupon_user_receive')->insert([ + [ + 'user_id' => $coupon->send_user_id, + 'system_coupon_user_id' => $active['repay'], + '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], + ]) + ->whereIn('system_coupon_user_id',$coupon_ids) + ->update(['rebate_type' => 2]); + //更新库存操作 + Db::table('ims_system_coupon_user') + ->where('id', $active['repay']) + ->increment('inventory_use'); + + //添加领取记录到ssdb + $data = [ + $order_id,$coupon->user_id, + ]; + $ssdb->exec('multi_hset', SsdbKeysPrefix::COUPON_REBATE_LIST.$coupon->send_user_id, $data); + // 提交 + Db::commit(); + } catch (\Exception $e) { + //写入日志文件 + $log_Data = array(); + $log_Data['name'] = '返券'; + $log_Data['order_id'] = $order_id; + $log_Data['msg'] = '返券失败'; + $this->log->event( + LogLabel::COUPON_LOG, + $log_Data + ); + // 回滚 + Db::rollBack(); + } + } + return true; + } /** * 清优惠券领取记录(SSDB) diff --git a/app/Service/CouponRebateServiceInterface.php b/app/Service/CouponRebateServiceInterface.php index df8fc5d..4208c28 100644 --- a/app/Service/CouponRebateServiceInterface.php +++ b/app/Service/CouponRebateServiceInterface.php @@ -16,7 +16,9 @@ interface CouponRebateServiceInterface public function tieCouponActive($couponActivity,$couponForward,$couponRepay); public function couponRebate($order_id); - + + public function couponRebateInTask($order_id); + public function clearSsdbCouponReceiveByName($activity, $userId, $get = 0, $isAll = 0); } \ No newline at end of file diff --git a/config/autoload/dependencies.php b/config/autoload/dependencies.php index e7ca333..0f7e101 100644 --- a/config/autoload/dependencies.php +++ b/config/autoload/dependencies.php @@ -25,4 +25,5 @@ return [ \App\Service\MqttServiceInterface::class => \App\Service\MqttSpeakerService::class, \App\Service\FeiePrintServiceInterface::class => \App\Service\FeiePrintService::class, \App\Service\MiniprogramServiceInterface::class => \App\Service\MiniprogramService::class, + \App\Service\UserServiceInterface::class => \App\Service\UserService::class, ];