<?php

namespace App\Console\Commands\ProductStats;

use App\Modules\Statistic\Services\ProductionStatsEmailService;

use App\Modules\Statistic\Services\SendStatsEmailService;
use Illuminate\Console\Command;
use DB;

class SendDailyProductionStats extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'SendDailyProductsStats';

    /**
     * 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()
    {
        //
        $date = date('Y-m-d',strtotime('-1 day'));
        $data = DB::table('product_stats')
            ->where([['date','=',$date],['type','=','day']])
            ->first();
        if($data) {
            $data = json_decode(json_encode($data),true);
            $data['date'] = $date;
        }else{
            $data = ProductionStatsEmailService::getAll(date('Y-m-d 00:00:00',strtotime('-1 day')),date('Y-m-d 23:59:59',strtotime('-1 day')));
            $data['date'] = $date;
        }

        $suspends = ProductionStatsEmailService::getSuspendProducts(false);
        $suspends_format = array();
        foreach ($suspends as $suspend){
            $time_diff = time()-strtotime($suspend->updated_at);
            $product_type = $suspend->is_on_shelf;
            switch ($product_type){
                case 1:$product_type = '内部作品';break;
                case 2:$product_type = '外部作品';break;
                default:;
            }
            $suspends_format[] = array($suspend->id,$suspend->name,$suspend->pid,$suspend->updated_at,round(($time_diff)/3600,1),round($time_diff/(3600*24),1),$product_type);
        }
        $suspends_header = ['bid','书名','一级分类','最近更新','累计断更时长（小时）','断更天数','作品类型'];
        saveExcelData($suspends_header ,$suspends_format,storage_path('app/suspend_books'.date('Y-m-d').'.xlsx'));
        $to_user = array(
                ['address'=>'zhangzg@iqiyoo.com','name'=>'张总'],
                //['address'=>'1373617746@qq.com','name'=>'tusx'],
                ['address'=>'songdb@iqiyoo.com','name'=>'songdb'],
                ['address'=>'zhaojp@yqsd.net','name'=>'赵君平'],
                ['address'=>'huangrr@yqsd.net','name'=>'黄蓉蓉'],
                ['address'=>'wankw@zw88.net','name'=>'万开伟'],
                ['address'=>'liur@iqiyoo.com','name'=>'刘嵘'],
                ['address'=>'chensj@zw88.net','name'=>'chensj'],
                ['address'=>'sijj@yqsd.net','name'=>'sijj'],
                ['address'=>'yuyy@yqsd.net','name'=>'俞焰艳']
        );

        $page = view('email.productStats',['data'=>$data,'datas2'=>$suspends_format,'title'=>$date.' 作品数据报表','title2'=>$date.' 断更作品']);
        $html = response($page)->getContent();
        SendStatsEmailService::SendHtmlEmailWithAcce($to_user,['subject'=>"每日作品数据报表",'body'=>$html],storage_path('app/suspend_books'.date('Y-m-d').'.xlsx'));

    }
}
