<?php
/**
 * Created by PhpStorm.
 * User: stanley-king
 * Date: 2017/9/28
 * Time: 上午10:16
 */

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

require_once(BASE_ROOT_PATH . '/helper/notify_helper.php');
require_once(BASE_ROOT_PATH . '/helper/stat_helper.php');
require_once(BASE_ROOT_PATH . '/helper/predeposit_helper.php');
require_once(BASE_DATA_PATH . '/mobile/omsid.php');
require_once(BASE_ROOT_PATH . '/helper/bonus_helper.php');
require_once(BASE_ROOT_PATH . '/helper/bonus/witholder.php');
require_once(BASE_ROOT_PATH . '/helper/notify_helper.php');
require_once(BASE_ROOT_PATH . '/helper/account_helper.php');
require_once(BASE_ROOT_PATH . '/data/mobile/lrlz_staff.php');


class commandControl extends BaseCronControl
{
    public function __construct()
    {
        parent::__construct();
    }

    //中秋节给员工发红包
    public function zqbonusOp()
    {
        $staffs  = lrlz_staff();
        $type_sn = '76341506502791044726';
        bonus_helper::send_mobile($type_sn,$staffs,2);
    }
    public function init_commentsOp()
    {
        $mod = Model('goods');
        $i = 0;
        while (true)
        {
            $start = $i * 1000;
            $items = Model()->table('evaluate_goods')->field('geval_commonid,geval_goodsid')->order('geval_id asc')->limit("{$start},1000")->select();
            if(empty($items)) {
                return;
            }
            $i++;

            foreach ($items as $item)
            {
                $cid = intval($item['geval_commonid']);
                if($cid <= 0) continue;
                $mod->editGoodsCommon(['comments' => array('exp', "comments+1")],['goods_commonid' => $cid]);
            }
        }
    }

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

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

                $val = $mod_member->field('count(*) inviter_count' )->where(['inviter_id' => $user])->select();
                $invitees = intval($val[0]['inviter_count']);
                if($invitees <= 0) continue;

                $ret = $mod_member->editMember(['member_id' => $user],['invitees' => $invitees,'reward_amount' => $invitees * 30]);
                if($ret == false) {
                    Log::record("update member_id = {$user} invitees and reward",Log::ERR);
                }
            }
        }
    }

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

            foreach ($items as $item)
            {
                $user = intval($item['member_id']);
                if($user <= 0) continue;
                relation_helper::onLogin($user);
            }
        }
    }

    public function order_pushOp()
    {
        QueueClient::push('onAsyncBroadcastOrder',['pay_sn' => '330564181105825442','amount' => 1,'days' => 30]);
    }
    public function init_userOp()
    {
        $mod_stat = Model('stat_daily');
        $items = $mod_stat->field('date_id')->limit(false)->select();
        foreach ($items as $item)
        {
            $time = intval($item['date_id']);
            $stat = new statistics\stmember($time);
            $stat->init_user();
        }
    }

    private function find_user($mobile)
    {
        if(empty($mobile)) return false;

        $mod_member = Model('member');
        $infos = $mod_member->where(['member_mobile' => $mobile])->select();
        if(empty($infos)) return false;

        return intval($infos[0]['member_id']);
    }
    public function reset_bonusOp()
    {
        $mod_bonus = Model('user_bonus');
        $i = 0;
        while (true)
        {
            $start = $i * 1000;
            $items = $mod_bonus->where(['bonus_id' => ['between',[1066429,1393645]]])->field('*')->order('bonus_id asc')->limit("{$start},1000")->select();
//            $items = $mod_bonus->where(['bonus_id' => ['between',[10000,1393645]]] )->field('*')->order('bonus_id asc')->limit("{$start},1000")->select();

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

            foreach ($items as $item)
            {
                try
                {
                    $bonus = bonus\user_bonus::create_by_param($item);
                    $remain_amount = $bonus->bonus_value() - $bonus->send_amount() - $bonus->pay_amount();
                    $ret = $mod_bonus->where(['bonus_id' => $bonus->bonus_id()])->update(['remain_amount' => $remain_amount]);
                    Log::record("updata bonus_id = {$bonus->bonus_id()} ret={$ret}",Log::DEBUG);
                }
                catch (Exception $ex)
                {

                }

            }
        }
    }
}