<?php

/**
 * Created by PhpStorm.
 * User: stanley-king
 * Date: 2017/1/31
 * Time: 上午11:28
 */

define('BASE_ROOT_PATH',str_replace('/test','',dirname(__FILE__)));
define('MOBILE_SERVER',true);
require_once(BASE_ROOT_PATH . '/fooder.php');

class DBTest extends PHPUnit_Framework_TestCase
{
    public static function setUpBeforeClass()
    {
        Base::run_util();
    }

    public function testTranslog()
    {
    }

    public function testSyscount()
    {
        $bonus_type = Model('bonus_type');
        $result = $bonus_type->field('*')->where(['send_start_date' => ['between',[1495592858,1495601456]]])->limit(false)->select();
    }

    public function testCount()
    {
        $mod_member = Model('member');
        $num = $mod_member->count();
    }

    public function testQuery()
    {
//        $sql = "select * from lrlz_user_bonus where type_id=\'{$type_id}\' and \'session_id=\'{$sess_id}\' order by bonus_status asc,bonus_id asc limit 1";
        $mobile = '18911779278';
        $sql = "select * from lrlz_member where member_mobile='{$mobile}'";
        $items = Db::getAll($sql,array('lock' => true));
        //$items = Db::getAll('select * from lrlz_member');
    }

    public static function tearDownAfterClass()
    {

    }
    public function testModel()
    {
        $result = Model()->table('config')->select();
        $y = Model('member');
        $start = microtime(true);
        for ($i = 0; $i < 100; ++$i) {
            $member1 = Model('member');
        }
        $t = $member1;
        $period = microtime(true) - $start;

        $x = sprintf("%.6f",$period);

        echo  $period;
    }
    public function testTransaction()
    {
        try
        {
            $mod_member = Model('member');
            $trans = new trans_wapper(null,__METHOD__);
            $mod_member->where(array('member_id' => 36490))->update(array('member_name' => '江南舟子F2'));
            $affect_rows_a = $mod_member->affected_rows();
            $trans->commit();
            throw new Exception("xxxx");
        }
        catch (Exception $ex) {
            $trans->rollback();
        }
    }

    public function testMaster()
    {
        $mod_member = Model('member');
        //$items = $mod_member->where(array('member_mobile' => '18911779278'))->limit(false)->select(array('lock' => true));
        $item = $mod_member->where(array('member_mobile' => '18911779278'))->find(array('lock' => true));
    }

    public function testConnection()
    {
        $mysql = @new mysqli('192.168.1.205', 'root', '55668899', 'pandashop', '3306');
    }

    public function testConnectionhost()
    {
        $mysql = @new mysqli('192.168.1.103', 'root', '55668899', 'pandashop', '3306');
    }

    public function testTableInfo()
    {
        $mod_member = Model('member');
        $iterm = $mod_member->tableInfo('memble');

        $mod_member = Model();
        $iterm = $mod_member->tableInfo('memble');

    }
    public function testNull()
    {
        $x = intval(null);
    }

    public function testUpdate()
    {
        $mod_member = Model('member');
        $data = array(
            'member_mobile' => '15512468630'
        );
        $mod_member->where(array('member_id'=>1))->update($data);

        $mod_member->execute('UPDATE `lrlz_member` SET member_mobile=\'15512468638\' WHERE ( member_id = \'1\' )');
    }

    public function testInsert()
    {
        $mod_member = Model('member');
        $data = array(
            'member_mobile' => '18330118841'
        );
        $ret = $mod_member->insert($data);
    }

    public function testInsertAll()
    {
        $mod_member = Model('member');
        $data = array(
            array(
                'member_mobile'=>'18330118840'
            ),
            array(
                'member_mobile'=>'18330118839'
            )
        );
        $mod_member->insertAll($data);
    }

    public function testDelete()
    {
        $mod_member = Model('member');
        $mod_member->where(array('member_mobile'=>'18330118841'))->delete();
    }

    public function testSelect()
    {
        $mod_member = Model('member');
        $iterm = $mod_member->where(array('member_mobile' => '15512468630'))->select();
    }

    public function testFind()
    {
        $mod_member = Model('member');
        $iterm = $mod_member->where(array('member_mobile' => '15512468630'))->find();
    }

    public function testLimit()
    {
        $mod_member = Model('member');
        $iterm = $mod_member->limit(4)->select();
    }

    public function testTable()
    {
        $mod_member = Model();
        $iterm = $mod_member->table('member')->limit(4)->where(array('member_mobile'=>'15512468630'))->select();
    }

    public function testJoin()
    {
        $mod_member = Model();
        $on = 'member.member_id=member_common.member_id';
        $iterm = $mod_member->table('member,member_common')->join('inner')->on($on)->limit(2)->select();
    }

    public function testOrder()
    {
        $mod_member = Model();
        $iterm = $mod_member->field('member_id,member_sex')->table('member')->order('member_id desc')->limit(50)->select();
        $itermb = $mod_member->field('member_id,member_sex')->table('member')->order('member_id desc,member_sex asc')->limit(50)->select();
    }

