Browse Source

修改bug

chenbo 4 years ago
parent
commit
a1551fb0e7

+ 4 - 9
lib/day_k_qfq.class.php

@@ -16,18 +16,13 @@ class Day_k_qfq {
     }
 
 
-    static public function addOrUpdateByCode($attrs)
+    static public function addOrUpdateByCodeDate($attrs)
     {
         if (empty($attrs)) throw new Exception('参数不能为空', 101);
+        if (empty($attrs['code'])) throw new Exception('股票代码不能为空', 102);
+        if (empty($attrs['date'])) throw new Exception('日期不能为空', 103);
         $Table_day_k_qfq = new Table_day_k_qfq();
-        //  是否已经插入一条数据
-        $rs = $Table_day_k_qfq->getInfoByCodeAndDate($attrs['code'],$attrs['date']);
-        if (empty($rs)) {
-            $id = $Table_day_k_qfq->add($attrs);
-        } else {
-            $id = $rs['id'];
-            $Table_day_k_qfq->update($id, $attrs);
-        }
+        $id = $Table_day_k_qfq->addOrUpdateByCodeDate($attrs);
         return $id;
     }
 

+ 1 - 1
lib/input_162_day_k_qfq.class.php

@@ -69,7 +69,7 @@ class Input_162_day_k_qfq
             $a['amount'] = (int)$d[5];
 //            $a['turnover'] =floatval($d[6]);
             $a['turnover'] =empty(floatval($d[6]))?"0.00":floatval($d[6]);
-            Day_k_qfq::addOrUpdateByCode($a);
+            Day_k_qfq::addOrUpdateByCodeDate($a);
         }
         return $msg;
     }

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

@@ -92,6 +92,32 @@ abstract class Table {
     }
 
 
+    //此项目每个表都有code字段,大部分表有date字段,放在基类里面统一处理
+    public function getInfoByCodeAndDate($code, $date){
+
+        //查询语句必须用sql_check_input检查参数
+        $code = trim($code);
+        $code = $this->pdo->sql_check_input(array('string', $code));
+
+        //查询语句必须用sql_check_input检查参数
+        $date = trim($date);
+        $date = $this->pdo->sql_check_input(array('number', $date));
+
+        $sql = "select * from ". $this->table_fullname ." where ".$this->table_name."_code = $code and ".$this->table_name."_date = $date 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时,返回全部结果(适用于无需分页的情况)
     //
@@ -182,6 +208,23 @@ abstract class Table {
         return $id;
     }
 
+    //(code,date)为唯一标识,不存在则add,否则update
+    public function addOrUpdateByCodeDate($attrs)
+    {
+        if (empty($attrs) || empty($attrs['code']) || empty($attrs['date'])) {
+            return 0;
+        }
+
+        $rs = $this->getInfoByCodeAndDate($attrs['code'], $attrs['date']);
+        if (empty($rs)) {
+            $id = $this->add($attrs);
+        } else {
+            $id = $rs['id'];
+            $this->update($id, $attrs);
+        }
+        return $id;
+    }
+
 	//--------------------------------------------------
 	//------------依赖于字段设置的常见方法,可以重载----
 	//--------------------------------------------------

+ 0 - 23
lib/table/table_day_k_qfq.class.php

@@ -46,28 +46,5 @@ class Table_day_k_qfq extends Table {
         return $this->pdo->sqlinsert($this->table_fullname, $param);
     }
 
-
-    public function getInfoByCodeAndDate($code,$date){
-
-        //查询语句必须用sql_check_input检查参数
-        $code = trim($code);
-        $code = $this->pdo->sql_check_input(array('string', $code));
-        $date = $this->pdo->sql_check_input(array('number', trim($date)));
-
-        $sql = "select * from ". $this->table_fullname ." where day_k_qfq_code = ".$code." and  day_k_qfq_date=".$date ." 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;
-        }
-    }
-
-
 }
 ?>