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

409 lines
15 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\Order;
use App\Model\OrderPrescription;
use App\Model\OrderProduct;
use App\Model\UserPatient;
use App\Services\MessagePush;
use App\Services\OrderPrescriptionService;
use App\Services\OrderProductService;
use App\Services\OrderService;
use App\Utils\Log;
use Extend\Kuaidi100\Kuaidi;
use Extend\Prescription\Prescription;
use Hyperf\Command\Command as HyperfCommand;
use Hyperf\Command\Annotation\Command;
use Hyperf\DbConnection\Db;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\ContainerInterface;
use Psr\Container\NotFoundExceptionInterface;
/**
* 更新处方平台订单数据
*/
#[Command]
class getPrescriptionOrderStatusCommand extends HyperfCommand
{
public function __construct(protected ContainerInterface $container)
{
parent::__construct('getPrescriptionOrderStatus:command');
}
public function configure()
{
parent::configure();
$this->setDescription('获取处方平台订单数据');
}
public function handle()
{
$this->line("开始");
try {
// 获取可查询商品订单
$order_products = $this->getSearchableProductOrder();
if (empty($order_products)){
$this->line("结束:无可执行的商品订单");
return;
}
foreach ($order_products as $order_product) {
Db::beginTransaction();
$this->line("本次请求订单号:" . $order_product['order_product_id']);
// 获取药品订单处方数据
$params = array();
$params['order_prescription_id'] = $order_product['order_prescription_id'];
$order_prescription = OrderPrescription::getOne($params);
if (empty($order_prescription)){
Db::rollBack();
$this->line("结束:无处方数据");
continue;
}
try {
$Prescription = new Prescription();
$result = $Prescription->getPrescription($order_product['order_product_no'],$order_prescription['prescription_code']);
$this->line("处方平台数据:" . json_encode($result,JSON_UNESCAPED_UNICODE));
if (!isset($result['status'])){
Db::rollBack();
$this->line("失败:处方平台返回数据错误");
continue;
}
if ($result['status'] == "CFD01"){
// 正在处理中
Db::rollBack();
$this->line("结束:处方平台正在处理中,无需处理。");
continue;
}
if ($result['status'] == "CFD03" || $result['status'] == "CFD04" || $result['status'] == "CFD06" || $result['status'] == "CFD07"){
// 药师审核未通过/库存不足/已取消/申请退款
// 检测药品订单数据
$res = $this->checkPreFailedOrderStatus($order_product);
if (!$res){
Db::rollBack();
continue;
}
// 修改失败时药品订单数据
if ($result['status'] == "CFD03"){
$this->savePreFailedOrderStatus($order_product,"复核失败");
}elseif($result['status'] == "CFD04"){
$this->savePreFailedOrderStatus($order_product,"库存不足");
}else{
$this->savePreFailedOrderStatus($order_product,"订单取消");
}
// 执行退款
$OrderService = new OrderService();
$OrderService->orderRefund($order_product['order_product_no'],"订单退款");
}
if ($result['status'] == "CFD05"){
// 已发货/已取货
// 检测成功时药品订单数据
$res = $this->checkPreSuccessOrderStatus($order_product);
if (!$res){
Db::rollBack();
continue;
}
// 检测处方平台入参快递单号
$res = $this->checkPreLogisticsNo($result);
if (!$res){
Db::rollBack();
continue;
}
// 获取对应快递公司编码
$logistics_company_code = $this->getLogisticsCompanyCode($result['logisticsCompany']);
if (empty($logistics_company_code)){
Db::rollBack();
$this->line("处方平台已发货,快递公司编码识别失败!");
continue;
}
if (!isset($result['deliveryTime'])){
$result['deliveryTime'] = date('Y-m-d H:i:s',time());
}
// 检测物流编号是否已经存在并订阅
if (empty($order_product['logistics_no']) || $order_product['logistics_no'] != $result['deliveryId']){
$this->line("开始订阅快递推送");
// 订阅快递推送
$Kuaidi = new Kuaidi();
$Kuaidi->subscribe($result['deliveryId'],$logistics_company_code,$order_product['consignee_tel']);
// 修改成功时药品订单数据
$this->savePreSuccessOrderStatus($order_product,$result['deliveryId'],$logistics_company_code,$result['deliveryTime']);
try {
// 获取患者数据
$params = array();
$params['patient_id'] = $order_prescription['patient_id'];
$user_patient = UserPatient::getOne($params);
if (empty($user_patient)){
$this->line("推送消息错误:用户数据错误");
}
// 患者-药品已发货-订阅失败发送短信
$MessagePush = new MessagePush($user_patient['user_id']);
$MessagePush->productDelivery($order_product['order_product_id']);
$this->line("推送消息成功");
}catch (\Exception $e){
$this->line("推送消息错误:" . $e->getMessage());
}
}
}
$this->line("获取处方平台订单数据结束:处理完毕");
Db::commit();
} catch (\Throwable $e) {
Db::rollBack();
// 记录失败次数
$this->line("获取处方平台订单数据失败:" . $e->getMessage());
}
}
$this->line("获取处方平台订单数据全部处理结束");
} catch (\Exception $e) {
$this->line("获取处方平台订单数据失败:" . $e->getMessage());
}
}
/**
* 获取可查询商品订单
* @return array
*/
protected function getSearchableProductOrder(): array
{
$params = array();
$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'] = 1; // 上报处方平台状态0:未上报 1:已上报 2:上报失败))
$order_product_status = [2,3];// 订单状态1:待支付 2:待发货 3:已发货 4:已签收 5:已取消)
$order_product = OrderProduct::getStatusList($params,$order_product_status);
if (empty($order_product)){
return [];
}
return $order_product->toArray();
}
/**
* 检测失败时药品订单数据
* @param array|object $order_product
* @return bool
*/
protected function checkPreFailedOrderStatus(array|object $order_product): bool
{
// 订单状态1:待支付 2:待发货 3:已发货 4:已签收 5:已取消)
if ($order_product['order_product_status'] == 1){
$this->line("药品订单未支付,无需处理");
return false;
}
if ($order_product['order_product_status'] == 3){
$this->line("药品订单已发货,无需处理");
return false;
}
if ($order_product['order_product_status'] == 4){
$this->line("药品订单已签收,无需处理");
return false;
}
if ($order_product['order_product_status'] == 5){
$this->line("药品订单已取消,无需处理");
return false;
}
return true;
}
/**
* 检测成功时药品订单数据
* @param array|object $order_product
* @return bool
*/
protected function checkPreSuccessOrderStatus(array|object $order_product): bool
{
// 订单状态1:待支付 2:待发货 3:已发货 4:已签收 5:已取消)
if ($order_product['order_product_status'] == 1){
$this->line("药品订单未支付,无需处理");
return false;
}
if ($order_product['order_product_status'] == 4){
$this->line("药品订单已签收,无需处理");
return false;
}
if ($order_product['order_product_status'] == 5){
$this->line("药品订单已取消,无需处理");
return false;
}
return true;
}
/**
* 修改失败时药品订单数据
* @param array|object $order_product
* @param string $cancel_remarks
*/
protected function savePreFailedOrderStatus(array|object $order_product,string $cancel_remarks): void
{
// 取消订单
$order_data = array();
$order_data['cancel_status'] = 1;
$order_data['cancel_time'] = date("Y-m-d H:i:s", time());
$order_data['cancel_remarks'] = $cancel_remarks;
$order_data['updated_at'] = date("Y-m-d H:i:s", time());
$params = array();
$params['order_id'] = $order_product['order_id'];
Order::edit($params, $order_data);
// 订单状态1:待支付 2:待发货 3:已发货 4:已签收 5:已取消)
$params = array();
$params['order_product_id'] = $order_product['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 array|object $order_product
* @param string $logistics_no // 物流编号
* @param string $logistics_company_code // 快递公司编码
* @param string $delivery_time
*/
protected function savePreSuccessOrderStatus(array|object $order_product,string $logistics_no,string $logistics_company_code,string $delivery_time = "")
{
$params = array();
$params['order_product_id'] = $order_product['order_product_id'];
$data['order_product_status'] = 3; // 订单状态1:待支付 2:待发货 3:已发货 4:已签收 5:已取消)
$data['logistics_no'] = $logistics_no; // 物流编号
$data['logistics_company_code'] = $logistics_company_code; // 快递公司编码
$data['sub_logistics_status'] = 1; // 快递推送订阅状态0:未订阅/无需订阅 1:已订阅 2:订阅失败)
$data['delivery_time'] = $delivery_time ?: date('Y-m-d H:i:s',time()); // 发货时间
OrderProduct::edit($params,$data);
}
/**
* 检测处方平台入参快递单号
* @param array $result 处方平台返回数据 response.data.result
* @return bool
*/
protected function checkPreLogisticsNo(array $result): bool
{
if (!isset($result['deliveryId'])){
// 快递单号
$this->line("处方平台已发货,但未返回快递单号!");
return false;
}
if (!isset($result['logisticsCompany'])){
// 快递公司编码
$this->line("处方平台已发货,但未返回快递公司编码!");
return false;
}
return true;
}
/**
* 获取快递公司编码
* @param string $logistics_company 处方平台快递公司编码
* @return string
*/
protected function getLogisticsCompanyCode(string $logistics_company): string
{
switch ($logistics_company) {
case '1':
// 顺丰快递
$company_code = "shunfeng";
break;
case '2':
// 圆通快递
$company_code = "yuantong";
break;
case '3':
// 中通快递
$company_code = "zhongtong";
break;
case '4':
// 申通快递
$company_code = "shentong";
break;
case '5':
// 韵达快递
$company_code = "yunda";
break;
case '6':
// EMS快递
$company_code = "youzhengguonei";
break;
case '7':
// 京东快递
$company_code = "jd";
break;
case '8':
// 天天快递
$company_code = "tiantian";
break;
default:
// code...
break;
}
if (!isset($company_code)){
$this->line("处方平台已发货,快递公司编码识别失败!");
return "";
}
return $company_code;
}
/**
* 获取患者用户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'];
}
}