diff --git a/app/Command/ReportPreProductOrderCommand.php b/app/Command/ReportPreProductOrderCommand.php index 15abd71..3876486 100644 --- a/app/Command/ReportPreProductOrderCommand.php +++ b/app/Command/ReportPreProductOrderCommand.php @@ -42,6 +42,7 @@ class ReportPreProductOrderCommand extends HyperfCommand $this->line("上报处方平台商品订单开始"); try { + // 获取可上报商品订单 $order_product_ids = $this->getExecProductOrder(); if (empty($order_product_ids)){ $this->line("上报处方平台商品订单结束,无可上报的商品订单"); @@ -89,12 +90,11 @@ class ReportPreProductOrderCommand extends HyperfCommand Db::rollBack(); // 记录失败次数 $redis->incr($redis_key); - $this->line("上报处方平台商品订单失败:" . $e->getMessage()); + $this->line("商品订单上报处方平台失败:商品单号" . $item['order_product_id'] . " 失败原因:" . $e->getMessage()); $this->savaReportFail($item['order_product_id'],$e->getMessage()); } $this->line("商品订单上报处方平台成功:商品单号" . $item['order_product_id']); - return; } } diff --git a/app/Command/getPrescriptionOrderStatusCommand.php b/app/Command/getPrescriptionOrderStatusCommand.php index a2a830d..225c81e 100644 --- a/app/Command/getPrescriptionOrderStatusCommand.php +++ b/app/Command/getPrescriptionOrderStatusCommand.php @@ -4,12 +4,21 @@ 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 @@ -22,29 +31,338 @@ class getPrescriptionOrderStatusCommand extends HyperfCommand public function configure() { parent::configure(); - $this->setDescription('获取处方平台订单状态'); + $this->setDescription('获取处方平台订单数据'); } public function handle() { - $this->line("获取处方平台订单状态开始"); + $this->line("获取处方平台订单数据开始"); try { - // 获取需执行订单数据 - $params = array(); - $params['order_product_status'] = 2; // 订单状态(1:待支付 2:待发货 3:已发货 4:已签收 5:已取消) - $params['pay_status'] = 2; - $params['refund_status'] = 0; + // 获取可查询商品订单 + $order_product_ids = $this->getSearchableProductOrder(); + if (empty($order_product_ids)){ + $this->line("获取处方平台订单数据结束,无可执行的商品订单"); + return; + } - $field = [ - 'order_product_id', - 'order_inquiry_id', - 'order_inquiry_id', - ]; + foreach ($order_product_ids as $item) { + 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()); + $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; + } } diff --git a/app/Model/BasicBank.php b/app/Model/BasicBank.php index 720feea..e145161 100644 --- a/app/Model/BasicBank.php +++ b/app/Model/BasicBank.php @@ -31,11 +31,6 @@ class BasicBank extends Model */ protected array $fillable = ['bank_id', 'bank_code', 'bank_name', 'bank_img_path', 'created_at', 'updated_at']; - /** - * The attributes that should be cast to native types. - */ - protected array $casts = ['bank_id' => 'string', 'created_at' => 'datetime', 'updated_at' => 'datetime']; - protected string $primaryKey = "bank_id"; /** diff --git a/app/Model/BasicLogisticsCompany.php b/app/Model/BasicLogisticsCompany.php new file mode 100644 index 0000000..eaeff58 --- /dev/null +++ b/app/Model/BasicLogisticsCompany.php @@ -0,0 +1,57 @@ +first($fields); + } + + /** + * 获取信息-多条 + * @param array $params + * @param array $fields + * @return Collection|array + */ + public static function getList(array $params, array $fields = ['*']): Collection|array + { + return self::where($params)->get($fields); + } +} diff --git a/app/Model/OrderProduct.php b/app/Model/OrderProduct.php index c7040f3..97ec108 100644 --- a/app/Model/OrderProduct.php +++ b/app/Model/OrderProduct.php @@ -29,6 +29,7 @@ use Hyperf\Snowflake\Concern\Snowflake; * @property string $payment_amount_total 实际付款金额 * @property string $logistics_fee 运费金额 * @property string $logistics_no 物流编号 + * @property string $logistics_company_code 快递公司编码 * @property string $delivery_time 发货时间 * @property string $pay_time 支付时间 * @property string $remarks 订单备注 @@ -69,7 +70,7 @@ class OrderProduct extends Model /** * The attributes that are mass assignable. */ - protected array $fillable = ['order_product_id', 'order_inquiry_id', 'order_prescription_id', 'doctor_id', 'patient_id', 'family_id', 'order_product_no', 'escrow_trade_no', 'order_product_status', 'pay_channel', 'pay_status', 'is_delete', 'cancel_reason', 'amount_total', 'payment_amount_total', 'logistics_fee', 'logistics_no', 'delivery_time', 'pay_time', 'remarks', 'refund_status', 'cancel_time', 'cancel_remarks', 'report_pre_status', 'report_pre_time', 'report_pre_fail_reason', 'province_id', 'province', 'city_id', 'city', 'county_id', 'county', 'address', 'address_mask', 'consignee_name', 'consignee_name_mask', 'consignee_tel', 'consignee_tel_mask', 'created_at', 'updated_at']; + protected array $fillable = ['order_product_id', 'order_inquiry_id', 'order_prescription_id', 'doctor_id', 'patient_id', 'family_id', 'order_product_no', 'escrow_trade_no', 'order_product_status', 'pay_channel', 'pay_status', 'is_delete', 'cancel_reason', 'amount_total', 'payment_amount_total', 'logistics_fee', 'logistics_no', 'logistics_company_code', 'delivery_time', 'pay_time', 'remarks', 'refund_status', 'cancel_time', 'cancel_remarks', 'report_pre_status', 'report_pre_time', 'report_pre_fail_reason', 'province_id', 'province', 'city_id', 'city', 'county_id', 'county', 'address', 'address_mask', 'consignee_name', 'consignee_name_mask', 'consignee_tel', 'consignee_tel_mask', 'created_at', 'updated_at']; protected string $primaryKey = "order_product_id"; @@ -221,4 +222,17 @@ class OrderProduct extends Model return $data; } + + /** + * 获取某一状态下的订单 + * @param array $params + * @param array $order_product_status + * @param array $fields + * @return Collection|array + */ + public static function getStatusList(array $params = [],array $order_product_status = [], array $fields = ['*']): Collection|array + { + return self::where($params)->whereIn('order_product_status',$order_product_status)->get($fields); + } + } diff --git a/extend/Prescription/Prescription.php b/extend/Prescription/Prescription.php index deb642b..90a0495 100644 --- a/extend/Prescription/Prescription.php +++ b/extend/Prescription/Prescription.php @@ -226,7 +226,17 @@ class Prescription throw new BusinessException(HttpEnumCode::getMessage(HttpEnumCode::SERVER_ERROR)); } - return $response; + if (empty($response['data'])){ + // 返回值为空 + throw new BusinessException(HttpEnumCode::getMessage(HttpEnumCode::SERVER_ERROR)); + } + + if (empty($response['data']['result'])){ + // 返回值为空 + throw new BusinessException(HttpEnumCode::getMessage(HttpEnumCode::SERVER_ERROR)); + } + + return $response['data']['result']; } catch (GuzzleException $e) { throw new BusinessException($e->getMessage()); }