<?php
/**
 * Created by PhpStorm.
 * User: stanley-king
 * Date: 2017/7/2
 * Time: 下午7:47
 */

defined('InShopNC') or exit('Access Invalid!');

class initdataControl extends BaseCronControl
{
    public function order_numOp()
    {
        $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];
            }
        }
    }
}