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

255 lines
8.1 KiB

9 months ago
<?php
/**
*
*/
include_once(SERVER_ROOT . "/model/mBase.php");
9 months ago
class mCase extends mBase {
private $obj;
private $tbl;
private $case_herb_tbl;
private $herb_tbl;
private $collect_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';
}
9 months ago
public function createCase($name, $source, $original, $method, $herbs) {
if (empty($name)) {
$this->setError('药方名称不能为空');
return false;
}
if (empty($source)) {
$this->setError('药方来源不能为空');
return false;
}
if (empty($original)) {
$this->setError('药方原方不能为空');
return false;
}
if (empty($method)) {
$this->setError('药方用法不能为空');
return false;
}
if (empty($herbs)) {
$this->setError('药方药材不能为空');
return false;
}
$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($case_id, $herbs) {
9 months ago
$case_herb = array();
9 months ago
foreach ($herbs as $key => $item) {
9 months ago
$temp = array();
9 months ago
$temp['case_id'] = $case_id;
$temp['num'] = $item['num'];
$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 {
9 months ago
$temp['herb_id'] = $this->createHerb(array('name' => $item['name']));
9 months ago
}
$case_herb[] = $temp;
}
return $case_herb;
}
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) {
9 months ago
$res = $this->obj->mutiInsert($this->case_herb_tbl, $info);
9 months ago
if (!$res) {
$this->setError('添加药方药材失败');
return false;
}
9 months ago
return true;
}
public function compareCaseHerb($id, $new_data) {
9 months ago
$old_data = $this->getCaseHerbByCaseId($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->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;
9 months ago
}
}
} else {
9 months ago
//需要增加药方对应的药材
foreach ($new_data as $key => $value) {
9 months ago
if (!isset($old_data[$key])) {
9 months ago
$res = $this->obj->insert($this->case_herb_tbl, $value);
9 months ago
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;
9 months ago
}
}
}
return true;
}
9 months ago
public function getCaseHerbByCaseId($case_id) {
return $this->obj->selectAll($this->case_herb_tbl, array('sql' => '`case_id`=?', 'vals' => array($case_id)), 'sort asc ');
}
9 months ago
public function getCaseByName($name, $page_num, $page_size) {
9 months ago
$sql = " `name` like '%{$name}%'";
$offset = ($page_num - 1) * $page_size;
$res = $this->obj->selectAll($this->tbl, array('sql' => $sql, 'vals' => array()), 'use_num desc,sort asc ', array($offset, $page_size));
9 months ago
9 months ago
if (empty($res)) return array();
9 months ago
return $res;
}
9 months ago
public function getCaseByNameTotal($name) {
$sql = " `name` like '%{$name}%'";
return $this->obj->count($this->tbl, array('sql' => $sql, 'vals' => array()));
}
9 months ago
public function getCaseInfo($id) {
//药方信息
$case = $this->getCaseById($id);
9 months ago
if (empty($case)) {
$this->setError('查询不到此药方');
return false;
}
9 months ago
//药方药材信息
$case_herb = $this->getCaseHerbByCaseId($case['id']);
9 months ago
if (empty($case_herb)) {
$this->setError('药方药材查询失败');
return false;
}
9 months ago
//药材名称
$herb_ids = array_column($case_herb, 'herb_id');
9 months ago
$herb = $this->getHerbByIds($herb_ids);
9 months ago
if (empty($herb)) {
$this->setError('药材名称查询失败');
return false;
}
return array(
9 months ago
'case' => $case,
'case_herb' => $case_herb,
'herb' => array_column($herb,null,'id'),
);
}
9 months ago
public function getCaseById($id) {
return $this->obj->select($this->tbl, array('sql' => '`id`=?', 'vals' => array($id)));
9 months ago
}
9 months ago
public function getHerbByIds($ids) {
return $this->obj->selectIn($this->herb_tbl, array('id' => $ids));
}
9 months ago
public function getCaseByIds($ids) {
return $this->obj->selectIn($this->tbl, array('id' => $ids));
9 months ago
}
9 months ago
public function updateCaseSearchNum($id) {
$res = $this->obj->increase($this->tbl, array('sql' => '`id`=?', 'vals' => array($id)), 'use_num');
9 months ago
if (!$res) {
9 months ago
$tool_obj = new qTool();
$tool_obj->trackLog('tcm', $id, sprintf(LOG_TRACK_SAVE_PATH, date('Y-m-d'), 'tcm_case_use_num'));
$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
}