setDescription('获取处方平台订单数据'); } public function handle() { $this->line("获取处方平台订单数据开始"); try { // 获取可查询商品订单 $order_product_ids = $this->getSearchableProductOrder(); if (empty($order_product_ids)){ $this->line("获取处方平台订单数据结束,无可执行的商品订单"); return; } foreach ($order_product_ids as $item) { Db::beginTransaction(); // 是否执行了退款-预定义变量 $is_refund = 0; $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" || $result['status'] == "CFD06" || $result['status'] == "CFD07"){ // 药师审核未通过/库存不足/已取消/申请退款 // 检测药品订单数据 $res = $this->checkPreFailedOrderStatus($item); if (!$res){ Db::rollBack(); continue; } // 修改失败时药品订单数据 if ($result['status'] == "CFD03"){ $this->savePreFailedOrderStatus($item,"复核失败"); }elseif($result['status'] == "CFD04"){ $this->savePreFailedOrderStatus($item,"库存不足"); }else{ $this->savePreFailedOrderStatus($item,"订单取消"); } // 执行退款 $OrderProductService = new OrderProductService(); $OrderProductService->OrderProductRefund($item['order_product_id'],"订单退款"); // 标记为执行了退款 $is_refund = 1; } if ($result['status'] == "CFD05"){ // 已发货/已取货 // 检测成功时药品订单数据 $res = $this->checkPreSuccessOrderStatus($item); 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; } if (!isset($result['deliveryTime'])){ $result['deliveryTime'] = date('Y-m-d H:i:s',time()); } // 订阅快递推送 $Kuaidi = new Kuaidi(); $Kuaidi->subscribe($result['deliveryId'],$logistics_company_code,$item['consignee_tel']); // 修改成功时药品订单数据 $this->savePreSuccessOrderStatus($item,$result['deliveryId'],$logistics_company_code,$result['deliveryTime']); } $this->line("获取处方平台订单数据结束:处理完毕"); Db::commit(); } catch (\Exception $e) { Db::rollBack(); // 记录失败次数 $this->line("获取处方平台订单数据失败:" . $e->getMessage()); } // 检测是否执行了退款 if ($is_refund == 1){ try { $this->line("发送消息推送"); // 获取患者用户id $user_id = $this->getPatientUserId($item['patient_id']); if (!empty($user_id)){ // 发送站内、订阅、短信消息-药品订单退款成功 $MessagePush = new MessagePush($user_id); $MessagePush->refundProductSuccess($item['order_product_id']); } }catch(\Exception $e){ $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', 'patient_id', 'order_product_status', 'consignee_tel', ]; $order_product = OrderProduct::getStatusList($params,$order_product_status,$fields); 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) { // 订单状态(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']; } }