修改优惠卷过期数据
This commit is contained in:
parent
35ad7ae43a
commit
5e9f7bed99
@ -42,8 +42,6 @@ class UserCouponExpiredDelayDirectConsumer extends ConsumerMessage
|
||||
{
|
||||
Log::getInstance("queue-UserCouponExpired")->info("开始:" . json_encode($data, JSON_UNESCAPED_UNICODE));
|
||||
|
||||
return Result::DROP;
|
||||
|
||||
// 检测参数
|
||||
if (!isset($data['user_coupon_id'])){
|
||||
Log::getInstance("queue-UserCouponExpired")->error("入参错误");
|
||||
@ -77,12 +75,11 @@ class UserCouponExpiredDelayDirectConsumer extends ConsumerMessage
|
||||
try {
|
||||
// 处理未过期事件
|
||||
// 先删除-重新添加队列
|
||||
if ($valid_end_time > time()){
|
||||
$time = $valid_end_time - time();
|
||||
if ($time > 0){
|
||||
$time = $valid_end_time - time();
|
||||
Log::getInstance("queue-UserCouponExpired")->info($time);
|
||||
|
||||
if ($time < 60 * 60 * 24){
|
||||
$time = 60 * 60 * 24;
|
||||
if ($time <= 0){
|
||||
$time = 60 * 5;
|
||||
}
|
||||
|
||||
$queue_data = array();
|
||||
|
||||
163
app/Command/AddUserCouponExpiredQueueCommand.php
Normal file
163
app/Command/AddUserCouponExpiredQueueCommand.php
Normal file
@ -0,0 +1,163 @@
|
||||
<?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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取需执行的用户优惠卷
|
||||
* @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();
|
||||
}
|
||||
}
|
||||
@ -35,6 +35,7 @@ use App\Model\OrderPrescription;
|
||||
use App\Model\OrderPrescriptionIcd;
|
||||
use App\Model\UserDoctor;
|
||||
use App\Model\UserDoctorInfo;
|
||||
use App\Services\CouponService;
|
||||
use App\Services\ImService;
|
||||
use App\Services\InquiryService;
|
||||
use App\Services\MessagePush;
|
||||
@ -458,26 +459,6 @@ class TestController extends AbstractController
|
||||
}
|
||||
|
||||
public function test_17(){
|
||||
$coupon['valid_days'] = 90;
|
||||
$data['valid_end_time'] = date("Y-m-d H:i:s", strtotime($coupon['valid_days'] . " day"));
|
||||
|
||||
$valid_end_time = strtotime($data['valid_end_time']);
|
||||
|
||||
$data = array();
|
||||
$data['user_coupon_id'] = 1;
|
||||
|
||||
$time = $valid_end_time - time();
|
||||
dump($time);die;
|
||||
if ($time < 0){
|
||||
return false;
|
||||
}
|
||||
$message = new UserCouponExpiredDelayDirectProducer($data);
|
||||
$message->setDelayMs(1000 * $time);
|
||||
$producer = $this->container->get(Producer::class);
|
||||
$res = $producer->produce($message);
|
||||
if (!$res) {
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@ -6,6 +6,7 @@ namespace App\Model;
|
||||
|
||||
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Hyperf\Database\Model\Collection;
|
||||
use Hyperf\Database\Model\Relations\HasOne;
|
||||
use Hyperf\Snowflake\Concern\Snowflake;
|
||||
@ -19,8 +20,8 @@ use Hyperf\Snowflake\Concern\Snowflake;
|
||||
* @property string $coupon_use_date 使用时间
|
||||
* @property string $valid_start_time 开始使用时间
|
||||
* @property string $valid_end_time 过期使用时间
|
||||
* @property \Carbon\Carbon $created_at 创建时间
|
||||
* @property \Carbon\Carbon $updated_at 修改时间
|
||||
* @property Carbon $created_at 创建时间
|
||||
* @property Carbon $updated_at 修改时间
|
||||
* @property-read Coupon $Coupon
|
||||
*/
|
||||
class UserCoupon extends Model
|
||||
@ -183,4 +184,18 @@ class UserCoupon extends Model
|
||||
->get($fields);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取患者今日过期优惠卷
|
||||
* @param array $params
|
||||
* @param array $valid_end_time
|
||||
* @param array $fields
|
||||
* @return Collection|array
|
||||
*/
|
||||
public static function getUserTodayExpiredCoupon(array $params,array $valid_end_time,array $fields = ['*']): Collection|array
|
||||
{
|
||||
return self::where($params)
|
||||
// ->where('valid_start_time',">=","2024-05-01 00:00:00") // 此处是为了区分于1.3版本上线前的优惠卷
|
||||
->whereBetween('valid_end_time', $valid_end_time)
|
||||
->get($fields);
|
||||
}
|
||||
}
|
||||
|
||||
@ -51,15 +51,15 @@ class CouponService extends BaseService
|
||||
}
|
||||
|
||||
// 判断用户是否已有该优惠卷
|
||||
$params = array();
|
||||
$params['user_id'] = $user_id;
|
||||
$params['coupon_id'] = $coupon['coupon_id'];
|
||||
$user_coupon = UserCoupon::getOne($params);
|
||||
if (!empty($user_coupon)){
|
||||
if ($user_coupon['user_coupon_status'] == 0){
|
||||
return true;
|
||||
}
|
||||
}
|
||||
// $params = array();
|
||||
// $params['user_id'] = $user_id;
|
||||
// $params['coupon_id'] = $coupon['coupon_id'];
|
||||
// $user_coupon = UserCoupon::getOne($params);
|
||||
// if (!empty($user_coupon)){
|
||||
// if ($user_coupon['user_coupon_status'] == 0){
|
||||
// return true;
|
||||
// }
|
||||
// }
|
||||
|
||||
// 判断该优惠卷状态
|
||||
if ($coupon['coupon_status'] != 1){
|
||||
@ -132,22 +132,24 @@ class CouponService extends BaseService
|
||||
}
|
||||
}
|
||||
|
||||
// 添加优惠卷过期队列
|
||||
// 优惠卷过期时间
|
||||
$valid_end_time = strtotime($user_coupon['valid_end_time']);
|
||||
|
||||
$data = array();
|
||||
$data['user_coupon_id'] = $user_coupon['user_coupon_id'];
|
||||
// 当天结束时间
|
||||
$day_end_time = strtotime(date('Y-m-d 23:59:59',time()));
|
||||
|
||||
$time = $valid_end_time - time();
|
||||
if ($time < 0){
|
||||
return false;
|
||||
}
|
||||
$message = new UserCouponExpiredDelayDirectProducer($data);
|
||||
$message->setDelayMs(1000 * $time);
|
||||
$producer = $this->container->get(Producer::class);
|
||||
$res = $producer->produce($message);
|
||||
if (!$res) {
|
||||
return false;
|
||||
if ($valid_end_time <= $day_end_time){
|
||||
$data = array();
|
||||
$data['user_coupon_id'] = $user_coupon['user_coupon_id'];
|
||||
|
||||
$time = $valid_end_time - time();
|
||||
$message = new UserCouponExpiredDelayDirectProducer($data);
|
||||
$message->setDelayMs(1000 * $time);
|
||||
$producer = $this->container->get(Producer::class);
|
||||
$res = $producer->produce($message);
|
||||
if (!$res) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}catch (\Throwable $e){
|
||||
Log::getInstance("CouponService-GrantUserCoupon")->error($e->getMessage());
|
||||
@ -155,20 +157,6 @@ class CouponService extends BaseService
|
||||
}
|
||||
|
||||
try {
|
||||
// 添加优惠卷即将过期提醒队列
|
||||
$time = floor($time * 0.75);
|
||||
|
||||
// 时间低于5小时,不进行过期提醒
|
||||
if ($time > 60 * 60 * 5){
|
||||
$message = new UserCouponExpiredNoticeDelayDirectProducer($data);
|
||||
$message->setDelayMs(1000 * $time);
|
||||
$producer = $this->container->get(Producer::class);
|
||||
$res = $producer->produce($message);
|
||||
if (!$res) {
|
||||
Log::getInstance("CouponService-GrantUserCoupon")->error("添加优惠卷即将过期提醒队列");
|
||||
}
|
||||
}
|
||||
|
||||
// 通知-患者-优惠卷发放
|
||||
$MessagePush = new MessagePush($user_id);
|
||||
$MessagePush->patientDistributeCoupon($coupon['coupon_name']);
|
||||
@ -212,14 +200,16 @@ class CouponService extends BaseService
|
||||
public function GrantBuyOrderServicePackageCoupon(string $user_id): bool
|
||||
{
|
||||
// 获取购买服务包的用户可领取的优惠卷列表
|
||||
$coupon = Coupon::getOrderServicePackageCouponList();
|
||||
if (empty($coupon)) {
|
||||
$coupons = Coupon::getOrderServicePackageCouponList();
|
||||
if (empty($coupons)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
foreach ($coupon as $value){
|
||||
// dump($coupon->toArray());die;
|
||||
foreach ($coupons as $coupon){
|
||||
// 发放优惠卷
|
||||
$res = $this->GrantUserCoupon($value['coupon_id'],$user_id);
|
||||
$res = $this->GrantUserCoupon($coupon['coupon_id'],$user_id);
|
||||
dump($res);die;
|
||||
if (!$res){
|
||||
// 发放失败
|
||||
return false;
|
||||
|
||||
@ -892,7 +892,7 @@ Router::addGroup('/case', function () {
|
||||
|
||||
// 测试使用
|
||||
Router::addGroup('/test', function () {
|
||||
Router::get('', [TestController::class, 'test1111']);
|
||||
Router::get('', [TestController::class, 'test']);
|
||||
|
||||
//
|
||||
// Router::get('/uninquiry', [TestController::class, 'uninquiry']);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user