wanggangtao лет назад: 5
Родитель
Сommit
b06efa97cb

+ 1 - 1
data/get_boniu_index_csi300.php

@@ -15,4 +15,4 @@ require('../init.php');
  */
 
 
-sina::get_boniu_index_csi300();//默认是今天
+Sina::get_boniu_index_csi300();//默认是今天

+ 1 - 1
data/get_boniu_index_gem.php

@@ -9,4 +9,4 @@
 require('../init.php');
 
 
-sina::get_boniu_index_gem();//默认是今天
+Sina::get_boniu_index_gem();//默认是今天

+ 1 - 1
data/get_boniu_index_kc50.php

@@ -15,4 +15,4 @@ require('../init.php');
  */
 
 
-sina::get_boniu_index_kc50();//默认是今天
+Sina::get_boniu_index_kc50();//默认是今天

+ 1 - 1
data/get_boniu_index_scz.php

@@ -12,4 +12,4 @@ require('../init.php');
 
 
 
-sina::get_boniu_index_scz();//默认是今天
+Sina::get_boniu_index_scz();//默认是今天

+ 1 - 1
data/get_boniu_index_sh.php

@@ -15,4 +15,4 @@ require('../init.php');
  */
 
 
-sina::get_boniu_index_sh();//默认是今天
+Sina::get_boniu_index_sh();//默认是今天

+ 1 - 1
data/get_boniu_index_sh50.php

@@ -15,4 +15,4 @@ require('../init.php');
  */
 
 
-sina::get_boniu_index_sh50();//默认是今天
+Sina::get_boniu_index_sh50();//默认是今天

+ 1 - 1
data/get_day_k_base.php

@@ -16,4 +16,4 @@ require('../init.php');
 
 $date=date('Ym');
 $code=601006;
-sina::get_day_k_base($code,$date);//默认是今天
+Sina::get_day_k_base($code,$date);//默认是今天

+ 19 - 0
data/get_history_trade_data.php

@@ -0,0 +1,19 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: 王刚涛
+ * Date: 2020/11/24
+ * Time: 19:16
+ * 获取某天的某个股票的数据
+ */
+require('../init.php');
+
+
+/***
+ * 1。获取所有的股票代码
+ * 2.遍历股票代码,住区数据,插入到数据库中
+ */
+
+$end_date = date("Ymd", time());
+$start_date = date("Ymd", time() - 86400 * 10);
+Finance163::get_history_trade_data(601006,$start_date,$end_date);//默认是今天

+ 11 - 0
data/get_stock_all_code_name_by_sina.php

@@ -0,0 +1,11 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: 陈波
+ * Date: 2020/11/24
+ * Time: 19:16
+ * 获取所有个股的Code和Name,入库
+ */
+require('../init.php');
+
+sina::getAllStockCodeAndName();

+ 22 - 3
lib/day_k_base.class.php

