110 lines
3.6 KiB
PHP
110 lines
3.6 KiB
PHP
<?php
|
||
|
||
declare(strict_types=1);
|
||
|
||
namespace App\Command;
|
||
|
||
use App\Model\OrderServicePackage;
|
||
use Hyperf\Command\Command as HyperfCommand;
|
||
use Hyperf\Command\Annotation\Command;
|
||
use Hyperf\DbConnection\Db;
|
||
use Psr\Container\ContainerInterface;
|
||
|
||
#[Command]
|
||
class AddServicePackageFinishQueueCommand extends HyperfCommand
|
||
{
|
||
public function __construct(protected ContainerInterface $container)
|
||
{
|
||
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);
|
||
|
||
// 添加服务包订单完成延迟队列
|
||
|
||
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_finish_time = date('Y-m-d H:i:s',strtotime('+3 days', time()));
|
||
$finish_time_params = [date('Y-m-d H:i:s',time()),$three_day_finish_time];
|
||
|
||
$params = array();
|
||
$params['order_service_status'] = 3; // 订单状态(1:待支付 2:未开始 3:服务中 4:服务完成 5:服务取消)
|
||
$params['pay_status'] = 2; // 支付状态(1:未支付 2:已支付 3:支付中 4:支付失败 5:支付超时 6:支付关闭 7:已撤销 8:转入退款)
|
||
$params['refund_status'] = 0; // 商品订单退款状态(0:无退款 1:申请退款 2:退款中 3:退款成功 4:拒绝退款 5:退款关闭 6:退款异常)
|
||
$params['add_finish_status'] = 0; // 添加完成订单延迟队列状态(0:未添加 1:已添加 2:添加失败)
|
||
|
||
|
||
$order_service_packages = OrderServicePackage::getInquiryWithFinishTime($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);
|
||
}
|
||
}
|