Files
hospital-applets-api/app/Command/getPrescriptionOrderStatusCommand.php
T
2023-04-12 13:34:05 +08:00

372 lines
14 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Command;
use App\Model\OrderPrescription;
use App\Model\OrderProduct;
use App\Services\OrderPrescriptionService;
use App\Services\OrderProductService;
use App\Utils\Log;
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_product_ids = $this->getSearchableProductOrder();
if (empty($order_product_ids)){
$this->line("获取处方平台订单数据结束,无可执行的商品订单");
return;
}
// dump($order_product_ids);die;
foreach ($order_product_ids as $item) {
if ($item['order_product_id'] != "504371738743726081"){
continue;
}
Db::beginTransaction();
$this->line("本次请求订单号:" . $item['order_product_id']);
// 获取药品订单处方数据
$params = array();
$params['order_prescription_id'] = $item['order_prescription_id'];
$order_prescription = OrderPrescription::getOne($params);
if (empty($order_prescription)){
Db::rollBack();
$this->line("获取处方平台订单数据失败:无处方数据。");
continue;
}
try {
$Prescription = new Prescription();
$result = $Prescription->getPrescription($item['order_product_no'],$order_prescription['prescription_code']);
Log::getInstance()->info("处方平台返回查询药品订单数据:",$result);
$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"){
// 药师审核未通过/库存不足
// 检测药品订单数据
$res = $this->checkPreFailedOrderStatus($order_prescription);
if (!$res){
Db::rollBack();
continue;
}
// 修改失败时药品订单数据
if ($result['status'] == "CFD03"){
$this->savePreFailedOrderStatus($order_prescription,"复核失败");
}else{
$this->savePreFailedOrderStatus($order_prescription,"库存不足");
}
// 执行退款
$OrderProductService = new OrderProductService();
$OrderProductService->OrderProductRefund($item['order_product_id'],"订单退款");
}
if ($result['status'] == "CFD05"){
// 已发货/已取货
// 检测成功时药品订单数据
$res = $this->checkPreSuccessOrderStatus($order_prescription);
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)){
$this->line("处方平台已发货,快递公司编码识别失败!");
continue;
}
// 修改成功时药品订单数据
$this->savePreSuccessOrderStatus($order_prescription,$result['deliveryId'],$logistics_company_code);
}
if ($result['status'] == "CFD06"){
// 已取消
// 检测药品订单数据
$res = $this->checkPreFailedOrderStatus($order_prescription);
if (!$res){
Db::rollBack();
continue;
}
// 修改药品订单数据
$this->savePreFailedOrderStatus($order_prescription,"订单取消");
// 执行退款
$OrderProductService = new OrderProductService();
$OrderProductService->OrderProductRefund($item['order_product_id'],"订单退款");
}
if ($result['status'] == "CFD07"){
// 申请退款
$OrderProductService = new OrderProductService();
$OrderProductService->OrderProductRefund($item['order_product_id'],"订单退款");
}
$this->line("获取处方平台订单数据结束:处理完毕");
Db::commit();
} catch (\Exception $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:已取消)
$fields = [
'order_product_id',
'order_product_no',
'order_prescription_id',
];
$order_product = OrderProduct::getStatusList($params,$order_product_status,$fields);
if (empty($order_product)){
return [];
}
return $order_product->toArray();
}
/**
* 检测失败时药品订单数据
* @param array|object $order_prescription
* @return bool
*/
protected function checkPreFailedOrderStatus(array|object $order_prescription): bool
{
// 订单状态(1:待支付 2:待发货 3:已发货 4:已签收 5:已取消)
if ($order_prescription['order_product_status'] == 1){
$this->line("药品订单未支付,无需处理");
return false;
}
if ($order_prescription['order_product_status'] == 3){
$this->line("药品订单已发货,无需处理");
return false;
}
if ($order_prescription['order_product_status'] == 4){
$this->line("药品订单已签收,无需处理");
return false;
}
if ($order_prescription['order_product_status'] == 5){
$this->line("药品订单已取消,无需处理");
return false;
}
return true;
}
/**
* 检测成功时药品订单数据
* @param array|object $order_prescription
* @return bool
*/
protected function checkPreSuccessOrderStatus(array|object $order_prescription): bool
{
// 订单状态(1:待支付 2:待发货 3:已发货 4:已签收 5:已取消)
if ($order_prescription['order_product_status'] == 1){
$this->line("药品订单未支付,无需处理");
return false;
}
if ($order_prescription['order_product_status'] == 4){
$this->line("药品订单已签收,无需处理");
return false;
}
if ($order_prescription['order_product_status'] == 5){
$this->line("药品订单已取消,无需处理");
return false;
}
return true;
}
/**
* 修改失败时药品订单数据
* @param array|object $order_prescription
* @param string $cancel_remarks
*/
protected function savePreFailedOrderStatus(array|object $order_prescription,string $cancel_remarks)
{
// 订单状态(1:待支付 2:待发货 3:已发货 4:已签收 5:已取消)
$params = array();
$params['order_product_id'] = $order_prescription['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_prescription
* @param string $logistics_no // 物流编号
* @param string $logistics_company_code // 快递公司编码
*/
protected function savePreSuccessOrderStatus(array|object $order_prescription,string $logistics_no,string $logistics_company_code)
{
$params = array();
$params['order_product_id'] = $order_prescription['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['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;
}
}