@ -26,25 +26,29 @@ class mFormula extends mBase {
$offset = ($page_num - 1) * $page_size;
$offset = ($page_num - 1) * $page_size;
$where = "1=1";
$where = "1=1";
$use_limit = "";
if (!empty($condition)) {
if (!empty($condition)) {
foreach ($condition as $key => $val) {
foreach ($condition as $key => $val) {
if ($key == 'name') {
if ($key == 'name') {
$where .= " and s.{$key} like '%{$val}%'";
$where .= " and s.{$key} like '%{$val}%'";
} elseif ($key == 'uid' & & is_array($val)) {
$val = implode(',', $val);
$where .= " and s.{$key} in ({$val})";
//兼容查药方只计算当前用户对药方的使用次数
$use_limit .= " and u.uid in ({$val})";
} else {
} else {
$where .= " and s.{$key}={$val}";
$where .= " and s.{$key}={$val}";
}
}
}
}
}
}
$sql = "select s.*,COALESCE(SUM(u.use_num), 0) AS use_num from {$this->tbl} as s left join {$this->formula_use_log_tbl} as u on s.id=u.formula_id where {$where} group by s.id order by use_num desc,s.sort desc,s.id desc limit {$offset},{$page_size}";
$sql = "select s.*,COALESCE(SUM(u.use_num), 0) AS use_num from {$this->tbl} as s left join {$this->formula_use_log_tbl} as u on s.id=u.formula_id {$use_limit} where {$where} group by s.id order by use_num desc,s.sort desc,s.id desc limit {$offset},{$page_size}";
// echo "< pre > ";
// print_r($sql);
// exit;
return $this->obj->execute($sql, true, true);
return $this->obj->execute($sql, true, true);
}
}
public function getAll FormulaList($condition, $page_num, $page_size) {
public function getFormulaList($condition, $page_num, $page_size) {
$data = $this->getFormulaByCondition($condition, $page_num, $page_size);
$data = $this->getFormulaByCondition($condition, $page_num, $page_size);
$formula_ids = array_column($data, 'id');
$formula_ids = array_column($data, 'id');
@ -75,7 +79,7 @@ class mFormula extends mBase {
return $data;
return $data;
}
}
public function getAll FormulaTotal($condition) {
public function getFormulaTotal($condition) {
$where = "1=1";
$where = "1=1";
if (!empty($condition)) {
if (!empty($condition)) {
foreach ($condition as $key => $val) {
foreach ($condition as $key => $val) {
@ -94,126 +98,142 @@ class mFormula extends mBase {
return $num;
return $num;
}
}
public function createFormula($uid, $name, $source, $method, $herbs) {
public function getFormulaById($id) {
if (empty($name)) {
return $this->obj->select($this->tbl, array('sql' => '`id`=?', 'vals' => array($id)));
$this->setError('药方名称不能为空');
return false;
}
}
$herbs = json_decode($herbs, true);
if (empty($herbs)) {
public function getHerbs($ids) {
$this->setError('药方药材不能为空');
return $this->obj->selectIn($this->herb_tbl, array('id' => $ids));
return false;
}
}
$data = array(
public function getFormulasHerb($formula_ids) {
'uid' => $uid,
$data = $this->obj->selectIn($this->formula_herb_tbl, array('formula_id' => $formula_ids));
'name' => $name,
if (empty($data)) return array();
'source' => $source,
return array_column($data, null, 'id');
);
}
//录入药方
public function getFormulaHerbData($formula_ids) {
if ($uid == 0) {
$formula_herbs = $this->getFormulasHerb($formula_ids);
$org_herb = array();
foreach ($herbs as $key => $herb) {
$herb_ids = array_column($formula_herbs, 'herb_id');
$org_herb[$key] = array(
$herbs = $this->getHerbs($herb_ids);
'name' => trim($herb['name']),
$herb_arr = array_column($herbs, null, 'id');
'num' => $this->convertToNum(trim($herb['name']), trim($herb['num'])),
//'desc' => trim($herb['desc']),
$formula_arr = array();
foreach ($formula_herbs as $formula_herb) {
$formula_arr[$formula_herb['formula_id']][] = array(
'id' => $formula_herb['herb_id'],
'name' => $herb_arr[$formula_herb['herb_id']]['name'],
'num' => $formula_herb['num'],
//'desc' => $formula_herb['desc'],
'num_str' => $formula_herb['num'] . 'g',
);
);
}
}
return $formula_arr;
//更新原方和用法
$data['original'] = json_encode($org_herb, JSON_UNESCAPED_UNICODE);
$data['method'] = $method;
}
}
//格式化药方对应的药材数据
public function getFormulaInfo($id, $uid) {
$formula_herb = $this->dealForulaHerb($uid, $herbs);
//药方信息
if (!$formula_herb) {
$formula = $this->getFormulaById($id);
$this->writeLog('formula', 'insert_error_log', '添加药方,药材初始化失败|' . $uid . '|' . json_encode($herbs, JSON_UNESCAPED_UNICODE));
if (empty($formula)) {
$this->setError('查询不到此药方');
return false;
return false;
}
}
$id = $this->insertFormula($data);
//自拟药方 判断药方归属
if (!$id) {
if ($uid > 0 & & $formula['uid'] > 0 & & $formula['uid'] != $u id) {
$this->writeLog('formula', 'insert_error_log', '添加药方失败|' . $uid . '|' . json_encode($data, JSON_UNESCAPED_UNICODE) );
$this->setError('这不是你的药方' );
return false;
return false;
}
}
//创建药方对应的药材
//获取药方药材信息
foreach ($formula_herb as & $item) {
$formula_arr = $this->getFormulaHerbData(array($formula['id']));
$item['formula_id'] = $id;
$formulaHerb = $formula_arr[$formula['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;
//原方信息
}
$original = $formula['original'];
unset($formula['original']);
public function saveFormula($uid, $id, $name, $source, $method, $herbs) {
if (empty($original)) {
$herbs = json_decode($herbs, true);
$formula['formula'] = $formulaHerb;
if (empty($herbs)) {
return $formula;
$this->setError('药方药材不能为空');
return false;
}
}
$formula = $this->getFormulaById($id);
$original = json_decode($original, true);
if (empty($formula)) {
$original = array_column($original, null, 'name');
$this->setError('查询不到此药方');
foreach ($formulaHerb as & $v) {
return false;
//药方克重为0 去查询原方
if (isset($original[$v['name']])) {
$v['num_str'] = $original[$v['name']]['num'];
//$v['desc'] = $original[$v['name']]['desc'];
}
}
if ($formula['uid'] > 0 & & $formula['uid'] != $uid) {
$this->setError('这不是你的药方');
return false;
}
}
$data = array() ;
$formula['formula'] = $formulaHerb;
if (!empty($name)) $data['name'] = $name ;
return $formula ;
if (!empty($source)) $data['source'] = $source;
}
//录入药方
public function convertToNum($herb_name, $str, $onlyconertNum = true, $from = '汉') {
if (!$uid) {
$num_list = $GLOBALS['num_list'];
$org_herb = array();
$num_list_arr = array_merge(array_keys($GLOBALS['num_list']['num']), array_keys($GLOBALS['num_list']['unit']));
foreach ($herbs as $key => $herb) {
$org_herb[$key] = array(
$weight_list = $GLOBALS['weight_list'][$from];
'name' => trim($herb['name']),
$weight_list_arr = array_keys($GLOBALS['weight_list'][$from]);
'num' => $this->convertToNum(trim($herb['name']), trim($herb['num'])),
'desc' => trim($herb['desc']),
//计量数字
);
$num_str = str_replace($weight_list_arr, '', $str);
//计量单位
$unit = str_replace($num_list_arr, '', $str);
//计量单位
$unit = str_replace($num_str, '', $unit);
//非数字转换
$num = $num_str;
if (!is_numeric($num) & & $num) {
$num = 0;
$temp = 0;
$length = mb_strlen($num_str);
for ($i = 0; $i < $length; $i++) {
$char = mb_substr($num_str, $i, 1);
if (isset($num_list['num'][$char])) {
$temp += $num_list['num'][$char];
} elseif (isset($num_list['unit'][$char])) {
$temp = ($temp == 0 ? 1 : $temp) * $num_list['unit'][$char];
$num += $temp; // 将临时结果累加到最终结果中
$temp = 0; // 重置临时结果
}
}
}
//更新原方和用法
$num += $temp;
$data['original'] = json_encode($org_herb, JSON_UNESCAPED_UNICODE);
$data['method'] = $method;
}
}
//格式化药方对应的药材数据
//只将汉字数字转换成number
$formula_herb = $this->dealForulaHerb($uid, $herbs);
if ($onlyconertNum) {
if (!$formula_herb) {
return $num . $unit;
$this->writeLog('formula', 'update_error_log', '更新药方,药材初始化失败|' . $uid . '|' . json_encode($herbs, JSON_UNESCAPED_UNICODE));
return false;
}
}
$res = $this->updateFormula($id, $data);
//单位换算 直接换算成克数
if (!$res) {
$weight = isset($weight_list[$unit]) ? $weight_list[$unit] : 0;
$this->writeLog('formula', 'update_error_log', '更新药方失败|' . $uid . '|' . json_encode($data, JSON_UNESCAPED_UNICODE));
if (isset($weight[$herb_name])) $weight = $weight[$herb_name];
return false;
if (!is_numeric($weight)) $weight = 0;
return $num * $weight;
}
}
//批量更新药方药材关联关系
public function getHerb($name) {
foreach ($formula_herb as & $item) {
return $this->obj->select($this->herb_tbl, array('sql' => '`name`=?', 'vals' => array($name)));
$item['formula_id'] = $id;
}
}
$res = $this->mutiUpdateFormulaHerb($id, $formula_herb);
if (!$res) {
public function insertHerb($info) {
$this->writeLog('formula', 'update_error_log', '更新药方,药材关联失败|' . $uid . '|' . $id . '|' . json_encode($formula_herb, JSON_UNESCAPED_UNICODE));
$herb_id = $this->obj->insert($this->herb_tbl, $info);
if (!$herb_id) {
$this->setError('添加药材失败');
return false;
return false;
}
}
return true ;
return $herb_id ;
}
}
public function dealForulaHerb($uid, $herbs) {
public function dealForulaHerb($uid, $herbs) {
@ -266,55 +286,6 @@ class mFormula extends mBase {
return $formula_herb;
return $formula_herb;
}
}
public function convertToNum($herb_name, $str, $onlyconertNum = true, $from = '汉') {
$num_list = $GLOBALS['num_list'];
$num_list_arr = array_merge(array_keys($GLOBALS['num_list']['num']), array_keys($GLOBALS['num_list']['unit']));
$weight_list = $GLOBALS['weight_list'][$from];
$weight_list_arr = array_keys($GLOBALS['weight_list'][$from]);
//计量数字
$num_str = str_replace($weight_list_arr, '', $str);
//计量单位
$unit = str_replace($num_list_arr, '', $str);
//计量单位
$unit = str_replace($num_str, '', $unit);
//非数字转换
$num = $num_str;
if (!is_numeric($num) & & $num) {
$num = 0;
$temp = 0;
$length = mb_strlen($num_str);
for ($i = 0; $i < $length; $i++) {
$char = mb_substr($num_str, $i, 1);
if (isset($num_list['num'][$char])) {
$temp += $num_list['num'][$char];
} elseif (isset($num_list['unit'][$char])) {
$temp = ($temp == 0 ? 1 : $temp) * $num_list['unit'][$char];
$num += $temp; // 将临时结果累加到最终结果中
$temp = 0; // 重置临时结果
}
}
$num += $temp;
}
//只将汉字数字转换成number
if ($onlyconertNum) {
return $num . $unit;
}
//单位换算 直接换算成克数
$weight = isset($weight_list[$unit]) ? $weight_list[$unit] : 0;
if (isset($weight[$herb_name])) $weight = $weight[$herb_name];
if (!is_numeric($weight)) $weight = 0;
return $num * $weight;
}
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)) {
@ -325,44 +296,89 @@ class mFormula extends mBase {
return $id;
return $id;
}
}
public function updateFormula($id, $info) {
public function insertFormulaHerb( $info) {
$res = $this->obj->upda te($this->tbl, $info, array('sql' => '`id`=?', 'vals' => array($id)) );
$res = $this->obj->m utiIns ert ($this->formula_herb_ tbl, $info);
if (!$res) {
if (!$res) {
$this->setError('更新 失败');
$this->setError('添加药方药材 失败');
return false;
return false;
}
}
return true;
return true;
}
}
public function getHerbById($id) {
public function createFormula($uid, $name, $source, $method, $herbs) {
return $this->obj->select($this->herb_tbl, array('sql' => '`id`=?', 'vals' => array($id)));
if (empty($name)) {
$this->setError('药方名称不能为空');
return false;
}
$herbs = json_decode($herbs, true);
if (empty($herbs)) {
$this->setError('药方药材不能为空');
return false;
}
}
public function getHerb($name) {
$data = array(
return $this->obj->select($this->herb_tbl, array('sql' => '`name`=?', 'vals' => array($name)));
'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']),
);
}
}
public function insertHerb($info) {
//更新原方和用法
$herb_id = $this->obj->insert($this->herb_tbl, $info);
$data['original'] = json_encode($org_herb, JSON_UNESCAPED_UNICODE);
if (!$herb_id) {
$data['method'] = $method;
$this->setError('添加药材失败');
}
//格式化药方对应的药材数据
$formula_herb = $this->dealForulaHerb($uid, $herbs);
if (!$formula_herb) {
$this->writeLog('formula', 'insert_error_log', '添加药方,药材初始化失败|' . $uid . '|' . json_encode($herbs, JSON_UNESCAPED_UNICODE));
return false;
return false;
}
}
return $herb_id;
$id = $this->insertFormula($data);
if (!$id) {
$this->writeLog('formula', 'insert_error_log', '添加药方失败|' . $uid . '|' . json_encode($data, JSON_UNESCAPED_UNICODE));
return false;
}
}
public function insertFormulaHerb($info) {
//创建药方对应的药材
$res = $this->obj->mutiInsert($this->formula_herb_tbl, $info);
foreach ($formula_herb as & $item) {
$item['formula_id'] = $id;
}
$res = $this->insertFormulaHerb($formula_herb);
if (!$res) {
if (!$res) {
$this->setError('添加药方药材失败');
$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('更新失败');
return false;
return false;
}
}
return true;
return true;
}
}
public function getFormulaHerb($formula_id) {
return $this->obj->selectAll($this->formula_herb_tbl, array('sql' => '`formula_id`=?', 'vals' => array($formula_id)), 'sort asc ');
}
public function mutiUpdateFormulaHerb($id, $new_data) {
public function mutiUpdateFormulaHerb($id, $new_data) {
$old_data = $this->getFormulaHerb($id);
$old_data = $this->getFormulaHerb($id);
@ -406,198 +422,71 @@ class mFormula extends mBase {
return true;
return true;
}
}
public function getFormulaHerb($formula_id) {
public function saveFormula($uid, $id, $name, $source, $method, $herbs) {
return $this->obj->selectAll($this->formula_herb_tbl, array('sql' => '`formula_id`=?', 'vals' => array($formula_id)), 'sort asc ');
$herbs = json_decode($herbs, true);
}
if (empty($herbs)) {
$this->setError('药方药材不能为空');
public function getFormulasHerb($formula_ids) {
return false;
$data = $this->obj->selectIn($this->formula_herb_tbl, array('formula_id' => $formula_ids));
if (empty($data)) return array();
return array_column($data, null, 'id');
}
public function getUserFormulaByUid($uid, $page_num, $page_size) {
$offset = ($page_num - 1) * $page_size;
$sql = "select s.id,s.uid,s.name,s.source,COALESCE(u.use_num, 0) AS use_num,s.sort,s.create_time from {$this->tbl} as s left join {$this->formula_use_log_tbl} as u on s.id=u.formula_id and u.uid={$uid} where s.uid={$uid} order by use_num desc,s.sort desc,s.id desc limit {$offset},{$page_size}";
return $this->obj->execute($sql, true, true);
}
public function getUserFormulaList($uid, $page_num, $page_size) {
$data = $this->getUserFormulaByUid($uid, $page_num, $page_size);
$formula_ids = array_column($data, 'id');
$formula_arr = $this->getFormulaHerbData($formula_ids);
foreach ($data as & $da) {
unset($da['original']);
$da['create_time'] = date('Y-m-d', strtotime($da['create_time']));
$da['formula'] = $formula_arr[$da['id']];
}
return $data;
}
public function getUserFormulaTotal($uid) {
return $this->obj->count($this->tbl, array('sql' => '`uid`=?', 'vals' => array($uid)));
}
}
public function getUserFormulaInfo($id, $uid) {
//药方信息
$formula = $this->getFormulaById($id);
$formula = $this->getFormulaById($id);
if (empty($formula)) {
if (empty($formula)) {
$this->setError('查询不到此药方');
$this->setError('查询不到此药方');
return false;
return false;
}
}
//自拟药方药方归属
if ($formula['uid'] > 0 & & $formula['uid'] != $uid) {
if ($formula['uid'] != $uid) {
$this->setError('这不是你的药方');
$this->setError('这不是你的药方');
return false;
return false;
}
}
$formulaHerb = array();
$data = array();
if (!empty($name)) $data['name'] = $name;
$formula_arr = $this->getFormulaHerbData(array($formula['id']));
if (!empty($source)) $data['source'] = $source;
foreach ($formula_arr as $ar) {
foreach ($ar as & $v) {
//药方克重为0 去查询原方
if (isset($original[$v['name']])) {
$v['num_str'] = $original[$v['name']]['num'];
$v['desc'] = $original[$v['name']]['desc'];
}
}
$formulaHerb = $ar;
}
$formula['formula'] = $formulaHerb;
return $formula;
}
public function getFormulaByName($uid, $page_num, $page_size, $name) {
$offset = ($page_num - 1) * $page_size;
$where = $uid > 0 ? "s.uid in (0,{$uid})" : "s.uid=0";
if ($name) {
$where .= " and s.`name` like '%{$name}%'";
}
$sql = "select s.id,s.uid,s.name,s.source,s.original,s.method,COALESCE(u.use_num, 0) AS use_num,s.sort from {$this->tbl} as s left join {$this->formula_use_log_tbl} as u on s.id=u.formula_id and u.uid={$uid} where {$where} order by use_num desc,s.sort desc,s.id desc limit {$offset},{$page_size}";
return $this->obj->execute($sql, true, true);
}
public function getSearchFormulaList($uid, $page_num, $page_size, $name = '') {
$data = $this->getFormulaByName($uid, $page_num, $page_size, $name);
$formula_ids = array_column($data, 'id');
$formula_arr = $this->getFormulaHerbData($formula_ids);
foreach ($data as & $da) {
if (isset($formula_arr[$da['id']])) {
$da['formula'] = $formula_arr[$da['id']];
}
$original = array();
if (!empty($da['original'])) {
$original = json_decode($da['original'], true);
$original = array_column($original, null, 'name');
}
unset($da['original']);
foreach ($da['formula'] as & $v) {
//药方克重为0 去查询原方
if (isset($original[$v['name']]) & & $v['num'] == 0) {
$v['num_str'] = $original[$v['name']]['num'];
//$v['desc'] = $original[$v['name']]['desc'];
}
}
}
return $data;
}
public function getFormulaHerbData($formula_ids) {
$formula_herbs = $this->getFormulasHerb($formula_ids);
$herb_ids = array_column($formula_herbs, 'herb_id');
$herbs = $this->getHerbs($herb_ids);
$herb_arr = array_column($herbs, null, 'id');
$formula_arr = array();
//录入药方
foreach ($formula_herbs as $formula_herb ) {
if (!$uid) {
$formula_arr[$formula_herb['formula_id']][] = array(
$org_herb = array();
'id' => $formula_herb['herb_id'],
foreach ($herbs as $key => $herb) {
'name' => $herb_arr[$formula_herb['herb_id']]['name'],
$org_herb[$key] = array(
'num' => $formula_herb['num'] ,
'name' => trim($herb['name']),
//'desc' => $formula_herb['desc'] ,
'num' => $this->convertToNum(trim($herb['name']), trim($herb['num'])),
'num_str' => $formula_herb['num'] . 'g' ,
'desc' => trim($herb['desc']),
);
);
}
}
return $formula_arr;
}
public function getSearchFormulaTotal($uid, $name = '') {
$where = $uid > 0 ? "uid in (0,{$uid})" : "uid=0";
if ($name) {
$where .= " and `name` like '%{$name}%'";
}
$sql = "select count(*) as count from $this->tbl where {$where}";
//更新原方和用法
$res = $this->obj->execute($sql, true, true);
$data['original'] = json_encode($org_herb, JSON_UNESCAPED_UNICODE);
$num = $res[0]['count'];
$data['method'] = $method;
return $num;
}
}
public function getFormulaInfo($id, $uid) {
//格式化药方对应的药材数据
//药方信息
$formula_herb = $this->dealForulaHerb($uid, $herbs);
$formula = $this->getFormulaById($id);
if (!$formula_herb) {
if (empty($formula)) {
$this->writeLog('formula', 'update_error_log', '更新药方,药材初始化失败|' . $uid . '|' . json_encode($herbs, JSON_UNESCAPED_UNICODE));
$this->setError('查询不到此药方');
return false;
return false;
}
}
//自拟药方 判断药方归属
$res = $this->updateFormula($id, $data);
if ($fo rmula['uid'] > 0 & & $formula['uid'] != $uid ) {
if (!$res) {
$this->setError('这不是你的药方' );
$this->writeLog('formula', 'update_error_log', '更新药方失败|' . $uid . '|' . json_encode($data, JSON_UNESCAPED_UNICODE));
return false;
return false;
}
}
if (!empty($formula['original'])) {
//批量更新药方药材关联关系
$original = json_decode($formula['original'], true);
foreach ($formula_herb as & $item) {
$original = array_column($original, null, 'name');
$item['formula_id'] = $id;
}
unset($formula['original']);
$formulaHerb = array();
$formula_arr = $this->getFormulaHerbData(array($formula['id']));
foreach ($formula_arr as $ar) {
foreach ($ar as & $v) {
//药方克重为0 去查询原方
if (isset($original[$v['name']])) {
$v['num_str'] = $original[$v['name']]['num'];
//$v['desc'] = $original[$v['name']]['desc'];
}
}
$formulaHerb = $ar;
}
}
$res = $this->mutiUpdateFormulaHerb($id, $formula_herb);
$formula['formula'] = $formulaHerb;
if (!$res) {
$this->writeLog('formula', 'update_error_log', '更新药方,药材关联失败|' . $uid . '|' . $id . '|' . json_encode($formula_herb, JSON_UNESCAPED_UNICODE));
return $formula ;
return false;
}
}
public function getFormulaById($id) {
return true;
return $this->obj->select($this->tbl, array('sql' => '`id`=?', 'vals' => array($id)));
}
}
public function getHerbs($ids ) {
public function getHerbById($id) {
return $this->obj->selectIn($this->herb_tbl, array('id' => $ids ));
return $this->obj->select($this->herb_tbl, array('sql' => '`id`=?', 'vals' => array($id)));
}
}
public function getFormulaByIds($ids) {
public function getFormulaByIds($ids) {