录医案
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

422 lines
14 KiB

9 months ago
<?php
/**
*
*/
include_once(SERVER_ROOT . "/model/mBase.php");
9 months ago
class mFormula extends mBase {
9 months ago
private $obj;
private $tbl;
private $formula_herb_tbl;
9 months ago
private $herb_tbl;
private $collect_log_tbl;
private $formula_use_log_tbl;
9 months ago
public function __construct() {
$this->obj = new dFormula();
$this->tbl = 'tcm_formula';
$this->formula_herb_tbl = 'tcm_formula_herb';
9 months ago
$this->herb_tbl = 'tcm_herb';
$this->collect_log_tbl = 'tcm_collect_log';
$this->formula_use_log_tbl = 'tcm_formula_use_log';
9 months ago
}
public function createCase($name, $source, $method, $herbs) {
9 months ago
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));
9 months ago
if (!$id) {
$this->setError('添加失败');
return false;
}
9 months ago
//格式化药方对应的药材数据
$case_herb = $this->formatCaseHerb($id, $herbs);
//创建药方对应的药材
$this->createCaseHerb($case_herb);
9 months ago
return $id;
}
9 months ago
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;
9 months ago
$res = $this->obj->update($this->tbl, $data, array('sql' => '`id`=?', 'vals' => array($id)));
if (!$res) {
$this->setError('更新失败');
return false;
}
9 months ago
//格式化药方对应的药材数据
$case_herb = $this->formatCaseHerb($id, $herbs);
//更新药方对应的药材
$this->compareCaseHerb($id, $case_herb);
9 months ago
return true;
}
public function formatCaseHerb($formula_id, $herbs) {
9 months ago
$case_herb = array();
9 months ago
foreach ($herbs as $key => $item) {
9 months ago
$temp = array();
$temp['name'] = $item['name'];
$temp['formula_id'] = $formula_id;
$temp['num'] = $this->convertToNum('汉', $item['num'], $item['name']);
9 months ago
$temp['sort'] = $key;
9 months ago
$herb = $this->getHerbByName($item['name']);
9 months ago
if ($herb) {
9 months ago
$temp['herb_id'] = $herb['id'];
} else {
$temp['herb_id'] = $this->createHerb(array('name' => $item['name'], 'desc' => $item['desc']));
9 months ago
}
$case_herb[] = $temp;
}
9 months ago
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;
}
9 months ago
public function getHerbByName($name) {
return $this->obj->select($this->herb_tbl, array('sql' => '`name`=?', 'vals' => array($name)));
}
9 months ago
public function createHerb($info) {
9 months ago
$herb_id = $this->obj->insert($this->herb_tbl, $info);
9 months ago
if (empty($herb_id)) {
$this->setError('添加药材失败');
return false;
}
9 months ago
return $herb_id;
}
9 months ago
public function createCaseHerb($info) {
$res = $this->obj->mutiInsert($this->formula_herb_tbl, $info);
9 months ago
if (!$res) {
$this->setError('添加药方药材失败');
return false;
}
9 months ago
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) {
9 months ago
//需要删除药方对应的药材
foreach ($old_data as $key => $value) {
9 months ago
if (!isset($new_data[$key])) {
$res = $this->obj->delete($this->formula_herb_tbl, array('sql' => '`id`=?', 'vals' => array($value['id'])));
9 months ago
if (!$res) {
$this->setError('删除药方药材失败');
return false;
}
}
$res = $this->obj->update($this->formula_herb_tbl, $new_data[$key], array('sql' => '`id`=?', 'vals' => array($value['id'])));
9 months ago
if (!$res) {
$this->setError('更新药方药材失败');
return false;
9 months ago
}
}
} else {
9 months ago
//需要增加药方对应的药材
foreach ($new_data as $key => $value) {
9 months ago
if (!isset($old_data[$key])) {
$res = $this->obj->insert($this->formula_herb_tbl, $value);
9 months ago
if (!$res) {
$this->setError('添加药方药材失败');
return false;
}
}
$res = $this->obj->update($this->formula_herb_tbl, $value, array('sql' => '`id`=?', 'vals' => array($old_data[$key]['id'])));
9 months ago
if (!$res) {
$this->setError('更新药方药材失败');
return false;
9 months ago
}
}
}
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 ');
}
9 months ago
public function getFormulasHerb($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 getUserFormulaByUid($uid, $page_num, $page_size) {
$offset = ($page_num - 1) * $page_size;
return $this->obj->selectAll($this->tbl, array('sql' => '`uid`=?', 'vals' => array($uid)), 'sort desc ', array($offset, $page_size));
}
public function getUserFormulaList($uid, $page_num, $page_size) {
$data = $this->getUserFormulaByUid($uid, $page_num, $page_size);
$formula_ids = array_column($data, 'id');
$formula_arr = $this->getFormulaHerbData($formula_ids);
foreach ($data as &$da) {
unset($da['original']);
$da['formula'] = $formula_arr[$da['id']];
}
return $data;
}
public function getUserFormulaInfo($id, $uid) {
//药方信息
$formula = $this->getFormulaById($id);
if (empty($formula)) {
$this->setError('查询不到此药方');
return false;
}
//uid==0 不判断药方归属
if ($uid > 0 && $formula['uid'] != $uid) {
$this->setError('这不是你的药方');
return false;
}
$formulaHerb = array();
$formula_arr = $this->getFormulaHerbData(array($formula['id']));
foreach ($formula_arr as $ar) {
foreach ($ar as &$v) {
//药方克重为0 去查询原方
if (isset($original[$v['name']])) {
$v['num_str'] = $original[$v['name']]['num'];
$v['desc'] = $original[$v['name']]['desc'];
}
}
$formulaHerb = $ar;
}
$formula['formula'] = $formulaHerb;
return $formula;
}
public function getUserFormulaTotal($uid) {
return $this->obj->count($this->tbl, array('sql' => '`uid`=?', 'vals' => array($uid)));
}
public function getFormulaByName($uid, $page_num, $page_size, $name) {
$offset = ($page_num - 1) * $page_size;
9 months ago
$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']];
}
$original = array();
if (!empty($da['original'])) {
$original = json_decode($da['original'], true);
$original = array_column($original, null, 'name');
}
unset($da['original']);
foreach ($da['formula'] as &$v) {
//药方克重为0 去查询原方
if (isset($original[$v['name']]) && $v['num'] == 0) {
$v['num_str'] = $original[$v['name']]['num'];
$v['desc'] = $original[$v['name']]['desc'];
}
}
}
return $data;
}
public function getFormulaHerbData($formula_ids) {
$formula_herbs = $this->getFormulasHerb($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) {
$formula_arr[$formula_herb['formula_id']][] = array(
'id' => $formula_herb['herb_id'],
'name' => $herb_arr[$formula_herb['herb_id']]['name'],
'num' => $formula_herb['num'],
'desc' => $formula_herb['desc'],
'num_str' => $formula_herb['num'] . 'g',
);
9 months ago
}
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) {
9 months ago
//药方信息
$formula = $this->getFormulaById($id);
if (empty($formula)) {
9 months ago
$this->setError('查询不到此药方');
return false;
}
//uid==0 不判断药方归属
if ($uid > 0 && $formula['uid'] > 0 && $formula['uid'] != $uid) {
$this->setError('这不是你的药方');
9 months ago
return false;
}
if (!empty($formula['original'])) {
$original = json_decode($formula['original'], true);
$original = array_column($original, null, 'name');
}
unset($formula['original']);
$formulaHerb = array();
$formula_arr = $this->getFormulaHerbData(array($formula['id']));
foreach ($formula_arr as $ar) {
foreach ($ar as &$v) {
//药方克重为0 去查询原方
if (isset($original[$v['name']])) {
$v['num_str'] = $original[$v['name']]['num'];
$v['desc'] = $original[$v['name']]['desc'];
}
}
$formulaHerb = $ar;
}
$formula['formula'] = $formulaHerb;
return $formula;
9 months ago
}
public function getFormulaById($id) {
return $this->obj->select($this->tbl, array('sql' => '`id`=?', 'vals' => array($id)));
9 months ago
}
public function getHerbs($ids) {
9 months ago
return $this->obj->selectIn($this->herb_tbl, array('id' => $ids));
}
public function getFormulaByIds($ids) {
9 months ago
return $this->obj->selectIn($this->tbl, array('id' => $ids));
9 months ago
}
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');
9 months ago
if (!$res) {
9 months ago
$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('药方使用次数更新失败');
9 months ago
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() {
9 months ago
return $this->obj->count($this->collect_log_tbl);
}
9 months ago
}