166 lines
5.0 KiB
PHP
166 lines
5.0 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Command;
|
|
|
|
use App\Amqp\Producer\UserCouponExpiredDelayDirectProducer;
|
|
use App\Amqp\Producer\UserCouponExpiredNoticeDelayDirectProducer;
|
|
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 AddUserCouponExpiredQueueCommand extends HyperfCommand
|
|
{
|
|
public function __construct(protected ContainerInterface $container)
|
|
{
|
|
parent::__construct('AddUserCouponExpiredQueue:command');
|
|
}
|
|
|
|
public function configure(): void
|
|
{
|
|
parent::configure();
|
|
$this->setDescription('添加用户即将过期优惠卷添加至优惠卷过期队列');
|
|
}
|
|
|
|
public function handle(): void
|
|
{
|
|
$this->line('开始');
|
|
|
|
try {
|
|
// 获取需执行的用户优惠卷
|
|
$user_coupons = $this->getExecUserCoupon();
|
|
if (empty($user_coupons)){
|
|
$this->line("结束,无优惠卷可执行");
|
|
return;
|
|
}
|
|
}catch (\Throwable $e){
|
|
$this->line($e->getMessage());
|
|
return;
|
|
}
|
|
|
|
foreach ($user_coupons as $user_coupon){
|
|
Db::beginTransaction();
|
|
try {
|
|
// 添加优惠卷过期队列
|
|
$valid_end_time = strtotime($user_coupon['valid_end_time']);
|
|
|
|
$data = array();
|
|
$data['user_coupon_id'] = $user_coupon['user_coupon_id'];
|
|
|
|
$time = $valid_end_time - time();
|
|
if ($time < 0){
|
|
$time = 60 * 5;
|
|
}
|
|
|
|
$message = new UserCouponExpiredDelayDirectProducer($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());
|
|
}
|
|
}
|
|
|
|
try {
|
|
// 获取需执行提醒的用户优惠卷
|
|
$user_coupons = $this->getExecNoticeUserCoupon();
|
|
if (empty($user_coupons)){
|
|
$this->line("结束,无可执行需要提醒的优惠卷");
|
|
return;
|
|
}
|
|
}catch (\Throwable $e){
|
|
$this->line($e->getMessage());
|
|
return;
|
|
}
|
|
|
|
foreach ($user_coupons as $user_coupon){
|
|
try {
|
|
// 添加优惠卷过期队列
|
|
$valid_end_time = strtotime($user_coupon['valid_end_time']);
|
|
|
|
$data = array();
|
|
$data['user_coupon_id'] = $user_coupon['user_coupon_id'];
|
|
|
|
$time = $valid_end_time - time();
|
|
if ($time < 0){
|
|
continue;
|
|
}
|
|
|
|
$message = new UserCouponExpiredNoticeDelayDirectProducer($data);
|
|
$message->setDelayMs(1000 * $time);
|
|
$producer = $this->container->get(Producer::class);
|
|
$res = $producer->produce($message);
|
|
if (!$res) {
|
|
$this->line("添加提醒队列失败");
|
|
}
|
|
}catch (\Throwable $e){
|
|
// 修改优惠卷执行失败
|
|
$this->line($e->getMessage());
|
|
}
|
|
}
|
|
|
|
$this->line('全部结束');
|
|
}
|
|
|
|
/**
|
|
* 获取需执行的用户优惠卷
|
|
* @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['user_coupon_status'] = 0;
|
|
|
|
$user_coupons = UserCoupon::getUserTodayExpiredCoupon($params,$valid_end_time);
|
|
if (empty($user_coupons)){
|
|
return [];
|
|
}
|
|
|
|
return $user_coupons->toArray();
|
|
}
|
|
|
|
/**
|
|
* 获取需执行提醒的用户优惠卷
|
|
* @return array
|
|
*/
|
|
public function getExecNoticeUserCoupon(): array
|
|
{
|
|
// 获取三天后时间
|
|
$start_time = date('Y-m-d 00:00:00', strtotime(" +3 days")); // 当月第一天的开始时间
|
|
$end_time = date('Y-m-d 23:59:59', strtotime(" +3 days")); // 从开始时间起的指定天数后的结束时间
|
|
|
|
$valid_end_time = [$start_time,$end_time];
|
|
|
|
$params = array();
|
|
$params['user_coupon_status'] = 0;
|
|
|
|
$user_coupons = UserCoupon::getUserTodayExpiredCoupon($params,$valid_end_time);
|
|
if (empty($user_coupons)){
|
|
return [];
|
|
}
|
|
|
|
return $user_coupons->toArray();
|
|
}
|
|
}
|