feat : auto dispense coupon
This commit is contained in:
@@ -10,19 +10,22 @@ declare(strict_types=1);
|
||||
|
||||
namespace App\Service\Amqp\Coupon;
|
||||
|
||||
use App\Amqp\Producer\WxSubMessageProducer;
|
||||
use App\Constants\Common\CouponCode;
|
||||
use App\Constants\Common\WxMiniCode;
|
||||
use App\Model\CouponDispenseLog;
|
||||
use App\Model\CouponDispenseUser;
|
||||
use App\Model\CouponTemplate;
|
||||
use App\Model\UserCoupon;
|
||||
use App\Service\ServiceTrait\Api\CouponTrait;
|
||||
use Exception;
|
||||
use Hyperf\Coroutine\Coroutine;
|
||||
use Hyperf\Amqp\Producer;
|
||||
use Hyperf\DbConnection\Db;
|
||||
use Hyperf\Di\Annotation\Inject;
|
||||
|
||||
class AutoDispenseService
|
||||
{
|
||||
|
||||
const int SINGLE_MAX = 1;
|
||||
use CouponTrait;
|
||||
|
||||
/**
|
||||
* @var CouponDispenseLog
|
||||
@@ -36,59 +39,108 @@ class AutoDispenseService
|
||||
#[Inject]
|
||||
protected CouponDispenseUser $couponDispenseUserModel;
|
||||
|
||||
/**
|
||||
* @var UserCoupon
|
||||
*/
|
||||
#[Inject]
|
||||
protected UserCoupon $userCouponModel;
|
||||
|
||||
/**
|
||||
* @var CouponTemplate
|
||||
*/
|
||||
#[Inject]
|
||||
protected CouponTemplate $couponTemplateModel;
|
||||
|
||||
/**
|
||||
* @var Producer
|
||||
*/
|
||||
#[Inject]
|
||||
protected Producer $producer;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
public int $couponDispenseId;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
public int $userId;
|
||||
|
||||
/**
|
||||
* @return void
|
||||
* @throws Exception
|
||||
*/
|
||||
public function handle(): void
|
||||
{
|
||||
$dispenseInfo = $this->couponDispenseLogModel->getInfoById($this->couponDispenseId);
|
||||
|
||||
if (empty($dispenseInfo)) throw new Exception('分发记录不存在');
|
||||
|
||||
$userIds = $this->couponDispenseUserModel->getNoSendUserListByDispenseId($this->couponDispenseId);
|
||||
if (empty($userIds)) throw new Exception('该分发已发送完毕');
|
||||
$templateInfo = $this->couponTemplateModel->getInfoById($dispenseInfo->coupon_template_id);
|
||||
if (empty($templateInfo)) throw new Exception('优惠券模板不存在');
|
||||
|
||||
$partArr = array_chunk($userIds, self::SINGLE_MAX);
|
||||
$validityTime = $this->getValidityTime((int)$dispenseInfo->validity_time_type, $dispenseInfo->validity_time_value);
|
||||
//有效期不存在或者到期销毁当前分发逻辑
|
||||
if (empty($validityTime) || empty($validityTime['start_time']) || empty($validityTime['end_time']) || $validityTime['end_time'] < date('Y-m-d H:i:s')) {
|
||||
$dispenseInfo->status = CouponCode::DISPENSE_NULL;
|
||||
if (!$dispenseInfo->save()) throw new Exception('修改分发记录状态失败');
|
||||
throw new Exception('该分发逻辑已失效');
|
||||
}
|
||||
|
||||
foreach ($partArr as $onePart) {
|
||||
$insertData = [];
|
||||
foreach ($onePart as $oneUser) {
|
||||
// 模拟数据
|
||||
$oneUserData = [
|
||||
'user_id' => $oneUser,
|
||||
'coupon_template_id' => $dispenseInfo->coupon_template_id,
|
||||
'coupon_name' => $dispenseInfo->coupon_name,
|
||||
'coupon_dispense_id' => $dispenseInfo->id,
|
||||
'status' => CouponCode::COUPON_STATUS_UNUSED,
|
||||
'validity_start_time' => $dispenseInfo->validity_start_time,
|
||||
'validity_end_time' => $dispenseInfo->validity_end_time,
|
||||
];
|
||||
$userDispenseInfo = $this->couponDispenseUserModel->getInfoByDispenseIdAndUserId($this->couponDispenseId,$this->userId);
|
||||
if (empty($userDispenseInfo)) throw new Exception('该用户不属于该分发逻辑');
|
||||
|
||||
$copies = array_map(function () use ($oneUserData) {
|
||||
return $oneUserData;
|
||||
}, array_fill(0, $dispenseInfo->item_count, null));
|
||||
$userCount = $this->userCouponModel->where('coupon_dispense_id',$this->couponDispenseId)->where('user_id',$this->userId)->count() ?? 0;
|
||||
if ($userCount >= $dispenseInfo->item_count) throw new Exception('该用户该分发逻辑已分发完毕');
|
||||
|
||||
$insertData = array_merge($insertData, $copies);
|
||||
}
|
||||
// 数据
|
||||
$oneData = [
|
||||
'user_id' => $this->userId,
|
||||
'coupon_template_id' => $dispenseInfo->coupon_template_id,
|
||||
'coupon_name' => $dispenseInfo->coupon_name,
|
||||
'coupon_dispense_id' => $dispenseInfo->id,
|
||||
'status' => CouponCode::COUPON_STATUS_UNUSED,
|
||||
'validity_start_time' => $validityTime['start_time'],
|
||||
'validity_end_time' => $validityTime['end_time'],
|
||||
];
|
||||
|
||||
Db::transaction(function () use ($insertData,$dispenseInfo,$onePart) {
|
||||
//添加优惠券信息
|
||||
if (!(new UserCoupon())->insert($insertData)) throw new Exception('写入数据失败');
|
||||
//修改分发用户信息
|
||||
if (!(new CouponDispenseUser())->updateReceiveCountByUserIds($onePart,$dispenseInfo->item_count)) throw new Exception('修改分发用户信息失败');
|
||||
$insertData = array_map(function () use ($oneData) {
|
||||
return $oneData;
|
||||
}, array_fill(0, $dispenseInfo->item_count - $userCount, null));
|
||||
|
||||
//修改分发 log 信息 (修改receive_count = origin_value + count($insertData) ps : 查询还没发的用户 * item_count / total_count = 完成百分比)
|
||||
if (!(new CouponDispenseLog())->updateReceiveCountById($dispenseInfo->id,count($insertData))) throw new Exception('修改分发log信息失败');
|
||||
});
|
||||
Db::transaction(function () use ($insertData,$dispenseInfo,$userDispenseInfo) {
|
||||
// 添加优惠券信息
|
||||
if (!(new UserCoupon())->insert($insertData)) throw new Exception('写入数据失败');
|
||||
|
||||
// 休息1秒
|
||||
Coroutine::sleep(1);
|
||||
// 修改分发用户信息
|
||||
$userDispenseInfo->receive_count = $userDispenseInfo->receive_count + count($insertData);
|
||||
$userDispenseInfo->is_receive = CouponCode::DISPENSE_STATUS_IS_RECEIVED;
|
||||
if (!$userDispenseInfo->save()) throw new Exception('修改分发用户信息失败');
|
||||
|
||||
//修改分发 log 信息 (修改receive_count = origin_value + count($insertData) ps : 查询还没发的用户 * item_count / total_count = 完成百分比)
|
||||
$dispenseInfo->receive_count = $dispenseInfo->receive_count + count($insertData);
|
||||
if (!$dispenseInfo->save()) throw new Exception('修改分发log信息失败');
|
||||
});
|
||||
|
||||
$nominalValue = match ($templateInfo->coupon_type) {
|
||||
CouponCode::COUPON_TYPE_INSTANT_REDUCTION => $templateInfo->amount.'元',
|
||||
CouponCode::COUPON_TYPE_DISCOUNT => ($templateInfo->ratio * 10).'%',
|
||||
};
|
||||
|
||||
foreach ($insertData as $item) {
|
||||
$msgData = [
|
||||
'thing2' => ['value' => $dispenseInfo->coupon_name],
|
||||
'thing9' => ['value' => $nominalValue],
|
||||
'thing8' => ['value' => $validityTime['start_time'].'/'.$validityTime['end_time']],
|
||||
'thing4' => ['value' => 'xxx'],
|
||||
];
|
||||
|
||||
$message = new WxSubMessageProducer([
|
||||
'type' => WxMiniCode::SEND_MSG_TYPE_GET_COUPON,
|
||||
'user_id' => $this->userId,
|
||||
'data' => $msgData,
|
||||
'page' => null
|
||||
]);
|
||||
$this->producer->produce($message);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user