diff --git a/control/index.php b/control/index.php index 13ba6ba..719c5c8 100644 --- a/control/index.php +++ b/control/index.php @@ -72,7 +72,7 @@ class index extends publicBase { $page_size = $this->post('page_size') ? $this->post('page_size') : 20; $m_case = new mCase(); - $data = $m_case->getCaseByName($uinfo['uid'], $content, $page_num, $page_size); + $data = $m_case->getCaseList($uinfo['uid'], $content, $page_num, $page_size); $total = $m_case->getCaseByNameTotal($content); $rdata = array( 'total' => $total, diff --git a/model/mCase.php b/model/mCase.php index 36fc3a4..32d247b 100644 --- a/model/mCase.php +++ b/model/mCase.php @@ -207,12 +207,52 @@ class mCase extends mBase { return $this->obj->selectAll($this->case_herb_tbl, array('sql' => '`case_id`=?', 'vals' => array($case_id)), 'sort asc '); } + public function getCaseHerbByCaseIds($case_ids) { + return $this->obj->selectIn($this->case_herb_tbl, array('case_id' => $case_ids)); + } + public function getCaseByName($uid, $name, $page_num, $page_size) { $offset = ($page_num - 1) * $page_size; $sql = "select c.id,c.name,c.source,c.original,c.method,u.use_num,c.sort from {$this->tbl} as c left join {$this->case_use_log_tbl} as u on c.id=u.case_id and u.uid={$uid} where c.`name` like '%{$name}%' order by u.use_num desc,c.sort desc limit {$offset},{$page_size}"; return $this->obj->execute($sql, true, true); } + public function getCaseList($uid, $name, $page_num, $page_size) { + $case = $this->getCaseByName($uid, $name, $page_num, $page_size); + if (empty($case)) { + $this->setError('查询不到此药方'); + return false; + } + + $case_ids = array(); + foreach ($case as &$item){ + $item['original'] = json_decode($item['original'], true); + $case_ids[] = $item['id']; + } + + //药方药材信息 + $case_herbs = $this->getCaseHerbByCaseIds($case_ids); + if (empty($case_herbs)) { + $this->setError('药方药材查询失败'); + return false; + } + + //药材名称 + $herb_ids = array_column($case_herbs, 'herb_id'); + $herb = $this->getHerbByIds($herb_ids); + if (empty($herb)) { + $this->setError('药材名称查询失败'); + return false; + } + + $case_herb = array(); + foreach ($case_herbs as $item){ + $case_herb[$item['case_id']][] = $item; + } + + return array('case' => $case, 'case_herb' => $case_herb, 'herb' => array_column($herb, null, 'id')); + } + public function getCaseByNameTotal($name) { $where = array(); if (!empty($name)) { @@ -246,7 +286,7 @@ class mCase extends mBase { return false; } - return array('case' => $case, 'case_herb' => $case_herb, 'herb' => array_column($herb, null, 'id'),); + return array('case' => $case, 'case_herb' => $case_herb, 'herb' => array_column($herb, null, 'id')); } public function getCaseById($id) {