|
|
<?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 Hyperf\Di\Annotation\Inject;use App\Model\CouponUserRecType;use App\Model\Coupon;use App\Model\CouponRec;use Hyperf\DbConnection\Db;use Hyperf\Redis\Redis;use Hyperf\Utils\ApplicationContext;use App\Request\CouponGetListRequest;use App\Service\CouponServiceInterface;
class CouponController extends BaseController{ /** * @Inject * @var CouponServiceInterface */ protected $couponService;
/** * 获取用户可领取优惠卷接口 */ public function getSystemCouponUserList(CouponGetListRequest $validator) { $userId = $this->request->input('user_id', 0); $receiveType = $this->request->input('receive_type', 0); $res = $this->couponService->getSystemCouponUserList($userId,$receiveType); return $this->success($res); }
//统计用户
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) {
$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; //如果优惠卷库存小于等于已领取的数量, 则返回领取失败的优惠券
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.title','u.discounts','u.full_amount','u.discount_type','u.introduce','u.usable_end_time','receive.number_remain') ->orderBy('u.weigh','desc') ->get();
foreach ($coupons as $key => $coupon) { //拼接满减文字提示
$coupon->full_amount_text = '满' . $coupon->full_amount . "可用"; //判断是折扣优惠券还是满减优惠券
if($coupon->discount_type == 1){ $coupon->discounts_text = '¥'.$coupon->discounts; }elseif($coupon->discount_type == 2){ $coupon->discounts_text = floatval($coupon->discounts)."折"; } //失效时间格式转换
$usable_end_time = date('Y-m-d H:i:s',$coupon->usable_end_time); $coupon->usable_end_time_text = '有效期至:'.$usable_end_time;
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); # 购物车商品id
$carIds = $this->request->input('car_ids', 0);
$res = $this->couponService->getUserAvailableCoupons($orderAmount,$userId,$marketId,$type,$storetypeId,$carIds); return $this->success($res);
}
}
|