@ -5,21 +5,21 @@
include_once(SERVER_ROOT . "/model/mBase.php");
class mCase extends mBase {
class mFormula extends mBase {
private $obj;
private $tbl;
private $case _herb_tbl;
private $formula _herb_tbl;
private $herb_tbl;
private $collect_log_tbl;
private $case _use_log_tbl;
private $formula _use_log_tbl;
public function __construct() {
$this->obj = new dCase ();
$this->tbl = 'tcm_case ';
$this->case_herb_tbl = 'tcm_case _herb';
$this->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->case_use_log_tbl = 'tcm_case _use_log';
$this->formula_use_log_tbl = 'tcm_formula _use_log';
}
public function createCase($name, $source, $method, $herbs) {
@ -83,12 +83,12 @@ class mCase extends mBase {
return true;
}
public function formatCaseHerb($case _id, $herbs) {
public function formatCaseHerb($formula _id, $herbs) {
$case_herb = array();
foreach ($herbs as $key => $item) {
$temp = array();
$temp['name'] = $item['name'];
$temp['case_id'] = $case _id;
$temp['formula_id'] = $formula _id;
$temp['num'] = $this->convertToNum('汉', $item['num'], $item['name']);
$temp['sort'] = $key;
@ -151,7 +151,7 @@ class mCase extends mBase {
}
public function createCaseHerb($info) {
$res = $this->obj->mutiInsert($this->case _herb_tbl, $info);
$res = $this->obj->mutiInsert($this->formula _herb_tbl, $info);
if (!$res) {
$this->setError('添加药方药材失败');
return false;
@ -161,7 +161,7 @@ class mCase extends mBase {
}
public function compareCaseHerb($id, $new_data) {
$old_data = $this->getCaseHerbByCaseId ($id);
$old_data = $this->getFormulaHerb ($id);
$old_num = count($old_data);
$new_num = count($new_data);
@ -170,13 +170,13 @@ class mCase extends mBase {
//需要删除药方对应的药材
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'])));
$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->case _herb_tbl, $new_data[$key], array('sql' => '`id`=?', 'vals' => array($value['id'])));
$res = $this->obj->update($this->formula _herb_tbl, $new_data[$key], array('sql' => '`id`=?', 'vals' => array($value['id'])));
if (!$res) {
$this->setError('更新药方药材失败');
return false;
@ -186,13 +186,13 @@ class mCase extends mBase {
//需要增加药方对应的药材
foreach ($new_data as $key => $value) {
if (!isset($old_data[$key])) {
$res = $this->obj->insert($this->case _herb_tbl, $value);
$res = $this->obj->insert($this->formula _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'])));
$res = $this->obj->update($this->formula _herb_tbl, $value, array('sql' => '`id`=?', 'vals' => array($old_data[$key]['id'])));
if (!$res) {
$this->setError('更新药方药材失败');
return false;
@ -203,165 +203,143 @@ class mCase extends mBase {
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 getFormulaHerb($formula _id) {
return $this->obj->selectAll($this->formula_herb_tbl, array('sql' => '`formula_id`=?', 'vals' => array($formula _id)), 'sort asc ');
}
public function getCaseHerbByCaseIds($case_ids) {
return $this->obj->selectIn($this->case_herb_tbl, array('case_id' => $case_ids));
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 getCaseByName($uid, $name, $page_num, $page_siz e) {
public function getFormulaByName($uid, $page_num, $page_size, $nam e) {
$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}";
$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 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;
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']];
}
$case_ids = array_column($case_data, 'id');
//药方药材信息
$case_herbs = $this->getCaseHerbByCaseIds($case_ids);
if (empty($case_herbs)) {
$this->setError('药方药材查询失败');
return false;
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'];
}
}
}
$herb_ids = array_column($case_herbs, 'herb_id');
//药材名称
$herb = $this->getHerbByIds($herb_ids);
if (empty($herb)) {
$this->setError('药材名称查询失败');
return false;
unset($da['original']);
}
$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;
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' => $value['herb_id'],
'name' => $herb_list[$value['herb_id']]['name'],
'num' => $value['num'],
'org_num' => '',
'desc' => $value['desc'],
'org_desc' => '',
'id' => $formula_herb['herb_id'],
'name' => '',
'num' => $formula_herb['num'],
'desc' => $formula_herb['desc'],
);
foreach ($original as $v) {
if ($temp['name'] == $v['name']) {
$temp['org_num'] = $v['num'];
$temp['org_desc'] = $v['desc'];
}
}
$case['case_herb'][] = $temp;
if (isset($herb_arr[$formula_herb['herb_id']])) {
$temp['name'] = $herb_arr[$formula_herb['herb_id']]['name'];
}
$case_list[] = $case ;
$formula_arr[$formula_herb['formula_id']][] = $temp;
}
return $case_list;
return $formula_arr;
}
public function getCaseByNameTotal($name) {
$where = array();
if (!empty($name)) {
$sql = " `name` like '%{$name}%'";
$where = array('sql' => $sql, 'vals' => array());
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 $this->obj->count($this->tbl, $where);
return $num ;
}
public function getCaseInfo($id ) {
public function getFormulaInfo($id, $uid = 0 ) {
//药方信息
$case = $this->getCase ById($id);
if (empty($case )) {
$formula = $this->getFormula ById($id);
if (empty($formula )) {
$this->setError('查询不到此药方');
return false;
}
//药方药材信息
$case_herb_date = $this->getCaseHerbByCaseId($case['id']);
if (empty($case_herb_date)) {
$this->setError('药方药材查询失败');
if ($uid > 0 & & $formula['uid'] != $uid) {
$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');
$formula['formula'] = $this->getFormulaHerbData($formula['id'])[$formula['id']];
$original = json_decode($case['original'], true);
unset($case['original']);
if (!empty($formula['original'])) {
$original = json_decode($formula['original'], true);
$original = array_column($original, null, 'name');
$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'];
foreach ($formula['formula'] as & $v) {
if (isset($original[$v['name']])) {
$v['num'] = (float)$original[$v['name']]['num'];
$v['desc'] = $original[$v['name']]['desc'];
}
}
$case_herb[] = $temp;
}
$case['case_herb'] = $case_herb ;
unset($formula['original']);
return $case ;
return $formula;
}
public function getCase ById($id) {
public function getFormulaById($id) {
return $this->obj->select($this->tbl, array('sql' => '`id`=?', 'vals' => array($id)));
}
public function getHerbById s($ids) {
public function getHerbs($ids) {
return $this->obj->selectIn($this->herb_tbl, array('id' => $ids));
}
public function getCase ByIds($ids) {
public function getFormula ByIds($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');
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 . "|" . $case_id, sprintf(LOG_TRACK_SAVE_PATH, date('Y-m-d'), 'tcm_case_use_num '));
$tool_obj->trackLog('tcm', $uid . "|" . $formula_id, sprintf(LOG_TRACK_SAVE_PATH, date('Y-m-d'), 'tcm_formula_use_log '));
$this->setError('药方使用次数更新失败');
return false;