<?php
/**
 * DataFix.php UTF-8
 * 数据修复
 *
 * @date    : 2018/10/29 15:20
 *
 * @license 这不是一个自由软件,未经授权不许任何使用和传播。
 * @author  : wuyonghong <wyh@huosdk.com>
 * @version : HuoMp 1.0
 */

namespace console\index\controller;

use huo\controller\member\MemCache;
use huo\controller\request\Channel;
use huo\controller\request\Device;
use huo\controller\request\Game;
use huo\controller\request\Mem;
use huo\model\log\MemLoginLogModel;
use huo\model\member\MemberModel;
use huo\model\order\OrderModel;
use think\console\Command;
use think\console\Input;
use think\console\Output;

class DataFix extends Command {
    protected $rq_data = [];
    protected $g_i;

    protected function configure() {
        $this->setName('datafix')->setDescription('数据生成');
    }

    public function index() {
//        $this->create();
//        $this->login();
//        $this->charge();
        echo 'test';
        exit;
    }

    /**
     * /www/wdlinux/php/bin/php /mini/think datafix
     *
     * @param Input  $input
     * @param Output $output
     *
     * @return int|null|void
     */
    protected function execute(Input $input, Output $output) {
        $this->create();
        $this->login();
    }

    /**
     * 添加游戏
     */
    public function addGame() {
    }

    /**
     * 创建账号
     *
     */
    public function create() {
//        $_map['create_time'] = ['between', ['1527782400', '2527782400']];
        $_map = [];
        $_data = (new MemberModel())->with('ext')->where($_map)->field(
            'id,username,app_id,create_time,status,update_time'
        )->select();
        $_cnt = 0;
        foreach ($_data as $_k => $_v) {
            $_v = $_v->toArray();
            /* 登陆授权队列 */
            $_device_rq = new \huo\controller\request\Device();
            $_game_rq = new \huo\controller\request\Game();
            $_channel_rq = new \huo\controller\request\Channel();
            $_mem_rq = new \huo\controller\request\Mem();
            $_device_rq->setIp($_v['reg_ip']);
            $_channel_rq->setCh($_v['agent_id']);
            $_mem_rq->setMemId($_v['id']);
            $_mem_rq->setRegTime($_v['create_time']);
            $_mem_rq->setLoginTime($_v['create_time']);
            $_mem_rq->setStatus($_v['status']);
            (new \huo\controller\queue\Mem($_v['app_id']))->create(
                $_device_rq, $_game_rq, $_channel_rq, $_mem_rq
            );
            $_cnt++;
        }
        $this->g_i += $_cnt;
        print_r($_cnt.' create ok'.PHP_EOL);
    }

    /**
     * 玩家登陆
     */
    public function login() {
        $_cnt = 0;
        for ($_i = 0; $_i <= 0; $_i++) {
            $_date = '2018-1'.$_i;
            $_data = (new MemLoginLogModel())->computeTable($_date)->where('mem_id>0')->select();
            foreach ($_data as $_k => $_v) {
                $_v = $_v->toArray();
                $_mem_data = MemCache::ins()->getInfoById($_v['mem_id']);
                /* 登陆队列 */
                $_device_rq = new \huo\controller\request\Device();
                $_game_rq = new \huo\controller\request\Game();
                $_channel_rq = new \huo\controller\request\Channel();
                $_mem_rq = new \huo\controller\request\Mem();
                $_device_rq->setIp($_v['ip']);
                $_channel_rq->setCh($_v['agent_id']);
                $_mem_rq->setMemId($_v['mem_id']);
                $_mem_rq->setRegTime($_v['reg_time']);
                $_status = $_mem_data['status'];
                $_mem_rq->setStatus($_status);
                $_mem_rq->setLoginTime($_v['create_time']);
                (new \huo\controller\queue\Mem($_v['app_id']))->online(
                    $_device_rq, $_game_rq, $_channel_rq, $_mem_rq
                );
                $_cnt++;
            }
        }
        $this->g_i += $_cnt;
        print_r($_cnt.' login ok'.PHP_EOL);
    }

    /**
     * 玩家充值
     */
    public function charge() {
        $_map['status'] = 2;
        $_order_ids = (new OrderModel())->where($_map)->limit(0, 100000)->order('id asc')->column('order_id');
        foreach ($_order_ids as $_k => $order_id) {
            print_r($order_id.' ');
            (new \huo\controller\queue\Order())->fromSdkOrder($order_id);
        }
        $this->g_i += count($_order_ids);
        $_map['order_id'] = ['in', $_order_ids];
        print_r((new OrderModel())->where($_map)->order('id asc')->sum('amount'));
        print_r(PHP_EOL.'g_i='.$this->g_i);
    }

    /**
     * @return Device
     */
    public function setDeviceData() {
        $_device = new Device();

        return $_device;
    }

    /**
     * @return Game
     */
    public function setGameData() {
        $_game = new Game();

        return $_game;
    }

    /**
     * @return Channel
     *
     */
    public function setChannelData() {
        $_channel = new Channel();

        return $_channel;
    }

    /**
     * 设置玩家入参
     *
     * @param bool $is_reg
     *
     * @return Mem
     */
    public function setMemData($is_reg = false) {
        $_mem = new Mem();

        return $_mem;
    }
}