hospital-applets-api/app/Command/AddServicePackageCompleteQueueCommand.php

130 lines
4.5 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
declare(strict_types=1);
namespace App\Command;
use App\Amqp\Producer\AutoCompleteServicePackageDelayDirectProducer;
use App\Model\OrderServicePackage;
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 AddServicePackageCompleteQueueCommand extends HyperfCommand
{
public function __construct(protected ContainerInterface $container)
{
date_default_timezone_set('Asia/Shanghai');
parent::__construct('addServicePackageFinishQueue');
}
public function configure(): void
{
parent::configure();
$this->setDescription('添加快结束服务包订单至服务包结束队列');
}
public function handle(): void
{
$this->line('开始');
try {
// 获取需执行的订单
$order_service_packages = $this->getExecOrder();
if (empty($order_service_packages)){
$this->line("结束,无订单可执行");
return;
}
}catch (\Throwable $e){
$this->line($e->getMessage());
return;
}
foreach ($order_service_packages as $order_service_package){
Db::beginTransaction();
try {
// 修改订单成功执行
$this->putAddFinishStatus($order_service_package['order_service_id'],1);
// 添加服务包订单完成延迟队列
$finish_time = strtotime($order_service_package['finish_time']);
$time = $finish_time - time();
if (\Hyperf\Config\config('app_env') == "dev"){
$time = 5;
}
$queue_data = array();
$queue_data['order_no'] = $order_service_package['order_service_no'];
$message = new AutoCompleteServicePackageDelayDirectProducer($queue_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());
$this->putAddFinishStatus($order_service_package['order_service_id'],2,$e->getMessage());
}
}
$this->line("全部结束");
}
/**
* 获取需执行的订单
* @return array
*/
public function getExecOrder(): array
{
// 获取三天后结束时间
$three_day_start_time = date('Y-m-d 00:00:00',strtotime('+3 days', strtotime(date('Y-m-d',time()))));
$three_day_finish_time = date('Y-m-d 23:59:59',strtotime('+3 days', strtotime(date('Y-m-d',time()))));
$finish_time_params = [$three_day_start_time,$three_day_finish_time];
$params = array();
$params['pay_status'] = 2; // 支付状态1:未支付 2:已支付 3:支付中 4:支付失败 5:支付超时 6:支付关闭 7:已撤销 8:转入退款)
$params['add_finish_status'] = 0; // 添加完成订单延迟队列状态0:未添加 1:已添加 2:添加失败)
$order_service_status_params = [3,5]; // 订单状态1:待支付 2:未开始 3:服务中 4:服务完成 5:服务取消)
$order_service_packages = OrderServicePackage::getInquiryWithFinishTime($params,$order_service_status_params,$finish_time_params);
if (empty($order_service_packages)){
return [];
}
return $order_service_packages->toArray();
}
/**
* 修改订单状态
* @param string $order_service_id
* @param int $add_finish_status
* @param string $add_finish_fail_reason
* @return void
*/
public function putAddFinishStatus(string $order_service_id,int $add_finish_status,string $add_finish_fail_reason = ""): void
{
$params = array();
$params['order_service_id'] = $order_service_id;
$data = array();
$data['add_finish_status'] = $add_finish_status;
$data['add_finish_time'] = date('Y-m-d H:i:s',time());
if (!empty($add_finish_fail_reason)){
$data['add_finish_fail_reason'] = $add_finish_fail_reason;
}
OrderServicePackage::edit($params,$data);
}
}