Browse Source

药方录入优化

pull/1/head
pengda 9 months ago
parent
commit
8339296fb8
  1. 35
      control/index.php
  2. 88
      model/mCase.php
  3. 24
      view/templates/index/home.html

35
control/index.php

@ -18,40 +18,35 @@ class index extends publicBase {
$this->setViewTpl('index/home.html');
}
public function ajax_save_case() {
$id = $this->post('id');
$name = $this->post('name');
$source = $this->post('source');
$original = $this->post('original');
$method = $this->post('method');
$herbs = $this->post('herbs');
if(empty($name))$this->ajax_json(false, '姓名不能为空');
if(empty($source))$this->ajax_json(false, '药方来源不能为空');
if(empty($herbs))$this->ajax_json(false, '药方药材不能为空');
//新增药方
$mCase = new mCase();
$id = $mCase->createCase($name, $source, $original, $method, $herbs);
if (!$id) $this->ajax_json(false, $mCase->getError());
$case_data = array(
'name'=>$name,
'source'=>$source,
'original'=>$original,
'method'=>$method,
);
$this->ajax_json(true, '添加成功');
}
if($id){
//更新药方
$res = $mCase->updateCase($id, $case_data, $herbs);
public function ajax_update_case() {
$id = $this->post('id');
$name = $this->post('name');
$source = $this->post('source');
$original = $this->post('original');
$method = $this->post('method');
$herbs = $this->post('herbs');
$mCase = new mCase();
$res = $mCase->updateCase($id, $name, $source, $original, $method, $herbs);
if (!$res) $this->ajax_json(false, $mCase->getError());
$this->ajax_json(true, '保存成功');
}
//新增药方
$id = $mCase->createCase($case_data, $herbs);
if (!$id) $this->ajax_json(false, $mCase->getError());
$this->ajax_json(true, '添加成功');
}
public function ajax_search() {
$content = $this->post('content');
if(empty($content))$this->ajax_json(false, '非法请求');

88
model/mCase.php

@ -20,12 +20,16 @@ class mCase extends mBase {
$this->collect_log_tbl = 'tcm_collect_log';
}
public function createCase($data, $herbs){
$id = $this->obj->insert($this->tbl, $data);
if(!$id){
$this->setError('添加失败');
return false;
}
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));
if(!$id){$this->setError('添加失败');return false;}
//格式化药方对应的药材数据
$case_herb = $this->formatCaseHerb($id, $herbs);
//创建药方对应的药材
@ -34,12 +38,18 @@ class mCase extends mBase {
return $id;
}
public function updateCase($id, $data, $herbs){
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;
}
if(!$res){$this->setError('更新失败');return false;}
//格式化药方对应的药材数据
$case_herb = $this->formatCaseHerb($id, $herbs);
//更新药方对应的药材
@ -73,19 +83,15 @@ class mCase extends mBase {
public function createHerb($info){
$herb_id = $this->obj->insert($this->herb_tbl, $info);
if(empty($herb_id)){
$this->setError('添加药材失败');
return false;
}
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;
}
if(!$res){$this->setError('添加药方药材失败');return false;}
return true;
}
@ -100,32 +106,20 @@ 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'])));
if(!$res){
$this->setError('删除药方药材失败');
return false;
}
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;
}
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;
}
}
$this->obj->update($this->case_herb_tbl, $value, array('sql'=>'`id`=?', 'vals'=>array($old_data[$key]['id'])));
if(!$res){
$this->setError('更新药方药材失败');
return false;
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;}
}
}
@ -148,29 +142,20 @@ class mCase extends mBase {
public function getCaseInfo($id) {
//药方信息
$case = $this->getCaseById($id);
if(empty($case)){
$this->setError('查询不到此药方');
return false;
}
if(empty($case)){$this->setError('查询不到此药方');return false;}
//药方药材信息
$case_herb = $this->getCaseHerbByCaseId($case['id']);
if(empty($case_herb)){
$this->setError('药方药材查询失败');
return false;
}
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;
}
$data = array(
if(empty($herb)){$this->setError('药材名称查询失败');return false;}
return array(
'case' => $case,
'case_herb' => $case_herb,
'herb' => array_column($herb,null,'id'),
);
return $data;
}
public function getCaseById($id){
@ -188,9 +173,10 @@ class mCase extends mBase {
public function updateCaseSearchNum($id){
$res = $this->obj->increase($this->tbl, array('sql'=>'`id`=?', 'vals'=>array($id)),'use_num');
if(!$res){
$this->setError('药方使用次数更新失败');
$tool_obj = new qTool();
$tool_obj->trackLog('tcm', $id, sprintf(LOG_TRACK_SAVE_PATH, date('Y-m-d'), 'tcm_case_use_num'));
$this->setError('药方使用次数更新失败');
return false;
}

24
view/templates/index/home.html

@ -110,6 +110,7 @@
id: formData.get('id'),
name: formData.get('name'),
source: formData.get('source'),
original: formData.get('original'),
method: formData.get('method'),
herbs: []
};
@ -125,6 +126,24 @@
});
});
if(data.id.length>0){
$.ajax({
url: 'ajax_update_case', // 替换为你的服务器端处理文件
type: 'POST',
data: data,
dataType: 'json',
success: function(response) {
alert(response.info);
if (response.status==true) {
window.location.reload();
}
},
error: function(xhr, status, error) {
console.error('错误:', error);
alert('提交失败,请重试。');
}
});
}else{
$.ajax({
url: 'ajax_save_case', // 替换为你的服务器端处理文件
type: 'POST',
@ -142,6 +161,7 @@
}
});
}
}
</script>
{/literal}
</head>
@ -159,6 +179,10 @@
<textarea id="source" name="source" rows="4" required>{$data.case.source}</textarea>
</div>
<div class="form-group">
<label for="original">原方:</label>
<textarea id="original" name="original" rows="4" required>{$data.case.original}</textarea>
</div>
<div class="form-group">
<label for="method">用法:</label>
<textarea id="method" name="method" rows="4" required>{$data.case.method}</textarea>
</div>

Loading…
Cancel
Save