<?php

/**
 * Created by PhpStorm.
 * User: stanley-king
 * Date: 2017/1/13
 * Time: 下午11:46
 */

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

require_once (BASE_ROOT_PATH . '/fooder.php');
require_once (BASE_ROOT_PATH . '/helper/util_helper.php');

class TestOrder extends PHPUnit_Framework_TestCase
{
    public static function setUpBeforeClass()
    {
        Base::run_util();
    }
    public static function tearDownAfterClass()
    {
    }
    public function testCancelOms()
    {
        $oms = Logic('delivery');
        $oms->cancel_oms('9000000002010310');
    }

    public static function testSuccess()
    {
        $model_order = Model('order');
        $logic_order = Logic('order');
        $condition = array();
        $condition['order_state'] = ORDER_STATE_SEND;
        $condition['lock_state'] = 0;
        //$condition['delay_time'] = array('lt',time() - ORDER_AUTO_RECEIVE_DAY * 86400);
        $condition['order_id'] = 6370;

        //分批，每批处理100个订单，最多处理5W个订单
        $order_list = $model_order->getOrderList($condition, '', '*', 'delay_time asc', 100);
        foreach ($order_list as $order_info) {
            $result = $logic_order->changeOrderStateReceive($order_info,'system','系统','超期未收货系统自动完成订单');
        }
    }
    public function testSend()
    {
        $order_sn = '9000000001035202';
        $status = 1;
        $consign_time = '2017-01-15 18:07:05.0';
        $logistics_no = '3322415835338';
        $logistics_company = '申通快递';
        $express = Model('express')->field('id')->where(array('e_name' => $logistics_company))->select();
        if (empty($express) || count($express) == 0) {
            echo 'FAIL';
            return;
        }

        $express_id = $express[0]['id'];
        $time = strtotime($consign_time);

    }
    public function testInitOrder()
    {
        $mod_member = Model('member');
        $i = 0;
        while (true)
        {
            $start = $i * 1000;
            $items = Model()->table('member')->field('member_id')->order('member_id asc')->limit("{$start},1000")->select();
            if(empty($items)) {
                return;
            }
            $i++;

            foreach ($items as $item)
            {
                $member_id = intval($item['member_id']);
                if($member_id <= 0) continue;

                $result = $this->stat_order($member_id);
                if($result == false) continue;

                $order_num = intval($result['order_num']);
                $lasted_time = intval($result['lastest_time']);
                $mod_member->editMember(['member_id' => $member_id],['order_num' => $order_num,'lastest_order' => $lasted_time]);
            }
        }
    }
    private function stat_order($member_id)
    {
        $items = Model()->table('order')
            ->field('count(*) as order_num,max(payment_time) lastest_time')
            ->where(['buyer_id' => $member_id,'order_state' => array('in',[20,30,40])])
            ->select();
        if(empty($items)) {
            return false;
        }
        else
        {
            if($items[0]['order_num'] == 0){
                return false;
            } else {
                return $items[0];
            }
        }
    }

    public function testMemberOrder()
    {
        $mod_member = Model('member');

        $this->mStartm = strtotime(date('Y-m-d',time()));
        $this->mEndtm  = $this->mStartm + 86400 - 1;

        $cond['payment_time'] = ['between',"{$this->mStartm},{$this->mEndtm}"];
        $cond['order_state']  = array('in',[20,30,40]);

        $i = 0;
        while (true)
        {
            $start = $i * 1000;
            $items = Model()->table('order')->field('buyer_id,payment_time')->where($cond)->order('payment_time asc')->limit("{$start},1000")->select();

            if(empty($items)) {
                return;
            }
            $i++;

            foreach ($items as $item)
            {
                $member_id = intval($item['buyer_id']);
                if($member_id <= 0) continue;

                $payment_time = intval($item['payment_time']);
                if($payment_time <= 0) continue;

                $mod_member->editMember(['member_id' => $member_id],['order_num' => array('exp', 'order_num+1'),'lastest_order' => $payment_time]);
            }
        }
    }
    public function testStatCall()
    {
        $x = 'a:4:{s:8:"function";a:16:{s:10:"app_update";a:2:{s:13:"check_version";a:1:{s:5:"count";i:7;}s:4:"area";a:1:{s:5:"count";i:7;}}s:6:"config";a:1:{s:11:"getconfigex";a:1:{s:5:"count";i:9;}}s:11:"member_info";a:2:{s:3:"get";a:1:{s:5:"count";i:7;}s:4:"edit";a:1:{s:5:"count";i:1;}}s:7:"special";a:1:{s:5:"index";a:1:{s:5:"count";i:10;}}s:5:"index";a:2:{s:6:"splash";a:1:{s:5:"count";i:8;}s:4:"tabs";a:1:{s:5:"count";i:8;}}s:5:"login";a:1:{s:6:"status";a:1:{s:5:"count";i:8;}}s:12:"member_order";a:1:{s:15:"orderCountState";a:1:{s:5:"count";i:5;}}s:12:"member_bonus";a:1:{s:5:"topup";a:1:{s:5:"count";i:16;}}s:13:"member_logout";a:1:{s:5:"index";a:1:{s:5:"count";i:2;}}s:14:"member_address";a:1:{s:12:"address_list";a:1:{s:5:"count";i:1;}}s:7:"bonusex";a:1:{s:4:"open";a:1:{s:5:"count";i:2;}}s:6:"search";a:3:{s:13:"suggest_words";a:1:{s:5:"count";i:4;}s:5:"index";a:1:{s:5:"count";i:7;}s:7:"history";a:1:{s:5:"count";i:1;}}s:12:"goods_common";a:1:{s:5:"index";a:1:{s:5:"count";i:2;}}s:5:"fcode";a:2:{s:5:"index";a:1:{s:5:"count";i:4;}s:4:"open";a:1:{s:5:"count";i:4;}}s:5:"mshop";a:1:{s:7:"signurl";a:1:{s:5:"count";i:3;}}s:4:"cart";a:2:{s:5:"addex";a:1:{s:5:"count";i:1;}s:10:"rate_money";a:1:{s:5:"count";i:1;}}}s:5:"other";a:3:{s:10:"goods_4919";a:1:{s:5:"count";i:1;}s:10:"goods_6212";a:1:{s:5:"count";i:1;}s:11:"special_466";a:1:{s:5:"count";i:1;}}s:9:"app_count";i:107;s:9:"wap_count";i:11;}';
        $y = unserialize($x);
        $x = json_encode($y);
        $x = json_decode($x,true);
    }
    public function testBonusRate()
    {
        $data = 'a:2:{i:50;d:5.1799999999999997;i:30;d:17.59;}';
        $x = unserialize($data);
    }
    public function testRefund()
    {
        //SELECT * FROM lrlz_order WHERE add_time > UNIX_TIMESTAMP(DATE ('2017-09-28 00:00:00')) AND order_state = 0;

        $items = Model()->query('SELECT * FROM lrlz_order WHERE pd_amount=0 AND LENGTH(bonus_rate) > 0 AND add_time > UNIX_TIMESTAMP(DATE (\'2017-09-01 00:00:00\')) AND order_state = 0');
        foreach ($items as $item) {
            $order_sn = $item['order_sn'];
            $member_id = intval($item['buyer_id']);
            account_helper::order_cancel($order_sn,$member_id,true);
        }
    }
}
