188 lines
5.0 KiB
PHP
188 lines
5.0 KiB
PHP
<?php
|
|
|
|
namespace app\common\traits\app;
|
|
|
|
use think\Db;
|
|
use app\admin\model\app\coupon\Coupon;
|
|
use app\admin\model\app\coupon\CouponUser as CouponUserModel;
|
|
use think\exception\HttpResponseException;
|
|
use app\common\exception\Exception;
|
|
/**
|
|
* 发放优惠券
|
|
*/
|
|
trait CouponSend
|
|
{
|
|
|
|
/**
|
|
* 用户自己领取优惠券
|
|
*
|
|
* @param [type] $id
|
|
* @return void
|
|
*/
|
|
public function getCoupon($id)
|
|
{
|
|
$user = user_info();
|
|
|
|
$userCoupon = Db::transaction(function () use ($user, $id) {
|
|
$coupon = Coupon::canGet() // 在领取时间之内的
|
|
->lock(true)
|
|
->where('id', $id)
|
|
->find();
|
|
if (!$coupon) {
|
|
throw new Exception('优惠券未找到');
|
|
}
|
|
|
|
$userCoupon = $this->send($user, $coupon);
|
|
|
|
return $userCoupon;
|
|
});
|
|
|
|
return $userCoupon;
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
* 赠送优惠券
|
|
*
|
|
* @param array $user
|
|
* @param array|string $ids
|
|
* @return array
|
|
*/
|
|
public function giveCoupons($user, $ids)
|
|
{
|
|
$ids = is_array($ids) ? $ids : explode(',', $ids);
|
|
|
|
$result = Db::transaction(function () use ($user, $ids) {
|
|
$errors = []; // 发送失败的优惠券,包含失败原因
|
|
$success = []; // 发送成功的优惠券
|
|
$coupons = Coupon::statusHidden() // 只查询隐藏券(后台发放的券)
|
|
->canGet()
|
|
->whereIn('id', $ids)
|
|
->select();
|
|
|
|
$findCouponIds = array_column($coupons, 'id'); // 找到的优惠券 ids
|
|
$nofundIds = array_diff($ids, $findCouponIds);
|
|
foreach ($nofundIds as $nofund_id) {
|
|
$errors[] = ['id' => $nofund_id, 'error' => '优惠券未找到'];
|
|
}
|
|
foreach ($coupons as $coupon) {
|
|
try {
|
|
$userCoupon = $this->send($user, $coupon);
|
|
$success[] = $coupon->id;
|
|
} catch (HttpResponseException $e) {
|
|
$data = $e->getResponse()->getData();
|
|
$message = $data ? ($data['msg'] ?? '') : $e->getMessage();
|
|
$errors[] = ['id' => $coupon->id, 'error' => $message];
|
|
} catch (\Exception $e) {
|
|
$errors[] = ['id' => $coupon->id, 'error' => $e->getMessage()];
|
|
}
|
|
}
|
|
$result['success'] = $success;
|
|
$result['errors'] = $errors;
|
|
|
|
return $result;
|
|
});
|
|
return $result;
|
|
}
|
|
|
|
|
|
/**
|
|
* 发放优惠券
|
|
* @param $users 用户ID集合
|
|
* @param $couponId 优惠券Id
|
|
* @param $num 发放数量
|
|
* @return void
|
|
*/
|
|
public function manualSend($users, $couponId,$num=1)
|
|
{
|
|
$coupon = Coupon::canGet() // 在领取时间之内的
|
|
->lock(true)
|
|
->where('id', $couponId)
|
|
->find();
|
|
if (!$coupon) {
|
|
// 库存不足
|
|
throw new \app\common\exception\Exception('优惠券不在发放时间段');
|
|
}
|
|
if ($coupon->stock < count($users)) {
|
|
// 库存不足
|
|
throw new \app\common\exception\Exception('优惠券库存不足');
|
|
}
|
|
|
|
// 扣除库存
|
|
$coupon->setDec('stock', count($users));
|
|
|
|
$sends = [];
|
|
foreach ($users as $user) {
|
|
|
|
$current = [
|
|
'uniacid'=>UNIACID,
|
|
'user_id' => $user->id,
|
|
'coupon_id' => $coupon->id,
|
|
'use_time' => null,
|
|
'get_type'=>2,
|
|
'createtime' => time(),
|
|
'updatetime' => time(),
|
|
];
|
|
|
|
for($i=0;$i<$num;$i++){
|
|
$sends[] = $current;
|
|
}
|
|
|
|
}
|
|
|
|
CouponUserModel::insertAll($sends);
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
* 发放优惠券
|
|
*
|
|
* @param array|object $user 发放用户
|
|
* @param array|object $coupon 要发放的优惠券
|
|
* @return array|object
|
|
*/
|
|
private function send($user, $coupon) {
|
|
if ($coupon->get_status == 'cannot_get') {
|
|
throw new Exception('你已经领取过了');
|
|
}
|
|
|
|
if ($coupon->stock <= 0) {
|
|
throw new Exception('优惠券已经被领完了');
|
|
}
|
|
|
|
$coupon->setDec('stock');
|
|
$userCoupon = new CouponUserModel();
|
|
$userCoupon->uniacid = UNIACID;
|
|
$userCoupon->user_id = $user->id;
|
|
$userCoupon->coupon_id = $coupon->id;
|
|
$userCoupon->use_time = null;
|
|
$userCoupon->status = 1;
|
|
$userCoupon->get_type = 1;
|
|
$userCoupon->createtime = time();
|
|
$userCoupon->save();
|
|
|
|
return $userCoupon;
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
* 退回用户优惠券
|
|
*
|
|
* @param integer $user_coupon_id
|
|
* @return void
|
|
*/
|
|
public function backUserCoupon($user_coupon_id)
|
|
{
|
|
$userCoupon = CouponUserModel::where('id', $user_coupon_id)->find();
|
|
|
|
if ($userCoupon) {
|
|
$userCoupon->use_time = null;
|
|
$userCoupon->save();
|
|
}
|
|
}
|
|
}
|