hospital-applets-api/app/Command/ReportPreProductOrderCommand.php
2023-04-12 14:38:47 +08:00

230 lines
8.0 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\Model\OrderProduct;
use App\Model\UserPatient;
use App\Services\MessagePush;
use App\Services\OrderPrescriptionService;
use App\Services\OrderProductService;
use Hyperf\Command\Command as HyperfCommand;
use Hyperf\Command\Annotation\Command;
use Hyperf\DbConnection\Db;
use Hyperf\Redis\Redis;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\ContainerInterface;
use Psr\Container\NotFoundExceptionInterface;
/**
* 商品订单上报处方平台
* 此处单商品订单上报两次
*/
#[Command]
class ReportPreProductOrderCommand extends HyperfCommand
{
public function __construct(protected ContainerInterface $container)
{
parent::__construct('ReportPreProductOrder:command');
}
public function configure()
{
parent::configure();
$this->setDescription('上报处方平台商品订单');
}
/**
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function handle()
{
$this->line("上报处方平台商品订单开始");
try {
// 获取可上报商品订单
$order_product_ids = $this->getExecProductOrder();
if (empty($order_product_ids)){
$this->line("上报处方平台商品订单结束,无可上报的商品订单");
return;
}
// 获取缓存
$redis = $this->container->get(Redis::class);
} catch (\Exception $e) {
$this->line("上报处方平台商品订单失败:" . $e->getMessage());
return;
}
foreach ($order_product_ids as $item){
$this->line("本次请求订单号:" . $item['order_product_id']);
Db::beginTransaction();
$redis_key = "ReportPreProductOrder" . $item['order_product_id'];
// 处理超出最大执行次数/退款问题
try {
$redis_value = $redis->get($redis_key);
$redis->incr($redis_key);
if(!empty($redis_value)){
if ($redis_value >= 100){
// 存储上报失败
$this->savaReportFail($item['order_product_id'],"超出最大上报次数");
// 修改失败时药品订单数据
$this->savePreFailedOrderStatus($item['order_product_id'],"复核失败");
// 退款
$this->line("超出最大执行次数,执行退款");
$OrderProductService = new OrderProductService();
$OrderProductService->OrderProductRefund($item['order_product_id'],"药品订单退款");
// 获取患者用户id
$user_id = $this->getPatientUserId($item['patient_id']);
if (!empty($user_id)){
// 发送站内、订阅、短信消息-药品订单退款成功
$MessagePush = new MessagePush($user_id);
$MessagePush->refundProductSuccess($item['order_product_id']);
}
// 清空缓存
$redis->del($redis_key);
Db::commit();
continue;
}
}
Db::commit();
} catch (\Exception $e) {
Db::rollBack();
// 记录失败次数
$this->line("商品订单上报处方平台失败:失败原因" . $e->getMessage());
}
// 处理上报处方平台问题
try {
// 上报处方平台
$orderPrescriptionService = new OrderPrescriptionService();
$orderPrescriptionService->reportPrescription($item['order_product_id']);
// 存储上报成功
$this->savaReportSuccess($item['order_product_id']);
Db::commit();
} catch (\Exception $e) {
Db::rollBack();
// 记录失败次数
$redis->incr($redis_key);
$this->line("上报失败:失败原因" . $e->getMessage());
}
// 清空缓存
$redis->del($redis_key);
$this->line("商品订单上报处方平台成功");
}
$this->line("商品订单上报处方平台全部结束");
}
/**
* 获取可上报商品订单
* @return array
*/
protected function getExecProductOrder(): array
{
$params = array();
$params['order_product_status'] = 2; // 订单状态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['report_pre_status'] = 0; // 上报处方平台状态0:未上报 1:已上报 2:上报失败))
$fields = [
'order_product_id',
'patient_id',
];
$order_product = OrderProduct::getList($params,$fields);
if (empty($order_product)){
return [];
}
return $order_product->toArray();
}
/**
* 存储上报失败
* @param string $order_product_id 商品订单id
* @param string $report_pre_fail_reason 上报失败原因
* @return void
*/
protected function savaReportFail(string $order_product_id,string $report_pre_fail_reason = ""): void
{
$params = array();
$params['order_product_id'] = $order_product_id;
$data = array();
$data['report_pre_status'] = 2; // 上报处方平台状态0:未上报 1:已上报 2:上报失败))
if (!empty($report_pre_fail_reason)){
$data['report_pre_fail_reason'] = $report_pre_fail_reason;
}
OrderProduct::edit($params,$data);
}
/**
* 存储上报成功
* @param string $order_product_id 商品订单id
* @return void
*/
protected function savaReportSuccess(string $order_product_id): void
{
$params = array();
$params['order_product_id'] = $order_product_id;
$data = array();
$data['report_pre_status'] = 1; // 上报处方平台状态0:未上报 1:已上报 2:上报失败))
$data['report_pre_time'] = date('Y-m-d H:i:s',time()); // 上报处方平台时间
$data['report_pre_fail_reason'] = ""; // 上报失败原因 置为空
OrderProduct::edit($params,$data);
}
/**
* 修改失败时药品订单数据
* @param string $order_product_id
* @param string $cancel_remarks
*/
protected function savePreFailedOrderStatus(string $order_product_id,string $cancel_remarks)
{
// 订单状态1:待支付 2:待发货 3:已发货 4:已签收 5:已取消)
$params = array();
$params['order_product_id'] = $order_product_id;
$data['order_product_status'] = 5;
$data['cancel_reason'] = 2; // 订单取消原因1:主动取消 2:复核失败/库存不足 3:支付超时 4:客服取消)
$data['cancel_time'] = date('Y-m-d H:i:s'); // 订单取消时间
$data['cancel_remarks'] = $cancel_remarks; // 订单取消备注(自动添加)
OrderProduct::edit($params,$data);
}
/**
* 获取患者用户id
* @param string $patient_id
* @return string
*/
protected function getPatientUserId(string $patient_id): string
{
// 获取患者数据
$params = array();
$params['patient_id'] = $patient_id;
$user_patient = UserPatient::getOne($params);
if (empty($user_patient)){
return "";
}
return $user_patient['user_id'];
}
}