Commit e9e5454d authored by hujun's avatar hujun

发放

parent 2b73a757
<?php
namespace app\api\service;
use app\model\CActivity;
use app\model\CCoupon;
use app\model\Users;
/**
* Created by PhpStorm.
* User: zw
......@@ -8,5 +12,125 @@ namespace app\api\service;
*/
class CouponService{
private $m_coupon;
private $m_user;
private $m_activity;
public function __construct()
{
$this->m_coupon = new CCoupon();
$this->m_user = new Users();
$this->m_activity = new CActivity();
}
/**
* @param int $user_id
* @param int $type
* @return mixed
* @throws \think\Exception
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function sendCoupon(int $user_id, int $type) {
$result['status'] = 'successful';
$result['msg'] = '发券成功';
$is_exist = $this->m_user->getUserByWhereValue('id', ['id'=>$user_id]);
if (empty($is_exist)) {
$result['status'] = 'fail';
$result['msg'] = '该用户不存在';
return $result;
}
$date = date('Y-m-d H:i:s');
$activity_data = $this->checkUserCoupon($user_id, $type);
if (empty($activity_data)) {
$result['status'] = 'fail';
$result['msg'] = '没有可参加的活动';
return $result;
}
$save_data = [];
foreach ($activity_data as $k=>$v) {
$save_data['activity_id'] = $v['id'];
$save_data['rule_no'] = date('YMDHIS').mt_rand(100,100000);
$save_data['user_id'] = $user_id;
$save_data['report_id'] = $user_id;
$save_data['bargain_id'] = $user_id;
$save_data['order_id'] = $user_id;
$save_data['status'] = $user_id;
$save_data['use_time'] = $date;
$insert_status = $this->m_coupon->insertData($save_data);
if (empty($insert_status)) {
$this->m_activity->plusGetNumber($v['id']); //增加用户领取数量
}
}
return $result;
}
/**
* 查询奖励动作
*
* @param int $return_action @奖励动作 0首次登陆 1邀请登陆 2邀请成交
* @return false|\PDOStatement|string|\think\Collection
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function selectActivity(int $return_action) {
$field = 'id,return_type,money,activity_start_time,activity_end_time,use_period,,total,available,total';
$field .= ',available,get_number,status';
//奖励动作 0首次登陆 1邀请登陆 2邀请成交
switch ($return_action) {
case 1 :
$return_action = 1;
break;
case 2 :
$return_action = 2;
break;
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'] = ['>', $date];
$where['activity_end_time'] = ['<', $date];
$data = $this->m_activity->getActivity($field, $where);
return $data;
}
/**
* 检查可以参加的活动
*
* @param int $user_id
* @param int $type
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function checkUserCoupon(int $user_id, int $type)
{
$return_activity = [];
$activity_data = $this->selectActivity($type);
$count_where['user_id'] = $user_id;
foreach ($activity_data as $k=>$v) {
if ($v['total'] - $v['get_number'] > 0 ) {
continue;
}
$count_where['activity_id'] = $v['id'];
$num = $this->m_coupon->getCount($count_where);
if ($num < $v['available']) {
$return_activity[] = $v;
}
}
return $return_activity;
}
}
\ No newline at end of file
......@@ -71,4 +71,15 @@ class CActivity extends Model
return $result;
}
/**
* @param int $activity_id
* @return int|true
* @throws \think\Exception
*/
public function plusGetNumber(int $activity_id)
{
return $this->db_->where('id', $activity_id)
->setInc('get_number',1);
}
}
......@@ -22,4 +22,50 @@ class CCoupon
$this->db_ = Db::name($this->table);
}
/**
* 查询
*
* @param $field
* @param $params
* @return false|\PDOStatement|string|\think\Collection
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function getCoupon($field,$params)
{
if (!isset($params['status'])) {
$params['status'] = 0;
}
$result = $this->db_
->field($field)
->where($params)
->select();
//dump($this->getLastSql());
return $result;
}
/**
* @param $where
* @param string $field
* @return mixed
*/
public function getCount($where, $field = 'id'){
return $this->db_->where($where)
->count($field);
}
/**
* @param $data
* @return int|string
*/
public function insertData($data) {
if (is_array($data)) {
$result = $this->db_->insertAll($data);
} else {
$result = $this->db_->insert();
}
return $result;
}
}
\ 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