You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

295 lines
10 KiB

<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://doc.hyperf.io
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace App\Controller;
use App\Model\CouponUserRecType;
use App\Model\Coupon;
use App\Model\CouponRec;
use Hyperf\DbConnection\Db;
use Hyperf\Redis\Redis;
use Hyperf\Utils\ApplicationContext;
class CouponController extends BaseController
{
/**
* 获取用户可领取优惠卷接口
*/
public function getSystemCouponUserList()
{
$user_id = $this->request->input('user_id', 0);
$receive_type = $this->request->input('receive_type', 0);
if ($this->empty($user_id) || $this->empty($receive_type)) {
$this->success(['not_reveive' => []]);
}
$c_ids = [];
// 渠道开启,查询该渠道可以领取的优惠券ID
// 渠道未开启,查询所有优惠券
if (env('SUB_CHANNEL') == 1) {
$c_ids = CouponUserRecType::where('receive_type', $receive_type)->pluck('system_coupon_user_id');
} else {
$c_ids = Coupon::pluck('id');
}
$nowTime = time();
$where = [
['user_id',"=",$user_id]
];
// 渠道开启,查询该用户在此渠道领过的优惠券ID
if (env('SUB_CHANNEL') == 1) {
array_push($where, ['receive_type', "=", $receive_type]);
}
$cr_ids = CouponRec::where($where)->pluck('system_coupon_user_id');
//领过券的ID
$c_ids = $c_ids->toArray();
$cr_ids = $cr_ids->toArray();
// 当前用户可领的优惠券ID
$couponIds = array_diff($c_ids, $cr_ids);
$c = Db::table('ims_system_coupon_user as u')
->where([
['u.end_time','>',$nowTime],
['u.start_time','<=',$nowTime],
['type.receive_type','=',$receive_type],
['u.status','=',1],
])
->join('ims_system_coupon_user_receivetype as type', 'u.id', '=', 'type.system_coupon_user_id')
->whereRaw('u.inventory_use <= u.inventory and u.inventory-u.inventory_use > type.one_receive_number')
// ->whereIn('u.id',$c_ids)
// ->whereNotIn('u.id',$cr_ids)
->whereIn('u.id', $couponIds)
->select('u.*','type.one_receive_number')
->orderBy('u.weigh','desc')
// ->orderByRaw('FIELD(u.id, '.implode(", " , $ids).')')
->limit(4)
->get();
foreach ($c as $k => &$v){
if($v->discount_type == 2){
$v->discounts = floatval($v->discounts);
}
}
return $this->success(['not_reveive'=>$c]);
}
//统计用户
public function userCouponAccount()
{
$userId = $this->request->input("user_id", 0);
$nowTime = time();
$num = Db::table('ims_system_coupon_user_receive as receive')
->join('ims_system_coupon_user as u', 'u.id', '=', 'receive.system_coupon_user_id')
->where([
['receive.user_id','=',$userId],
['receive.number_remain','>',0],
['u.usable_end_time','>',$nowTime],
])
->whereIn('receive.status',[0,1])
->sum('receive.number_remain');
return $this->success(['total' => $num]);
}
/**
* 用户领取优惠卷
*/
public function userReceiveCoupon()
{
$userId = $this->request->input("user_id", 0);
$receiveType = $this->request->input("receive_type", 0);
$ids = $this->request->input("ids", '');
$ids = explode(',', $ids);
$now = time();
$success = [];
$fail = [];
if ($this->empty($userId) || $this->empty($receiveType) || $this->empty($ids)) {
return $this->success([
'success' => $success,
'fail' => $fail,
]);
}
Db::transaction(function () use ($ids,$receiveType,$userId,&$success,&$fail,$now) {
//读写锁,完全控制,性能底
$cps = Coupon::whereIn('id', $ids)->lockForUpdate()->get();
//写锁,可能在高并发下,读取到脏数据,写入可能有超发情况
//$cps = Coupon::whereIn('id', $ids)->sharedLock()->get();
foreach ($cps as $key => $cp) {
$crt = CouponUserRecType::where(
[
'system_coupon_user_id' => $cp->id,
'receive_type' => $receiveType
]
)->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;
//如果优惠卷库存小于等于已领取的数量, 则返回领取失败的优惠券
if ($cp->inventory<=$cp->inventory_use||$cp->inventory<($cp->inventory_use+$cr->number)){
$fail[] = $cp;
}else{
$cp->inventory_use += $cr->number;//记录已领取的数量
if ($cr->save()&&$cp->save()) {
$success[] = $cp;
} else {
$fail[] = $cp;
}
}
}
});
return $this->success([
'success' => $success,
'fail' => $fail,
]);
}
/**
* 获取用户已经领取的优惠卷列表
*/
public function getUserReceiveCouponList()
{
$userId = $this->request->input("user_id");
$not_expired = [];
$expired = [];
if ($this->empty($userId)) {
return $this->success(['not_expired' => $not_expired, 'expired' => $expired]);
}
$nowTime = time();
$coupons = Db::table('ims_system_coupon_user_receive as receive')
->join('ims_system_coupon_user as u', 'u.id', '=', 'receive.system_coupon_user_id')
->where([
['receive.user_id','=',$userId],
])
->whereIn('receive.status',[0,1])
->select('u.*','receive.number_remain')
->orderBy('u.weigh','desc')
->get();
foreach ($coupons as $key => $coupon) {
if ($coupon->usable_end_time < $nowTime || $coupon->number_remain <= 0) {
$expired[] = $coupon;
} else {
$not_expired[] = $coupon;
}
}
$ret = ['not_expired' => $not_expired, 'expired' => $expired];
return $this->success($ret);
}
/**
* 获取用户当前订单可用的优惠券列表
* 按分类(1订单 等优惠)分组返回
*/
public function getUserAvailableCoupons()
{
// 获取参数
# 订单金额
$orderAmount = $this->request->input('order_amount', 0);
# 用户id
$userId = $this->request->input('user_id', 0);
# 市场id
$marketId = $this->request->input('market_id', 0);
# 类型,1全平台 2线上 3线下,20200718默认全平台
$type = $this->request->input('type', 1);
# 店铺类型id
$storetypeId = $this->request->input('storetype_id', 0);
$storetypeIds = explode(',', str_replace(',', ',', $storetypeId));
$available = [];
$notAvailable = [];
if ($this->empty($orderAmount) || $this->empty($userId)) {
return $this->success([
'available' => $available,
'not_available' => array_values($notAvailable)
]);
}
// 获取用户优惠券
$currentTime = time();
$data = Db::table('ims_system_coupon_user_receive as receive')
->select([
'receive.id as receive_id',
'receive.user_id',
'receive.number_remain',
'coupon.id',
'coupon.title',
'coupon.full_amount',
'coupon.discounts',
'coupon.usable_start_time',
'coupon.usable_end_time',
'coupon.discount_type'
])
->join('ims_system_coupon_user as coupon', 'coupon.id', '=', 'receive.system_coupon_user_id')
->where(['receive.user_id' => $userId])
->whereIn('receive.status', [0,1])
->where('receive.number_remain', '>', 0)
->whereIn('coupon.type', [1,$type])
->where('coupon.full_amount', '<=', $orderAmount)
->where('coupon.usable_start_time', '<=', $currentTime)
->where('coupon.usable_end_time', '>=', $currentTime)
->where('coupon.usable_number', '<=', Db::raw('receive.number_remain'))
->where('coupon.market_id', 'in', [0, $marketId])
->whereIn('coupon.storetype_id', $storetypeIds)
->orderByRaw('coupon.discounts DESC, coupon.full_amount DESC')
->get();
// 分离用户今天用过的优惠券种类
$container = ApplicationContext::getContainer();
$redis = $container->get(Redis::class);
$couponIds = $redis->sMembers('coupon_'.date('Ymd').'_used_'.$userId);
foreach ($data as $key => &$item) {
if (in_array($item->id, $couponIds)) {
$notAvailable[$item->id] = $item;
} else {
$available[] = $item;
}
}
return $this->success([
'available' => $available,
'not_available' => array_values($notAvailable)
]);
}
}