Browse Source

Merge branch 'coupon_rebate' of http://120.24.33.109:11081/hyzjshwo/lanzu_api_hyperf into coupon_rebate

master
parent
commit
acdee0d846
  1. 2
      app/Constants/LogLabel.php
  2. 79
      app/Controller/CouponRebateController.php
  3. 135
      app/Service/CouponRebateService.php
  4. 11
      app/Service/CouponRebateServiceInterface.php

2
app/Constants/LogLabel.php

@ -16,4 +16,6 @@ class LogLabel extends AbstractConstants
* @Message("Ssdb Log Label") * @Message("Ssdb Log Label")
*/ */
const SSDB_LOG = 'ssdb_log'; const SSDB_LOG = 'ssdb_log';
const COUPON_LOG = 'coupon_log';
} }

79
app/Controller/CouponRebateController.php

@ -21,6 +21,7 @@ use App\Model\Coupon;
use App\Model\CouponUserRecType; use App\Model\CouponUserRecType;
use App\Model\CouponRec; use App\Model\CouponRec;
use App\Request\CouponRebateReceiveRequest; use App\Request\CouponRebateReceiveRequest;
use App\Service\CouponRebateService;
class CouponRebateController extends BaseController class CouponRebateController extends BaseController
{ {
/** /**
@ -28,6 +29,13 @@ class CouponRebateController extends BaseController
* @var CouponRebateInterface * @var CouponRebateInterface
*/ */
protected $CouponRebate; protected $CouponRebate;
/**
* @Inject
* @var CouponRebateService
*/
protected $CouponRebateService;
/** /**
* 用户是否领取过领取优惠券 * 用户是否领取过领取优惠券
*/ */
@ -54,75 +62,6 @@ class CouponRebateController extends BaseController
*/ */
public function userReceiveCoupon(CouponRebateReceiveRequest $validator) public function userReceiveCoupon(CouponRebateReceiveRequest $validator)
{ {
$userId = $this->request->input("user_id", 0);
$receiveType = $this->request->input("receive_type", 0);
$id = $this->request->input("id", 0);
$sendUserId = $this->request->input("send_user_id", 0);
$phone = $this->request->input("phone", '');
$now = time();
// status : 0 领取成功 >0 领取失败
$return = [
'status' => 1,
'data' => [],
'coupon_text' => '继续努力~'
];
Db::transaction( function () use ($id,$receiveType,$userId,$sendUserId,$phone,&$return,$now) {
//读写锁,完全控制,性能低
$cp = Coupon::where('id', $id)->lockForUpdate()
->select('id','title','status','inventory','inventory_use','start_time','end_time','full_amount','discounts')
->first();
$where = [
'system_coupon_user_id' => $cp->id,
];
if (env('SUB_CHANNEL') == 1) {
$where['receive_type'] = $receiveType;
}
// 查询领取类型一次能领取的数量
$crt = CouponUserRecType::where($where)->first();
$cr = new CouponRec;
$cr->user_id = $userId;
$cr->system_coupon_user_id = $cp->id;
$cr->order_main_id = 0;
$cr->receive_time = $now;
$cr->number = $crt->one_receive_number;
$cr->number_remain = $crt->one_receive_number;
$cr->status = 0;
$cr->update_time = $now;
$cr->receive_type = $receiveType;
$cr->send_user_id = $sendUserId;
$cr->phone = $phone;
//如果优惠卷库存小于等于已领取的数量 或者 未在活动时间内, 则返回领取失败的优惠券
if (
$cp->status != 1
||
$cp->inventory <= $cp->inventory_use
||
$cp->inventory < ($cp->inventory_use+$cr->number)
||
$cp->start_time > $now
||
$cp->end_time < $now
)
{
$return['status'] = 2;
$return['coupon_text'] = '红包已经领完~';
}else{
$cp->inventory_use += $cr->number;//记录已领取的数量
if ( $cr->save() && $cp->save() ) {
$return['status'] = 0;
$return['data'] = $cp;
$return['coupon_text'] = $cp['discounts'].'元红包';
}
}
});
return $this->success($return);
return $this->success($this->CouponRebateService->userReceiveCoupon($this->request->all()));
} }
} }

