|
|
@ -235,7 +235,7 @@ class mFormula extends mBase { |
|
|
|
public function insertHerb($info) { |
|
|
|
$herb_id = $this->obj->insert($this->herb_tbl, $info); |
|
|
|
if (!$herb_id) { |
|
|
|
$this->writeLog('formula', 'update_error_log', '添加药材失败|' . json_encode($info)); |
|
|
|
$this->writeLog('formula', 'save_error_log', '添加药材失败|' . json_encode($info)); |
|
|
|
return false; |
|
|
|
} |
|
|
|
|
|
|
@ -295,7 +295,7 @@ class mFormula extends mBase { |
|
|
|
public function insertFormula($info) { |
|
|
|
$id = $this->obj->insert($this->tbl, $info); |
|
|
|
if (empty($id)) { |
|
|
|
$this->setError('添加药材失败'); |
|
|
|
$this->writeLog('formula', 'save_error_log', '添加药方失败|' . json_encode($info, JSON_UNESCAPED_UNICODE)); |
|
|
|
return false; |
|
|
|
} |
|
|
|
|
|
|
@ -305,76 +305,17 @@ class mFormula extends mBase { |
|
|
|
public function insertFormulaHerb($info) { |
|
|
|
$res = $this->obj->mutiInsert($this->formula_herb_tbl, $info); |
|
|
|
if (!$res) { |
|
|
|
$this->setError('添加药方药材失败'); |
|
|
|
$this->writeLog('formula', 'save_error_log', '批量添加药方药材关联失败|' . json_encode($info, JSON_UNESCAPED_UNICODE)); |
|
|
|
return false; |
|
|
|
} |
|
|
|
|
|
|
|
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) { |
|
|
|
$res = $this->obj->update($this->tbl, $info, array('sql' => '`id`=?', 'vals' => array($id))); |
|
|
|
if (!$res) { |
|
|
|
$this->setError('更新失败'); |
|
|
|
$this->writeLog('formula', 'save_error_log', '更新药方失败|' . $id . '|' . json_encode($info, JSON_UNESCAPED_UNICODE)); |
|
|
|
return false; |
|
|
|
} |
|
|
|
|
|
|
@ -397,14 +338,14 @@ class mFormula extends mBase { |
|
|
|
if (!isset($new_data[$key])) { |
|
|
|
$res = $this->obj->delete($this->formula_herb_tbl, array('sql' => '`id`=?', 'vals' => array($value['id']))); |
|
|
|
if (!$res) { |
|
|
|
$this->setError('删除药方药材失败'); |
|
|
|
$this->writeLog('formula', 'save_error_log', '删除药方药材关联失败|' . $value['id']); |
|
|
|
return false; |
|
|
|
} |
|
|
|
continue; |
|
|
|
} |
|
|
|
$res = $this->obj->update($this->formula_herb_tbl, $new_data[$key], array('sql' => '`id`=?', 'vals' => array($value['id']))); |
|
|
|
if (!$res) { |
|
|
|
$this->setError('更新药方药材失败'); |
|
|
|
$this->writeLog('formula', 'save_error_log', '更新药方药材关联失败1|' . $value['id'] . '|' . json_encode($new_data[$key], JSON_UNESCAPED_UNICODE)); |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
@ -414,14 +355,14 @@ class mFormula extends mBase { |
|
|
|
if (!isset($old_data[$key])) { |
|
|
|
$res = $this->obj->insert($this->formula_herb_tbl, $value); |
|
|
|
if (!$res) { |
|
|
|
$this->setError('添加药方药材失败'); |
|
|
|
$this->writeLog('formula', 'save_error_log', '添加药方药材关联失败|' . $value['id']); |
|
|
|
return false; |
|
|
|
} |
|
|
|
continue; |
|
|
|
} |
|
|
|
$res = $this->obj->update($this->formula_herb_tbl, $value, array('sql' => '`id`=?', 'vals' => array($old_data[$key]['id']))); |
|
|
|
if (!$res) { |
|
|
|
$this->setError('更新药方药材失败'); |
|
|
|
$this->writeLog('formula', 'save_error_log', '更新药方药材关联失败2|' . $value['id'] . '|' . json_encode($new_data[$key], JSON_UNESCAPED_UNICODE)); |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
@ -431,69 +372,6 @@ class mFormula extends mBase { |
|
|
|
} |
|
|
|
|
|
|
|
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)) { |
|
|
|
$this->setError('药方名称不能为空'); |
|
|
|
return false; |
|
|
@ -542,7 +420,7 @@ class mFormula extends mBase { |
|
|
|
//格式化药方对应的药材数据 |
|
|
|
$formula_herb = $this->dealForulaHerb($uid, $herbs); |
|
|
|
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; |
|
|
|
} |
|
|
|
|
|
|
@ -550,7 +428,7 @@ class mFormula extends mBase { |
|
|
|
//添加药方 并添加药方药材关联关系 |
|
|
|
$id = $this->insertFormula($data); |
|
|
|
if (!$id) { |
|
|
|
$this->writeLog('formula', 'save_error', '添加药方失败|' . $uid . '|' . json_encode($data, JSON_UNESCAPED_UNICODE)); |
|
|
|
$this->setError('添加药材失败'); |
|
|
|
return false; |
|
|
|
} |
|
|
|
|
|
|
@ -560,14 +438,14 @@ class mFormula extends mBase { |
|
|
|
} |
|
|
|
$res = $this->insertFormulaHerb($formula_herb); |
|
|
|
if (!$res) { |
|
|
|
$this->writeLog('formula', 'save_error', '添加药方,药材关联失败|' . $uid . '|' . json_encode($formula_herb, JSON_UNESCAPED_UNICODE)); |
|
|
|
$this->setError('批量添加药方药材关联失败'); |
|
|
|
return false; |
|
|
|
} |
|
|
|
} else { |
|
|
|
//更新药方 并更新药方药材关联关系 |
|
|
|
$res = $this->updateFormula($id, $data); |
|
|
|
if (!$res) { |
|
|
|
$this->writeLog('formula', 'save_error', '更新药方失败|' . $uid . '|' . json_encode($data, JSON_UNESCAPED_UNICODE)); |
|
|
|
$this->setError('更新药方失败'); |
|
|
|
return false; |
|
|
|
} |
|
|
|
|
|
|
@ -577,7 +455,7 @@ class mFormula extends mBase { |
|
|
|
} |
|
|
|
$res = $this->mutiUpdateFormulaHerb($id, $formula_herb); |
|
|
|
if (!$res) { |
|
|
|
$this->writeLog('formula', 'save_error', '更新药方,药材关联失败|' . $uid . '|' . $id . '|' . json_encode($formula_herb, JSON_UNESCAPED_UNICODE)); |
|
|
|
$this->setError('批量更新药方药材关联失败'); |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
@ -598,7 +476,7 @@ class mFormula extends mBase { |
|
|
|
|
|
|
|
$res = $this->updateFormula($id, array('is_delete' => 1)); |
|
|
|
if (!$res) { |
|
|
|
$this->writeLog('formula', 'update_error_log', '更新药方失败|' . $uid . '|' . json_encode($data, JSON_UNESCAPED_UNICODE)); |
|
|
|
$this->setError('删除药方失败'); |
|
|
|
return false; |
|
|
|
} |
|
|
|
|
|
|
|