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

namespace console\index\controller;

use huo\controller\agent\Agent;
use huo\controller\member\MemCache;
use huo\model\game\GameModel;
use huo\model\log\DayMemLogModel;
use huo\model\member\MemberModel;
use huo\model\user\RoleModel;
use huo\model\user\UserModel;
use huolib\constant\AgentConst;
use huolib\constant\GameConst;
use huolib\status\CommonStatus;
use huolib\tool\Ip;
use huolib\tool\Time;
use think\console\Command;
use think\console\Input;
use think\console\Output;

class DataCreate extends Command {
    protected function configure() {
        $this->setName('datacreate')->setDescription('数据生成');
    }

    /**
     * /www/wdlinux/php/bin/php /huosdk/mini/think datacreate
     *
     * @param Input  $input
     * @param Output $output
     *
     * @return int|null|void
     */
    protected function execute(Input $input, Output $output) {
        $this->addAgent();
        $this->addMember(10000000, '2018-09-01');
        $this->addData('2018-07-01', '2018-10-27');
    }

    public function getAppIds() {
        $_map['classify'] = ['in', [GameConst::GAME_MP, GameConst::GAME_MP_BOX, GameConst::GAME_MP_RPBOX]];

        return (new GameModel())->where($_map)->column('id');
    }

    public function addData($start_date = '', $end_date = '') {
//        (new DayMemLogModel())->execute('TRUNCATE `h_log_day_mem_201810`;');
        $_add_cnt = 0;
        $_start_date = $start_date;
        $_end_date = $end_date;
        $_start_time = strtotime($_start_date);
        $_end_time = strtotime($_end_date);
        $_mem_ids = (new MemberModel())->getIdsByParentMemId(0);
        $_app_ids = $this->getAppIds();
        $_mem_cnt = count($_mem_ids);
        $_game_cnt = count($_app_ids);
        while ($_start_time <= $_end_time) {
            $_date = date('Y-m-d', $_start_time);
            $_rand_mem_cnt = rand(10, $_mem_cnt);
            $_rand_game_cnt = rand(1, $_game_cnt);
            for ($_game_i = 0; $_game_i < $_rand_game_cnt; $_game_i++) {
                $_game_id = $_app_ids[array_rand($_app_ids, 1)];
                for ($_mem_i = 0; $_mem_i < $_rand_mem_cnt; $_mem_i++) {
                    $_mem_id = $_mem_ids[array_rand($_mem_ids, 1)];
                    $_mem_data = MemCache::ins()->getInfoById($_mem_id);
                    if ($_mem_data['create_time'] > $_start_time + 86400) {
                        break;
                    }
                    $_time = $_start_time + rand(1, 86400);
                    $_login_cnt = rand(1, 200);
                    $_auth_cnt = rand(0, $_login_cnt);
                    $_sum_money = rand(0, 200);
                    $_diff = $_time - $_start_time;
                    $_first_pay_time = 0;
                    $_last_pay_time = 0;
                    $_order_cnt = 0;
                    $_order_suc_cnt = 0;
                    $_last_money = 0;
                    if (!empty($_sum_money)) {
                        $_order_cnt = rand(0, 200);
                        $_order_suc_cnt = rand(0, $_order_cnt);
                        if ($_order_suc_cnt > 0) {
                            $_first_pay_time = $_mem_data['create_time'] + rand(1, $_diff);
                        }
                        if ($_order_suc_cnt > 1) {
                            $_last_money = rand(0, $_sum_money);
                            if (!empty($_last_money)) {
                                $_last_pay_time = $_first_pay_time + rand(1, $_diff);
                            }
                        }
                    }
                    $_sum_real_money = rand(0, $_sum_money);
                    $_data = [
                        'date'           => $_date,
                        'mem_id'         => $_mem_id,
                        'agent_id'       => $_mem_data['agent_id'],
                        'app_id'         => $_game_id,
                        'reg_time'       => $_mem_data['create_time'],
                        'reg_days'       => Time::timeDateDiff($_mem_data['create_time'], $_time),
                        'device_id'      => '',
                        'device_cnt'     => 0,
                        'login_cnt'      => $_login_cnt,
                        'sum_money'      => $_sum_money,
                        'sum_real_money' => $_sum_real_money,
                        'first_pay_time' => $_first_pay_time,
                        'last_pay_time'  => $_last_pay_time,
                        'last_money'     => $_last_money,
                        'order_cnt'      => $_order_cnt,
                        'order_suc_cnt'  => $_order_suc_cnt,
                        'last_login_ip'  => Ip::ipToLong($_mem_data['reg_ip']),
                        'auth_cnt'       => $_auth_cnt,
                    ];
                    $_rs = (new DayMemLogModel())->insertLog($_data, true);
                    if (false !== $_rs) {
                        $_add_cnt++;
                    }
                }
            }
            $_start_time = $_start_time + 86400;
        }
        print_r($_add_cnt.' mem create ok'.PHP_EOL);
    }

