From f59bb583f742edaa443acdfae620fb5acff70f98 Mon Sep 17 00:00:00 2001 From: pengda <10266652509@qq.com> Date: Tue, 15 Oct 2024 18:22:16 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AE=A1=E9=87=8F=E5=8D=95=E4=BD=8D=E8=BD=AC?= =?UTF-8?q?=E6=8D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- control/index.php | 34 +++++++++++++++++++++ data/dFormula.php | 22 +++++++++++++ model/mFormula.php | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 146 insertions(+) diff --git a/control/index.php b/control/index.php index c1639f1..cd256b7 100644 --- a/control/index.php +++ b/control/index.php @@ -327,4 +327,38 @@ class index extends publicBase { $this->view['data'] = array($data); $this->setViewTpl('index/export_pdf.html'); } + + public function ajax_unit_conv() { + $uinfo = $this->_check_login(); + + $name = trim($this->post('name')) ? trim($this->post('name')) : '两'; + + $m_formula = new mFormula(); + $unit = $m_formula->getUnitByName($name); + + $unit_conv = $m_formula->getUnitConvList($unit['id']); + + $user_conv = $m_formula->getUserConv($uinfo['uid'], $unit['id']); + foreach ($unit_conv as &$conv) { + $conv['conv_txt'] = "1{$unit['name']}={$conv['num']}g"; + if($user_conv['conv_id'] == $conv['id']){ + $user_conv['conv_txt'] = "当前1{$unit['name']}={$conv['num']}g"; + } + } + + $this->ajax_json(true, '获取成功', array('user_conv' => $user_conv, 'unit_conv' => $unit_conv)); + } + + public function ajax_set_user_conv() { + $uinfo = $this->_check_login(); + + $unit_id = trim($this->post('unit_id')) + 0; + $conv_id = trim($this->post('conv_id')) + 0; + + $m_formula = new mFormula(); + $res = $m_formula->saveUserConv($uinfo['uid'], $unit_id, $conv_id); + if (!$res) $this->ajax_json(false, $m_formula->getError()); + + $this->ajax_json(true, '保存成功'); + } } diff --git a/data/dFormula.php b/data/dFormula.php index bcbf2dd..518a5bb 100644 --- a/data/dFormula.php +++ b/data/dFormula.php @@ -44,6 +44,25 @@ class dFormula extends dBase { 'formula_id', 'use_num', ), + 'tcm_unit_list' => array( + 'id', + 'name', + ), + 'tcm_unit_conv' => array( + 'id', + 'uid', + 'unit_id', + 'num', + 'from', + 'is_delete', + 'create_date', + ), + 'tcm_user_conv' => array( + 'id', + 'uid', + 'unit_id', + 'conv_id', + ), ); protected $primary_keys = array( @@ -52,6 +71,9 @@ class dFormula extends dBase { 'tcm_herb' => 'id', 'tcm_collect_log' => 'id', 'tcm_formula_use_log' => 'id', + 'tcm_unit_list' => 'id', + 'tcm_unit_conv' => 'id', + 'tcm_user_conv' => 'id', ); } diff --git a/model/mFormula.php b/model/mFormula.php index f994194..58b1e44 100644 --- a/model/mFormula.php +++ b/model/mFormula.php @@ -12,6 +12,9 @@ class mFormula extends mBase { private $herb_tbl; private $collect_log_tbl; private $formula_use_log_tbl; + private $unit_list; + private $unit_conv; + private $user_conv; public function __construct() { $this->obj = new dFormula(); @@ -20,6 +23,9 @@ class mFormula extends mBase { $this->herb_tbl = 'tcm_herb'; $this->collect_log_tbl = 'tcm_collect_log'; $this->formula_use_log_tbl = 'tcm_formula_use_log'; + $this->unit_list = 'tcm_unit_list'; + $this->unit_conv = 'tcm_unit_conv'; + $this->user_conv = 'tcm_user_conv'; } public function searchFormulaList($condition, $page_num, $page_size) { @@ -59,6 +65,9 @@ class mFormula extends mBase { foreach ($da['formula'] as &$v) { if (isset($original[$v['name']])) { + $num = $this->convByUserChoice($uid, $original[$v['name']]['num']); + if ($num) $v['num'] = $num; + $v['num_str'] = $original[$v['name']]['num']; $v['desc'] = $original[$v['name']]['desc']; } @@ -70,6 +79,25 @@ class mFormula extends mBase { return $data; } + public function convByUserChoice($uid, $num_str) { + $unit_name = preg_replace('/\d/', '', $num_str); + $unit = $this->getUnitByName($unit_name); + if (!$unit) return false; + + $user_conv = $this->getUserConv($uid, $unit['id']); + if (!$user_conv) return false; + + $unit_conv = $this->getUnitConvById($user_conv['conv_id']); + if (!$unit_conv) return false; + + $unit_num = filter_var($num_str, FILTER_SANITIZE_NUMBER_INT); + + $num = $unit_num * $unit_conv['num']; + if (!$num) return false; + + return $num; + } + public function searchFormulaTotal($condition) { $where = "1=1"; if (!empty($condition)) { @@ -266,6 +294,9 @@ class mFormula extends mBase { $original = array_column($original, null, 'name'); foreach ($formulaHerb as &$v) { if (isset($original[$v['name']])) { + $num = $this->convByUserChoice($uid, $original[$v['name']]['num']); + if ($num) $v['num'] = $num; + $v['num_str'] = $original[$v['name']]['num']; $v['desc'] = $original[$v['name']]['desc']; } @@ -611,4 +642,63 @@ class mFormula extends mBase { return $this->obj->count($this->collect_log_tbl); } + public function getUnitByName($name) { + return $this->obj->select($this->unit_list, array('sql' => '`name`=?', 'vals' => array($name))); + } + + public function getUnitConvById($id) { + return $this->obj->select($this->unit_conv, array('sql' => '`id`=? and `is_delete`=?', 'vals' => array($id, 0))); + } + + public function getUnitConvList($unit_id) { + return $this->obj->selectAll($this->unit_conv, array('sql' => '`unit_id`=? and `is_delete`=?', 'vals' => array($unit_id, 0))); + } + + public function getUserConv($uid, $unit_id) { + return $this->obj->select($this->user_conv, array('sql' => '`uid`=? and `unit_id`=?', 'vals' => array($uid, $unit_id))); + } + + public function saveUserConv($uid, $unit_id, $conv_id) { + if (!$unit_id || !$conv_id) { + $this->setError('参数错误'); + return false; + } + + $user_conv = $this->getUserConv($uid, $unit_id); + if ($user_conv) { + $res = $this->updateUserConv($user_conv['id'], array('conv_id' => $conv_id)); + if (!$res) { + $this->setError('更新失败'); + return false; + } + return true; + } + + $res = $this->insertUserConv(array('uid' => $uid, 'unit_id' => $unit_id, 'conv_id' => $conv_id)); + if (!$res) { + $this->setError('设置失败'); + return false; + } + return true; + } + + public function updateUserConv($id, $info) { + $res = $this->obj->update($this->user_conv, $info, array('sql' => '`id`=?', 'vals' => array($id))); + if (!$res) { + $this->writeLog('formula', 'save_error_log', '更新用户计量单位转换设置失败|' . $id . '|' . json_encode($info, JSON_UNESCAPED_UNICODE)); + return false; + } + + return true; + } + + public function insertUserConv($info) { + $res = $this->obj->insert($this->user_conv, $info); + if (!$res) { + $this->writeLog('formula', 'save_error_log', '添加用户计量单位转换设置失败|' . json_encode($info)); + return false; + } + + return true; + } } \ No newline at end of file