['prescription_id', 'fulfillment_status'], '%like%' => ['order_no'], ]; } public function lists(): array { $query = PrescriptionOrder::where($this->searchWhere)->whereNull('delete_time'); if (!PrescriptionOrderLogic::canSeeAllPrescriptionOrders($this->adminInfo)) { $diagIds = Diagnosis::where('assistant_id', $this->adminId)->whereNull('delete_time')->column('id'); $query->where(function ($q) use ($diagIds) { $q->where('creator_id', $this->adminId); if ($diagIds !== []) { $q->whereOr('diagnosis_id', 'in', $diagIds); } }); } $lists = $query ->order('id', 'desc') ->limit($this->limitOffset, $this->limitLength) ->select() ->toArray(); $poIds = array_values(array_filter(array_map('intval', array_column($lists, 'id')))); $paidSumByPo = []; if ($poIds !== []) { $links = PrescriptionOrderPayOrder::whereIn('prescription_order_id', $poIds) ->field(['prescription_order_id', 'pay_order_id']) ->select() ->toArray(); $byPo = []; foreach ($links as $l) { $poid = (int) ($l['prescription_order_id'] ?? 0); $oid = (int) ($l['pay_order_id'] ?? 0); if ($poid > 0 && $oid > 0) { $byPo[$poid][] = $oid; } } $allOids = []; foreach ($byPo as $oids) { $allOids = array_merge($allOids, $oids); } $allOids = array_values(array_unique($allOids)); $amountByOid = []; if ($allOids !== []) { $amountByOid = Order::whereIn('id', $allOids)->whereNull('delete_time')->column('amount', 'id'); } foreach ($poIds as $pid) { $s = 0.0; foreach ($byPo[$pid] ?? [] as $oid) { $s += (float) ($amountByOid[$oid] ?? 0); } $paidSumByPo[$pid] = round($s, 2); } } // 获取创建人姓名 $creatorIds = array_values(array_unique(array_filter(array_map('intval', array_column($lists, 'creator_id'))))); $creatorNames = []; if ($creatorIds !== []) { $creatorNames = \app\common\model\auth\Admin::whereIn('id', $creatorIds)->column('name', 'id'); } // 获取处方ID对应的开方人姓名 $prescriptionIds = array_values(array_unique(array_filter(array_map('intval', array_column($lists, 'prescription_id'))))); $prescriptionCreators = []; if ($prescriptionIds !== []) { $prescriptions = \app\common\model\tcm\Prescription::whereIn('id', $prescriptionIds) ->whereNull('delete_time') ->field(['id', 'creator_id', 'doctor_name']) ->select() ->toArray(); $doctorIds = []; foreach ($prescriptions as $rx) { $rxId = (int) ($rx['id'] ?? 0); $doctorId = (int) ($rx['creator_id'] ?? 0); $doctorName = (string) ($rx['doctor_name'] ?? ''); if ($rxId > 0) { $prescriptionCreators[$rxId] = [ 'doctor_id' => $doctorId, 'doctor_name' => $doctorName ]; if ($doctorId > 0) { $doctorIds[] = $doctorId; } } } // 如果 doctor_name 为空,从 Admin 表获取 $doctorIds = array_values(array_unique($doctorIds)); if ($doctorIds !== []) { $doctorNamesFromAdmin = \app\common\model\auth\Admin::whereIn('id', $doctorIds)->column('name', 'id'); foreach ($prescriptionCreators as &$pc) { if (empty($pc['doctor_name']) && $pc['doctor_id'] > 0) { $pc['doctor_name'] = $doctorNamesFromAdmin[$pc['doctor_id']] ?? ''; } } unset($pc); } } $depMin = PrescriptionOrderLogic::depositMinAmount(); $diagIdsForAssist = array_values(array_unique(array_filter(array_map('intval', array_column($lists, 'diagnosis_id'))))); $assistantByDiag = []; if ($diagIdsForAssist !== []) { $assistantByDiag = Diagnosis::whereIn('id', $diagIdsForAssist)->whereNull('delete_time')->column('assistant_id', 'id'); } foreach ($lists as &$item) { PrescriptionOrderLogic::maskInternalCostIfNeeded($item, $this->adminInfo); $pid = (int) ($item['id'] ?? 0); $item['linked_pay_order_count'] = (int) PrescriptionOrderPayOrder::where( 'prescription_order_id', $pid )->count(); $item['linked_pay_paid_total'] = $paidSumByPo[$pid] ?? 0.0; $item['deposit_min_amount'] = $depMin; $item['can_upload_gancao_reciperl'] = PrescriptionOrderLogic::canUploadGancaoRecipel( $item, $this->adminId, $this->adminInfo, $assistantByDiag ); // 添加创建人姓名 $creatorId = (int) ($item['creator_id'] ?? 0); $item['creator_name'] = $creatorId > 0 ? ($creatorNames[$creatorId] ?? '') : ''; // 添加开方人姓名 $rxId = (int) ($item['prescription_id'] ?? 0); $item['doctor_name'] = $rxId > 0 ? ($prescriptionCreators[$rxId]['doctor_name'] ?? '') : ''; } unset($item); return $lists; } public function extend(): array { return [ 'deposit_min_amount' => PrescriptionOrderLogic::depositMinAmount(), 'gancao_scm_enabled' => GancaoScmRecipelService::isConfigured(), ]; } public function count(): int { $query = PrescriptionOrder::where($this->searchWhere)->whereNull('delete_time'); if (!PrescriptionOrderLogic::canSeeAllPrescriptionOrders($this->adminInfo)) { $diagIds = Diagnosis::where('assistant_id', $this->adminId)->whereNull('delete_time')->column('id'); $query->where(function ($q) use ($diagIds) { $q->where('creator_id', $this->adminId); if ($diagIds !== []) { $q->whereOr('diagnosis_id', 'in', $diagIds); } }); } return (int) $query->count(); } }