<?php

namespace App\Console\Commands\Temp;

use Log;
use Illuminate\Console\Command;
use DB;
use App\Modules\User\Services\ReadRecordService;
use Redis;
class SelectPayUser extends Command
{
    /**
     * 执行命令   php artisan tmep:user_sign
     *
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'temp:select_user_pay';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '查付费用户情况';

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {

        print_r("======查付费用户情况 【任务执行开始】=====".date("y-m-d H:i:s"."\n"));
        Log::info("======查付费用户情况 【任务执行开始】=====".date("y-m-d H:i:s"."\n"));
        $date = date('Y-m-d');
        /**
         * update temp_pay_users,users set temp_pay_users.charge_balance = users.charge_balance where users.id = temp_pay_users.uid
        清空 update temp_pay_users set bids = null,latest_read_at=null,to_do_date=null,to_do_fee=null
         */

        //2个月前付费用户id

        //更新付费用户实际余额
        print_r("=======更新付费用户实际余额 start=====");
        DB::update("update temp_pay_users,users set temp_pay_users.charge_balance = users.charge_balance,has_done = 0 where users.id = temp_pay_users.uid");
        print_r("=======更新付费用户实际余额 end=====");

        //查最近的阅读记录是否在1个月内没有记录

        //先更新阅读记录
        //选用户去除包年

        //$bids = [1697,1148,1345,1342,1340,1696,1311,1368,1475,1460,1900,1347,1014,1965,10,1005,1858,1580,1017,1550,1528,1634,1301,857,930,1625,1851,1458,1826,1021,1094,1695,1823,1792,1877,917,1903,1987,1685,1566,1406,1488,1878,1677,1852,1304,1905,1589,907,1924,901];
        $bids = [1697,1148,1345,1342,1340,1696,1311,1368,1475,1460,1900,1347,1014,1965,10,1005,1858,1580,1017,1550,1528,1634,1301,857,930,1625,1851,1458,1826,1021,1094,1695,1823,1792,1877,917,1903,1987,1685,1566,1406,1488,1878,1677,1852,1304,1905,1589,907,1924,901,636,613,614,640,633,632,630,631,689,1249,615,617,625,626,627,324,750,762,733,732,779,754,794,807,766,691,692,628,853,856,852,854,845,837,835,834,832,831,830,836,884,883,864,876,871,870,869,868,867,873,959,960,961,962,979,968,970,969,976,974,967];
        $day_max_fee = 500000;//日上限
        $day_user_max_fee = 1500;//单日用户最大消耗
        $day_user_min_fee = 200;//单日用户最小消耗
        $user_min_left_fee = 1;//用户剩余充值币
        //充值书币>1000
        $to_do_real_fee = 0;

        //uid黑名单
        $black_uids = DB::table('temp_pay_black_users')->pluck('uid')->all();

        DB::table('temp_pay_users')->where('charge_balance','>=',$user_min_left_fee)->whereNotIn('uid',$black_uids)->orderBy('done_num','asc')->chunk(1,function ($users) use ($bids,$day_user_max_fee,$day_user_min_fee,$day_max_fee,&$to_do_real_fee){
            foreach ($users as $user)
            {
                if($to_do_real_fee >= $day_max_fee)
                {
                    dd('reach day max fee: '.$to_do_real_fee);
                }

                $read_record_info =  Redis::hgetall('book_read:' . $user->uid);
                $last_record = isset($read_record_info['last_read']) ? explode('_',$read_record_info['last_read']) : null;

                $latest_read_at = $last_record ? $last_record[2] : 0;

                //判断最近一个月是否有阅读 有则continue 无阅读记录可能存储有误 刷过的用户不限制
                if(!$latest_read_at || (time() - $latest_read_at < 4*30*86400 && $user->done_num==0)) continue;

                shuffle($bids);//打乱bids顺序

                $read_bids = [];//阅读过的bid列表  todo

                foreach ($read_record_info as $k=>$_re)
                {
                    is_numeric($k) && $read_bids[] = $k;
                }

                //查用户阅读记录里的图书是否有哪本书在书单内
                //如果有之前阅读中的图书则继续阅读
                foreach ($read_bids as $k=>$_bid)
                {
                    $is_read_to_last = false;
                    //判断是否订阅完毕 todo
                    if(in_array($_bid,$bids) && !$is_read_to_last)
                    {
                        unset($read_bids[$k]);
                        array_unshift($bids,$_bid);
                    }
                }

                //实际刷的bid顺序为$bids
                $to_do_date = date('Y-m-d',time()+86400);
                $to_do_fee = min(mt_rand($day_user_min_fee,$day_user_max_fee),$user->charge_balance);//不超过总充值余额
                $latest_read_at = date('Y-m-d H:i:s',$latest_read_at);
                $bids = implode(',',$bids);
                $up_data = compact('bids','latest_read_at','to_do_date','to_do_fee');
                dump($up_data);
                if(DB::table('temp_pay_users')->where('id',$user->id)->update($up_data))
                {
                    $to_do_real_fee += $to_do_fee;
                }
            }
        });

        Log::info("======查付费用户情况 【任务执行结束】=====" . date("y-m-d H:i:s" . "\n"));
        print_r("======查付费用户情况 【任务执行结束】=====" . date("y-m-d H:i:s" . "\n"));
    }
}