<?php

namespace App\Console\Commands\Temp;

use Log;
use Illuminate\Console\Command;
use App\Modules\Book\Services\NewBookTestService;
use App\Modules\Book\Services\BookService;
use DB;

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

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '判断新书测试类型';

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $book_tests = DB::table('new_book_tests')
            ->where('status',2 )
//            ->where('test_end_time','>',date('Y-m-d H:i:s',strtotime('-1 days')) )//24小时后不再重置状态
            ->get();
        $test_books = BookService::getBookStatisticsNew($book_tests);
        foreach ($test_books as $key=>&$item){

            if($item->sex == 2){
                $retention_rate_30 = $item->first_chapter_uv ? $item->thirtieth_chapter_uv / $item->first_chapter_uv : 0;
                $retention_rate_50 = $item->first_chapter_uv ? $item->fiftieth_chapter_uv / $item->first_chapter_uv : 0;
                $arpu_24h = round(($item->uv_in_one_day ? ($item->sub_amount_in_one_day / $item->uv_in_one_day) / 100 : 0), 2);
                //类型：优质书,爆款书,不达标
                //优质书筛选标准：24小时订阅UV比>0.65,30章留存率>5.32%,50章留存率>3.24%
                if ($arpu_24h < 0.65  ||  round($retention_rate_30 * 100, 2) < 5.32 || round($retention_rate_50 * 100, 2) < 3.24) {
                    $type = 1;//不达标
                }
                //爆款书标准：24小时订阅UV比>0.98,30章留存率>6.15%,50章留存率>4.36%
                elseif ($arpu_24h >= 0.98  &&  round($retention_rate_30 * 100, 2) >= 6.15 && round($retention_rate_50 * 100, 2) >= 4.36) {
                    $type = 3;//爆款书
                } else {
                    $type = 2;//优质书
                }
                DB::table('new_book_tests')->where('id',$item->id)->update(['type'=>$type]);
            }
            //男频
            if($item->sex == 1){
                $retention_rate_30 = $item->first_chapter_uv ? $item->thirtieth_chapter_uv / $item->first_chapter_uv : 0;
                $retention_rate_50 = $item->first_chapter_uv ? $item->fiftieth_chapter_uv / $item->first_chapter_uv : 0;
                $retention_rate_110 = $item->first_chapter_uv ? $item->chapter_uv_110 / $item->first_chapter_uv : 0;
                //即新书测试30章留存率大于1.91%且50章留存率大于1.35%且110章留存率大于0.89%且30-50章流失率小于38%的书，为男频优质书，否则视为不达标
                $u50_div_uv30 = $item->fiftieth_chapter_uv? $item->fiftieth_chapter_uv / $item->thirtieth_chapter_uv : 0;
                $lost_rate_50_30 = 1 - $u50_div_uv30;
                if($retention_rate_30 >= 1.91/100 && $retention_rate_50 >= 1.35/100 &&  $retention_rate_110 >= 0.89/100 && $lost_rate_50_30 <= 38/100){
                    $type = 2;//优质书
                }else{
                    $type = 1;//不达标
                }
                DB::table('new_book_tests')->where('id',$item->id)->update(['type'=>$type]);
            }
            DB::table('new_book_test_stats')->updateOrInsert(['test_id'=>$item->id]
                ,[
                    'type'=>$type,
                    'bid'=>$item->bid,
                    'sex'=>$item->sex,
                    'first_chapter_uv'=>$item->first_chapter_uv,
                    'thirtieth_chapter_uv'=>$item->thirtieth_chapter_uv,
                    'fiftieth_chapter_uv'=>$item->fiftieth_chapter_uv,
                    'chapter_uv_110'=>$item->chapter_uv_110,
                    'created_at'=>date('Y-m-d H:i:s'),
                    'updated_at'=>date('Y-m-d H:i:s'),
                ]);
        }
    }
}