From 59fce2bf8d0d5a7c1b5ca2f1befe14af834fe6df Mon Sep 17 00:00:00 2001 From: pengda <10266652509@qq.com> Date: Thu, 10 Oct 2024 18:34:45 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=8D=AF=E6=9D=90?= =?UTF-8?q?=E8=AF=B4=E6=98=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- control/index.php | 8 +- model/mFormula.php | 142 ++++++++++++++++++++++++++++----- view/templates/admin/formula_add.html | 4 +- view/templates/admin/formula_list.html | 4 +- 4 files changed, 128 insertions(+), 30 deletions(-) diff --git a/control/index.php b/control/index.php index 709d72f..afd444c 100644 --- a/control/index.php +++ b/control/index.php @@ -79,7 +79,7 @@ class index extends publicBase { if ($content) $condition['name'] = $content; $mformula = new mFormula(); - $list = $mformula->getFormulaList($condition, $page_num, $page_size); + $list = $mformula->getUserFormulaList($condition, $page_num, $page_size); $total = $mformula->getFormulaTotal($condition); $rdata = array( @@ -116,13 +116,13 @@ class index extends publicBase { $page_size = $this->post('page_size') ? $this->post('page_size') : 20; $condition = array(); - $condition['uid'] = array(0, $uinfo['uid']); + $condition['uid'] = $uinfo['uid']; $condition['is_delete'] = 0; if ($content) $condition['name'] = $content; $mformula = new mFormula(); - $list = $mformula->getFormulaList($condition, $page_num, $page_size); - $total = $mformula->getFormulaTotal($condition); + $list = $mformula->searchFormulaList($condition, $page_num, $page_size); + $total = $mformula->searchFormulaTotal($condition); $rdata = array( 'total' => $total, diff --git a/model/mFormula.php b/model/mFormula.php index 0fba1ba..a6e1ad1 100644 --- a/model/mFormula.php +++ b/model/mFormula.php @@ -22,36 +22,29 @@ class mFormula extends mBase { $this->formula_use_log_tbl = 'tcm_formula_use_log'; } - public function getFormulaByCondition($condition, $page_num, $page_size) { + public function searchFormulaList($condition, $page_num, $page_size) { $offset = ($page_num - 1) * $page_size; + $uid = 0; $where = "1=1"; - $use_limit = ""; if (!empty($condition)) { foreach ($condition as $key => $val) { if ($key == 'name') { $where .= " and s.{$key} like '%{$val}%'"; - } elseif ($key == 'uid' && is_array($val)) { + } elseif ($key == 'uid') { + $uid = $val; + $where .= " and s.{$key} in (0,{$val})"; + } elseif (is_array($val)) { $val = implode(',', $val); $where .= " and s.{$key} in ({$val})"; - - //兼容查药方只计算当前用户对药方的使用次数 - $use_limit .= " and u.uid in ({$val})"; - } elseif ($key == 'is_delete' && is_array($val)) { - $val = implode(',', $val); - $where .= " and {$key} in ({$val})"; } else { $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 {$use_limit} where {$where} group by s.id order by use_num desc,s.sort desc,s.id desc limit {$offset},{$page_size}"; - return $this->obj->execute($sql, true, true); - } - - public function getFormulaList($condition, $page_num, $page_size) { - $data = $this->getFormulaByCondition($condition, $page_num, $page_size); + $sql = "select s.*,COALESCE(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 and u.uid={$uid} where {$where} group by s.id order by use_num desc,s.sort desc,s.id desc limit {$offset},{$page_size}"; + $data = $this->obj->execute($sql, true, true); $formula_ids = array_column($data, 'id'); $formula_arr = $this->getFormulaHerbData($formula_ids); @@ -67,7 +60,7 @@ class mFormula extends mBase { foreach ($da['formula'] as &$v) { if (isset($original[$v['name']])) { $v['num_str'] = $original[$v['name']]['num']; - //$v['desc'] = $original[$v['name']]['desc']; + $v['desc'] = $original[$v['name']]['desc']; } } } @@ -77,16 +70,61 @@ class mFormula extends mBase { return $data; } - public function getFormulaTotal($condition) { + public function searchFormulaTotal($condition) { $where = "1=1"; if (!empty($condition)) { foreach ($condition as $key => $val) { if ($key == 'name') { $where .= " and {$key} like '%{$val}%'"; - } elseif ($key == 'uid' && is_array($val)) { + } elseif ($key == 'uid') { + $where .= " and {$key} in (0,{$val})"; + } elseif (is_array($val)) { $val = implode(',', $val); $where .= " and {$key} in ({$val})"; - } elseif ($key == 'is_delete' && is_array($val)) { + } else { + $where .= " and {$key}={$val}"; + } + } + } + + $sql = "select count(*) as count from $this->tbl where {$where}"; + $res = $this->obj->execute($sql, true, true); + $num = $res[0]['count']; + + return $num; + } + + public function getUserFormulaList($condition, $page_num, $page_size) { + $offset = ($page_num - 1) * $page_size; + + $uid = 0; + $where = "1=1"; + if (!empty($condition)) { + foreach ($condition as $key => $val) { + if ($key == 'uid') $uid = $val; + + if ($key == 'name') { + $where .= " and s.{$key} like '%{$val}%'"; + } elseif (is_array($val)) { + $val = implode(',', $val); + $where .= " and s.{$key} in ({$val})"; + } else { + $where .= " and s.{$key}={$val}"; + } + } + } + + $sql = "select s.*,COALESCE(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 and u.uid={$uid} where {$where} group by s.id order by use_num desc,s.sort desc,s.id desc limit {$offset},{$page_size}"; + return $this->obj->execute($sql, true, true); + } + + public function getUserFormulaTotal($condition) { + $where = "1=1"; + if (!empty($condition)) { + foreach ($condition as $key => $val) { + if ($key == 'name') { + $where .= " and {$key} like '%{$val}%'"; + } elseif (is_array($val)) { $val = implode(',', $val); $where .= " and {$key} in ({$val})"; } else { @@ -102,6 +140,67 @@ class mFormula extends mBase { return $num; } + public function getFormulaList($condition, $page_num, $page_size) { + $offset = ($page_num - 1) * $page_size; + + $where = "1=1 "; + if (!empty($condition)) { + foreach ($condition as $key => $val) { + if ($key == 'name') { + $where .= " and {$key} like '%{$val}%'"; + } elseif (is_array($val)) { + $val = implode(',', $val); + $where .= " and {$key} in ({$val})"; + } else { + $where .= " and {$key}={$val}"; + } + } + } + + $data = $this->obj->selectAll($this->tbl, array('sql' => $where, 'vals' => array()), 'id desc ', array($offset, $page_size)); + + $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']]; + + //原方信息 + if (!empty($da['original'])) { + $original = json_decode($da['original'], true); + $original = array_column($original, null, 'name'); + + foreach ($da['formula'] as &$v) { + if (isset($original[$v['name']])) { + $v['num_str'] = $original[$v['name']]['num']; + $v['desc'] = $original[$v['name']]['desc']; + } + } + } + unset($da['original']); + } + + return $data; + } + + public function getFormulaTotal($condition) { + $where = "1=1 "; + if (!empty($condition)) { + foreach ($condition as $key => $val) { + if ($key == 'name') { + $where .= " and {$key} like '%{$val}%'"; + } elseif (is_array($val)) { + $val = implode(',', $val); + $where .= " and {$key} in ({$val})"; + } else { + $where .= " and {$key}={$val}"; + } + } + } + + return $this->obj->count($this->tbl, array('sql' => $where, 'vals' => array())); + } + public function getFormulaById($id) { return $this->obj->select($this->tbl, array('sql' => '`id`=?', 'vals' => array($id))); } @@ -168,7 +267,7 @@ class mFormula extends mBase { foreach ($formulaHerb as &$v) { if (isset($original[$v['name']])) { $v['num_str'] = $original[$v['name']]['num']; - //$v['desc'] = $original[$v['name']]['desc']; + $v['desc'] = $original[$v['name']]['desc']; } } @@ -246,6 +345,7 @@ class mFormula extends mBase { foreach ($herbs as $key => $item) { $name = trim($item['name']); $num = trim($item['num']); + $desc = trim($item['desc']); //自拟药方 克重不能为0 if ($uid > 0 && $num == 0) { @@ -260,7 +360,7 @@ class mFormula extends mBase { if ($herb) { $temp['herb_id'] = $herb['id']; } else { - $temp['herb_id'] = $this->insertHerb(array('uid' => $uid, 'name' => $name)); + $temp['herb_id'] = $this->insertHerb(array('uid' => $uid, 'name' => $name, 'desc' => $desc)); if (!$temp['herb_id']) { $this->setError('添加药材失败'); return false; diff --git a/view/templates/admin/formula_add.html b/view/templates/admin/formula_add.html index 10db603..0244fe8 100644 --- a/view/templates/admin/formula_add.html +++ b/view/templates/admin/formula_add.html @@ -69,11 +69,11 @@