<?php
/**
 * Created by PhpStorm.
 * User: tandunzhao
 * Date: 2017/11/20
 * Time: 下午5:26
 */

namespace App\Console\Commands;

use App\Modules\Finance\Services\FinanceService;
use Log;
use Illuminate\Console\Command;
use DB;
use Redis;
use App\Modules\Book\Services\BookSubscribleChapterService;

class ForceUserActiveDetail extends Command
{
    /**
     * 执行命令   php artisan force_user_active
     *
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'force_user_active_detail';

    /**
     * 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"));

        $_start = new \DateTime('2018-03-16');
        $_end = new \DateTime('2018-03-17');

        foreach (new \DatePeriod($_start, new \DateInterval('P1D'), $_end) as $d)
        {

            $date = $d->format('Y-m-d');
            $start = $date;
            $end = date('Y-m-d', strtotime($date) + 86400);

            $offset = 0;
            $limit = 10000;
            $data = [];
            $_m_data = [];

            while (true) {

                print_r("select send_order_id,uid,appid,unsubscribe_time,distribution_channel_id,created_at from force_subscribe_users where created_at > '{$start}' and created_at < '{$end}' limit $offset,$limit" . date("y-m-d H:i:s" . "\n"));
                $users = DB::select("select send_order_id,uid,appid,unsubscribe_time,distribution_channel_id,created_at from force_subscribe_users where created_at > '{$start}' and created_at < '{$end}' limit $offset,$limit");
                print_r("users_count:".count($users));
                if (count($users) == 0) break;

                foreach ($users as $user) {
                    $is_unsub_in_one_day = false;
                    $is_pay_in_one_day = false;

                    $_read_data = [
                        'date' => $date,
                        'appid' => $user->appid,
                        'uid' => $user->uid,
                        'sub_time' => strtotime($user->created_at),
                        'unsub_time' => strtotime($user->unsubscribe_time),
                    ];

                    $order = DB::table('orders')->where('uid', $user->uid)->where('status', 'PAID')->first();
                    if ($order) {
                        if (strtotime($order->created_at) < strtotime($user->created_at) + 86400)//24小时内
                        {
                            $_read_data['recharge_time_in_one_day'] = strtotime($order->created_at);
                            $is_pay_in_one_day = true;
                        }

                    }

                    $one_day_time = strtotime($user->created_at) + 86400;
                    if ($user->unsubscribe_time && strtotime($user->unsubscribe_time) < strtotime($user->created_at) + 86400)//24小时内取注
                    {
                        $is_unsub_in_one_day = true;
                        $_read_data['is_unsub_in_one_day'] = 1;
                    }

                    $last_read_info = Redis::hget('book_base:' . $user->uid, 'last_read');

                    $send_order = DB::table('send_orders')->where('id', $user->send_order_id)->first();
                    if ($send_order && $send_order->book_id && $last_read_info) {
                        $book = Redis::hget('book_read:' . $user->uid, $send_order->book_id);
                        $book_info = explode('_', $book);

                        $cid = $book_info[0];
                        $vip_chapter = DB::table('chapters')->select(['sequence'])->where('bid', $send_order->book_id)->where('is_vip', 1)->orderby('sequence')->first();

                        $read_last_free = DB::table('read_records')->where('bid', $send_order->book_id)->where('uid', $user->uid)->where('created_at', '<=', date('Y-m-d H:i:s', $one_day_time))->where('seq', $vip_chapter->sequence - 1)->first();

                        $last_read_array = explode('_', $last_read_info);
                        $last_read_time = $last_read_array[4];

                        //$_read_data['last_read_time'] = $last_read_time;
                        $_read_data['last_free_read_time'] = $read_last_free ? strtotime($read_last_free->created_at) : 0;

                        $book_config = DB::table('book_configs')->select(['force_subscribe_chapter_seq','charge_type'])->where('bid', $send_order->book_id)->first();
                        //订阅
                        if($book_config->charge_type == 'CHAPTER') {
                            $first_sub = DB::connection('chapter_order_mysql')->table('chapter_orders' . $user->uid % 512)->where('uid', $user->uid)->where('created_at', '<=', date('Y-m-d H:i:s', $one_day_time))->where('bid', $send_order->book_id)->orderBy('id', 'asc')->first();
                            $_read_data['first_sub_time'] = $first_sub ? strtotime($first_sub->created_at) : 0;
                        }

                        if($book_config->charge_type == 'BOOK') {
                            $_read_last_free = DB::table('read_records')->where('bid', $send_order->book_id)->where('uid', $user->uid)->where('seq', $vip_chapter->sequence - 1)->first();
                            $_read_data['first_sub_time'] = $_read_last_free ? strtotime($_read_last_free->created_at) : 0;
                        }
                    }

                    //第一次点击返回时间
                    $back_info = DB::table('back_actives')->where('uid',$user->uid)->first();
                    if($back_info)
                    {
                        $_read_data['first_click_back_time'] = strtotime($back_info->created_at);
                    }
                    $_read_data['created_at'] = date("Y-m-d H:i:s");
                    DB::table('force_users_date_read')->insert($_read_data);
                }

                $offset = $offset + $limit;

            }

            Log::info("======每日强关用户行为详细生成 【任务执行结束】=====" . date("y-m-d H:i:s" . "\n"));
            print_r("======每日强关用户行为详细生成 【任务执行结束】=====" . date("y-m-d H:i:s" . "\n"));


        }
    }
}