diff --git a/model/mFormula.php b/model/mFormula.php index 28b2f78..0a3edca 100644 --- a/model/mFormula.php +++ b/model/mFormula.php @@ -22,127 +22,260 @@ class mFormula extends mBase { $this->formula_use_log_tbl = 'tcm_formula_use_log'; } - public function createCase($name, $source, $method, $herbs) { + public function createFormula($uid, $name, $source, $method, $herbs) { if (empty($name)) { $this->setError('药方名称不能为空'); return false; } - if (empty($source)) { - $this->setError('药方来源不能为空'); - return false; - } - if (empty($method)) { - $this->setError('药方用法不能为空'); - return false; - } + $herbs = json_decode($herbs, true); if (empty($herbs)) { $this->setError('药方药材不能为空'); return false; } - //保存原方 - $original = json_encode($herbs, JSON_UNESCAPED_UNICODE); + $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->obj->insert($this->tbl, array('name' => $name, 'source' => $source, 'original' => $original, 'method' => $method)); + $id = $this->insertFormula($data); if (!$id) { - $this->setError('添加失败'); + $this->writeLog('formula', 'insert_error_log', '添加药方失败|' . $uid . '|' . json_encode($data, JSON_UNESCAPED_UNICODE)); return false; } - //格式化药方对应的药材数据 - $case_herb = $this->formatCaseHerb($id, $herbs); //创建药方对应的药材 - $this->createCaseHerb($case_herb); + 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 updateCase($id, $name, $source, $original, $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 (!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 (!$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->setError('更新失败'); + $this->writeLog('formula', 'update_error_log', '更新药方失败|' . $uid . '|' . json_encode($data, JSON_UNESCAPED_UNICODE)); return false; } - //格式化药方对应的药材数据 - $case_herb = $this->formatCaseHerb($id, $herbs); - //更新药方对应的药材 - $this->compareCaseHerb($id, $case_herb); + //批量更新药方药材关联关系 + 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 formatCaseHerb($formula_id, $herbs) { - $case_herb = array(); + public function dealForulaHerb($uid, $herbs) { + $formula_herb = array(); + + $herb_ids = array(); foreach ($herbs as $key => $item) { + + + $name = trim($item['name']); + $num = trim($item['num']); + + //自拟药方 克重不能为0 + if ($uid != 0 && $num == 0) { + $this->setError("{$name}克重不能为零"); + return false; + } + $temp = array(); - $temp['name'] = $item['name']; - $temp['formula_id'] = $formula_id; - $temp['num'] = $this->convertToNum('汉', $item['num'], $item['name']); - $temp['sort'] = $key; - $herb = $this->getHerbByName($item['name']); + //格式化药材id + $herb = $this->getHerb($name); if ($herb) { $temp['herb_id'] = $herb['id']; } else { - $temp['herb_id'] = $this->createHerb(array('name' => $item['name'], 'desc' => $item['desc'])); + $temp['herb_id'] = $this->insertHerb(array('uid' => $uid, 'name' => $name)); + if (!$temp['herb_id']) { + $this->writeLog('formula', 'update_error_log', '添加药材失败|' . $uid . '|' . $name); + return false; + } } - $case_herb[] = $temp; + + if (isset($herb_ids[$temp['herb_id']])) { + $this->setError("药材不能重复"); + return false; + } + $herb_ids[$temp['herb_id']] = $temp['herb_id']; + + //录药方上传的原方需要 单位换算 + $temp['num'] = $num; + if ($uid == 0) { + //目前先不换算 默认为0 使用原方用量 + $temp['num'] = 0; + //$temp['num'] = $this->convertToNum($name, $num, false); + } + + $temp['sort'] = $key; + + $formula_herb[] = $temp; } - return $case_herb; + return $formula_herb; } - public function convertToNum($from, $str, $herb_name) { + 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; + } - $num = 0; - $temp = 0; + public function insertFormula($info) { + $id = $this->obj->insert($this->tbl, $info); + if (empty($id)) { + $this->setError('添加药材失败'); + return false; + } - $num_str = str_replace($weight_list_arr, '', $str); - $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; // 重置临时结果 - } + 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; } - $num += $temp; + return true; + } - return $num * $weight; + public function getHerbById($id) { + return $this->obj->select($this->herb_tbl, array('sql' => '`id`=?', 'vals' => array($id))); } - public function getHerbByName($name) { + public function getHerb($name) { return $this->obj->select($this->herb_tbl, array('sql' => '`name`=?', 'vals' => array($name))); } - public function createHerb($info) { + public function insertHerb($info) { $herb_id = $this->obj->insert($this->herb_tbl, $info); - if (empty($herb_id)) { + if (!$herb_id) { $this->setError('添加药材失败'); return false; } @@ -150,7 +283,7 @@ class mFormula extends mBase { return $herb_id; } - public function createCaseHerb($info) { + public function insertFormulaHerb($info) { $res = $this->obj->mutiInsert($this->formula_herb_tbl, $info); if (!$res) { $this->setError('添加药方药材失败'); @@ -160,7 +293,7 @@ class mFormula extends mBase { return true; } - public function compareCaseHerb($id, $new_data) { + public function mutiUpdateFormulaHerb($id, $new_data) { $old_data = $this->getFormulaHerb($id); $old_num = count($old_data); @@ -234,6 +367,10 @@ class mFormula extends mBase { 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); @@ -242,7 +379,7 @@ class mFormula extends mBase { return false; } //uid==0 不判断药方归属 - if ($uid > 0 && $formula['uid'] != $uid) { + if ($uid != 0 && $formula['uid'] != $uid) { $this->setError('这不是你的药方'); return false; } @@ -267,23 +404,22 @@ class mFormula extends mBase { return $formula; } - public function getUserFormulaTotal($uid) { - return $this->obj->count($this->tbl, array('sql' => '`uid`=?', 'vals' => array($uid))); - } - public function getFormulaByName($uid, $page_num, $page_size, $name) { $offset = ($page_num - 1) * $page_size; - $where = "s.uid in (0,{$uid})"; + $where = "1=1"; + if ($uid != 0) { + $where .= " and s.uid in (0,{$uid})"; + } if ($name) { $where .= " and s.`name` like '%{$name}%'"; } - $sql = "select s.id,s.uid,s.name,s.source,s.original,s.method,u.use_num,s.sort from {$this->tbl} as s left join {$this->formula_use_log_tbl} as u on s.id=u.formula_id where {$where} order by u.use_num desc,s.sort desc limit {$offset},{$page_size}"; + $sql = "select s.id,s.uid,s.name,s.source,s.original,s.method,u.use_num,s.sort from {$this->tbl} as s left join {$this->formula_use_log_tbl} as u on s.id=u.formula_id where {$where} order by u.use_num desc,s.sort desc,s.id desc limit {$offset},{$page_size}"; return $this->obj->execute($sql, true, true); } - public function getFormulaList($uid, $page_num, $page_size, $name) { + public function getFormulaList($uid, $page_num, $page_size, $name = '') { $data = $this->getFormulaByName($uid, $page_num, $page_size, $name); $formula_ids = array_column($data, 'id'); @@ -306,7 +442,7 @@ class mFormula extends mBase { //药方克重为0 去查询原方 if (isset($original[$v['name']]) && $v['num'] == 0) { $v['num_str'] = $original[$v['name']]['num']; - $v['desc'] = $original[$v['name']]['desc']; + //$v['desc'] = $original[$v['name']]['desc']; } } } @@ -327,18 +463,23 @@ class mFormula extends mBase { 'id' => $formula_herb['herb_id'], 'name' => $herb_arr[$formula_herb['herb_id']]['name'], 'num' => $formula_herb['num'], - 'desc' => $formula_herb['desc'], + //'desc' => $formula_herb['desc'], 'num_str' => $formula_herb['num'] . 'g', ); } return $formula_arr; } - public function getFormulaTotal($uid, $name) { - $sql = "select count(*) as count from $this->tbl where uid in (0,{$uid})"; + public function getFormulaTotal($uid, $name = '') { + $where = "1=1"; + if ($uid != 0) { + $where .= " and s.uid in (0,{$uid})"; + } if ($name) { - $sql .= " and `name` like '%{$name}%'"; + $where .= " and `name` like '%{$name}%'"; } + + $sql = "select count(*) as count from $this->tbl where {$where}"; $res = $this->obj->execute($sql, true, true); $num = $res[0]['count']; @@ -354,7 +495,7 @@ class mFormula extends mBase { } //uid==0 不判断药方归属 - if ($uid > 0 && $formula['uid'] > 0 && $formula['uid'] != $uid) { + if ($uid != 0 && $formula['uid'] > 0 && $formula['uid'] != $uid) { $this->setError('这不是你的药方'); return false; } @@ -373,7 +514,7 @@ class mFormula extends mBase { //药方克重为0 去查询原方 if (isset($original[$v['name']])) { $v['num_str'] = $original[$v['name']]['num']; - $v['desc'] = $original[$v['name']]['desc']; + //$v['desc'] = $original[$v['name']]['desc']; } } @@ -397,13 +538,10 @@ class mFormula extends mBase { return $this->obj->selectIn($this->tbl, array('id' => $ids)); } - public function updateCaseUseLog($uid, $formula_id) { + public function updateFormulaUseLog($uid, $formula_id) { $res = $this->obj->increase($this->formula_use_log_tbl, array('sql' => '`uid`=? and `formula_id`=?', 'vals' => array($uid, $formula_id)), 'use_num'); if (!$res) { - $tool_obj = new qTool(); - $tool_obj->trackLog('tcm', $uid . "|" . $formula_id, sprintf(LOG_TRACK_SAVE_PATH, date('Y-m-d'), 'tcm_formula_use_log')); - - $this->setError('药方使用次数更新失败'); + $this->setError('添加药方药材失败'); return false; }