Commit 2d2bf1a0 authored by hujun's avatar hujun

邀请

parent a170fd25
<?php
namespace app\api\service;
use app\extra\RedisExt;
use app\model\CActivity;
use app\model\CCoupon;
use app\model\Users;
......@@ -15,6 +16,7 @@ class CouponService{
private $m_coupon;
private $m_user;
private $m_activity;
private $redis_;
public function __construct()
{
......@@ -24,55 +26,73 @@ class CouponService{
}
/**
* 发券
*
* @param int $user_id
* @param int $type
* @param int $type 奖励动作 0首次登陆 1邀请登陆 2邀请成交
* @param int $report_id
* @param int $bargain_id
* @param int $order_id
* @return mixed
* @throws \think\Exception
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
* @throws \think\exception\PDOException
*/
public function sendCoupon(int $user_id, int $type, $report_id = 0, $bargain_id = 0, $order_id = 0) {
$result['status'] = 'successful';
$result['msg'] = '发券成功';
$result['data'] = [];
$is_exist = $this->m_user->getUserByWhereValue('id', ['id'=>$user_id]);
if (empty($is_exist)) {
$this->redis_ = RedisExt::getRedis();
$key = 'send_coupon_'.$user_id;
if ($this->redis_->get($key)) {
$result['status'] = 'fail';
$result['msg'] = '该用户不存在';
$result['msg'] = '正在发券中,请稍等...';
return $result;
} else {
$this->redis_->set($key, $user_id, 3); //上线后改为120
}
$activity_data = $this->checkUserCoupon($user_id, $type);
if (empty($activity_data)) {
$result['status'] = 'fail';
$result['msg'] = '没有可参加的活动';
return $result;
}
$save_data = [];
$save_data['user_id'] = $user_id;
if ($report_id) {
$save_data['report_id'] = $report_id;
}
if ($bargain_id) {
$save_data['bargain_id'] = $bargain_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');
foreach ($activity_data as $k=>$v) {
$save_data['activity_id'] = $v['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']); //增加用户领取数量
try {
$is_exist = $this->m_user->getUserByWhereValue('id', ['id'=>$user_id]);
if (empty($is_exist)) {
$result['status'] = 'fail';
$result['msg'] = '该用户不存在';
return $result;
}
$activity_data = $this->checkUserCoupon($user_id, $type);
if (empty($activity_data)) {
$result['status'] = 'fail';
$result['msg'] = '没有可参加的活动';
return $result;
}
$save_data = [];
$save_data['user_id'] = $user_id;
if ($report_id) {
$save_data['report_id'] = $report_id;
}
if ($bargain_id) {
$save_data['bargain_id'] = $bargain_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;
}
......@@ -101,12 +121,12 @@ class CouponService{
default :
$return_action = 0;
}
$date = date('Y-m-d H:i:s');
$where['available'] = ['>', 0];
$where['total'] = ['>', 0];
$where['return_action'] = $return_action;
$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);
return $data;
}
......
......@@ -123,6 +123,15 @@ class RegisterService
$jwt_data['userNick'] = $user_nick;
$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();
$AuthToken = $jwt->createToken($jwt_data);
......
......@@ -125,8 +125,7 @@ class CActivity extends Model
*/
public function plusGetNumber(int $activity_id)
{
return $this->db_->where('id', $activity_id)
->setInc('get_number',1);
return $this->db_->table($this->table)->where('id', $activity_id)->setInc('get_number',1);
}
}
......@@ -68,4 +68,25 @@ class CCoupon
}
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