<?php

/**
 * Created by PhpStorm.
 * User: Steven
 * Date: 2016/10/12
 * Time: 10:56
 */
require_once __DIR__ . '/../Utils/commonUtil.class.php';

class DataBaseConfig
{
    //正式数据库
    private $MYSQL_HOST = "180.168.4.58";
    private $MYSQL_USER = "root";
    private $MYSQL_PASSWORD = "123456";
    private $MYSQL_DB = "ctsdata";
    private $MYSQL_PORT = "8090";

    //测试数据库
    private $MYSQL_HOST1 = "180.168.4.58";
    private $MYSQL_USER1 = "root";
    private $MYSQL_PASSWORD1 = "123456";
    private $MYSQL_DB1 = "ctsdata_test";
    private $MYSQL_PORT1 = "8090";

    //waice数据库
    private $MYSQL_HOST2 = "180.168.4.58";
    private $MYSQL_USER2 = "root";
    private $MYSQL_PASSWORD2 = "123456";
    private $MYSQL_DB2 = "waice";
    private $MYSQL_PORT2 = "8090";

    //waice2数据库
    private $MYSQL_HOST3 = "192.168.6.245";
    private $MYSQL_USER3 = "root";
    private $MYSQL_PASSWORD3 = "123456";
    private $MYSQL_DB3 = "waice2";
    private $MYSQL_PORT3 = "3306";


    //Memcache配置
    private $MEMCACHE_HOST = "139.224.30.29";
    private $MEMCACHE_PORT = "11211";
    private $MEMCACHE_USER = "memcached";
    private $API_GPS_ACCOUNT = "15618818219";
    private $API_GPS_PASSWORD = "123456";

    /**
     * 链接正式数据库
     * @return PDO
     */
    function conn()
    {
        try {
            $pdo = new PDO("mysql:host=" . $this->MYSQL_HOST . ";port=" . $this->MYSQL_PORT . ";dbname=" . $this->MYSQL_DB, $this->MYSQL_USER, $this->MYSQL_PASSWORD, array(PDO::MYSQL_ATTR_INIT_COMMAND => "set names utf8"));
            return $pdo;
        } catch (PDOException $e) {
            utils::writeLog('new PDO failed:' . $e->getMessage());
        }
    }

    /**
     * 链接测试数据库
     * @return PDO
     */
    function conn_test()
    {
        try {
            $pdo = new PDO("mysql:host=" . $this->MYSQL_HOST1 . ";port=" . $this->MYSQL_PORT1 . ";dbname=" . $this->MYSQL_DB1, $this->MYSQL_USER1, $this->MYSQL_PASSWORD1, array(PDO::MYSQL_ATTR_INIT_COMMAND => "set names utf8"));
            return $pdo;
        } catch (PDOException $e) {
            utils::writeLog('new PDO failed:' . $e->getMessage());
        }
    }

    /**
     * 链接waice数据库
     * @return PDO
     */
    function conn_waice()
    {
        try {
            $pdo = new PDO("mysql:host=" . $this->MYSQL_HOST3 . ";port=" . $this->MYSQL_PORT3 . ";dbname=" . $this->MYSQL_DB3, $this->MYSQL_USER3, $this->MYSQL_PASSWORD3, array(PDO::MYSQL_ATTR_INIT_COMMAND => "set names utf8"));
            return $pdo;
        } catch (PDOException $e) {
            utils::writeLog('new PDO failed:' . $e->getMessage());
        }
    }

    /**
     * 添加缓存
     * @param $key
     * @param $value
     */
    public function set_memcache($key, $value)
    {
        $mem = new Memcache;
        $mem->connect($this->MEMCACHE_HOST, $this->MEMCACHE_PORT);
        $mem->set($key, $value);
    }

    /**
     * 获取内存中的数据
     * @param $key
     * @return array|string
     */
    public function get_memcache($key)
    {
        $mem = new Memcache;
        $mem->connect($this->MEMCACHE_HOST, $this->MEMCACHE_PORT);
        $value = $mem->get($key);
        return $value;
    }

    /**
     * 删除键值对应的数据
     * @param $key
     * @return bool
     */
    public function delete_memcache($key)
    {
        $mem = new Memcache;
        $mem->connect($this->MEMCACHE_HOST, $this->MEMCACHE_PORT);
        $value = $mem->delete($key);
        return $value;
    }

}