@@ -13,7 +13,7 @@ class Day_k_base {
      * @param $attrs
      * @return mixed
      * @throws Exception
-     * 添加信息
+     * 添加股票的某一天数据信息
      */
     static public function add($attrs,$date=0)
     {
@@ -27,8 +27,27 @@ class Day_k_base {
         return $id;
     }
 
-    
-	/** 
+    /***
+     * @param $attrs
+     * @param int $date
+     * @return mixed
+     * @throws Exception
+     * 添加某一直股票的历史数据
+     */
+    static public function insert($attrs,$date=0)
+    {
+        if (empty($attrs)) throw new Exception('参数不能为空', 102);
+        if($date===0)
+        {
+            $date=strtotime(date('Ym'));
+        }
+        $Table_day_k_base = new Table_day_k_base($date);
+        $id = $Table_day_k_base->insert($attrs);
+        return $id;
+    }
+
+
+    /**
 	 * 管理员日志记录列表
 	 * 
 	 * @param $page        当前页

+ 88 - 0
lib/finance163.class.php

@@ -0,0 +1,88 @@
+<?php
+
+/**
+ * Created by PhpStorm.
+ * User: 王刚涛
+ * Date: 2020/11/26
+ * Time: 11:38
+ */
+class Finance163
+{
+    /****
+     * @param $code
+     * @return string
+     * 返回股票所属的板块,在股票代码前拼接0或者1
+     */
+    static public function stock_block($code)
+    {
+        $f = substr($code, 0, 1);
+        if ($f == "6") {
+            $stock_code = "0" . $code;
+        } else {
+            $stock_code = "1" . $code;
+        }
+        return $stock_code;
+    }
+
+    /***
+     * @param $code
+     * @return int
+     * 获取历史交易数据
+     */
+    static public function get_history_trade_data($stock_code,$start_date,$end_date)
+    {
+        $code = self::stock_block($stock_code);
+        $url = "http://quotes.money.163.com/service/chddata.html?code=" . $code . "&start=" . $start_date . "&end=" . $end_date . "&fields=TOPEN;HIGH;LOW;TCLOSE;VATURNOVER;VOTURNOVER;LCLOSE;CHG;PCHG;TURNOVER";
+        //构建所要发送的url
+        $curlHandle = curl_init();
+        curl_setopt($curlHandle, CURLOPT_URL, $url);
+        curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, 1);
+        curl_setopt($curlHandle, CURLOPT_SSL_VERIFYPEER, false);
+        curl_setopt($curlHandle, CURLOPT_SSL_VERIFYHOST, false);
+        curl_setopt($curlHandle, CURLOPT_TIMEOUT, 10);
+        $content = curl_exec($curlHandle);
+        curl_close($curlHandle);
+        $content1 = explode("\n", $content);//以换行符进行分割字符串
+        if (!empty($content1[1])) {
+            for ($i = count($content1) - 1; $i >0; $i--) {//排除日期,代码等标题栏
+                if (!empty($content1[$i])) {
+                    $content3 = explode(",", iconv("gbk", "utf-8", $content1[$i]));
+                }
+                else{
+                    continue;
+                }
+//                解析返回的历史数据
+                $t = explode("-",$content3[0]);
+                $date = trim($t[0].$t[1]);
+                $time = trim($t[0].$t[1].$t[2]);
+
+                $data['date'] = $date;//用于创建表
+                $data['time'] = $time;//用于获取时间戳
+                $data['code'] = intval(explode("'",$content3[1])[1]);
+                $data['open_price'] = floatval($content3[3]);
+                $data['hightest_price'] = floatval($content3[4]);
+                $data['lowest_price'] = floatval($content3[5]);
+                $data['close_price'] = floatval($content3[6]);
+                $data['value'] = floatval($content3[7]);
+                $data['amount'] = floatval($content3[8]);
+                $data['close_price_qfq'] = floatval($content3[9]);
+                $data['increase_price'] = floatval($content3[10]);
+                $data['increase_value'] = floatval($content3[11]);
+                $data['turnover'] = floatval($content3[12]);//换手率
+                //将所要存储的数据放入一个数组中
+                $stock_name = $content3[1];
+                if ($data['close_price'] == 0) {//当收盘价为0的时候,表示此时的基于9-3点,所以不存储
+                    continue;
+                }
+                if (!empty($data)) {
+//                    Index::add($stock_name, $content3[0]);//将这支股票的股票名称和股票代码存入
+                    $msg= Day_k_base::insert($data,$date);//将这支股票的历史数据存入数据库
+                }
+            }
+        }
+
+        return $msg;
+    }
+}
+
+?>

+ 11 - 14
lib/index_csi300.class.php

@@ -8,26 +8,23 @@
 
 class Index_csi300 {
 
-	/**
-	 * 记录管理员日志
-	 * 
-	 * @param $log       日志内容
-	 * 
-	 */
+    /****
+     * @param $attrs
+     * @return mixed
+     * @throws Exception
+     *
+     */
 	static public function add($attrs){
         if (empty($attrs)) throw new Exception('参数不能为空', 102);
         $Table_index_csi300 = new Table_index_csi300();
         $id = $Table_index_csi300->add($attrs);
         return $id;
 	}
-    
-	/** 
-	 * 管理员日志记录列表
-	 * 
-	 * @param $page        当前页
-	 * @param $pagesize    每页大小
-	 * 
-	 */
+
+    /****
+     * @param $page
+     * @param $pagesize
+     */
 	static public function getListByPage($page, $pagesize){
 		$filter = array();
 

+ 45 - 16
lib/sina.class.php

@@ -6,14 +6,14 @@
 error_reporting(E_ALL & ~E_NOTICE);
 
 
-class sina
+class Sina
 {
     /****
      * @param $url
      * @return array|bool|string
      * 对curl_setopt进行封装,消除代码冗余
      */
-      static public function curl_get_data($url){
+      static private function curl_get_data($url){
         $curlHandle = curl_init();
         curl_setopt($curlHandle, CURLOPT_URL, $url);
         curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, 1);
@@ -23,21 +23,46 @@ class sina
         $content = curl_exec($curlHandle);
         curl_close($curlHandle);//防止中文乱码
         $content2 = iconv("gbk", "utf-8", $content);//子串
-        $content3 = substr($content2, strripos($content2, "=") + 1);
+        $content3 = substr($content2, strripos($content2, "=\"") + 2); //获取 var hq_str_sz000002="万 科A,30.780,30.800,30.510,  ="之后部分
         $content3 = explode(",", $content3);//分割
         return $content3;
     }
 
-
-//市场上所有的股票 boniu_stock
-     static public function get_boniu_stock($code)
+    //获取全部股票代码和名称列表
+    static public function getAllStockCodeAndName()
     {
-
+        $url = "http://hq.sinajs.cn/list=s_sz000002";
+        $content = self::curl_get_data($url);
+        $data['code'] = '000002';
+        $data['name'] = trim($content[0]);
+        $data['exchange'] = 'sz';
+        $data['sector'] = 20;
+        return Stock::updateCodeAndName($data);
     }
 
+    /****
+     * @param $code
+     * @return string
+     * 判断股票的类型,属于创业板,科创板,主板,$code
+     */
+    static public function get_boniu_block($code)
+    {
+        if (strncmp($code, "30", 2) == 0) {
+            //创业板
+            $code .= "sz";
+        } else if (strncmp($code, "68", 2) == 0) {
+            //科创板
+            $code .= "sh";
+        } else if (strncmp($code, "60", 2) == 0) {
+            //主板
+            $code .= "sh";
+        } else if (strncmp($code, "00", 2) == 0) {
+            //中小板
+            $code .= "sz";
+        }
+        return $code;
 
-
-
+    }
     /****
      * @param string $code
      * @return mixed
@@ -188,13 +213,14 @@ class sina
      * 根据股票代码获取股票的实时信息
      */
     static public function get_info_code($code){
-        $stock_code = $code;
-        $f = substr($stock_code, 0, 1);
-        if ($f == "6"  || $f == "0" ) {
-            $stock_code = "sh" . $code;
-        } else {
-            $stock_code = "sz" . $code;
-        }
+//        $stock_code = $code;
+//        $f = substr($stock_code, 0, 1);
+//        if ($f == "6"  || $f == "0" ) {
+//            $stock_code = "sh" . $code;
+//        } else {
+//            $stock_code = "sz" . $code;
+//        }
+        $stock_code= self::get_boniu_block($code);
         $url = "http://hq.sinajs.cn/list=" . $stock_code;
         $content3=sina::curl_get_data($url);//封装为函数,执行url获取结果并解析
         return $content3;
@@ -231,6 +257,9 @@ class sina
 
 //除权除息表(boniu_xrxd_log)
 //st记录表(boniu_st_log)
+
+
+//获取历史数据的表
 }
 
 ?>

+ 43 - 29
lib/stock.class.php

@@ -8,36 +8,50 @@
 
 class Stock {
 
-	/**
-	 * 记录管理员日志
-	 * 
-	 * @param $log       日志内容
-	 * 
-	 */
-	static public function add($log){
-
-	}
-    
-	/** 
-	 * 管理员日志记录列表
-	 * 
-	 * @param $page        当前页
-	 * @param $pagesize    每页大小
-	 * 
-	 */
-	static public function getListByPage($page, $pagesize){
-		$filter = array();
+    public function __construct()
+    {
+
+    }
+
+    static public function getInfoById($id)
+    {
+        $Table_stock = new Table_stock();
+        return $Table_stock->getInfoById($id);
+    }
+
+
+    static public function add($attrs)
+    {
+        if (empty($attrs)) throw new Exception('参数不能为空', 101);
+
+        $Table_stock = new Table_stock();
+        $id = $Table_stock->add($attrs);
+
+        return $id;
+    }
+
+    static public function getList($filter = array(), $count=0, $page=0, $pageSize=0)
+    {
+        $Table_stock = new Table_stock();
+        return $Table_stock->getList($filter, $count, $page, $pageSize);
+    }
+
+    static public function updateCodeAndName($attrs)
+    {
+        if (empty($attrs)) throw new Exception('参数不能为空', 101);
 
         $Table_stock = new Table_stock();
-		return $Table_stock->getList($filter, 0, $page, $pagesize);
-	}
-    
-	//管理员日志总数
-	static public function getCountAll(){
-		$filter = array();
-
-        $Table_stock = new Table_st_log();
-		return $Table_stock->getList($filter, 1);
-	}
+        $rs = $Table_stock->getInfoByCode($attrs['code']);
+        if (empty($rs)) {
+            $id = $Table_stock->addCodeAndName($attrs);
+        } else {
+            $id = $rs['id'];
+            $Table_stock->update($id, $attrs);
+        }
+
+        return $id;
+    }
+
 }
+
 ?>

+ 41 - 9
lib/table/table.class.php

@@ -48,15 +48,6 @@ abstract class Table {
 	//@param $attr array -- 键值同struct()返回的数组
 	abstract public function add($attr);
 
-	//获取列表(分页)
-	//$count、$page和$pagesize都为0时,返回全部结果(适用于无需分页的情况)
-	//
-	//@param $filter array -- 过滤条件,格式见Table::filterToWhere
-	//@param $count -- 0:返回列表 1:返回结果数量
-	//@param $page -- 当前第几页
-	//@param $pagesize -- 每页数量
-	abstract public function getList($filter = array(), $count = 0, $page = 0, $pagesize = 0);
-
 
 	///////////////////////////////////////////////////////////////
 	/**************非抽象方法,用于继承,也可以重载***************/
@@ -81,6 +72,47 @@ abstract class Table {
         }
 	}
 
+    //获取列表(分页)
+    //$count、$page和$pagesize都为0时,返回全部结果(适用于无需分页的情况)
+    //
+    //@param $filter array -- 过滤条件,格式见Table::filterToWhere
+    //@param $count -- 0:返回列表 1:返回结果数量
+    //@param $page -- 当前第几页
+    //@param $pagesize -- 每页数量
+    public function getList($filter = array(), $count = 0, $page = 0, $pagesize = 0){
+        $where = $this->filterToWhere($filter);
+
+        if($count == 0){//列表
+            $sql = "select * from ". $this->table_fullname ." $where order by ".$this->table_id." desc";
+
+            if($page > 0){//分页
+                $startrow = ($page - 1) * $pagesize;
+                $sql_limit = " limit $startrow, $pagesize";
+                $sql .= $sql_limit;
+            }
+
+            $rs  = $this->pdo->sqlQuery($sql);
+            $r   = array();
+            if($rs){
+                foreach($rs as $key => $val){
+                    $r[$key] = $this->dataToAttr($val);
+                }
+                return $r;
+            }else{
+                return $r;
+            }
+
+        }else{//统计
+            $sql = "select count(*) as c from ". $this->table_fullname . " $where ";
+            $rs  = $this->pdo->sqlQuery($sql);
+            if($rs){
+                return $rs[0]['c'];
+            }else{
+                return 0;
+            }
+        }
+    }
+
 	//删
 	public function del($id){
 		$where = array(

+ 28 - 0
lib/table/table_day_k_base.class.php

@@ -86,6 +86,34 @@ class Table_day_k_base extends Table {
         return $msg;
     }
 
+
+    /****
+     * @param $attr
+     * @return mixed
+     *
+     */
+    public function insert($attr){
+        $param = array (
+            'day_k_date'    => array('number', strtotime($attr['date'])),
+            'day_k_code'    => array('number', $attr['code']),
+            'day_k_open_price'    => array('number', $attr['open_price']),
+            'day_k_hightest_price'    => array('number', $attr['hightest_price']),
+            'day_k_lowest_price'    => array('number', $attr['lowest_price']),
+            'day_k_value'    => array('number', $attr['value']),
+            'day_k_amount'    => array('number', $attr['amount']),
+            'day_k_close_price'    => array('number', $attr['close_price']),
+            'day_k_close_price_qfq'    => array('number', $attr['close_price_qfq']),
+            'day_k_increase_price'    => array('number', $attr['increase_price']),
+            'day_k_increase_value'    => array('number', $attr['increase_value']),
+            'day_k_turnover'    => array('number', $attr['turnover']),
+
+        );
+
+        $msg=$this->pdo->sqlinsert($this->table_fullname, $param);
+        return $msg;
+    }
+
+
     //获取列表(分页)
     //$count、$page和$pagesize都为0时,返回全部结果(适用于无需分页的情况)
     //

+ 68 - 25
lib/table/table_stock.class.php

@@ -14,47 +14,90 @@ class Table_stock extends Table {
     protected $table_id         = 'stock_id';//指定ID字段名称,必须
     protected $table_status     = '';//指定状态字段名称,如果有
     protected $table_order      = '';//指定排序字段名称,如果有
+
 	//数据库结构
 	protected function struct(){
 		$attr = array();
-		
-		$attr['id']           = 'stock_id';
-		$attr['date']         = 'stock_code';
-		$attr['open_price']         = 'stock_name';
-		$attr['close_price']        = 'stock_desc';
-        $attr['tradable_amount']           = 'stock_tradable_amount';
-        $attr['tradable_value']         = 'stock_tradable_value';
-        $attr['total_amount']         = 'stock_total_amount';
-        $attr['total_value']        = 'stock_total_value';
-        $attr['profitable']        = 'stock_profitable';
-        $attr['pb']        = 'stock_pb';
+		$attr['id']               = 'stock_id';
+		$attr['code']             = 'stock_code';
+		$attr['name']             = 'stock_name';
+		$attr['desc']             = 'stock_desc';
+        $attr['tradable_amount']  = 'stock_tradable_amount';
+        $attr['tradable_value']   = 'stock_tradable_value';
+        $attr['total_amount']     = 'stock_total_amount';
+        $attr['total_value']      = 'stock_total_value';
+        $attr['profitable']       = 'stock_profitable';
+        $attr['pb']               = 'stock_pb';
         $attr['pe_static']        = 'stock_pe_static';
-        $attr['pe_dynamic']        = 'stock_pe_dynamic';
-        $attr['pe_ttm']        = 'stock_pe_ttm';
-        $attr['exchange']        = 'stock_exchange';
-        $attr['sector']        = 'stock_sector';
+        $attr['pe_dynamic']       = 'stock_pe_dynamic';
+        $attr['pe_ttm']           = 'stock_pe_ttm';
+        $attr['exchange']         = 'stock_exchange';
+        $attr['sector']           = 'stock_sector';
 		return $attr;
 	}
 
     public function add($attr){
         $param = array (
+            'stock_code'            => array('string', $attr['code']),
+            'stock_name'            => array('string', $attr['name']),
+            'stock_desc'            => array('string', $attr['desc']),
+            'stock_tradable_amount' => array('number', $attr['tradable_amount']),
+            'stock_tradable_value'  => array('number', $attr['tradable_value']),
+            'stock_total_amount'    => array('number', $attr['total_amount']),
+            'stock_total_value'     => array('number', $attr['total_value']),
+            'stock_profitable'      => array('number', $attr['profitable']),
+            'stock_pb'              => array('number', $attr['pb']),
+            'stock_pe_static'       => array('number', $attr['pe_static']),
+            'stock_pe_dynamic'      => array('number', $attr['pe_dynamic']),
+            'stock_pe_ttm'          => array('number', $attr['pe_ttm']),
+            'stock_exchange'        => array('string', $attr['exchange']),
+            'stock_sector'          => array('number', $attr['sector']),
+        );
 
-             'admingroup_name'    => array('string', $attr['name'])
+        return $this->pdo->sqlinsert($this->table_fullname, $param);
+    }
 
+    public function addCodeAndName($attr){
+        $param = array (
+            'stock_code'            => array('string', $attr['code']),
+            'stock_name'            => array('string', $attr['name']),
+            'stock_desc'            => array('string', ''),
+            'stock_tradable_amount' => array('number', 0),
+            'stock_tradable_value'  => array('number', 0),
+            'stock_total_amount'    => array('number', 0),
+            'stock_total_value'     => array('number', 0),
+            'stock_profitable'      => array('number', 0),
+            'stock_pb'              => array('number', 0),
+            'stock_pe_static'       => array('number', 0),
+            'stock_pe_dynamic'      => array('number', 0),
+            'stock_pe_ttm'          => array('number', 0),
+            'stock_exchange'        => array('string', $attr['exchange']),
+            'stock_sector'          => array('number', $attr['sector']),
         );
+
         return $this->pdo->sqlinsert($this->table_fullname, $param);
     }
 
-    //获取列表(分页)
-    //$count、$page和$pagesize都为0时,返回全部结果(适用于无需分页的情况)
-    //
-    //@param $filter array -- 过滤条件,格式见Table::filterToWhere
-    //@param $count -- 0:返回列表 1:返回结果数量
-    //@param $page -- 当前第几页
-    //@param $pagesize -- 每页数量
-    public function getList($filter = array(), $count = 0, $page = 0, $pagesize = 0)
-    {
+    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;
+        }
     }
+
+
 }
 ?>