录医案
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.
 
 
 
 
 

326 lines
11 KiB

<?php
/**
*
*/
include_once(SERVER_ROOT . "/model/mBase.php");
class mCase extends mBase {
private $obj;
private $tbl;
private $case_herb_tbl;
private $herb_tbl;
private $collect_log_tbl;
private $case_use_log_tbl;
public function __construct() {
$this->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 = $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)) {
$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 = $this->getCaseHerbByCaseId($case['id']);
if (empty($case_herb)) {
$this->setError('药方药材查询失败');
return false;
}
//药材名称
$herb_ids = array_column($case_herb, 'herb_id');
$herb = $this->getHerbByIds($herb_ids);
if (empty($herb)) {
$this->setError('药材名称查询失败');
return false;
}
return array('case' => $case, 'case_herb' => $case_herb, 'herb' => array_column($herb, null, 'id'));
}
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);
}
}