瀏覽代碼

修改bug

chenbo 4 年之前
父節點
當前提交
9d8df4ff7f
共有 3 個文件被更改,包括 40 次插入35 次删除
  1. 2 9
      lib/stock.class.php
  2. 38 0
      lib/table/table.class.php
  3. 0 26
      lib/table/table_stock.class.php

+ 2 - 9
lib/stock.class.php

@@ -44,16 +44,9 @@ class Stock {
     static public function addOrUpdateByCode($attrs)
     {
         if (empty($attrs)) throw new Exception('参数不能为空', 101);
+        if (empty($attrs['code'])) throw new Exception('股票代码不能为空', 102);
         $Table_stock = new Table_stock();
-        $rs = $Table_stock->getInfoByCode($attrs['code']);
-
-        if (empty($rs)) {
-            $id = $Table_stock->add($attrs);
-        } else {
-            $id = $rs['id'];
-            $Table_stock->update($id, $attrs);
-        }
-
+        $id = $Table_stock->addOrUpdateByCode($attrs);
         return $id;
     }
 

+ 38 - 0
lib/table/table.class.php

@@ -70,6 +70,27 @@ abstract class Table {
         }
 	}
 
+    //此项目每个表都有code字段,所以放在基类里面
+    public function getInfoByCode($code){
+
+        //查询语句必须用sql_check_input检查参数
+        $code = trim($code);
+        $code = $this->pdo->sql_check_input(array('string', $code));
+
+        $sql = "select * from ". $this->table_fullname ." where ".$this->table_name."_code = $code limit 1";
+
+        $rs = $this->pdo->sqlQuery($sql);
+        $r  = array();
+        if($rs){
+            foreach($rs as $key => $val){
+                $r[$key] = $this->dataToAttr($val);
+            }
+            return $r[0];
+        }else{
+            return $r;
+        }
+    }
+
 
     //获取列表(分页)
     //$count、$page和$pagesize都为0时,返回全部结果(适用于无需分页的情况)
@@ -144,6 +165,23 @@ abstract class Table {
         return $r;
     }
 
+    //code不存在则add,否则update
+    public function addOrUpdateByCode($attrs)
+    {
+        if (empty($attrs) || empty($attrs['code'])) {
+            return 0;
+        }
+
+        $rs = $this->getInfoByCode($attrs['code']);
+        if (empty($rs)) {
+            $id = $this->add($attrs);
+        } else {
+            $id = $rs['id'];
+            $this->update($id, $attrs);
+        }
+        return $id;
+    }
+
 	//--------------------------------------------------
 	//------------依赖于字段设置的常见方法,可以重载----
 	//--------------------------------------------------

+ 0 - 26
lib/table/table_stock.class.php

@@ -104,31 +104,5 @@ class Table_stock extends Table {
         }
     }
 
-
-    /***
-     * @param $code
-     * @return array|mixed
-     * wanggangtao
-     */
-    public function getInfoByCode($code){
-
-        //查询语句必须用sql_check_input检查参数
-        $code = trim($code);
-        $code = $this->pdo->sql_check_input(array('string', $code));
-
-        $sql = "select * from ". $this->table_fullname ." where stock_code = $code limit 1";
-
-        $rs = $this->pdo->sqlQuery($sql);
-        $r  = array();
-        if($rs){
-            foreach($rs as $key => $val){
-                $r[$key] = $this->dataToAttr($val);
-            }
-            return $r[0];
-        }else{
-            return $r;
-        }
-    }
-
 }
 ?>