From a028686fb49a7c54a43098f403b5f1a78c094683 Mon Sep 17 00:00:00 2001 From: wangxinglong <2371974647@qq.com> Date: Mon, 28 Mar 2022 19:34:35 +0800 Subject: [PATCH] setter --- app/controller/api/Coupon.php | 524 +++++++++++++++------------------- app/model/Redpack.php | 19 +- 2 files changed, 243 insertions(+), 300 deletions(-) diff --git a/app/controller/api/Coupon.php b/app/controller/api/Coupon.php index ae508d8..f720114 100644 --- a/app/controller/api/Coupon.php +++ b/app/controller/api/Coupon.php @@ -362,107 +362,12 @@ class Coupon extends Base $data["distribution_user_code"] = $distributionUserCode;//分销人user_code } - $receiveCoupon = CouponRepository::getInstance()->receiveCoupon($data); - $couponMain->save(["received_count"=>Db::raw("received_count + 1")]); - $deductionAmount = (($couponMain->deduction_money / 100) * $couponMain->commission_dis_distribution); - - //是否开启分销 并且有分销人 给分销人发红包 - if( - $couponMain->business->is_assign == BusinessModel::COMMON_OFF - && - !empty($distributionUserCode) - ) - { - $distributionUser = Account::findOne([["user_code","=",$distributionUserCode]]); - if(empty($distributionUser)){ - Db::rollback(); - return $this->json(4001,"分享人不存在"); - } - // 2. 写入优惠券流水 - $couponBillData = [ - "coupon_main_id" => $couponMain->id, - "coupon_id" => $receiveCoupon->id, - "user_code" => $account->user_code, - "business_code" => $couponMain->business->code, - "agency_code" => $couponMain->business->agency_code, - "commission_agency" => $couponMain->commission_agency, - "commission_admin" => $couponMain->commission_admin, - "commission_consumer" => $couponMain->commission_consumer, - "commission_dis_admin" => $couponMain->commission_dis_admin, - "commission_dis_distribution" => $couponMain->commission_dis_distribution, - "commission_dis_distributed" => $couponMain->commission_dis_distributed, - "money" => $couponMain->money, - "agency_money" => 0, - "admin_money" => 0, - "consumer_money" => 0, - "distribution_money" => $deductionAmount, - "lat" => $lat, - "lng" => $lng, - "create_time" => date("Y-m-d H:i:s" ,$time), - ]; - $amount = round($deductionAmount,2); //四舍五入 精确到分 - - CouponBill::create($couponBillData); - - $payment = WechatPay::getInstance(); - $distributionRedpack = Redpack::getDistributionRedpack($distributionUserCode,$couponMainId,$account->user_code); - //如果付款过一次了 就用同一个订单号发起 - if(!empty($distributionRedpack)){ - $mch_billno = $distributionRedpack->mch_billno; - }else{ - $mch_billno = createUuid(); - } + $receiveCoupon = CouponRepository::getInstance()->receiveCoupon($data); - $toBalanceData = [ - 'partner_trade_no' => $mch_billno,// 商户订单号,需保持唯一性(只能是字母或者数字,不能包含有符号) - 'openid' => $distributionUser->open_id, - 'check_name' => 'NO_CHECK',// NO_CHECK:不校验真实姓名, FORCE_CHECK:强校验真实姓名 - 'amount' => $amount * 100, //单位为分,不小于300 - 'desc' => '优惠券分销奖励', - ]; - - // 写入红包 - $redpackData = [ - "coupon_id" => 0, - "mch_billno" => $mch_billno, - "openid" => $distributionUser->open_id, - "user_code" => $distributionUser->user_code, - "money" => $amount * 100, //单位为分,不小于300 - "create_time" => date("Y-m-d H:i:s",$time), - "pay_time" => '0000-00-00 00:00:00', - - "user_type" => Redpack::userTypeDistribution, - "distributed_user_code" => $account->user_code, - "distributed_coupon_main_id" => $couponMainId, - ]; - - - //发起支付 - $result = $payment->transfer->toBalance($toBalanceData); - - //var_dump($result); - - //付款成功才提交!!!!!伪装成功 - if( isset($result['payment_no']) ){ - $redpackData['pay_time'] = date("Y-m-d H:i:s",$time); - Redpack::create($redpackData); - Db::commit(); - return $this->json(); - } - $redpackData['failure_reason'] = json_encode($result,JSON_UNESCAPED_UNICODE); - //否则失败 记录失败原因 - Log::info("企业发起付款【领券分销者红包】失败:" . json_encode($result,JSON_UNESCAPED_UNICODE)); - //默认回滚 - Db::rollback(); - //失败也要写入红包记录 - Redpack::create($redpackData); - return $this->json(5003, "领取失败,发放分享者红包失败"); - } - Db::commit(); return $this->json(); }catch (RepositoryException $e){ @@ -594,56 +499,66 @@ class Coupon extends Base $deductionMoney = $coupon->couponMain->deduction_money; + $returnConsumerMoney = 0;//需要返回提示的金额 - //可分配金额 如果是普通商家 - if($business->model == BusinessModel::model_ordinary) { - $agencyMoney = (($deductionMoney/100) * $coupon->couponMain->commission_agency); - $adminMoney = (($deductionMoney/100) * $coupon->couponMain->commission_admin); - $consumerMoney = (($deductionMoney/100) * $coupon->couponMain->commission_consumer); - - $agencyMoney = round($agencyMoney,2); //四舍五入 精确到分 - $adminMoney = round($adminMoney,2); //四舍五入 精确到分 - $consumerMoney = round($consumerMoney,2); //四舍五入 精确到分 + //未开启分销状态 + $agencyMoney = (($deductionMoney/100) * $coupon->couponMain->commission_agency); + $adminMoney = (($deductionMoney/100) * $coupon->couponMain->commission_admin); + $consumerMoney = (($deductionMoney/100) * $coupon->couponMain->commission_consumer); + $agencyMoney = round($agencyMoney,2); //四舍五入 精确到分 + $adminMoney = round($adminMoney,2); //四舍五入 精确到分 + $consumerMoney = round($consumerMoney,2); //四舍五入 精确到分 - //开启了分销 覆盖 平台应得金额 和消费者应得金额 并且取消 渠道商打款 - if( - $business->is_assign == BusinessModel::COMMON_OFF - || - (!empty($coupon->distribution_user_code)&&$coupon->is_distribution_coupon == CouponMain::COMMON_ON) - ){ - $redpackUser = Account::findOne([["user_code","=",$coupon->distribution_user_code]]); + //开启分销状态 + $disAdminMoney = (($deductionMoney/100) * $coupon->couponMain->commission_dis_admin); + $disDistributionMoney = (($deductionMoney/100) * $coupon->couponMain->commission_dis_distribution); + $disDistributedMoney = (($deductionMoney/100) * $coupon->couponMain->commission_dis_distributed); + $disAgencyMoney = 0 ;//渠道商就没得钱 - $disAdminMoney = (($deductionMoney/100) * $coupon->couponMain->commission_dis_admin); - //$disDistributionMoney = (($deductionMoney/100) * $coupon->couponMain->commission_dis_distribution); - $disDistributedMoney = (($deductionMoney/100) * $coupon->couponMain->commission_dis_distributed); - //渠道商就没得钱 - $agencyMoney = 0 ; - $adminMoney = round($disAdminMoney,2); //四舍五入 精确到分 - //$disDistributionMoney = round($disDistributionMoney,2); //四舍五入 精确到分 - $consumerMoney = round($disDistributedMoney,2); //四舍五入 精确到分 - }else{ - $redpackUser = $account; + $disAdminMoney = round($disAdminMoney,2); //四舍五入 精确到分 + $disDistributionMoney = round($disDistributionMoney,2); //四舍五入 精确到分 + $disDistributedMoney = round($disDistributedMoney,2); //四舍五入 精确到分 + + $consumerRedpackUser = $account; + $distributionRedpackUser = []; + $agencyRedpackUser = []; + + //开启了分销 + if( + $business->is_assign == BusinessModel::COMMON_OFF + || + (!empty($coupon->distribution_user_code)&&$coupon->is_distribution_coupon == CouponMain::COMMON_ON) + ){ + $distributionRedpackUser = Account::findOne([["user_code","=",$coupon->distribution_user_code]]); + //如果没有分销人 就全是平台 和分销人的 + if(empty($distributionRedpackUser)){ + $confirmAdminMoney = $disAdminMoney+$disDistributionMoney; + $confirmDistributionMoney = 0; + $confirmDistributedMoney = $disDistributedMoney; + $confirmAgencyMoney = 0; + }else{//存在分销人 就走分销人 + $confirmAdminMoney = $disAdminMoney; + $confirmDistributionMoney = $disDistributionMoney; + $confirmDistributedMoney = $disDistributedMoney; + $confirmAgencyMoney = 0; } }else{ - //开启了分销 覆盖 平台应得金额 和消费者应得金额 并且取消 渠道商打款 - if( - $business->is_assign == BusinessModel::COMMON_OFF - || - (!empty($coupon->distribution_user_code)&&$coupon->is_distribution_coupon == CouponMain::COMMON_ON) - ){ - $redpackUser = Account::findOne([["user_code","=",$coupon->distribution_user_code]]); - }else{ - $redpackUser = $account; + $confirmAdminMoney = $adminMoney; + $confirmDistributionMoney = 0; + $confirmDistributedMoney = $consumerMoney; + $confirmAgencyMoney = $adminMoney; + if(isset($business->agency) && $business->agency){ + $agencyRedpackUser = Account::findOne([ + ["business_code","=",$business->agency->code], + ["type","=",Account::type_business], + ]); } - - $adminMoney = 0; - $consumerMoney = round($deductionMoney,2) ; - $agencyMoney = 0; //四舍五入 精确到分 } + //开始数据操作 Db::startTrans(); try { @@ -659,40 +574,42 @@ class Coupon extends Base ]); - // 2. 写入优惠券流水 - $couponBill = CouponBill::getUserOne( $account->user_code,$coupon->couponMain->id,$coupon->id); - if(empty($couponBill)){ - $couponBillData = [ - "coupon_main_id" => $coupon->couponMain->id, - "coupon_id" => $coupon->id, - "user_code" => $account->user_code, - "business_code" => $business->code, - "agency_code" => $business->agency_code, - "commission_agency" => $coupon->couponMain->commission_agency, - "commission_admin" => $coupon->couponMain->commission_admin, - "commission_consumer" => $coupon->couponMain->commission_consumer, - "commission_dis_admin" => $coupon->couponMain->commission_dis_admin, - "commission_dis_distribution" => $coupon->couponMain->commission_dis_distribution, - "commission_dis_distributed" => $coupon->couponMain->commission_dis_distributed, - "money" => $coupon->couponMain->money, - "agency_money" => $agencyMoney, - "admin_money" => $adminMoney, - "consumer_money" => $consumerMoney, - "lat" => $lat, - "lng" => $lng, - "create_time" => date("Y-m-d H:i:s" ,$time), - ]; - $couponBill = CouponBill::create($couponBillData); - }else{ - $couponBillData = [ - "agency_money" => $agencyMoney, - "admin_money" => $adminMoney, - "consumer_money" => $consumerMoney, - "lat" => $lat, - "lng" => $lng, - ]; - $couponBill->save($couponBillData); - } + +// $couponBill = CouponBill::getUserOne($account->user_code,$coupon->couponMain->id,$coupon->id); +// if(empty($couponBill)){ + //2. 写入优惠券流水 + $couponBillData = [ + "coupon_main_id" => $coupon->couponMain->id, + "coupon_id" => $coupon->id, + "user_code" => $account->user_code, + "business_code" => $business->code, + "agency_code" => $business->agency_code, + "commission_agency" => $coupon->couponMain->commission_agency, + "commission_admin" => $coupon->couponMain->commission_admin, + "commission_consumer" => $coupon->couponMain->commission_consumer, + "commission_dis_admin" => $coupon->couponMain->commission_dis_admin, + "commission_dis_distribution" => $coupon->couponMain->commission_dis_distribution, + "commission_dis_distributed" => $coupon->couponMain->commission_dis_distributed, + "money" => $coupon->couponMain->money, + "agency_money" => $confirmAgencyMoney, + "admin_money" => $confirmAdminMoney, + "consumer_money" => $confirmDistributedMoney, + "distribution_money" => $confirmDistributionMoney, + "lat" => $lat, + "lng" => $lng, + "create_time" => date("Y-m-d H:i:s" ,$time), + ]; + $couponBill = CouponBill::create($couponBillData); +// }else{ +// $couponBillData = [ +// "agency_money" => $agencyMoney, +// "admin_money" => $adminMoney, +// "consumer_money" => $consumerMoney, +// "lat" => $lat, +// "lng" => $lng, +// ]; +// $couponBill->save($couponBillData); +// } // 3. 写入商家扣费记录 @@ -710,163 +627,190 @@ class Coupon extends Base ]; Deduction::create($deductionData); - //4. 商家扣钱 - $business->save(["balance"=>Db::raw("balance - " . $deductionMoney)]); + //4. 商家扣钱 普通商家才扣钱 否则不扣钱 + if($business->model == BusinessModel::model_ordinary){ + $business->save(["balance"=>Db::raw("balance - " . $deductionMoney)]); + } $payment = WechatPay::getInstance(); //5. 渠道商加钱 【只要指派了渠道商 并且没有分销人 渠道商才能加钱 红包方式直接打款 成功失败不管】 - if($business->is_assign == BusinessModel::COMMON_ON && (empty($coupon->distribution_user_code))){ - if(isset($business->agency) && $business->agency){ - //$business->agency->inc("balance",$agencyMoney)->update(); - if($agencyMoney > 0) { - //找到渠道商关联的用户 - $agencyUser = Account::findOne([ - ["business_code","=",$business->agency->code], - ["type","=",Account::type_business], - ]); - if(empty($agencyUser)){ - Db::rollback(); - return $this->json(4001,"渠道商用户不存在"); - } - - $agencyRedpack = Redpack::getDistributionRedpack($agencyUser->user_code,$coupon->couponMain->id,$account->user_code); - //如果付款过一次了 就用同一个订单号发起 - if(!empty($agencyRedpack)){ - $mch_billno = $agencyRedpack->mch_billno; - }else{ - $mch_billno = createUuid(); - } - - $agencyAmount = $agencyMoney * 100 ; - - $agencyToBalanceData = [ - 'partner_trade_no' => $mch_billno,// 商户订单号,需保持唯一性(只能是字母或者数字,不能包含有符号) - 'openid' => $agencyUser->open_id, - 'check_name' => 'NO_CHECK',// NO_CHECK:不校验真实姓名, FORCE_CHECK:强校验真实姓名 - 'amount' => $agencyAmount , //单位为分,不小于300 - 'desc' => '用户核销优惠券,渠道商奖励', - ]; - - // 写入红包 - $agencyRedpackData = [ - "coupon_id" => 0, - "mch_billno" => $mch_billno, - "openid" => $agencyUser->open_id, - "user_code" => $agencyUser->user_code, - "money" => $agencyAmount, //单位为分,不小于300 - "create_time" => date("Y-m-d H:i:s",$time), - "pay_time" => '0000-00-00 00:00:00', - - "user_type" => Redpack::userTypeAgency, - "distributed_user_code" => $account->user_code, - "distributed_coupon_main_id" => $coupon->couponMain->id, - ]; - - - //发起支付 - $agencyResult = $payment->transfer->toBalance($agencyToBalanceData); - - //var_dump($result); - - //付款成功才提交!!!!!伪装成功 - if( isset($agencyResult['payment_no']) ){ - $agencyRedpackData['pay_time'] = date("Y-m-d H:i:s",$time); - }else{ - $agencyRedpackData['failure_reason'] = json_encode($agencyResult,JSON_UNESCAPED_UNICODE); - //否则失败 记录失败原因 - Log::info("企业发起付款【渠道商红包】失败:" . json_encode($agencyResult,JSON_UNESCAPED_UNICODE)); - } - // 不管成功还是失败 都要写入 - Redpack::create($agencyRedpackData); - } - } - } - - - - //6. 分销者用户提现到零钱 写入红包记录 - if($consumerMoney > 0 && !empty($redpackUser)){ - //如果付款过一次了 就查询付款状态 - if(isset($coupon->redpack) && $coupon->redpack){ - $mch_billno = $coupon->redpack->mch_billno; -// $result = $payment->transfer->queryBalanceOrder($coupon->redpack->mch_billno); -// //var_dump($result); -// //企业付款成功 -// if(isset($result["status"]) && $result["status"]=="SUCCESS"){ -// Db::commit(); -// return $this->json( -// 0, -// "优惠券签到使用成功,您获得{$consumerMoney}元红包,已存入您的零钱", -// [ -// "redpack" =>$consumerMoney -// ]); -// } -// Log::info("查询企业付款失败:".json_encode($result,JSON_UNESCAPED_UNICODE)); + //$business->agency->inc("balance",$agencyMoney)->update(); + //修改成直接大款到关联的用户 + if($confirmAgencyMoney > 0 && !empty($agencyRedpackUser)) { + $agencyRedpack = Redpack::getDistributionRedpack( Redpack::userTypeAgency,$agencyRedpackUser->user_code,$coupon->id); + //如果付款过一次了 就用同一个订单号发起 + if(!empty($agencyRedpack)){ + $mch_billno = $agencyRedpack->mch_billno; }else{ $mch_billno = createUuid(); } - $amount = $consumerMoney * 100; + $agencyPayAmount = $confirmAgencyMoney * 100 ; - $toBalanceData = [ + $agencyToBalanceData = [ 'partner_trade_no' => $mch_billno,// 商户订单号,需保持唯一性(只能是字母或者数字,不能包含有符号) - 'openid' => $redpackUser->open_id, + 'openid' => $agencyRedpackUser->open_id, 'check_name' => 'NO_CHECK',// NO_CHECK:不校验真实姓名, FORCE_CHECK:强校验真实姓名 - 'amount' => $amount, //单位为分,不小于300 + 'amount' => $agencyPayAmount , //单位为分,不小于300 + 'desc' => '用户核销优惠券,渠道商奖励', + ]; + + // 写入红包 + $agencyRedpackData = [ + "coupon_id" => $coupon->id, + "mch_billno" => $mch_billno, + "openid" => $agencyRedpackUser->open_id, + "user_code" => $agencyRedpackUser->user_code, + "money" => $agencyPayAmount, //单位为分,不小于300 + "create_time" => date("Y-m-d H:i:s",$time), + "pay_time" => '0000-00-00 00:00:00', + "user_type" => Redpack::userTypeAgency, + ]; + + //发起支付 + $agencyResult = $payment->transfer->toBalance($agencyToBalanceData); + //付款成功才提交!!!!!伪装成功 + if( isset($agencyResult['payment_no']) ){ + $agencyRedpackData['pay_time'] = date("Y-m-d H:i:s",$time); + }else{ + $agencyRedpackData['failure_reason'] = json_encode($agencyResult,JSON_UNESCAPED_UNICODE); + //否则失败 记录失败原因 + Log::info("企业发起付款【渠道商红包】失败:" . json_encode($agencyResult,JSON_UNESCAPED_UNICODE)); + } + // 不管成功还是失败 都要写入 + if(empty($agencyRedpack)){ + Redpack::create($agencyRedpackData); + }else{ + $agencyRedpack->save($agencyRedpackData); + } + } + + + //6. 核销用户提现到零钱 写入红包记录 + if($confirmDistributedMoney > 0 && !empty($consumerRedpackUser)){ + $distributedRedpack = Redpack::getDistributionRedpack( Redpack::userTypeDistributed,$agencyRedpackUser->user_code,$coupon->id); + //如果付款过一次了 就查询付款状态 + if(!empty($distributedRedpack)){ + $consumerMchBillno = $distributedRedpack->mch_billno; + }else{ + $consumerMchBillno = createUuid(); + } + + $consumerAmount = $confirmDistributedMoney * 100; + + $consumerToBalanceData = [ + 'partner_trade_no' => $consumerMchBillno,// 商户订单号,需保持唯一性(只能是字母或者数字,不能包含有符号) + 'openid' => $consumerRedpackUser->open_id, + 'check_name' => 'NO_CHECK',// NO_CHECK:不校验真实姓名, FORCE_CHECK:强校验真实姓名 + 'amount' => $consumerAmount, //单位为分,不小于300 'desc' => '验证优惠券签到奖励', ]; // 写入红包 - $redpackData = [ + $consumerRedpackData = [ "coupon_id" => $coupon->id, - "mch_billno" => $mch_billno, - "openid" => $redpackUser->open_id, - "user_code" => $redpackUser->user_code, - "money" => $amount, //单位为分,不小于300 + "mch_billno" => $consumerMchBillno, + "openid" => $consumerRedpackUser->open_id, + "user_code" => $consumerRedpackUser->user_code, + "money" => $consumerAmount, //单位为分,不小于300 "create_time" => date("Y-m-d H:i:s",$time), "pay_time" => '0000-00-00 00:00:00', ]; //发起支付 - $result = $payment->transfer->toBalance($toBalanceData); + $consumerResult = $payment->transfer->toBalance($consumerToBalanceData); + //付款成功才提交!!!!!伪装成功 + if( isset($consumerResult['payment_no']) ){ + $returnConsumerMoney = $confirmDistributedMoney; + $consumerRedpackData['pay_time'] = date("Y-m-d H:i:s",$time); + }else{ + $consumerRedpackData['failure_reason'] = json_encode($consumerResult,JSON_UNESCAPED_UNICODE); + //否则失败 记录失败原因 + Log::info("企业发起付款【渠道商红包】失败:" . json_encode($consumerResult,JSON_UNESCAPED_UNICODE)); + } + + if(empty($distributedRedpack)){ + Redpack::create($consumerRedpackData); + }else{ + $distributedRedpack->save($consumerRedpackData); + } + } + + //7 分销者用户提现到零钱 写入红包记录 + if($confirmDistributionMoney > 0 && !empty($distributionRedpackUser)){ + + $distributionRedpack = Redpack::getDistributionRedpack( Redpack::userTypeDistribution,$agencyRedpackUser->user_code,$coupon->id); + //如果付款过一次了 就用同一个订单号发起 + if(!empty($distributionRedpack)){ + $distributionMchBillno = $distributionRedpack->mch_billno; + }else{ + $distributionMchBillno = createUuid(); + } + + $distributionToBalanceData = [ + 'partner_trade_no' => $distributionMchBillno,// 商户订单号,需保持唯一性(只能是字母或者数字,不能包含有符号) + 'openid' => $distributionRedpackUser->open_id, + 'check_name' => 'NO_CHECK',// NO_CHECK:不校验真实姓名, FORCE_CHECK:强校验真实姓名 + 'amount' => $confirmDistributionMoney * 100, //单位为分,不小于300 + 'desc' => '优惠券分销奖励', + ]; + + // 写入红包 + $distributionRedpackData = [ + "coupon_id" => 0, + "mch_billno" => $distributionMchBillno, + "openid" => $distributionRedpackUser->open_id, + "user_code" => $distributionRedpackUser->user_code, + "money" => $confirmDistributionMoney * 100, //单位为分,不小于300 + "create_time" => date("Y-m-d H:i:s",$time), + "pay_time" => '0000-00-00 00:00:00', + "user_type" => Redpack::userTypeDistribution, + ]; + + + //发起支付 + $distributionResult = $payment->transfer->toBalance($distributionToBalanceData); //var_dump($result); //付款成功才提交!!!!!伪装成功 - if( isset($result['payment_no']) ){ - $redpackData['pay_time'] = date("Y-m-d H:i:s",$time); - Redpack::create($redpackData); - Db::commit(); - return $this->json( - 0, - "优惠券签到使用成功,您获得{$consumerMoney}元红包,已存入您的零钱", - [ - "redpack" =>$consumerMoney, - "user_coupon_id" =>$couponId, - ]); + if( isset($distributionResult['payment_no']) ){ + $distributionRedpackData['pay_time'] = date("Y-m-d H:i:s",$time); + if(empty($distributionRedpack)){ + Redpack::create($distributionRedpackData); + }else{ + $distributionRedpack->save($distributionRedpackData); + } + + }else{ + $distributionRedpackData['failure_reason'] = json_encode($distributionResult,JSON_UNESCAPED_UNICODE); + //否则失败 记录失败原因 + Log::info("企业发起付款【领券分销者红包】失败:" . json_encode($distributionResult,JSON_UNESCAPED_UNICODE)); + //默认回滚 + Db::rollback(); + //失败也要写入红包记录 + if(empty($distributionRedpack)){ + Redpack::create($distributionRedpackData); + }else{ + $distributionRedpack->save($distributionRedpackData); + } + return $this->json(5003, "领取失败"); } - $redpackData['failure_reason'] = json_encode($result,JSON_UNESCAPED_UNICODE); - //否则失败 记录失败原因 - Log::info("企业发起付款【消费者红包】失败:" . json_encode($result,JSON_UNESCAPED_UNICODE)); - //默认回滚 - Db::rollback(); - //失败也要写入红包记录 - Redpack::create($redpackData); - return $this->json(5003, "验证失败,发放红包失败"); + } Db::commit(); return $this->json( 0, - "优惠券签到使用成功,您获得{$consumerMoney}元红包,已存入您的零钱", + "优惠券签到使用成功,您获得{$returnConsumerMoney}元红包,已存入您的零钱", [ - "redpack" =>$consumerMoney, + "redpack" =>$returnConsumerMoney, "user_coupon_id" =>$couponId, ]); - + }catch (RepositoryException $e){ Db::rollback(); return $this->json(5001, "Repository服务器错误"); diff --git a/app/model/Redpack.php b/app/model/Redpack.php index 5f97799..e21747b 100644 --- a/app/model/Redpack.php +++ b/app/model/Redpack.php @@ -5,23 +5,22 @@ namespace app\model; class Redpack extends Base { - const userTypeDistribution = "distribution";//领券分销者 - const userTypeDistributed = "distributed";//核销分销者 - const userTypeAgency = "agency";// 渠道商 + public const userTypeDistribution = "distribution";//分销者 + public const userTypeDistributed = "distributed";//被分销者 + public const userTypeAgency = "agency"; // 渠道商 /** * 获取分销红包记录 - * @param $distributionUserCode 分销人user_code - * @param $couponMainId 分销的couponMain 优惠券id - * @param $distributedUserCode 被分销人user_code + * @param string $userType 红包获取人的角色 + * @param string $distributionUserCode 分销人/渠道商/被分销者 user_code + * @param mixed $couponMainId 分销的couponMain 优惠券id * 以上三个参数就可以确定 分销数据的唯一性 * */ - public static function getDistributionRedpack($distributionUserCode,$couponMainId,$distributedUserCode) + public static function getDistributionRedpack($userType,$distributionUserCode,$couponMainId) { return self::where([ - ["user_type","=",self::userTypeDistribution], + ["user_type","=",$userType], ["user_code","=",$distributionUserCode], - ["distributed_user_code","=",$distributedUserCode], - ["distributed_coupon_main_id","=",$distributedUserCode], + ["coupon_id","=",$couponMainId], ]) ->find(); }