已开处方列表提速;处方 AI 拒答带全部候选键时按拒答处理

- 置顶风险处方改为从订单表出发计算并缓存 60 秒,分页只查 ID,不再整表扫描与排序
- 列表行不再返回 doctor_signature,查看/编辑仍另取详情
- 模型拒绝开方时把候选键置空返回,不再判为 INVALID_REPORT_OUTPUT,可进入追问

Co-Authored-By: Claude Opus 5 <[EMAIL]>
This commit is contained in:
Your Name
2026-12-04 15:01:39 +08:00
co-authored by Claude Opus 5
parent 63e52f6273
commit 14936cb28a
3 changed files with 118 additions and 42 deletions
@@ -13,6 +13,7 @@ use app\common\model\tcm\Prescription;
use app\common\model\tcm\PrescriptionOrder;
use app\common\service\esign\PrescriptionEsignService;
use think\db\Query;
use think\facade\Cache;
/**
* 处方列表
@@ -21,51 +22,92 @@ class PrescriptionLists extends BaseAdminDataLists implements ListsSearchInterfa
{
use HasDataScopeFilter;
/** 置顶风险处方 ID 的缓存键与有效期(秒);只影响置顶顺序,行内警示仍按当前数据实时计算 */
private const RISK_IDS_CACHE_KEY = 'tcm_prescription_lists_risk_ids';
private const RISK_IDS_CACHE_TTL = 60;
/** 列表行不返回的大字段:签名图 base64 动辄数十 KB,查看/编辑都会另取详情 */
private const LIST_OMIT_FIELDS = ['doctor_signature'];
/**
* 识别「有业务订单且处方药材为空白/重复」的处方 ID(用于全局置顶排序)
*
* @param array<int,int|string> $candidateIds
* 风险是处方自身的属性,与筛选条件无关:这里在全库范围计算一次并短时缓存,
* 筛选范围的交集由 pinnedIds() 在当前查询里取。以前每次请求都先把筛选结果的
* 全部 ID 拉出来,再逐个读药材,处方表行很宽(病历快照、签名图),整表扫描
* 是列表慢的主因。
*
* @return array<int,int>
*/
private function collectRiskPrescriptionIds(array $candidateIds): array
private function collectRiskPrescriptionIds(): array
{
$candidateIds = array_values(array_unique(array_filter(array_map('intval', $candidateIds), static function (int $id): bool {
return $id > 0;
})));
if ($candidateIds === []) {
return [];
$cached = Cache::get(self::RISK_IDS_CACHE_KEY);
if (is_array($cached)) {
return $cached;
}
// 仅在「存在有效业务订单」的处方里做风险判定
$orderRxIds = PrescriptionOrder::whereIn('prescription_id', $candidateIds)
->whereNull('delete_time')
// 仅在「存在有效业务订单」的处方里做风险判定;订单表很小,从订单侧出发
$orderRxIds = PrescriptionOrder::whereNull('delete_time')
->whereNotIn('fulfillment_status', PrescriptionOrder::RELEASED_PRESCRIPTION_STATUSES)
->column('prescription_id');
$orderRxIds = array_values(array_unique(array_filter(array_map('intval', $orderRxIds), static function (int $id): bool {
return $id > 0;
})));
if ($orderRxIds === []) {
$riskIds = [];
foreach (array_chunk($orderRxIds, 1000) as $chunk) {
$herbsById = Prescription::whereIn('id', $chunk)
->whereNull('delete_time')
->column('herbs', 'id');
foreach ($herbsById as $rid => $herbs) {
$rid = (int) $rid;
if ($rid > 0 && $this->isRiskHerbs($herbs)) {
$riskIds[] = $rid;
}
}
}
$riskIds = array_values(array_unique($riskIds));
Cache::set(self::RISK_IDS_CACHE_KEY, $riskIds, self::RISK_IDS_CACHE_TTL);
return $riskIds;
}
/**
* 当前页的处方 ID:先排当前筛选范围内的风险处方,再按 ID 倒序排其余处方。
*
* 拆成两段只查 ID,避免对整表做 ORDER BY CASE 的文件排序;第二段可直接沿主键倒序扫描,
* 取够一页即停止。
*
* @return array<int,int>
*/
private function pageIds(Query $query): array
{
$offset = max(0, (int) $this->limitOffset);
$length = max(0, (int) $this->limitLength);
if ($length === 0) {
return [];
}
$rows = Prescription::whereIn('id', $orderRxIds)
->whereNull('delete_time')
->field(['id', 'herbs'])
->select()
->toArray();
$riskIds = [];
foreach ($rows as $row) {
$rid = (int) ($row['id'] ?? 0);
if ($rid <= 0) {
continue;
}
if ($this->isRiskHerbs($row['herbs'] ?? null)) {
$riskIds[] = $rid;
}
$riskIds = $this->collectRiskPrescriptionIds();
$pinned = [];
if ($riskIds !== []) {
$pinned = array_map('intval', (clone $query)->whereIn('id', $riskIds)->order('id', 'desc')->column('id'));
}
return array_values(array_unique($riskIds));
$ids = array_slice($pinned, $offset, $length);
$remaining = $length - count($ids);
if ($remaining > 0) {
$rest = clone $query;
if ($pinned !== []) {
$rest->whereNotIn('id', $pinned);
}
$restIds = $rest->order('id', 'desc')
->limit(max(0, $offset - count($pinned)), $remaining)
->column('id');
$ids = array_merge($ids, array_map('intval', $restIds));
}
return $ids;
}
/**
@@ -253,22 +295,25 @@ class PrescriptionLists extends BaseAdminDataLists implements ListsSearchInterfa
*/
public function lists(): array
{
$query = $this->buildFilteredQuery();
// 全局置顶:有业务订单且处方药材为空白/重复
$candidateIds = (clone $query)->column('id');
$riskIds = $this->collectRiskPrescriptionIds(is_array($candidateIds) ? $candidateIds : []);
$listQuery = $query;
if ($riskIds !== []) {
$riskIdStr = implode(',', array_map('intval', $riskIds));
$listQuery->orderRaw("CASE WHEN id IN ({$riskIdStr}) THEN 0 ELSE 1 END ASC");
$pageIds = $this->pageIds($this->buildFilteredQuery());
if ($pageIds === []) {
return [];
}
$lists = $listQuery
->order('id', 'desc')
->limit($this->limitOffset, $this->limitLength)
$rowsById = [];
$rows = Prescription::whereIn('id', $pageIds)
->withoutField(self::LIST_OMIT_FIELDS)
->select()
->toArray();
foreach ($rows as $row) {
$rowsById[(int) $row['id']] = $row;
}
$lists = [];
foreach ($pageIds as $pageId) {
if (isset($rowsById[$pageId])) {
$lists[] = $rowsById[$pageId];
}
}
$rxIds = array_column($lists, 'id');
$rxIds = array_map('intval', $rxIds);