Browse Source

删除自拟药方

pull/1/head
pengda 8 months ago
parent
commit
3df5206b4f
  1. 18
      control/index.php
  2. 150
      model/mFormula.php

18
control/index.php

@ -113,7 +113,7 @@ class index extends publicBase {
if (!$request_times) $this->ajax_json(false, $mformula->getError()); if (!$request_times) $this->ajax_json(false, $mformula->getError());
//新增药方 //新增药方
$id = $mformula->formula($uinfo['uid'], $id, $name, $source, $method, $herbs, $is_delete); $id = $mformula->saveFormula($uinfo['uid'], $id, $name, $source, $method, $herbs, $is_delete);
if (!$id) $this->ajax_json(false, $mformula->getError()); if (!$id) $this->ajax_json(false, $mformula->getError());
$this->ajax_json(true, '请求成功', array('id' => $id)); $this->ajax_json(true, '请求成功', array('id' => $id));
@ -131,22 +131,6 @@ class index extends publicBase {
$this->ajax_json(true, '请求成功'); $this->ajax_json(true, '请求成功');
} }
// public function ajax_update_formula() {
// $uinfo = $this->_check_login();
//
// $id = $this->post('id') + 0;
// $name = trim($this->post('name'));
// $source = trim($this->post('source'));
// $method = trim($this->post('method'));
// $herbs = $this->post('herbs');
//
// $mformula = new mFormula();
// $res = $mformula->saveFormula($uinfo['uid'], $id, $name, $source, $method, $herbs);
// if (!$res) $this->ajax_json(false, $mformula->getError());
//
// $this->ajax_json(true, '保存成功');
// }
public function ajax_user_formula() { public function ajax_user_formula() {
$uinfo = $this->_check_login(); $uinfo = $this->_check_login();

150
model/mFormula.php

@ -235,7 +235,7 @@ class mFormula extends mBase {
public function insertHerb($info) { public function insertHerb($info) {
$herb_id = $this->obj->insert($this->herb_tbl, $info); $herb_id = $this->obj->insert($this->herb_tbl, $info);
if (!$herb_id) { if (!$herb_id) {
$this->writeLog('formula', 'update_error_log', '添加药材失败|' . json_encode($info)); $this->writeLog('formula', 'save_error_log', '添加药材失败|' . json_encode($info));
return false; return false;
} }
@ -295,7 +295,7 @@ class mFormula extends mBase {
public function insertFormula($info) { public function insertFormula($info) {
$id = $this->obj->insert($this->tbl, $info); $id = $this->obj->insert($this->tbl, $info);
if (empty($id)) { if (empty($id)) {
$this->setError('添加药材失败'); $this->writeLog('formula', 'save_error_log', '添加药方失败|' . json_encode($info, JSON_UNESCAPED_UNICODE));
return false; return false;
} }
@ -305,76 +305,17 @@ class mFormula extends mBase {
public function insertFormulaHerb($info) { public function insertFormulaHerb($info) {
$res = $this->obj->mutiInsert($this->formula_herb_tbl, $info); $res = $this->obj->mutiInsert($this->formula_herb_tbl, $info);
if (!$res) { if (!$res) {
$this->setError('添加药方药材失败'); $this->writeLog('formula', 'save_error_log', '批量添加药方药材关联失败|' . json_encode($info, JSON_UNESCAPED_UNICODE));
return false; return false;
} }
return true; return true;
} }
public function createFormula($uid, $name, $source, $method, $herbs) {
if (empty($name)) {
$this->setError('药方名称不能为空');
return false;
}
$herbs = json_decode($herbs, true);
if (empty($herbs)) {
$this->setError('药方药材不能为空');
return false;
}
$data = array(
'uid' => $uid,
'name' => $name,
'source' => $source,
);
//录入药方
if ($uid == 0) {
$org_herb = array();
foreach ($herbs as $key => $herb) {
$org_herb[$key] = array(
'name' => trim($herb['name']),
'num' => $this->convertToNum(trim($herb['name']), trim($herb['num'])),
//'desc' => trim($herb['desc']),
);
}
//更新原方和用法
$data['original'] = json_encode($org_herb, JSON_UNESCAPED_UNICODE);
$data['method'] = $method;
}
//格式化药方对应的药材数据
$formula_herb = $this->dealForulaHerb($uid, $herbs);
if (!$formula_herb) {
$this->writeLog('formula', 'insert_error_log', '添加药方,药材初始化失败|' . $uid . '|' . json_encode($herbs, JSON_UNESCAPED_UNICODE));
return false;
}
$id = $this->insertFormula($data);
if (!$id) {
$this->writeLog('formula', 'insert_error_log', '添加药方失败|' . $uid . '|' . json_encode($data, JSON_UNESCAPED_UNICODE));
return false;
}
//创建药方对应的药材
foreach ($formula_herb as &$item) {
$item['formula_id'] = $id;
}
$res = $this->insertFormulaHerb($formula_herb);
if (!$res) {
$this->writeLog('formula', 'insert_error_log', '添加药方,药材关联失败|' . $uid . '|' . json_encode($formula_herb, JSON_UNESCAPED_UNICODE));
return false;
}
return $id;
}
public function updateFormula($id, $info) { public function updateFormula($id, $info) {
$res = $this->obj->update($this->tbl, $info, array('sql' => '`id`=?', 'vals' => array($id))); $res = $this->obj->update($this->tbl, $info, array('sql' => '`id`=?', 'vals' => array($id)));
if (!$res) { if (!$res) {
$this->setError('更新失败'); $this->writeLog('formula', 'save_error_log', '更新药方失败|' . $id . '|' . json_encode($info, JSON_UNESCAPED_UNICODE));
return false; return false;
} }
@ -397,14 +338,14 @@ class mFormula extends mBase {
if (!isset($new_data[$key])) { if (!isset($new_data[$key])) {
$res = $this->obj->delete($this->formula_herb_tbl, array('sql' => '`id`=?', 'vals' => array($value['id']))); $res = $this->obj->delete($this->formula_herb_tbl, array('sql' => '`id`=?', 'vals' => array($value['id'])));
if (!$res) { if (!$res) {
$this->setError('删除药方药材失败'); $this->writeLog('formula', 'save_error_log', '删除药方药材关联失败|' . $value['id']);
return false; return false;
} }
continue; continue;
} }
$res = $this->obj->update($this->formula_herb_tbl, $new_data[$key], array('sql' => '`id`=?', 'vals' => array($value['id']))); $res = $this->obj->update($this->formula_herb_tbl, $new_data[$key], array('sql' => '`id`=?', 'vals' => array($value['id'])));
if (!$res) { if (!$res) {
$this->setError('更新药方药材失败'); $this->writeLog('formula', 'save_error_log', '更新药方药材关联失败1|' . $value['id'] . '|' . json_encode($new_data[$key], JSON_UNESCAPED_UNICODE));
return false; return false;
} }
} }
@ -414,14 +355,14 @@ class mFormula extends mBase {
if (!isset($old_data[$key])) { if (!isset($old_data[$key])) {
$res = $this->obj->insert($this->formula_herb_tbl, $value); $res = $this->obj->insert($this->formula_herb_tbl, $value);
if (!$res) { if (!$res) {
$this->setError('添加药方药材失败'); $this->writeLog('formula', 'save_error_log', '添加药方药材关联失败|' . $value['id']);
return false; return false;
} }
continue; continue;
} }
$res = $this->obj->update($this->formula_herb_tbl, $value, array('sql' => '`id`=?', 'vals' => array($old_data[$key]['id']))); $res = $this->obj->update($this->formula_herb_tbl, $value, array('sql' => '`id`=?', 'vals' => array($old_data[$key]['id'])));
if (!$res) { if (!$res) {
$this->setError('更新药方药材失败'); $this->writeLog('formula', 'save_error_log', '更新药方药材关联失败2|' . $value['id'] . '|' . json_encode($new_data[$key], JSON_UNESCAPED_UNICODE));
return false; return false;
} }
} }
@ -431,69 +372,6 @@ class mFormula extends mBase {
} }
public function saveFormula($uid, $id, $name, $source, $method, $herbs) { public function saveFormula($uid, $id, $name, $source, $method, $herbs) {
$herbs = json_decode($herbs, true);
if (empty($herbs)) {
$this->setError('药方药材不能为空');
return false;
}
$formula = $this->getFormulaById($id);
if (empty($formula)) {
$this->setError('查询不到此药方');
return false;
}
if ($formula['uid'] > 0 && $formula['uid'] != $uid) {
$this->setError('这不是你的药方');
return false;
}
$data = array();
if (!empty($name)) $data['name'] = $name;
if (!empty($source)) $data['source'] = $source;
//录入药方
if (!$uid) {
$org_herb = array();
foreach ($herbs as $key => $herb) {
$org_herb[$key] = array(
'name' => trim($herb['name']),
'num' => $this->convertToNum(trim($herb['name']), trim($herb['num'])),
'desc' => trim($herb['desc']),
);
}
//更新原方和用法
$data['original'] = json_encode($org_herb, JSON_UNESCAPED_UNICODE);
$data['method'] = $method;
}
//格式化药方对应的药材数据
$formula_herb = $this->dealForulaHerb($uid, $herbs);
if (!$formula_herb) {
$this->writeLog('formula', 'update_error_log', '更新药方,药材初始化失败|' . $uid . '|' . json_encode($herbs, JSON_UNESCAPED_UNICODE));
return false;
}
$res = $this->updateFormula($id, $data);
if (!$res) {
$this->writeLog('formula', 'update_error_log', '更新药方失败|' . $uid . '|' . json_encode($data, JSON_UNESCAPED_UNICODE));
return false;
}
//批量更新药方药材关联关系
foreach ($formula_herb as &$item) {
$item['formula_id'] = $id;
}
$res = $this->mutiUpdateFormulaHerb($id, $formula_herb);
if (!$res) {
$this->writeLog('formula', 'update_error_log', '更新药方,药材关联失败|' . $uid . '|' . $id . '|' . json_encode($formula_herb, JSON_UNESCAPED_UNICODE));
return false;
}
return true;
}
public function formula($uid, $id, $name, $source, $method, $herbs) {
if (empty($name)) { if (empty($name)) {
$this->setError('药方名称不能为空'); $this->setError('药方名称不能为空');
return false; return false;
@ -542,7 +420,7 @@ class mFormula extends mBase {
//格式化药方对应的药材数据 //格式化药方对应的药材数据
$formula_herb = $this->dealForulaHerb($uid, $herbs); $formula_herb = $this->dealForulaHerb($uid, $herbs);
if (!$formula_herb) { if (!$formula_herb) {
$this->writeLog('formula', 'save_error', '药方药材初始化失败|' . $uid . '|' . json_encode($herbs, JSON_UNESCAPED_UNICODE)); $this->writeLog('formula', 'save_error_log', '药方药材初始化失败|' . $uid . '|' . json_encode($herbs, JSON_UNESCAPED_UNICODE));
return false; return false;
} }
@ -550,7 +428,7 @@ class mFormula extends mBase {
//添加药方 并添加药方药材关联关系 //添加药方 并添加药方药材关联关系
$id = $this->insertFormula($data); $id = $this->insertFormula($data);
if (!$id) { if (!$id) {
$this->writeLog('formula', 'save_error', '添加药方失败|' . $uid . '|' . json_encode($data, JSON_UNESCAPED_UNICODE)); $this->setError('添加药材失败');
return false; return false;
} }
@ -560,14 +438,14 @@ class mFormula extends mBase {
} }
$res = $this->insertFormulaHerb($formula_herb); $res = $this->insertFormulaHerb($formula_herb);
if (!$res) { if (!$res) {
$this->writeLog('formula', 'save_error', '添加药方,药材关联失败|' . $uid . '|' . json_encode($formula_herb, JSON_UNESCAPED_UNICODE)); $this->setError('批量添加药方药材关联失败');
return false; return false;
} }
} else { } else {
//更新药方 并更新药方药材关联关系 //更新药方 并更新药方药材关联关系
$res = $this->updateFormula($id, $data); $res = $this->updateFormula($id, $data);
if (!$res) { if (!$res) {
$this->writeLog('formula', 'save_error', '更新药方失败|' . $uid . '|' . json_encode($data, JSON_UNESCAPED_UNICODE)); $this->setError('更新药方失败');
return false; return false;
} }
@ -577,7 +455,7 @@ class mFormula extends mBase {
} }
$res = $this->mutiUpdateFormulaHerb($id, $formula_herb); $res = $this->mutiUpdateFormulaHerb($id, $formula_herb);
if (!$res) { if (!$res) {
$this->writeLog('formula', 'save_error', '更新药方,药材关联失败|' . $uid . '|' . $id . '|' . json_encode($formula_herb, JSON_UNESCAPED_UNICODE)); $this->setError('批量更新药方药材关联失败');
return false; return false;
} }
} }
@ -598,7 +476,7 @@ class mFormula extends mBase {
$res = $this->updateFormula($id, array('is_delete' => 1)); $res = $this->updateFormula($id, array('is_delete' => 1));
if (!$res) { if (!$res) {
$this->writeLog('formula', 'update_error_log', '更新药方失败|' . $uid . '|' . json_encode($data, JSON_UNESCAPED_UNICODE)); $this->setError('删除药方失败');
return false; return false;
} }

Loading…
Cancel
Save