    /**
     * @param int $cnt
     */
    public function addAgent($cnt = 100) {
        for ($_i = 0; $_i < $cnt; $_i++) {
            $_user_login = 'login'.$_i;
            $_user_nicename = $_user_login;
            $_user_pass = $_user_login;
            $_parent_id = 0;
            $_role_id = AgentConst::AGENT_ROLE_MP_AGENT;
            $_rs = (new Agent())->addAgent($_user_login, $_user_nicename, $_user_pass, $_parent_id, $_role_id);
            if (CommonStatus::NO_ERROR == $_rs['code']) {
                print_r('create ok'.PHP_EOL.$_i);
            }
        }
    }

    /**
     *
     *
     * //        (new MemberModel())->execute('TRUNCATE `h_member`;');
     * //        (new MemberExtModel())->execute('TRUNCATE `h_mem_ext`;');
     *
     * @param int    $cnt
     * @param string $start_date
     */
    public function addMember($cnt = 1000, $start_date = '') {
        $_cnt = $cnt;
        $_role_type = AgentConst::ROLE_TYPE_AGENT;
        $_role_ids = (new RoleModel())->getIdsByRoleType($_role_type);
        $_agent_ids = (new UserModel())->getIdsByRoleId($_role_ids);
        if (empty($start_date)) {
            $start_date = date('Y-m-d');
        }
        $_now = time();
        $_time = strtotime($start_date);
        $_ocnt = 0;
        $_app_ids = $this->getAppIds();
        for ($_i = 1; $_i < $_cnt; $_i++) {
            $_key = array_rand($_agent_ids, 1);
            $_ip = rand(0xFFFFF10F, 0xFFFFFFFF);
            $_time = $_time + rand(120, 600);
            if ($_time > $_now) {
                break;
            }
            $_username = 'testmem'.$_i * 3;
            $_status_rand = rand(0, 98);
            $_status = 1;
            if ($_status_rand) {
                $_status = 2;
            }
            $_mem_data = [
                'username'    => $_username,
                'nickname'    => $_username,
                'password'    => $_username,
                'reg_ip'      => Ip::longToIp($_ip),
                'create_time' => $_time,
                'agent_id'    => $_agent_ids[$_key],
                'status'      => $_status
            ];
            $_rs = (new MemberModel())->addMem($_mem_data);
            if (false != $_rs) {
                $_game_id = $_app_ids[array_rand($_app_ids, 1)];
                $_data = [
                    'date'          => date('Y-m-d', $_time),
                    'mem_id'        => $_rs,
                    'agent_id'      => $_agent_ids[$_key],
                    'app_id'        => $_game_id,
                    'reg_time'      => $_time,
                    'reg_days'      => '0',
                    'device_id'     => '',
                    'device_cnt'    => 0,
                    'login_cnt'     => 1,
                    'last_login_ip' => $_ip,
                    'auth_cnt'      => 0,
                ];
                (new DayMemLogModel())->insertLog($_data, true);
                $_ocnt++;
                print_r($_ocnt.' data create ok'.PHP_EOL);
            }
        }
//        print_r($_ocnt.' data create ok'.PHP_EOL);
    }
}