158 lines
5.4 KiB
PHP
158 lines
5.4 KiB
PHP
<?php
|
|
/**
|
|
* This service file is part of item.
|
|
*
|
|
* @author ctexthuang
|
|
* @contact ctexthuang@qq.com
|
|
*/
|
|
|
|
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\Lib\Log;
|
|
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\Amqp\Producer;
|
|
use Hyperf\DbConnection\Db;
|
|
use Hyperf\Di\Annotation\Inject;
|
|
use Psr\Container\ContainerExceptionInterface;
|
|
use Psr\Container\NotFoundExceptionInterface;
|
|
|
|
class AutoDispenseService
|
|
{
|
|
use CouponTrait;
|
|
|
|
/**
|
|
* @var CouponDispenseLog
|
|
*/
|
|
#[Inject]
|
|
protected CouponDispenseLog $couponDispenseLogModel;
|
|
|
|
/**
|
|
* @var CouponDispenseUser
|
|
*/
|
|
#[Inject]
|
|
protected CouponDispenseUser $couponDispenseUserModel;
|
|
|
|
/**
|
|
* @var UserCoupon
|
|
*/
|
|
#[Inject]
|
|
protected UserCoupon $userCouponModel;
|
|
|
|
/**
|
|
* @var CouponTemplate
|
|
*/
|
|
#[Inject]
|
|
protected CouponTemplate $couponTemplateModel;
|
|
|
|
/**
|
|
* @var Producer
|
|
*/
|
|
#[Inject]
|
|
protected Producer $producer;
|
|
|
|
/**
|
|
* @var Log
|
|
*/
|
|
#[Inject]
|
|
protected Log $log;
|
|
|
|
/**
|
|
* @var int
|
|
*/
|
|
public int $couponDispenseId;
|
|
|
|
/**
|
|
* @var int
|
|
*/
|
|
public int $userId;
|
|
|
|
/**
|
|
* @return void
|
|
* @throws ContainerExceptionInterface
|
|
* @throws NotFoundExceptionInterface
|
|
* @throws Exception
|
|
*/
|
|
public function handle(): void
|
|
{
|
|
$dispenseInfo = $this->couponDispenseLogModel->getInfoById($this->couponDispenseId);
|
|
if (empty($dispenseInfo)) throw new Exception('分发记录不存在');
|
|
|
|
$templateInfo = $this->couponTemplateModel->getInfoById($dispenseInfo->coupon_template_id);
|
|
if (empty($templateInfo)) throw new Exception('优惠券模板不存在');
|
|
|
|
$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('该分发逻辑已失效');
|
|
}
|
|
|
|
$userDispenseInfo = $this->couponDispenseUserModel->getInfoByDispenseIdAndUserId($this->couponDispenseId,$this->userId);
|
|
if (empty($userDispenseInfo)) throw new Exception('该用户不属于该分发逻辑');
|
|
|
|
$userCount = $this->userCouponModel->where('coupon_dispense_id',$this->couponDispenseId)->where('user_id',$this->userId)->count() ?? 0;
|
|
if ($userCount >= $dispenseInfo->item_count) throw new Exception('该用户该分发逻辑已分发完毕');
|
|
|
|
// 数据
|
|
$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'],
|
|
];
|
|
|
|
$insertData = array_map(function () use ($oneData) {
|
|
return $oneData;
|
|
}, array_fill(0, $dispenseInfo->item_count - $userCount, null));
|
|
|
|
Db::transaction(function () use ($insertData,$dispenseInfo,$userDispenseInfo) {
|
|
// 添加优惠券信息
|
|
if (!(new UserCoupon())->insert($insertData)) throw new Exception('写入数据失败');
|
|
|
|
// 修改分发用户信息
|
|
$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' => '您的优惠券已送达哦'],
|
|
];
|
|
|
|
$message = new WxSubMessageProducer([
|
|
'type' => WxMiniCode::SEND_MSG_TYPE_GET_COUPON,
|
|
'user_id' => $this->userId,
|
|
'data' => $msgData,
|
|
'page' => null
|
|
]);
|
|
$this->producer->produce($message);
|
|
}
|
|
}
|
|
} |