<?php

/**
 * Created by PhpStorm.
 * User: stanley-king
 * Date: 16/6/23
 * Time: 下午10:21
 */

define('BASE_ROOT_PATH',str_replace('/test','',dirname(__FILE__)));

require_once(BASE_ROOT_PATH . '/fooder.php');
require_once(BASE_ROOT_PATH . '/helper/relation_helper.php');
require_once(BASE_ROOT_PATH . '/helper/performance_helper.php');
require_once(BASE_ROOT_PATH . '/helper/algorithm.php');

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

    public function testUpContactsA()
    {
        //36490
        $contacts = array('13911129868','13911129869','13911129870','13911129871','13911129867','15618951806','18612396521');
        relation_helper::onUpContacts(36490,'13911129867','江海苗',$contacts);

    }

    public function testUpContactsB()
    {
        //36490
        $contacts = array('13911129867','13911129869','13911129870','13911129872');
        relation_helper::onUpContacts(36511,'18612396521','秦梦洁',$contacts);
    }

    public function testOnLogin()
    {
        relation_helper::onLogin(36490);
        relation_helper::onLogin(36511);
    }
    
    public function testUpContactsC()
    {
        try
        {
            $contacts = '%5B%22178-0211-8910%22%2C%22182-6227-3057%22%2C%22189-6195-0030%22%5D';
            $contacts = session_helper::parse_contacts($contacts);
            relation_helper::onUpContacts(36511,'18612396521','秦梦洁',$contacts);
        } catch (Exception $ex) {
            echo $ex->getMessage();
        }
    }

    public function testMakeType()
    {
        $reg = '/make_type=(\d+)/i';

        $ret = preg_match($reg, '发送红包，红包单号: xxxxxx make_type=6', $arr);
    }



    public function testFollower()
    {
        $follower = relation_helper::follower(36490);
        if(!empty($follower)) {
            //$follower = self::separate_page($follower,$pages);
            $members = Model('member')->field('*')->where(array('member_id' => array('in',$follower)))->limit(false)->select();

            $desc = array();
            foreach ($members as $val) {
                $info = new member_info($val);
            }
            //return self::outsuccess(array('followers' => $follower,'mem_desc' =>  , 'mobile_page' => mobile_page($pages)));
        }
    }

    public function testDate()
    {
        $title = strftime("熊猫美妆%m月%d日红包风云榜",1469030400);
    }

    public function testSort()
    {
        $mod = Model('member_relation');
        $item = $mod->field('*')->where(array('member_mobile' => '13911129867'))->find();
        $mobiles = unserialize($item['unbuild_mobiles']);

//        perfor_start();
//        for($i = 0; $i < 10000; ++$i) {
//            sort($mobiles);
//        }
//        perfor_end("testSort","string sort");

        foreach ($mobiles as $mobile) {
            $iMobiles[] = intval($mobile);
        }

        perfor_start();
        for($i = 0; $i < 10000; ++$i) {
            sort($iMobiles);
        }
        perfor_end("testSort","int sort");


        $len = count($iMobiles);
        perfor_start();
        for($i = 0; $i < 10000; ++$i) {
            $pos = $i % $len;
            $val = $iMobiles[$pos];
            //algorithm::array_erase($iMobiles,$pos);
            $pos = algorithm::lower_bonud($iMobiles,$val);
            //algorithm::array_insert($iMobiles,$pos,$val);
        }
        perfor_end("testSort","int sort");

        performance_helper::instance()->format_log();
    }

    public function testArrayInsert()
    {
        $x = array(1,3,5,9,10);
        $pos = algorithm::lower_bonud($x,3);
        $posx = algorithm::upper_bound($x,3);

        $y = algorithm::array_insert($x,$pos,0);
        $pos = algorithm::lower_bonud($x,4);
        $posx = algorithm::upper_bound($x,4);

        //$find = algorithm_helper::binary_search($x,0);
    }

    public function testLowerbound()
    {
        //$x = array(1,3,5,9,10);
        $x = array(39625);
        $find = algorithm::binary_search($x,0);
        $find = algorithm::binary_search($x,39625);
        $pos = algorithm::lower_bonud($x,39625);
        $pos = algorithm::lower_bonud($x,2);
        $pos = algorithm::lower_bonud($x,3);
        $pos = algorithm::lower_bonud($x,14);
    }

    public function testUpper()
    {
        $x = array(1,3,5,9,10);
        $pos = algorithm::upper_bound($x,0);
        $pos = algorithm::upper_bound($x,2);
        $pos = algorithm::upper_bound($x,3);
        $pos = algorithm::upper_bound($x,14);
    }
    public function testRelation()
    {
        perfor_start();
        for($i = 0; $i < 10000; ++$i) {
            $mod = Model('member_relation');
            $mod->findByMobile('13911129867');
        }
        perfor_end("testRelation","10000 findByMobile");

        $mod = Model('member_relation');
        $item = $mod->findByMobile('13911129867');

        perfor_start();
        for($i = 0; $i < 10000; ++$i) {
            unserialize($item['unbuild_mobiles']);
        }
        perfor_end("testRelation","10000 unserialize");

        perfor_start();
        for($i = 0; $i < 10000; ++$i) {
            $relation = new \relation\mem_relation(36490);
            //$relation->pass_subscribe();
        }
        perfor_end("testRelation","10000 pass_subscribe");
        performance_helper::instance()->format_log();
    }

    public function testInitInvitors()
    {
        $path = BASE_DATA_PATH . "/mobile/invitors.txt";
        $file = fopen($path,'r');
        if(!is_resource($file)) {
            Log::record(__METHOD__ . " file is not exist",Log::ERR);
            return false;
        }

        while(!feof($file)) {
            $line = fgets($file);
            $kv = preg_split('/,/',$line);
            if(count($kv) != 2) continue;

            $key = trim($kv[0]);
            $val = trim($kv[1]);

            $this->update_invitor($key,$val);
        }

        fclose($file);
    }

    private function update_invitor($invitor,$user)
    {
        if(empty($invitor)) return false;

        $mod = Model('member');
        $invitor_info = $mod->where(['member_mobile' => $invitor])->find();
        if(empty($invitor_info)) return false;
        $member_id = intval($invitor_info['member_id']);

        $user_info = $mod->where(['member_mobile' => $user])->find();
        if(empty($user_info)) return false;

        $inviter_id = intval($user_info['inviter_id']);
        if($inviter_id > 0) return false;

        $ret = $mod->where(['member_mobile' => $user])->update(['inviter_id' => $member_id]);
        if($ret == false) {
            Log::record(__METHOD__ . "  update {$user} invitor id err",Log::ERR);
        }
        else {
            Log::record(__METHOD__ . "  update {$user} invitor id success",Log::DEBUG);
        }
    }
}
