<?php

namespace App\Console\Commands;

use App\Modules\User\Services\UserSignService;
use Illuminate\Console\Command;
use DB;
use Redis;
use App\Jobs\SendTexts;
use App\Modules\User\Services\ReadRecordService;
use App\Modules\User\Services\UserService;
use App\Modules\Book\Services\BookConfigService;
use Hashids;
use App\Modules\Activity\Models\Activity;
use App\Modules\Activity\Services\ActivityService;
class SignReward extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'sign:reward {--record}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '签到奖励';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $option = $this->option('record');
        if($option){
            $this->signRecord();
        }else{
            $this->signPushMsg();
        }
    }

    public function saveSignInfoAndReward($option)
    {
        if ($option) {
            $sign_key = 'sign-' . date('Y-m-d', time() - 86400);
        } else {
            $sign_key = 'sign-' . date('Y-m-d');
        }
        //print_r($sign_key);
        //return 1;
        $res = Redis::hgetall($sign_key);
        $sign_data = [];
        $uid_arr = [];
        if (!$res) return -1;

        foreach ($res as $key => $v) {
            if (Redis::SISMEMBER($sign_key, $key)) {
                continue;
            }
            $uid_arr[] = $key;
            $sign_data[] = ['uid' => $key, 'price' => 50, 'sign_time' => $v, 'created_at' => date('Y-m-d H:i:s'), 'updated_at' => date('Y-m-d H:i:s')];
            Redis::sadd($sign_key, $key);
        }
        DB::table('user_sign')->insert($sign_data);
        DB::table('users')->whereIn('id', $uid_arr)->increment('balance', 50);
        DB::table('users')->whereIn('id', $uid_arr)->increment('reward_balance', 50);
        return 0;
    }

    public function signRecord(){
        DB::table('order_day_stats')->select('id', 'distribution_channel_id', 'date')->where('date',date('Y-m-d',time()-86400))->orderBy('id')->chunk(1000, function ($res) {
            foreach ($res as $v) {
                $sql = "select count(*) as count from user_sign join users on user_sign.uid=users.id where date(user_sign.created_at)='%s' and user.distribution_channel_id=%s";
                $user_sign_info = DB::select($sql);
                DB::table('order_day_stats')->where('id',$v->id)->update([
                    'sign_num'=>(int)$user_sign_info[0]->count
                ]);
            }
        });
    }

    public function signPushMsg()
    {
        $sign_key = 'leyuee:wap:usersigni';
        //Redis::sadd($sign_key, 9);
        $records = Redis::hgetall($sign_key);
        if ($records ) {
            //$uids = Redis::SMEMBERS($sign_key);
            Redis::del($sign_key);
            $uids = [];
            foreach ($records as $u=>$p){
                $uids[] = $u;
            }
            if (empty($uids)) return false;
            $subscribe_user = DB::table('force_subscribe_users')->whereIn('uid', $uids)->where('is_subscribed', 1)->select('uid', 'appid', 'openid', 'distribution_channel_id')->get();
            if ($subscribe_user) {
                $delay = 0;
                foreach ($subscribe_user as $keys => $value) {
                    $content = UserSignService::userSignReturnContent3($value->openid);
                    $data['openid'] = $value->openid;
                    $data['appid'] = $value->appid;
                    $data['content'] = $content;
                    $data['type'] = 'one_task';
                    $data['send_time'] = date("Y-m-d H:i:s");
                    $data['task_id'] = md5(123);
                    $send_data = array(
                        'send_time' => date("Y-m-d H:i:s"),
                        'data' => $data
                    );
                    //print_r($send_data);
                    dispatch((new SendTexts($send_data))->onConnection('rabbitmq')->delay($delay)->onQueue('send_texts_list'));
                }
            }
            return false;
        }
        return false;
    }
}