    public function testxCount()
    {
        $mod_member = Model('member');
        $num = $mod_member->count();
        $num_big = $mod_member->where('member_id>15')->count();

    }

    public function testPage()
    {
        $mod_member = Model('member');
        //$reta = $mod_member->page(10)->order('member_id desc')->select();
        $retb = $mod_member->page(10, 100)->order('member_id desc')->select();
  //      $retc = $mod_member->page(10)->where('member_id=1')->select();
 //       $retd = $mod_member->where('member_id=1')->page(10)->select();
    }

    public function testGroup()
    {
        $mod_member = Model('member');
        $result = $mod_member->field('member_id,member_login_num,count(*)as count')->group('member_login_num')->select();
    }

    public function testHaving()
    {
       //$mod_goods = Model('goods');
        //$result = $mod_goods->field('store_id,count(*) as nc_count')->group('store_id')->having('nc_count>500')->select();

        $mod_member = Model('member');
        $result = $mod_member->field('member_id,member_login_num,count(*)as count')->group('member_login_num')->select();
        $result = $mod_member->field('member_id,member_login_num,count(*)as count')->group('member_login_num')->having('count>500')->select();

    }

    public function testQuerya()
    {
        $mod_member = Model('member');
        Model()->query('SELECT * FROM `shopnc_member` LIMIT 10');
        Model()->execute('UPDATE `shopnc_member` SET member_mobile=18330118841 WHERE member_id=1');

        $mod_member = Model();
        $mod_member->query('SELECT * FROM `shopnc_member` LIMIT 10');

    }

    public function testSum()
    {
        $mod_goods = Model();
        $mod_goods->table('goods')->sum('price');
    }

    public function testSetInc()
    {
        $mod_member = Model('member');
        $ret = $mod_member->where('member_id=2')->setInc('member_id', 1000);
        $retb = $mod_member->where(array('member_id=2'))->setInc('member_id', 1000);
    }

    public function testGetby_member_name()
    {
        $mod_member = Model('member');
        $ret = $mod_member->getby_member_name('江海苗');
    }

    public function testAttr()
    {
        $model = Model('goods');
        $ret = $model->where(array('goods_id' => 100))->attr('LOW_PRIORITY');
        $ret = $model->update(array('goods_click' =>array('exp','goods_click+1')));
    }

    public function testAutoCommit()
    {
        $model = Model('member');
        while (true)
        {
            $model->beginTransaction();
            $ret = $model->where(array('member_id' => 10000000))->update(array('member_mobilex' => '18911779278'));
            $affect_rows = $model->affected_rows();
            $model->beginTransaction();
            $ret = $model->where(array('member_id' => 36490))->update(array('member_mobile' => '18911779279'));
            $affect_rows = $model->affected_rows();

            $model->commit();
        }
    }

    public function testMessage()
    {
        $mod_message = Model('message');
        //INSERT INTO `lrlz_message` (`message_parent_id`,`from_member_id`,`from_member_name`,
        //`to_member_id`,`to_member_name`,`message_body`,
        //`message_time`,
        //`message_update_time`,`message_type`,`message_ismore`,
        //`read_member_id`,`del_member_id`)

        // VALUES('0','0','',
        //'36646','','你的账户于 2017-05-12 09:02:52 账户资金有变化，描述：领取红包，红包单号: 14281494512386767010 make_type=2，可用金额变化 ：4.30元，冻结金额变化：0.00元。<a href=\"http://p.lrlz.com/shop/index.php?act=predeposit&op=pd_log_list\" target=\"_blank\">点击查看余额</a>',
        //'1494550972',
        //'1494550972','1','0',
        //'','')

        $data = ['message_parent_id' => 0,
            'from_member_id' => 0,
            'from_member_name' => '',
            'to_member_name' => '',
            'msg_content' => '你的账户于 2017-05-12 09:02:52 账户资金有变化，描述：领取红包，红包单号: 14281494512386767010 make_type=2，可用金额变化 ：4.30元，冻结金额变化：0.00元。<a href=\"http://p.lrlz.com/shop/index.php?act=predeposit&op=pd_log_list\" target=\"_blank\">点击查看余额</a>',
            'message_time' => time(),
            'message_update_time' => time(),
            'message_type' => 1,
            'message_ismore' => 0,
            'read_member_id' => '',
            'del_member_id' => '',
        ];
        for ($i = 0; $i < 1000000; $i++) {
            $data['member_id'] = mt_rand(10000,100000);
            $insert_id = $mod_message->saveMessage($data);
            Log::record("id = {$insert_id}",Log::DEBUG);
        }
    }

    public function testGoods()
    {
        global $config;
        $exbrands = $config['exclude_brands'];

        $mod_goods = Model('goods');
        if(!empty($exbrands)) {
            $items = $mod_goods->getGoodsOnlineList(['brand_id' => ['not in',$exbrands]],'*',0,'goods_id desc',false);
        } else {

        }
    }
}
