<?php

namespace app\index\controller;

use think\Controller;
use think\Db;
use think\Cache;

class Plantask extends Controller
{
    //计划任务
    //定时跑用户活跃，改成离线
    public function user_active(){
        $actitime = time() - 600;
        //$map = ['requesttime' < $actitime];

        $sql = 'update `mt_user` set is_active = 0 where id in (select user_id from mt_user_active where requesttime < '.$actitime.')';
        //echo $sql;
        db()->query($sql);
    }
    //计划任务
    //代替公会的人10秒内发出五句话
    public function firstuser_send()
    {
        //找出24小时内注册的男人
        $map = [
            'jointime' => ['gt',time()-86400],
            'gender' => 1,
            'gh_id'  => 0,
        ];
        $oneuser = Db::name('user')->where($map)->orderRaw('rand()')->value('id');
        //dump($oneuser);

        //找出公会的人
        $map = [
            'gh_id'  => ['gt',0],
        ];
        $ghuser = Db::name('user')->where($map)->orderRaw('rand()')->limit(5)->column('id');
        //dump($ghuser);

        //随机取出一句话
        $oneword = Db::name('plantask_accost')->orderRaw('rand()')->limit(5)->column('title');
        //dump($oneword);

        //发送出去
        $cache = Cache::connect(['type'=>'Redis']);
        $times = $cache->get('plantask_first_word_'.$oneuser);
        //dump($times);

        if($times === false){
            $times = 0;
        }

        if($times < 5){
            $tenim = new \app\common\library\Tenim;
            for($i = 0;$i < 5;$i++){
                $ghuser_one  = isset($ghuser[$i])  ? $ghuser[$i]  : $ghuser[array_rand($ghuser)];
                $oneword_one = isset($oneword[$i]) ? $oneword[$i] : $oneword[array_rand($oneword)];
                $tenim->sendMessageToUser($ghuser_one,$oneuser,$oneword_one);
            }
            $cache->set('plantask_first_word_'.$oneuser, 5);
        }

    }
    //计划任务，二期之后，废弃
    //代替公会的人发出第一句话
    public function firstword_send()
    {
        //找出24小时内注册的男人
        $map = [
            'jointime' => ['gt',time()-86400],
            'gender' => 1,
            'gh_id'  => 0,
        ];
        $oneuser = Db::name('user')->where($map)->orderRaw('rand()')->value('id');
        //dump($oneuser);

        //找出公会的人
        $map = [
            'gh_id'  => ['gt',0],
        ];
        $ghuser = Db::name('user')->where($map)->orderRaw('rand()')->value('id');
        //dump($ghuser);

        //随机取出一句话
        $oneword = Db::name('plantask_accost')->orderRaw('rand()')->value('title');
        //dump($oneword);

        //发送出去
        $cache = Cache::connect(['type'=>'Redis']);
        $times = $cache->get('plantask_first_word_'.$oneuser);
        //dump($times);

        if($times === false){
            $times = 0;
        }
        if($times < 5){
            $tenim = new \app\common\library\Tenim;
            $tenim->sendMessageToUser($ghuser,$oneuser,$oneword);
            $cache->set('plantask_first_word_'.$oneuser, $times + 1);
        }

    }

    //计划任务
    //清空没用的redis
    public function firstword_clear(){
        $map = [
            'jointime' => ['between',[time()-172800,time()-86400]],
            'gender' => 1,
            'gh_id'  => 0,
        ];
        $map = [];

        $userlist = Db::name('user')->where($map)->order('id asc')->column('id');

        if(empty($userlist)){
            echo 'empty';
            exit;
        }

        //清除
        $cache = Cache::connect(['type'=>'Redis']);
        foreach($userlist as $key => $val){
            $cache->rm('plantask_first_word_'.$val);
        }
    }

    //测试用
    //清空redis
    public function clear_redis(){
        $val = input('uid');
        $cache = Cache::connect(['type'=>'Redis']);
        $cache->rm('plantask_first_word_'.$val);
    }

}