135
app/Service/CouponRebateService.php

@ -0,0 +1,135 @@
<?php
namespace App\Service;
use Hyperf\Di\Annotation\Inject;
use Hyperf\DbConnection\Db;
use App\Model\Coupon;
use App\Model\CouponUserRecType;
use App\Model\CouponRec;
use App\Constants\LogLabel;
use App\Commons\Log;
use Exception;
class CouponRebateService implements CouponRebateServiceInterface
{
/**
* @Inject
* @var Log
*/
protected $log;
/**
* 领取优惠券
*/
public function userReceiveCoupon($params)
{
$userId = $params["user_id"];
$receiveType = $params["receive_type"];
$id = $params["id"];
$sendUserId = $params["send_user_id"];
$phone = $params["phone"];
$now = time();
// status : 0 领取成功 >0 领取失败
$result = [
'status' => 1,
'data' => [],
'coupon_text' => '继续努力~'
];
// 错误日志记录
$errorData = [
'coupon_id' =>$id,
'user_id' =>$userId,
'receiveType' =>$receiveType,
'sendUserId' =>$sendUserId,
'phone' =>$phone
];
Db::transaction( function() use ($id,$receiveType,$userId,$sendUserId,$phone,$now,&$result,&$errorData) {
try{
//读写锁,完全控制,性能低
$cp = Coupon::where('id', $id)->lockForUpdate()
->select('id','title','status','inventory','inventory_use','start_time','end_time','full_amount','discounts')
->first();
$couponReceiveType = CouponUserRecType::where('system_coupon_user_id',$cp->id)->select('one_receive_number');
if (env('SUB_CHANNEL') == 1) {
$couponReceiveType->where('receive_type',$receiveType);
}
// 查询一次能领取的数量
$crt = $couponReceiveType->first();
$cr = new CouponRec;
$cr->user_id = $userId;
$cr->system_coupon_user_id = $cp->id;
$cr->order_main_id = 0;
$cr->receive_time = $now;
$cr->number = $crt->one_receive_number;
$cr->number_remain = $crt->one_receive_number;
$cr->status = 0;
$cr->update_time = $now;
$cr->receive_type = $receiveType;
$cr->send_user_id = $sendUserId;
$cr->phone = $phone;
//如果优惠卷库存小于等于已领取的数量 或者 未在活动时间内, 则返回领取失败的优惠券
if (
$cp->status != 1
||
$cp->inventory <= $cp->inventory_use
||
$cp->inventory < ($cp->inventory_use+$cr->number)
||
$cp->start_time > $now
||
$cp->end_time < $now
)
{
$result['status'] = 2;
$result['coupon_text'] = '红包已经领完~';
$errorData['msg'] = '优惠券已经过期或者被禁用或者已领完';
$this->log->event(
LogLabel::COUPON_LOG,
$errorData
);
}else{
// 判断是否领取过 存在记录则领取过
$isReceive = CouponRec::select('id')
->where('system_coupon_user_id',$id)
->where('user_id',$userId)
->exists();
if(!$isReceive){
$cp->inventory_use += $cr->number;//记录已领取的数量
if ( $cr->save() && $cp->save() ) {
$result['status'] = 0;
// $result['data'] = $cp;
$result['coupon_text'] = $cp['discounts'].'元红包';
}
}else{
$errorData['msg'] = '用户已经领取了此优惠券';
$this->log->event(
LogLabel::COUPON_LOG,
$errorData
);
}
}
} catch (Exception $e){
$errorData['msg'] = $e->getMessage();
$this->log->event(
LogLabel::COUPON_LOG,
$errorData
);
}
});
return $result;
}
}

11
app/Service/CouponRebateServiceInterface.php

@ -0,0 +1,11 @@
<?php
namespace App\Service;
interface CouponRebateServiceInterface
{
/**
* 领取优惠券
*/
public function userReceiveCoupon($params);
}
Loading…
Cancel
Save