Commit 2d2bf1a0 authored by hujun's avatar hujun

邀请

parent a170fd25
<?php <?php
namespace app\api\service; namespace app\api\service;
use app\extra\RedisExt;
use app\model\CActivity; use app\model\CActivity;
use app\model\CCoupon; use app\model\CCoupon;
use app\model\Users; use app\model\Users;
...@@ -15,6 +16,7 @@ class CouponService{ ...@@ -15,6 +16,7 @@ class CouponService{
private $m_coupon; private $m_coupon;
private $m_user; private $m_user;
private $m_activity; private $m_activity;
private $redis_;
public function __construct() public function __construct()
{ {
...@@ -24,55 +26,73 @@ class CouponService{ ...@@ -24,55 +26,73 @@ class CouponService{
} }
/** /**
* 发券
*
* @param int $user_id * @param int $user_id
* @param int $type * @param int $type 奖励动作 0首次登陆 1邀请登陆 2邀请成交
* @param int $report_id * @param int $report_id
* @param int $bargain_id * @param int $bargain_id
* @param int $order_id * @param int $order_id
* @return mixed * @return mixed
* @throws \think\Exception * @throws \think\exception\PDOException
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/ */
public function sendCoupon(int $user_id, int $type, $report_id = 0, $bargain_id = 0, $order_id = 0) { public function sendCoupon(int $user_id, int $type, $report_id = 0, $bargain_id = 0, $order_id = 0) {
$result['status'] = 'successful'; $result['status'] = 'successful';
$result['msg'] = '发券成功'; $result['msg'] = '发券成功';
$result['data'] = []; $result['data'] = [];
$is_exist = $this->m_user->getUserByWhereValue('id', ['id'=>$user_id]); $this->redis_ = RedisExt::getRedis();
if (empty($is_exist)) { $key = 'send_coupon_'.$user_id;
if ($this->redis_->get($key)) {
$result['status'] = 'fail'; $result['status'] = 'fail';
$result['msg'] = '该用户不存在'; $result['msg'] = '正在发券中,请稍等...';
return $result; return $result;
} else {
$this->redis_->set($key, $user_id, 3); //上线后改为120
} }
$activity_data = $this->checkUserCoupon($user_id, $type); try {
if (empty($activity_data)) { $is_exist = $this->m_user->getUserByWhereValue('id', ['id'=>$user_id]);
$result['status'] = 'fail'; if (empty($is_exist)) {
$result['msg'] = '没有可参加的活动'; $result['status'] = 'fail';
return $result; $result['msg'] = '该用户不存在';
} return $result;
$save_data = []; }
$save_data['user_id'] = $user_id;
if ($report_id) { $activity_data = $this->checkUserCoupon($user_id, $type);
$save_data['report_id'] = $report_id; if (empty($activity_data)) {
} $result['status'] = 'fail';
if ($bargain_id) { $result['msg'] = '没有可参加的活动';
$save_data['bargain_id'] = $bargain_id; return $result;
} }
if ($order_id) { $save_data = [];
$save_data['order_id'] = $order_id; $save_data['user_id'] = $user_id;
} if ($report_id) {
$save_data['status'] = 0; $save_data['report_id'] = $report_id;
$save_data['create_time'] = date('Y-m-d H:i:s'); }
foreach ($activity_data as $k=>$v) { if ($bargain_id) {
$save_data['activity_id'] = $v['id']; $save_data['bargain_id'] = $bargain_id;
$save_data['rule_no'] = date('YMDHIS').mt_rand(100,100000);
$insert_status = $this->m_coupon->insertData($save_data);
if (empty($insert_status)) {
$this->m_activity->plusGetNumber($v['id']); //增加用户领取数量
} }
if ($order_id) {
$save_data['order_id'] = $order_id;
}
$save_data['status'] = 0;
$save_data['create_time'] = date('Y-m-d H:i:s');
$this->m_coupon->startTrans();
foreach ($activity_data as $k=>$v) {
$save_data['activity_id'] = $v['id'];
$save_data['rule_no'] = mt_rand(100,100000);
$insert_status = $this->m_coupon->insertData($save_data);
if ($insert_status) {
$this->m_activity->plusGetNumber($v['id']); //增加用户领取数量
}
}
$this->redis_->del($key);
$this->m_coupon->commitTrans();
}catch (\Exception $e) {
$this->m_coupon->rollBackTrans();
$result['status'] = 'fail';
$result['msg'] = '发券失败';
} }
return $result; return $result;
} }
...@@ -101,12 +121,12 @@ class CouponService{ ...@@ -101,12 +121,12 @@ class CouponService{
default : default :
$return_action = 0; $return_action = 0;
} }
$date = date('Y-m-d H:i:s');
$where['available'] = ['>', 0]; $where['available'] = ['>', 0];
$where['total'] = ['>', 0]; $where['total'] = ['>', 0];
$where['return_action'] = $return_action; $where['return_action'] = $return_action;
$where['status'] = 0; //0正常 1手动停止 2数量为空 3时间过期 4删除 5未开始 $where['status'] = 0; //0正常 1手动停止 2数量为空 3时间过期 4删除 5未开始
$where['activity_start_time'] = ['<>', '0000-00-00 00:00:00'];
$where['activity_end_time'] = ['<>', '0000-00-00 00:00:00'];
$data = $this->m_activity->getActivity($field, $where); $data = $this->m_activity->getActivity($field, $where);
return $data; return $data;
} }
......
...@@ -123,6 +123,15 @@ class RegisterService ...@@ -123,6 +123,15 @@ class RegisterService
$jwt_data['userNick'] = $user_nick; $jwt_data['userNick'] = $user_nick;
$jwt_data['phone'] = $phone; $jwt_data['phone'] = $phone;
$coupon_service = new CouponService();
if (!empty($first_login_time)) {
$coupon_service->sendCoupon((int)$id, 0);
}
if (!empty($registration_time) && $referrer_source == 10 && $referrer_id) {
$coupon_service->sendCoupon((int)$referrer_id, 1);
}
$jwt = new JwtUntils(); $jwt = new JwtUntils();
$AuthToken = $jwt->createToken($jwt_data); $AuthToken = $jwt->createToken($jwt_data);
......
...@@ -125,8 +125,7 @@ class CActivity extends Model ...@@ -125,8 +125,7 @@ class CActivity extends Model
*/ */
public function plusGetNumber(int $activity_id) public function plusGetNumber(int $activity_id)
{ {
return $this->db_->where('id', $activity_id) return $this->db_->table($this->table)->where('id', $activity_id)->setInc('get_number',1);
->setInc('get_number',1);
} }
} }
...@@ -68,4 +68,25 @@ class CCoupon ...@@ -68,4 +68,25 @@ class CCoupon
} }
return $result; return $result;
} }
/**
*
*/
public function startTrans() {
$this->db_->startTrans();
}
/**
* @throws \think\exception\PDOException
*/
public function rollBackTrans() {
$this->db_->rollback();
}
/**
* @throws \think\exception\PDOException
*/
public function commitTrans() {
$this->db_->commit();
}
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment