diff --git a/member/modules/v1/controllers/ProductOrderController.php b/member/modules/v1/controllers/ProductOrderController.php index 0ae44e7..0d3d619 100644 --- a/member/modules/v1/controllers/ProductOrderController.php +++ b/member/modules/v1/controllers/ProductOrderController.php @@ -23,6 +23,7 @@ use common\models\oldDb\SystemConfig; use common\models\oldDb\User; use common\models\oldDb\UserPatient; use common\models\oldDb\WestRepice; +use common\models\newDb\DeliveryWarehouseModel; use common\modelsgii\oldDb\RefundReason; use common\services\ExpressService; use member\models\forms\ProductOrderSubmitForm; @@ -188,33 +189,41 @@ class ProductOrderController extends BaseAppController } $count = 0; + $west = null; + $chinese = null; + $granular = null; if($prescription && $prescription->wr_ids){ $wr_ids = explode(',', $prescription->wr_ids); if ($ProductOrder['prescription_type'] == 5) { - $west = ServicePackageRepice::find()->select('content,number,total_price')->where([ + $westModels = ServicePackageRepice::find()->select('content,number,total_price')->where([ 'in', 'id', $wr_ids ])->all(); } else { - $west = WestRepice::find()->select('content,number,total_price')->where([ + $westModels = WestRepice::find()->select('content,number,total_price')->where([ 'in', 'id', $wr_ids ])->all(); } - foreach ($west as $value) { - $value['content'] = Json::decode($value['content']); - if (is_array($value['content']) && !empty($value['content']['id']) && empty($value['content']['instruction'])) { - $drugInstruction = Drug::find()->select('instruction')->where(['id' => $value['content']['id']])->scalar(); + $west = []; + foreach ($westModels as $value) { + $row = $value->toArray(); + $content = Json::decode($row['content']); + if (is_array($content) && !empty($content['id']) && empty($content['instruction'])) { + $drugInstruction = Drug::find()->select('instruction')->where(['id' => $content['id']])->scalar(); if ($drugInstruction) { - $value['content']['instruction'] = $drugInstruction; + $content['instruction'] = $drugInstruction; } } + $row['content'] = $content; + $west[] = $row; } $count = count($west); }else if($prescription && $prescription->cr_ids){ $cr_ids = explode(',', $prescription->cr_ids); - $chinese = ChineseRepice::find()->select('content,total_price')->where([ + $chineseModels = ChineseRepice::find()->select('content,total_price')->where([ 'in', 'id', $cr_ids ])->all(); - foreach ($chinese as $val) { + $chinese = ['list' => []]; + foreach ($chineseModels as $val) { $content = Json::decode($val['content']); foreach ($content as $v){ $chinese['list'][]= $v; @@ -223,10 +232,11 @@ class ProductOrderController extends BaseAppController $count = count($chinese['list']); }else if($prescription && $prescription->gr_ids){ $gr_ids = explode(',', $prescription->gr_ids); - $granular = GranularRepice::find()->select('content,total_price')->where([ + $granularModels = GranularRepice::find()->select('content,total_price')->where([ 'in', 'id', $gr_ids ])->all(); - foreach ($granular as $val) { + $granular = ['list' => []]; + foreach ($granularModels as $val) { $content = Json::decode($val['content']); foreach ($content as $v){ $granular['list'][]= $v; @@ -241,14 +251,48 @@ class ProductOrderController extends BaseAppController } } + // 按订单明细回填仓名(appoint-medicine 展示用) + $orderItems = ProductOrderItems::find() + ->where(['product_order_id' => $ProductOrder['id']]) + ->asArray() + ->all(); + $enriched = $this->enrichOrderItemsWarehouse($orderItems); + $ProductOrder['has_delivery_warehouse'] = $enriched['has_delivery_warehouse'] ? 1 : 0; + $warehouseNameByDrugId = []; + foreach ($enriched['items'] as $it) { + $warehouseNameByDrugId[(int) ($it['drug_id'] ?? 0)] = (string) ($it['delivery_warehouse_name'] ?? ''); + } + if (!empty($west) && is_array($west)) { + foreach ($west as &$wItem) { + $drugId = (int) ($wItem['content']['id'] ?? 0); + $wItem['delivery_warehouse_name'] = $warehouseNameByDrugId[$drugId] ?? ''; + } + unset($wItem); + } + if (!empty($chinese['list']) && is_array($chinese['list'])) { + foreach ($chinese['list'] as &$cItem) { + $drugId = (int) ($cItem['id'] ?? $cItem['drug_id'] ?? 0); + $cItem['delivery_warehouse_name'] = $warehouseNameByDrugId[$drugId] ?? ''; + } + unset($cItem); + } + if (!empty($granular['list']) && is_array($granular['list'])) { + foreach ($granular['list'] as &$gItem) { + $drugId = (int) ($gItem['id'] ?? $gItem['drug_id'] ?? 0); + $gItem['delivery_warehouse_name'] = $warehouseNameByDrugId[$drugId] ?? ''; + } + unset($gItem); + } + $ProductOrder['count'] = $count; return [ 'ProductOrder' => $ProductOrder, 'userInfo' => $userInfo, 'hospital' => $hospital, - 'west' => $west ?? null, - 'chinese' => $chinese ?? null, - 'granular' => $granular ?? null, + 'west' => $west, + 'chinese' => $chinese, + 'granular' => $granular, + 'ProductOrderItems' => $enriched['items'], 'store' => $prescriptionStore ? ['store' => $prescriptionStore] : null, 'patient' => $this->getOrderPatientInfo((int) ($ProductOrder['up_id'] ?? 0), (int) ($ProductOrder['p_id'] ?? 0)), ]; @@ -442,8 +486,12 @@ class ProductOrderController extends BaseAppController $ProductOrderItems = ProductOrderItems::find()->where(['product_order_id' => $ProductOrder['id']])->with(['drug' => function($q){ $q->select('id,drug_name,is_otc,type,function,specification,usage,instruction'); }])->asArray()->all(); + // 回填配送仓名,供小程序按药展示「该药品由xxx配送」 + $enriched = $this->enrichOrderItemsWarehouse($ProductOrderItems); + $ProductOrderItems = $enriched['items']; + $ProductOrder['has_delivery_warehouse'] = $enriched['has_delivery_warehouse'] ? 1 : 0; $ProductOrder['count'] = count($ProductOrderItems); - + // 获取处方来源诊所信息 $prescriptionStore = null; if ($ProductOrder['p_id']) { @@ -452,7 +500,7 @@ class ProductOrderController extends BaseAppController $prescriptionStore = Store::find()->select('id,name,mobile,position')->where(['id' => $prescription['store_id']])->asArray()->one(); } } - + return [ 'ProductOrder' => $ProductOrder, 'userInfo' => $userInfo, @@ -461,7 +509,51 @@ class ProductOrderController extends BaseAppController 'store' => $prescriptionStore ? ['store' => $prescriptionStore] : null, 'patient' => $this->getOrderPatientInfo((int) ($ProductOrder['up_id'] ?? 0), (int) ($ProductOrder['p_id'] ?? 0)), ]; - + + } + + /** + * 为订单明细附加配送仓名称 + * delivery_warehouse_id=0 → 萧康医药本仓库;>0 → xk_delivery_warehouse.name + * @param array $items + * @return array{items: array, has_delivery_warehouse: bool} + */ + private function enrichOrderItemsWarehouse(array $items): array + { + $warehouseIds = []; + $hasDeliveryWarehouse = false; + foreach ($items as $item) { + $wid = (int) ($item['delivery_warehouse_id'] ?? 0); + if ($wid > 0) { + $hasDeliveryWarehouse = true; + $warehouseIds[$wid] = true; + } + } + $nameMap = []; + if (!empty($warehouseIds)) { + $rows = DeliveryWarehouseModel::find() + ->select(['id', 'name']) + ->where(['id' => array_keys($warehouseIds), 'deleted_at' => 0]) + ->asArray() + ->all(); + foreach ($rows as $row) { + $nameMap[(int) $row['id']] = (string) $row['name']; + } + } + foreach ($items as &$item) { + $wid = (int) ($item['delivery_warehouse_id'] ?? 0); + $item['delivery_warehouse_id'] = $wid; + if ($wid > 0) { + $item['delivery_warehouse_name'] = $nameMap[$wid] ?? ('仓库' . $wid); + } else { + $item['delivery_warehouse_name'] = '萧康医药本仓库'; + } + } + unset($item); + return [ + 'items' => $items, + 'has_delivery_warehouse' => $hasDeliveryWarehouse, + ]; }