239 lines
8.3 KiB
PHP
239 lines
8.3 KiB
PHP
<?php
|
||
|
||
declare(strict_types=1);
|
||
|
||
namespace App\Command;
|
||
|
||
use App\Exception\BusinessException;
|
||
use App\Model\OrderPrescription;
|
||
use App\Model\OrderProduct;
|
||
use App\Model\OrderSystem;
|
||
use App\Model\UserDoctor;
|
||
use App\Model\UserPatient;
|
||
use App\Services\MessagePush;
|
||
use App\Services\OrderPrescriptionService;
|
||
use App\Services\OrderProductService;
|
||
use App\Utils\Log;
|
||
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(): void
|
||
{
|
||
$this->line("开始");
|
||
|
||
// 获取可上报商品订单
|
||
$order_product_ids = $this->getExecProductOrder();
|
||
if (empty($order_product_ids)){
|
||
$this->line("结束,无可上报的商品订单");
|
||
return;
|
||
}
|
||
|
||
foreach ($order_product_ids as $item){
|
||
$this->line("本次请求订单号:" . $item['order_product_id']);
|
||
|
||
// 获取商品订单数据
|
||
$params = array();
|
||
$params['order_product_id'] = $item['order_product_id'];
|
||
$order_product = OrderProduct::getOne($params);
|
||
if (empty($order_product)) {
|
||
$this->line("结束,商品订单数据错误");
|
||
continue;
|
||
}
|
||
|
||
if ($order_product['report_pre_status'] == 1){
|
||
$this->line("结束,订单已上报");
|
||
continue;
|
||
}
|
||
|
||
Db::beginTransaction();
|
||
|
||
try {
|
||
// 检测是否已上报监管平台 未上报监管平台不进行处理
|
||
|
||
|
||
// 检测执行次数
|
||
$res = $this->checkHandleNumber($item['order_product_id']);
|
||
if (!$res) {
|
||
Log::getInstance("queue-AutoPharmacistCaVerify")->error("错误:超出最大执行次数或检测错误");
|
||
|
||
// 修改药品订单表上报处方平台状态
|
||
$this->saveOrderProductPeportPreStatus($item['order_product_id'],2,"超出最大上报次数");
|
||
|
||
// // 修改失败时药品订单数据
|
||
// $this->savePreFailedOrderStatus($item['order_product_id'],"药房处方复核失败,平台会在24小时内进行退款处理");
|
||
//
|
||
// // 退款
|
||
// $OrderProductService = new OrderProductService();
|
||
// $OrderProductService->OrderProductRefund($item['order_product_id'],"药品订单退款");
|
||
|
||
Db::commit();
|
||
|
||
// 获取系统配置
|
||
$params = array();
|
||
$params['system_permission'] = "reportPreNotice";
|
||
$order_system = OrderSystem::getOne($params);
|
||
if (!empty($order_system)){
|
||
if ($order_system['is_sms'] == 1 && !empty($order_system['sms_mobile'])){
|
||
// 客服-通知客服药品订单上报处方平台失败
|
||
$MessagePush = new MessagePush();
|
||
$MessagePush->noticeReport($order_product['order_product_no'],$order_system['sms_mobile']);
|
||
}
|
||
}
|
||
|
||
continue;
|
||
}
|
||
}catch(\Exception $e){
|
||
Db::rollBack();
|
||
// 记录失败次数
|
||
$this->line("错误:" . $e->getMessage());
|
||
continue;
|
||
}
|
||
|
||
try {
|
||
// 上报处方平台
|
||
$orderPrescriptionService = new OrderPrescriptionService();
|
||
$orderPrescriptionService->reportPrescription($item['order_product_id']);
|
||
|
||
// 修改药品订单表上报处方平台状态
|
||
$this->saveOrderProductPeportPreStatus($item['order_product_id'],1);
|
||
|
||
$this->line("成功");
|
||
Db::commit();
|
||
continue;
|
||
} catch (\Exception $e) {
|
||
Db::rollBack();
|
||
$this->line("错误" . $e->getMessage());
|
||
continue;
|
||
}
|
||
}
|
||
$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_prescription_id',
|
||
];
|
||
|
||
$order_product = OrderProduct::getList($params,$fields);
|
||
if (empty($order_product)){
|
||
return [];
|
||
}
|
||
|
||
return $order_product->toArray();
|
||
}
|
||
|
||
/**
|
||
* 修改药品订单表上报处方平台状态
|
||
* @param string $order_product_id
|
||
* @param int $report_pre_status
|
||
* @param string $report_pre_fail_reason
|
||
* @return void
|
||
*/
|
||
protected function saveOrderProductPeportPreStatus(string $order_product_id,int $report_pre_status,string $report_pre_fail_reason = ""): void
|
||
{
|
||
$params = array();
|
||
$params['order_product_id'] = $order_product_id;
|
||
|
||
$data = array();
|
||
$data['report_pre_status'] = $report_pre_status; // 上报处方平台状态(0:未上报 1:已上报 2:上报失败))
|
||
$data['report_pre_time'] = date('Y-m-d H:i:s',time()); // 上报处方平台时间
|
||
if (!empty($report_pre_fail_reason)){
|
||
$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);
|
||
}
|
||
|
||
/**
|
||
* 检测执行次数
|
||
* @param string $order_product_id
|
||
* @return bool
|
||
* @throws ContainerExceptionInterface
|
||
* @throws NotFoundExceptionInterface
|
||
*/
|
||
protected function checkHandleNumber(string $order_product_id): bool
|
||
{
|
||
try {
|
||
$redis = $this->container->get(Redis::class);
|
||
|
||
$redis_key = "ReportPreProductOrder" . $order_product_id;
|
||
$redis_value = $redis->get($redis_key);
|
||
if (empty($redis_value)) {
|
||
$redis->set($redis_key, 1, 60 * 60 * 24 * 5);
|
||
return true;
|
||
}
|
||
|
||
// 执行次数过多
|
||
if ($redis_value > 300) {
|
||
// 加入短信队列,通知管理员
|
||
|
||
return false;
|
||
}
|
||
|
||
$redis->incr($redis_key);
|
||
} catch (\Exception $e) {
|
||
return false;
|
||
}
|
||
|
||
return true;
|
||
}
|
||
}
|