Browse Source

模型更新

pull/1/head
pengda 9 months ago
parent
commit
93ec433271
  1. 2
      data/dCase.php
  2. 183
      model/mCase.php

2
data/dCase.php

@ -13,7 +13,7 @@ class dCase extends dBase {
'name', 'name',
'source', 'source',
'method', 'method',
'search_num', 'use_num',
'sort', 'sort',
), ),
'tcm_case_herb' => array( 'tcm_case_herb' => array(

183
model/mCase.php

@ -20,100 +20,183 @@ class mCase extends mBase {
$this->collect_log_tbl = 'tcm_collect_log'; $this->collect_log_tbl = 'tcm_collect_log';
} }
public function createCase($info){ public function createCase($data, $herbs){
return $this->obj->insert($this->tbl, $info); $id = $this->obj->insert($this->tbl, $data);
if(!$id){
$this->setError('添加失败');
return false;
} }
//格式化药方对应的药材数据
$case_herb = $this->formatCaseHerb($id, $herbs);
//创建药方对应的药材
$this->createCaseHerb($case_herb);
public function updateCase($id,$data){ return $id;
return $this->obj->update($this->tbl, $data, array('sql'=>'`id`=?', 'vals'=>array($id)));
} }
public function updateCaseSearchNum($id){ public function updateCase($id, $data, $herbs){
return $this->obj->increase($this->tbl, array('sql'=>'`id`=?', 'vals'=>array($id)),'search_num'); $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);
public function getCaseByName($name,$start,$pagesize) { return true;
$sql = " `name` like '%{$name}%'"; }
$res = $this->obj->selectAll($this->tbl, array('sql'=>$sql, 'vals'=>array()), 'search_num desc,sort asc ', array($start, $pagesize)); public function formatCaseHerb($case_id,$herbs){
$case_herb = array();
foreach ($herbs as $key=>$item) {
$temp = array();
$temp['case_id']=$case_id;
$temp['num']=$item['num'];
$temp['sort']=$key;
if(empty($res)) return array(); $herb = $this->getHerbByName($item['name']);
return $res; if($herb){
$temp['herb_id'] = $herb['id'];
}else{
$temp['herb_id'] = $this->createHerb(array('name'=>$item['name']));
} }
$case_herb[] = $temp;
public function getCaseById($id){ }
return $this->obj->select($this->tbl, array('sql'=>'`id`=?','vals'=>array($id))); return $case_herb;
} }
public function getCaseInfo($id) { public function getHerbByName($name){
//药方信息 return $this->obj->select($this->herb_tbl, array('sql'=>'`name`=?', 'vals'=>array($name)));
$case = $this->getCaseById($id);
//药方药材信息
$case_herb = $this->getCaseHerbByCaseId($case['id']);
//药材名称
$herb_ids = array_column($case_herb,'herb_id');
$herb = $this->getHerbByIds($herb_ids);
$data = array(
'case' => $case,
'case_herb' => $case_herb,
'herb' => array_column($herb,null,'id'),
);
return $data;
} }
public function getCaseByIds($ids){ public function createHerb($info){
return $this->obj->selectIn($this->tbl, array('id'=>$ids)); $herb_id = $this->obj->insert($this->herb_tbl, $info);
if(empty($herb_id)){
$this->setError('添加药材失败');
return false;
}
return $herb_id;
} }
public function createCaseHerb($info){ public function createCaseHerb($info){
return $this->obj->mutiInsert($this->case_herb_tbl, $info); $res = $this->obj->mutiInsert($this->case_herb_tbl, $info);
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 compareCaseHerb($old_data,$new_data){ public function compareCaseHerb($id,$new_data){
$old_data = $this->getCaseHerbByCaseId($id);
$old_num = count($old_data); $old_num = count($old_data);
$new_num = count($new_data); $new_num = count($new_data);
//需要删除
if($old_num>=$new_num){ if($old_num>=$new_num){
//需要删除药方对应的药材
foreach ($old_data as $key => $value) { foreach ($old_data as $key => $value) {
if(!isset($new_data[$key])){ if(!isset($new_data[$key])){
$this->obj->delete($this->case_herb_tbl, array('sql'=>'`id`=?', 'vals'=>array($value['id']))); $res = $this->obj->delete($this->case_herb_tbl, array('sql'=>'`id`=?', 'vals'=>array($value['id'])));
continue; 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;
} }
$this->obj->update($this->case_herb_tbl, $new_data[$key], array('sql'=>'`id`=?', 'vals'=>array($value['id'])));
} }
}else{ }else{
//需要增加药方对应的药材
foreach ($new_data as $key => $value) { foreach ($new_data as $key => $value) {
if(!isset($old_data[$key])){ if(!isset($old_data[$key])){
$this->obj->insert($this->case_herb_tbl, $value); $res = $this->obj->insert($this->case_herb_tbl, $value);
continue; if(!$res){
$this->setError('添加药方药材失败');
return false;
}
} }
$this->obj->update($this->case_herb_tbl, $value, array('sql'=>'`id`=?', 'vals'=>array($old_data[$key]['id']))); $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; return true;
} }
public function createHerb($info){ public function getCaseHerbByCaseId($case_id){
return $this->obj->insert($this->herb_tbl, $info); return $this->obj->selectAll($this->case_herb_tbl, array('sql'=>'`case_id`=?','vals'=>array($case_id)), 'sort asc ');
} }
public function getHerbByName($name){
return $this->obj->select($this->herb_tbl, array('sql'=>'`name`=?', 'vals'=>array($name))); public function getCaseByName($name,$start,$pagesize) {
$sql = " `name` like '%{$name}%'";
$res = $this->obj->selectAll($this->tbl, array('sql'=>$sql, 'vals'=>array()), 'use_num desc,sort asc ', array($start, $pagesize));
if(empty($res)) return array();
return $res;
}
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;
}
$data = array(
'case' => $case,
'case_herb' => $case_herb,
'herb' => array_column($herb,null,'id'),
);
return $data;
}
public function getCaseById($id){
return $this->obj->select($this->tbl, array('sql'=>'`id`=?','vals'=>array($id)));
} }
public function getHerbByIds($ids){ public function getHerbByIds($ids){
return $this->obj->selectIn($this->herb_tbl, array('id'=>$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 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'));
return false;
}
return true;
}
public function getCollectLog($start,$pagesize) { public function getCollectLog($start,$pagesize) {
return $this->obj->selectAll($this->collect_log_tbl, array(), 'collect_time desc ', array($start, $pagesize)); return $this->obj->selectAll($this->collect_log_tbl, array(), 'collect_time desc ', array($start, $pagesize));
} }

Loading…
Cancel
Save