From 8339296fb86ec195888afcb1d26e21a49a5bcb52 Mon Sep 17 00:00:00 2001 From: pengda <10266652509@qq.com> Date: Mon, 2 Sep 2024 10:39:41 +0800 Subject: [PATCH] =?UTF-8?q?=E8=8D=AF=E6=96=B9=E5=BD=95=E5=85=A5=E4=BC=98?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- control/index.php | 37 ++++++++---------- model/mCase.php | 88 ++++++++++++++++++------------------------ view/templates/index/home.html | 54 +++++++++++++++++++------- 3 files changed, 92 insertions(+), 87 deletions(-) diff --git a/control/index.php b/control/index.php index 2018243..248ddf8 100644 --- a/control/index.php +++ b/control/index.php @@ -18,38 +18,33 @@ 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, - ); - - if($id){ - //更新药方 - $res = $mCase->updateCase($id, $case_data, $herbs); - if (!$res) $this->ajax_json(false, $mCase->getError()); + $this->ajax_json(true, '添加成功'); + } - $this->ajax_json(true, '保存成功'); - } + 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'); - //新增药方 - $id = $mCase->createCase($case_data, $herbs); - if (!$id) $this->ajax_json(false, $mCase->getError()); + $mCase = new mCase(); + $res = $mCase->updateCase($id, $name, $source, $original, $method, $herbs); + if (!$res) $this->ajax_json(false, $mCase->getError()); - $this->ajax_json(true, '添加成功'); + $this->ajax_json(true, '保存成功'); } public function ajax_search() { diff --git a/model/mCase.php b/model/mCase.php index 8451cfd..e22c672 100644 --- a/model/mCase.php +++ b/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; } diff --git a/view/templates/index/home.html b/view/templates/index/home.html index cca79bc..f097c50 100644 --- a/view/templates/index/home.html +++ b/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,22 +126,41 @@ }); }); - $.ajax({ - url: 'ajax_save_case', // 替换为你的服务器端处理文件 - type: 'POST', - data: data, - dataType: 'json', - success: function(response) { - alert(response.info); - if (response.status==true) { - window.location.reload(); + 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('提交失败,请重试。'); } - }, - error: function(xhr, status, error) { - console.error('错误:', error); - alert('提交失败,请重试。'); - } - }); + }); + }else{ + $.ajax({ + url: 'ajax_save_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('提交失败,请重试。'); + } + }); + } } {/literal} @@ -159,6 +179,10 @@