新增了处理优惠卷过期队列和command
This commit is contained in:
@@ -0,0 +1,101 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Command;
|
||||
|
||||
use App\Amqp\Producer\CouponExpiredDelayDirectProducer;
|
||||
use App\Model\Coupon;
|
||||
use App\Model\UserCoupon;
|
||||
use Hyperf\Amqp\Producer;
|
||||
use Hyperf\Command\Command as HyperfCommand;
|
||||
use Hyperf\Command\Annotation\Command;
|
||||
use Hyperf\DbConnection\Db;
|
||||
use Psr\Container\ContainerInterface;
|
||||
|
||||
#[Command]
|
||||
class AddCouponExpiredQueueCommand extends HyperfCommand
|
||||
{
|
||||
public function __construct(protected ContainerInterface $container)
|
||||
{
|
||||
parent::__construct('AddCouponExpiredQueue');
|
||||
}
|
||||
|
||||
public function configure(): void
|
||||
{
|
||||
parent::configure();
|
||||
$this->setDescription('添加系统即将过期优惠卷添加至优惠卷过期队列');
|
||||
}
|
||||
|
||||
public function handle(): void
|
||||
{
|
||||
$this->line('开始');
|
||||
|
||||
try {
|
||||
// 获取需执行的用户优惠卷
|
||||
$coupons = $this->getExecUserCoupon();
|
||||
if (empty($coupons)){
|
||||
$this->line("结束,无优惠卷可执行");
|
||||
return;
|
||||
}
|
||||
}catch (\Throwable $e){
|
||||
$this->line($e->getMessage());
|
||||
return;
|
||||
}
|
||||
|
||||
foreach ($coupons as $coupon){
|
||||
Db::beginTransaction();
|
||||
try {
|
||||
// 添加优惠卷过期队列
|
||||
$valid_end_time = strtotime($coupon['valid_end_time']);
|
||||
|
||||
$data = array();
|
||||
$data['coupon_id'] = $coupon['coupon_id'];
|
||||
|
||||
$time = $valid_end_time - time();
|
||||
if ($time < 0){
|
||||
$time = 60 * 5;
|
||||
}
|
||||
|
||||
$message = new CouponExpiredDelayDirectProducer($data);
|
||||
$message->setDelayMs(1000 * $time);
|
||||
$producer = $this->container->get(Producer::class);
|
||||
$res = $producer->produce($message);
|
||||
if (!$res) {
|
||||
Db::rollBack();
|
||||
$this->line("添加队列失败");
|
||||
}
|
||||
|
||||
Db::commit();
|
||||
}catch (\Throwable $e){
|
||||
// 修改优惠卷执行失败
|
||||
Db::rollBack();
|
||||
$this->line($e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取需执行的优惠卷
|
||||
* @return array
|
||||
*/
|
||||
public function getExecUserCoupon(): array
|
||||
{
|
||||
// 过期使用时间
|
||||
$valid_end_time_1 = date('Y-m-d 00:00:00',time());
|
||||
$valid_end_time_2 = date('Y-m-d 23:59:59',time());
|
||||
$valid_end_time = [$valid_end_time_1,$valid_end_time_2];
|
||||
|
||||
$params = array();
|
||||
$params['coupon_status'] = 1;
|
||||
$params['valid_type'] = 1;
|
||||
|
||||
$coupons = Coupon::getTodayExpiredCoupon($params,$valid_end_time);
|
||||
if (empty($coupons)){
|
||||
return [];
|
||||
}
|
||||
|
||||
return $coupons->toArray();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user