From 07e73e0064815f5767bf6288fccc83758b827bca Mon Sep 17 00:00:00 2001 From: pengda <10266652509@qq.com> Date: Fri, 13 Sep 2024 17:43:48 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9F=A5=E8=8D=AF=E6=96=B9=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=20=E5=88=97=E8=A1=A8=E5=A2=9E=E5=8A=A0=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E8=87=AA=E6=8B=9F=E8=8D=AF=E6=96=B9=20=E6=9F=A5=E8=8D=AF?= =?UTF-8?q?=E6=96=B9=E8=AF=A6=E6=83=85=20=E5=85=BC=E5=AE=B9=E6=9F=A5?= =?UTF-8?q?=E8=AF=A2=E7=94=A8=E6=88=B7=E8=87=AA=E6=8B=9F=E8=8D=AF=E6=96=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- control/index.php | 57 ++++---- data/dCase.php | 54 -------- data/dFormula.php | 55 ++++++++ model/mCase.php | 382 ----------------------------------------------------- model/mFormula.php | 360 ++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 444 insertions(+), 464 deletions(-) delete mode 100644 data/dCase.php create mode 100644 data/dFormula.php delete mode 100644 model/mCase.php create mode 100644 model/mFormula.php diff --git a/control/index.php b/control/index.php index 719c5c8..9ad1463 100644 --- a/control/index.php +++ b/control/index.php @@ -27,27 +27,27 @@ class index extends publicBase { public function home() { $id = $this->get('id'); - $m_case = new mCase(); - $data = $m_case->getCaseInfo($id); + $mformula = new mFormulaNew(); + $data = $mformula->getFormulaInfo($id); $this->view['data'] = $data; } - public function ajax_save_case() { + public function ajax_save_formula() { $name = trim($this->post('name')); $source = trim($this->post('source')); $method = trim($this->post('method')); $herbs = $this->post('herbs'); //新增药方 - $m_case = new mCase(); - $id = $m_case->createCase($name, $source, $method, $herbs); - if (!$id) $this->ajax_json(false, $m_case->getError()); + $mformula = new mFormulaNew(); + $id = $mformula->createCase($name, $source, $method, $herbs); + if (!$id) $this->ajax_json(false, $mformula->getError()); $this->ajax_json(true, '添加成功'); } - public function ajax_update_case() { + public function ajax_update_formula() { $id = $this->post('id') + 0; $name = trim($this->post('name')); $source = trim($this->post('source')); @@ -55,9 +55,9 @@ class index extends publicBase { $method = trim($this->post('method')); $herbs = $this->post('herbs'); - $m_case = new mCase(); - $res = $m_case->updateCase($id, $name, $source, $original, $method, $herbs); - if (!$res) $this->ajax_json(false, $m_case->getError()); + $mformula = new mFormulaNew(); + $res = $mformula->updateCase($id, $name, $source, $method, $herbs); + if (!$res) $this->ajax_json(false, $mformula->getError()); $this->ajax_json(true, '保存成功'); } @@ -71,27 +71,30 @@ class index extends publicBase { $page_num = $this->post('page_num') ? $this->post('page_num') : 1; $page_size = $this->post('page_size') ? $this->post('page_size') : 20; - $m_case = new mCase(); - $data = $m_case->getCaseList($uinfo['uid'], $content, $page_num, $page_size); - $total = $m_case->getCaseByNameTotal($content); + $mformula = new mFormulaNew(); + $list = $mformula->getFormulaList($uinfo['uid'], $page_num, $page_size, $content); + $total = $mformula->getFormulaTotal($uinfo['uid'], $content); + $rdata = array( 'total' => $total, 'per_page' => $page_size, 'last_page' => ceil($total / $page_size), 'current_page' => $page_num, - 'data' => $data + 'list' => $list, ); $this->ajax_json(true, '获取成功', $rdata); } - public function ajax_case_detail() { - $id = $this->get('id') + 0; - if (empty($id)) $this->ajax_json(false, '参数错误'); + public function ajax_formula_detail() { + $uinfo = $this->_check_login(); - $m_case = new mCase(); - $data = $m_case->getCaseInfo($id); - if (!$data) $this->ajax_json(false, $m_case->getError()); + $id = $this->post('id') + 0; + if (!$id) $this->ajax_json(false, '参数错误'); + + $mformula = new mFormulaNew(); + $data = $mformula->getFormulaInfo($id); + if (!$data) $this->ajax_json(false, $mformula->getError()); $this->ajax_json(true, '获取成功', $data); } @@ -99,11 +102,9 @@ class index extends publicBase { public function ajax_save_user_case() { $uinfo = $this->_check_login(); - $case_id = $this->post('case_id') + 0; - if (empty($case_id)) $this->ajax_json(false, '参数错误'); + $formula_id = $this->post('formula_id') + 0; $data = array( - 'name' => trim($this->post('name')), 'patient_name' => trim($this->post('patient_name')), 'patient_age' => $this->post('patient_age') + 0, 'patient_sex' => $this->post('patient_sex') + 0, @@ -117,11 +118,11 @@ class index extends publicBase { $m_user_case = new mUserCase(); //生成唯一id 防止重复请求 - $request_id = md5($uinfo['uid'] . $case_id . $data['name'] . $data['patient_name']); + $request_id = md5($uinfo['uid'] . $formula_id . $data['name'] . $data['patient_name']); $request_times = $m_user_case->requestLimit(sprintf(_QR_REQUEST_LIMIT, $request_id), 1, 60); if (!$request_times) $this->ajax_json(false, $m_user_case->getError()); - $id = $m_user_case->createUserCase($uinfo['uid'], $case_id, $data); + $id = $m_user_case->createUserCase($uinfo['uid'], $formula_id, $data); if (!$id) $this->ajax_json(false, $m_user_case->getError()); $this->ajax_json(true, '保存成功', array('id' => $id)); @@ -185,9 +186,9 @@ class index extends publicBase { $page_num = $this->post('page_num') ? $this->post('page_num') : 1; $page_size = $this->post('page_size') ? $this->post('page_size') : 100; - $m_case = new mCase(); - $data = $m_case->getCollectLog($page_num, $page_size); - $total = $m_case->getCollectLogTotal(); + $mformula = new mFormulaNew(); + $data = $mformula->getCollectLog($page_num, $page_size); + $total = $mformula->getCollectLogTotal(); $rdata = array( 'total' => $total, diff --git a/data/dCase.php b/data/dCase.php deleted file mode 100644 index 46540dc..0000000 --- a/data/dCase.php +++ /dev/null @@ -1,54 +0,0 @@ - array( - 'id', - 'name', - 'source', - 'original', - 'method', - 'use_num', - 'sort', - ), - 'tcm_case_herb' => array( - 'id', - 'case_id', - 'herb_id', - 'num', - 'sort', - ), - 'tcm_herb' => array( - 'id', - 'name', - 'desc', - ), - 'tcm_collect_log' => array( - 'id', - 'collect_msg', - 'collect_time', - ), - 'tcm_case_use_log' => array( - 'id', - 'uid', - 'case_id', - 'use_num', - ), - ); - - protected $primary_keys = array( - 'tcm_case' => 'id', - 'tcm_case_herb' => 'id', - 'tcm_herb' => 'id', - 'tcm_collect_log' => 'id', - 'tcm_case_use_log' => 'id', - ); -} - diff --git a/data/dFormula.php b/data/dFormula.php new file mode 100644 index 0000000..6f5353c --- /dev/null +++ b/data/dFormula.php @@ -0,0 +1,55 @@ + array( + 'id', + 'uid', + 'name', + 'source', + 'original', + 'method', + 'sort', + ), + 'tcm_formula_herb' => array( + 'id', + 'formula_id', + 'herb_id', + 'num', + 'sort', + ), + 'tcm_herb' => array( + 'id', + 'uid', + 'name', + 'desc', + ), + 'tcm_collect_log' => array( + 'id', + 'collect_msg', + 'collect_time', + ), + 'tcm_formula_use_log' => array( + 'id', + 'uid', + 'formula_id', + 'use_num', + ), + ); + + protected $primary_keys = array( + 'tcm_formula' => 'id', + 'tcm_formula_herb' => 'id', + 'tcm_herb' => 'id', + 'tcm_collect_log' => 'id', + 'tcm_formula_use_log' => 'id', + ); +} + diff --git a/model/mCase.php b/model/mCase.php deleted file mode 100644 index 76a1864..0000000 --- a/model/mCase.php +++ /dev/null @@ -1,382 +0,0 @@ -obj = new dCase(); - $this->tbl = 'tcm_case'; - $this->case_herb_tbl = 'tcm_case_herb'; - $this->herb_tbl = 'tcm_herb'; - $this->collect_log_tbl = 'tcm_collect_log'; - $this->case_use_log_tbl = 'tcm_case_use_log'; - } - - public function createCase($name, $source, $method, $herbs) { - if (empty($name)) { - $this->setError('药方名称不能为空'); - return false; - } - if (empty($source)) { - $this->setError('药方来源不能为空'); - return false; - } - if (empty($method)) { - $this->setError('药方用法不能为空'); - return false; - } - if (empty($herbs)) { - $this->setError('药方药材不能为空'); - return false; - } - - //保存原方 - $original = json_encode($herbs, JSON_UNESCAPED_UNICODE); - - $id = $this->obj->insert($this->tbl, array('name' => $name, 'source' => $source, 'original' => $original, 'method' => $method)); - if (!$id) { - $this->setError('添加失败'); - return false; - } - - //格式化药方对应的药材数据 - $case_herb = $this->formatCaseHerb($id, $herbs); - //创建药方对应的药材 - $this->createCaseHerb($case_herb); - - return $id; - } - - public function updateCase($id, $name, $source, $original, $method, $herbs) { - if (empty($herbs)) { - $this->setError('药方药材不能为空'); - return false; - } - - $data = array(); - if (!empty($name)) $data['name'] = $name; - if (!empty($source)) $data['source'] = $source; - if (!empty($original)) $data['original'] = $original; - if (!empty($method)) $data['method'] = $method; - - $res = $this->obj->update($this->tbl, $data, array('sql' => '`id`=?', 'vals' => array($id))); - if (!$res) { - $this->setError('更新失败'); - return false; - } - - //格式化药方对应的药材数据 - $case_herb = $this->formatCaseHerb($id, $herbs); - //更新药方对应的药材 - $this->compareCaseHerb($id, $case_herb); - - return true; - } - - public function formatCaseHerb($case_id, $herbs) { - $case_herb = array(); - foreach ($herbs as $key => $item) { - $temp = array(); - $temp['name'] = $item['name']; - $temp['case_id'] = $case_id; - $temp['num'] = $this->convertToNum('汉', $item['num'], $item['name']); - $temp['sort'] = $key; - - $herb = $this->getHerbByName($item['name']); - if ($herb) { - $temp['herb_id'] = $herb['id']; - } else { - $temp['herb_id'] = $this->createHerb(array('name' => $item['name'], 'desc' => $item['desc'])); - } - $case_herb[] = $temp; - } - - return $case_herb; - } - - public function convertToNum($from, $str, $herb_name) { - $num_list = $GLOBALS['num_list']; - $num_list_arr = array_merge(array_keys($GLOBALS['num_list']['num']), array_keys($GLOBALS['num_list']['unit'])); - $weight_list = $GLOBALS['weight_list'][$from]; - $weight_list_arr = array_keys($GLOBALS['weight_list'][$from]); - - $unit = str_replace($num_list_arr, '', $str); - $weight = isset($weight_list[$unit]) ? $weight_list[$unit] : 0; - if (isset($weight[$herb_name])) $weight = $weight[$herb_name]; - - $num = 0; - $temp = 0; - - $num_str = str_replace($weight_list_arr, '', $str); - $length = mb_strlen($num_str); - for ($i = 0; $i < $length; $i++) { - $char = mb_substr($num_str, $i, 1); - - if (isset($num_list['num'][$char])) { - $temp = $num_list['num'][$char]; - } elseif (isset($num_list['unit'][$char])) { - $temp = ($temp == 0 ? 1 : $temp) * $num_list['unit'][$char]; - $num += $temp; // 将临时结果累加到最终结果中 - $temp = 0; // 重置临时结果 - } - } - - $num += $temp; - - return $num * $weight; - } - - public function getHerbByName($name) { - return $this->obj->select($this->herb_tbl, array('sql' => '`name`=?', 'vals' => array($name))); - } - - public function createHerb($info) { - $herb_id = $this->obj->insert($this->herb_tbl, $info); - if (empty($herb_id)) { - $this->setError('添加药材失败'); - return false; - } - - return $herb_id; - } - - public function createCaseHerb($info) { - $res = $this->obj->mutiInsert($this->case_herb_tbl, $info); - if (!$res) { - $this->setError('添加药方药材失败'); - return false; - } - - return true; - } - - public function compareCaseHerb($id, $new_data) { - $old_data = $this->getCaseHerbByCaseId($id); - - $old_num = count($old_data); - $new_num = count($new_data); - - if ($old_num >= $new_num) { - //需要删除药方对应的药材 - foreach ($old_data as $key => $value) { - if (!isset($new_data[$key])) { - $res = $this->obj->delete($this->case_herb_tbl, array('sql' => '`id`=?', 'vals' => array($value['id']))); - if (!$res) { - $this->setError('删除药方药材失败'); - return false; - } - } - $res = $this->obj->update($this->case_herb_tbl, $new_data[$key], array('sql' => '`id`=?', 'vals' => array($value['id']))); - if (!$res) { - $this->setError('更新药方药材失败'); - return false; - } - } - } else { - //需要增加药方对应的药材 - foreach ($new_data as $key => $value) { - if (!isset($old_data[$key])) { - $res = $this->obj->insert($this->case_herb_tbl, $value); - if (!$res) { - $this->setError('添加药方药材失败'); - return false; - } - } - $res = $this->obj->update($this->case_herb_tbl, $value, array('sql' => '`id`=?', 'vals' => array($old_data[$key]['id']))); - if (!$res) { - $this->setError('更新药方药材失败'); - return false; - } - } - } - - return true; - } - - public function getCaseHerbByCaseId($case_id) { - 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_data = $this->getCaseByName($uid, $name, $page_num, $page_size); - if (empty($case_data)) { - $this->setError('查询不到此药方'); - return false; - } - - $case_ids = array_column($case_data, '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; - } - $herb_list = array_column($herb, null, 'id'); - - $case_list = array(); - foreach ($case_data as $item) { - $case = array( - 'id' => $item['id'], - 'name' => $item['name'], - 'source' => $item['source'], - 'method' => $item['method'], - 'use_num' => $item['use_num'], - 'sort' => $item['sort'], - 'case_herb' => array() - ); - - $original = json_decode($item['original'], true); - foreach ($case_herbs as $value) { - if ($item['id'] != $value['case_id']) continue; - - - $temp = array( - 'id' => $value['herb_id'], - 'name' => $herb_list[$value['herb_id']]['name'], - 'num' => $value['num'], - 'org_num' => '', - 'desc' => $value['desc'], - 'org_desc' => '', - ); - - foreach ($original as $v) { - if ($temp['name'] == $v['name']) { - $temp['org_num'] = $v['num']; - $temp['org_desc'] = $v['desc']; - } - } - $case['case_herb'][] = $temp; - } - - $case_list[] = $case; - } - - return $case_list; - } - - public function getCaseByNameTotal($name) { - $where = array(); - if (!empty($name)) { - $sql = " `name` like '%{$name}%'"; - $where = array('sql' => $sql, 'vals' => array()); - } - - return $this->obj->count($this->tbl, $where); - } - - public function getCaseInfo($id) { - //药方信息 - $case = $this->getCaseById($id); - if (empty($case)) { - $this->setError('查询不到此药方'); - return false; - } - - //药方药材信息 - $case_herb_date = $this->getCaseHerbByCaseId($case['id']); - if (empty($case_herb_date)) { - $this->setError('药方药材查询失败'); - return false; - } - - //药材名称 - $herb_ids = array_column($case_herb_date, 'herb_id'); - $herb = $this->getHerbByIds($herb_ids); - if (empty($herb)) { - $this->setError('药材名称查询失败'); - return false; - } - $herb_list = array_column($herb, null, 'id'); - - $original = json_decode($case['original'], true); - unset($case['original']); - - $case_herb = array(); - foreach ($case_herb_date as $item) { - $temp = array( - 'id' => $item['herb_id'], - 'name' => $herb_list[$item['herb_id']]['name'], - 'num' => $item['num'], - 'org_num' => '', - 'desc' => $item['desc'], - 'org_desc' => '', - ); - - foreach ($original as $v) { - if ($temp['name'] == $v['name']) { - $temp['org_num'] = $v['num']; - $temp['org_desc'] = $v['desc']; - } - } - - $case_herb[] = $temp; - } - - $case['case_herb'] = $case_herb; - - return $case; - } - - public function getCaseById($id) { - return $this->obj->select($this->tbl, array('sql' => '`id`=?', 'vals' => array($id))); - } - - public function getHerbByIds($ids) { - return $this->obj->selectIn($this->herb_tbl, array('id' => $ids)); - } - - public function getCaseByIds($ids) { - return $this->obj->selectIn($this->tbl, array('id' => $ids)); - } - - public function updateCaseUseLog($uid, $case_id) { - $res = $this->obj->increase($this->case_use_log_tbl, array('sql' => '`uid`=? and `case_id`=?', 'vals' => array($uid, $case_id)), 'use_num'); - if (!$res) { - $tool_obj = new qTool(); - $tool_obj->trackLog('tcm', $uid . "|" . $case_id, sprintf(LOG_TRACK_SAVE_PATH, date('Y-m-d'), 'tcm_case_use_num')); - - $this->setError('药方使用次数更新失败'); - return false; - } - - return true; - } - - public function getCollectLog($page_num, $page_size) { - $offset = ($page_num - 1) * $page_size; - return $this->obj->selectAll($this->collect_log_tbl, array(), 'collect_time desc ', array($offset, $page_size)); - } - - public function getCollectLogTotal() { - return $this->obj->count($this->collect_log_tbl); - } - -} \ No newline at end of file diff --git a/model/mFormula.php b/model/mFormula.php new file mode 100644 index 0000000..f6a15ae --- /dev/null +++ b/model/mFormula.php @@ -0,0 +1,360 @@ +obj = new dFormula(); + $this->tbl = 'tcm_formula'; + $this->formula_herb_tbl = 'tcm_formula_herb'; + $this->herb_tbl = 'tcm_herb'; + $this->collect_log_tbl = 'tcm_collect_log'; + $this->formula_use_log_tbl = 'tcm_formula_use_log'; + } + + public function createCase($name, $source, $method, $herbs) { + if (empty($name)) { + $this->setError('药方名称不能为空'); + return false; + } + if (empty($source)) { + $this->setError('药方来源不能为空'); + return false; + } + if (empty($method)) { + $this->setError('药方用法不能为空'); + return false; + } + if (empty($herbs)) { + $this->setError('药方药材不能为空'); + return false; + } + + //保存原方 + $original = json_encode($herbs, JSON_UNESCAPED_UNICODE); + + $id = $this->obj->insert($this->tbl, array('name' => $name, 'source' => $source, 'original' => $original, 'method' => $method)); + if (!$id) { + $this->setError('添加失败'); + return false; + } + + //格式化药方对应的药材数据 + $case_herb = $this->formatCaseHerb($id, $herbs); + //创建药方对应的药材 + $this->createCaseHerb($case_herb); + + return $id; + } + + public function updateCase($id, $name, $source, $original, $method, $herbs) { + if (empty($herbs)) { + $this->setError('药方药材不能为空'); + return false; + } + + $data = array(); + if (!empty($name)) $data['name'] = $name; + if (!empty($source)) $data['source'] = $source; + if (!empty($original)) $data['original'] = $original; + if (!empty($method)) $data['method'] = $method; + + $res = $this->obj->update($this->tbl, $data, array('sql' => '`id`=?', 'vals' => array($id))); + if (!$res) { + $this->setError('更新失败'); + return false; + } + + //格式化药方对应的药材数据 + $case_herb = $this->formatCaseHerb($id, $herbs); + //更新药方对应的药材 + $this->compareCaseHerb($id, $case_herb); + + return true; + } + + public function formatCaseHerb($formula_id, $herbs) { + $case_herb = array(); + foreach ($herbs as $key => $item) { + $temp = array(); + $temp['name'] = $item['name']; + $temp['formula_id'] = $formula_id; + $temp['num'] = $this->convertToNum('汉', $item['num'], $item['name']); + $temp['sort'] = $key; + + $herb = $this->getHerbByName($item['name']); + if ($herb) { + $temp['herb_id'] = $herb['id']; + } else { + $temp['herb_id'] = $this->createHerb(array('name' => $item['name'], 'desc' => $item['desc'])); + } + $case_herb[] = $temp; + } + + return $case_herb; + } + + public function convertToNum($from, $str, $herb_name) { + $num_list = $GLOBALS['num_list']; + $num_list_arr = array_merge(array_keys($GLOBALS['num_list']['num']), array_keys($GLOBALS['num_list']['unit'])); + $weight_list = $GLOBALS['weight_list'][$from]; + $weight_list_arr = array_keys($GLOBALS['weight_list'][$from]); + + $unit = str_replace($num_list_arr, '', $str); + $weight = isset($weight_list[$unit]) ? $weight_list[$unit] : 0; + if (isset($weight[$herb_name])) $weight = $weight[$herb_name]; + + $num = 0; + $temp = 0; + + $num_str = str_replace($weight_list_arr, '', $str); + $length = mb_strlen($num_str); + for ($i = 0; $i < $length; $i++) { + $char = mb_substr($num_str, $i, 1); + + if (isset($num_list['num'][$char])) { + $temp = $num_list['num'][$char]; + } elseif (isset($num_list['unit'][$char])) { + $temp = ($temp == 0 ? 1 : $temp) * $num_list['unit'][$char]; + $num += $temp; // 将临时结果累加到最终结果中 + $temp = 0; // 重置临时结果 + } + } + + $num += $temp; + + return $num * $weight; + } + + public function getHerbByName($name) { + return $this->obj->select($this->herb_tbl, array('sql' => '`name`=?', 'vals' => array($name))); + } + + public function createHerb($info) { + $herb_id = $this->obj->insert($this->herb_tbl, $info); + if (empty($herb_id)) { + $this->setError('添加药材失败'); + return false; + } + + return $herb_id; + } + + public function createCaseHerb($info) { + $res = $this->obj->mutiInsert($this->formula_herb_tbl, $info); + if (!$res) { + $this->setError('添加药方药材失败'); + return false; + } + + return true; + } + + public function compareCaseHerb($id, $new_data) { + $old_data = $this->getFormulaHerb($id); + + $old_num = count($old_data); + $new_num = count($new_data); + + if ($old_num >= $new_num) { + //需要删除药方对应的药材 + foreach ($old_data as $key => $value) { + if (!isset($new_data[$key])) { + $res = $this->obj->delete($this->formula_herb_tbl, array('sql' => '`id`=?', 'vals' => array($value['id']))); + if (!$res) { + $this->setError('删除药方药材失败'); + return false; + } + } + $res = $this->obj->update($this->formula_herb_tbl, $new_data[$key], array('sql' => '`id`=?', 'vals' => array($value['id']))); + if (!$res) { + $this->setError('更新药方药材失败'); + return false; + } + } + } else { + //需要增加药方对应的药材 + foreach ($new_data as $key => $value) { + if (!isset($old_data[$key])) { + $res = $this->obj->insert($this->formula_herb_tbl, $value); + if (!$res) { + $this->setError('添加药方药材失败'); + return false; + } + } + $res = $this->obj->update($this->formula_herb_tbl, $value, array('sql' => '`id`=?', 'vals' => array($old_data[$key]['id']))); + if (!$res) { + $this->setError('更新药方药材失败'); + return false; + } + } + } + + return true; + } + + public function getFormulaHerb($formula_id) { + return $this->obj->selectAll($this->formula_herb_tbl, array('sql' => '`formula_id`=?', 'vals' => array($formula_id)), 'sort asc '); + } + + public function getFormulaHerbs($formula_ids) { + $data = $this->obj->selectIn($this->formula_herb_tbl, array('formula_id' => $formula_ids)); + if (empty($data)) return array(); + return array_column($data, null, 'id'); + } + + public function getFormulaByName($uid, $page_num, $page_size, $name) { + $offset = ($page_num - 1) * $page_size; + + $where = "s.uid in (0,{$uid})"; + if ($name) { + $where .= " and s.`name` like '%{$name}%'"; + } + + $sql = "select s.id,s.uid,s.name,s.source,s.original,s.method,u.use_num,s.sort from {$this->tbl} as s left join {$this->formula_use_log_tbl} as u on s.id=u.formula_id where {$where} order by u.use_num desc,s.sort desc limit {$offset},{$page_size}"; + return $this->obj->execute($sql, true, true); + } + + public function getFormulaList($uid, $page_num, $page_size, $name) { + $data = $this->getFormulaByName($uid, $page_num, $page_size, $name); + + $formula_ids = array_column($data, 'id'); + + $formula_arr = $this->getFormulaHerbData($formula_ids); + + foreach ($data as &$da) { + if (isset($formula_arr[$da['id']])) { + $da['formula'] = $formula_arr[$da['id']]; + } + + if (!empty($da['original'])) { + $original = json_decode($da['original'], true); + $original = array_column($original, null, 'name'); + + foreach ($da['formula'] as &$v) { + if (isset($original[$v['name']])) { + $v['num'] = $original[$v['name']]['num']; + $v['desc'] = $original[$v['name']]['desc']; + } + } + } + + unset($da['original']); + } + + return $data; + } + + public function getFormulaHerbData($formula_ids) { + $formula_herbs = $this->getFormulaHerbs($formula_ids); + + $herb_ids = array_column($formula_herbs, 'herb_id'); + $herbs = $this->getHerbs($herb_ids); + $herb_arr = array_column($herbs, null, 'id'); + + $formula_arr = array(); + foreach ($formula_herbs as $formula_herb) { + $temp = array( + 'id' => $formula_herb['herb_id'], + 'name' => '', + 'num' => $formula_herb['num'], + 'desc' => $formula_herb['desc'], + ); + + if (isset($herb_arr[$formula_herb['herb_id']])) { + $temp['name'] = $herb_arr[$formula_herb['herb_id']]['name']; + } + + $formula_arr[$formula_herb['formula_id']][] = $temp; + } + return $formula_arr; + } + + public function getFormulaTotal($uid, $name) { + $sql = "select count(*) as count from $this->tbl where uid in (0,{$uid})"; + if ($name) { + $sql .= " and `name` like '%{$name}%'"; + } + $res = $this->obj->execute($sql, true, true); + $num = $res[0]['count']; + + return $num; + } + + public function getFormulaInfo($id, $uid = 0) { + //药方信息 + $formula = $this->getFormulaById($id); + if (empty($formula)) { + $this->setError('查询不到此药方'); + return false; + } + + if ($uid > 0 && $formula['uid'] != $uid) { + $this->setError('这不是你的药方'); + return false; + } + + $formula['formula'] = $this->getFormulaHerbData($formula['id'])[$formula['id']]; + + if (!empty($formula['original'])) { + $original = json_decode($formula['original'], true); + $original = array_column($original, null, 'name'); + + foreach ($formula['formula'] as &$v) { + if (isset($original[$v['name']])) { + $v['num'] = (float)$original[$v['name']]['num']; + $v['desc'] = $original[$v['name']]['desc']; + } + } + } + + unset($formula['original']); + + return $formula; + } + + public function getFormulaById($id) { + return $this->obj->select($this->tbl, array('sql' => '`id`=?', 'vals' => array($id))); + } + + public function getHerbs($ids) { + return $this->obj->selectIn($this->herb_tbl, array('id' => $ids)); + } + + public function getFormulaByIds($ids) { + return $this->obj->selectIn($this->tbl, array('id' => $ids)); + } + + public function updateCaseUseLog($uid, $formula_id) { + $res = $this->obj->increase($this->formula_use_log_tbl, array('sql' => '`uid`=? and `formula_id`=?', 'vals' => array($uid, $formula_id)), 'use_num'); + if (!$res) { + $tool_obj = new qTool(); + $tool_obj->trackLog('tcm', $uid . "|" . $formula_id, sprintf(LOG_TRACK_SAVE_PATH, date('Y-m-d'), 'tcm_formula_use_log')); + + $this->setError('药方使用次数更新失败'); + return false; + } + + return true; + } + + public function getCollectLog($page_num, $page_size) { + $offset = ($page_num - 1) * $page_size; + return $this->obj->selectAll($this->collect_log_tbl, array(), 'collect_time desc ', array($offset, $page_size)); + } + + public function getCollectLogTotal() { + return $this->obj->count($this->collect_log_tbl); + } + +} \ No newline at end of file