<?php

namespace App\Console\Commands\ProductStats;

use App\Modules\Statistic\Services\ProductionStatsEmailService;

use App\Modules\Statistic\Services\SendStatsEmailService;
use Illuminate\Console\Command;
use DB;
use App\Modules\Message\MessageNotify;
class SendDailyNewProductionStats extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'SendDailyNewProductsStats';

    /**
     * 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'));
       // $date = date('Y-m-d',strtotime('-3 day'));
        $new_products = DB::select("SELECT books.id,books.name,book_configs.is_on_shelf,book_categories.pid FROM book_updates
 JOIN books ON books.id=book_updates.bid JOIN book_configs ON book_configs.bid=books.id
  LEFT JOIN book_categories ON book_categories.id=books.category_id
  WHERE book_updates.update_date='{$date}'
 AND book_configs.is_on_shelf IN (1,2) AND (book_updates.update_type='add_book' OR (book_updates.update_type='onshelfstatus'
  AND book_updates.update_chapter_count=0 AND update_words IN (1,2))) group by book_updates.bid");

        //$suspends = ProductionStatsEmailService::getSuspendProducts(false);
        $stats=array(
            ['total'=>0,'internal'=>0,'date'=>$date],
            ['total'=>0,'internal'=>0,'date'=>$date],
            ['total'=>0,'internal'=>0,'date'=>$date]
        );
        $book_list = array();
        foreach ($new_products as $product){
            $stats[0]['total'] = $stats[0]['total'] + 1;
            if($product->is_on_shelf == 1) {
                $stats[0]['internal']++;
            }
            if(isset($product->pid) && $product->pid==1) {
                $stats[1]['total'] = $stats[1]['total'] + 1;
                if($product->is_on_shelf == 1) {
                    $stats[1]['internal']++;
                }
            }
            if(isset($product->pid) && $product->pid==2) {
                $stats[2]['total'] = $stats[2]['total'] + 1;
                if($product->is_on_shelf == 1) {
                    $stats[2]['internal']++;
                }
            }
            $gender ='';
            switch ($product->pid){
                case 1:$gender='男频';break;
                case 2:$gender='女频';break;
                default :$gender='未知';
            }
            $book_list[] = array($product->id,$product->name,$gender,($product->is_on_shelf==1)?'内部上架':'外部上架');
        }
        $books_header = ['bid','书名','一级分类','作品类型'];
        $storage_file = 'app/new_books'.date('Y-m-d').'.xlsx';
        saveExcelData($books_header ,$book_list,storage_path($storage_file));
        $to_user = array(
            ['address'=>'songdb@iqiyoo.com','name'=>'songdb'],
            ['address'=>'zhaojp@yqsd.net','name'=>'赵君平'],
            ['address' => 'zhoulj@iqiyoo.com', 'name' => '周灵杰'],
        );

        $page = view('email.newProductsStats',['data'=>$stats,'datas2'=>$book_list,'title'=>$date.' 作品数据报表','title2'=>$date.' 新增作品']);
        $html = response($page)->getContent();

        $to_users = '';
        foreach ($to_user as $item){
            $to_users .= $item['address'].";";
        }


        $notify = MessageNotify::mail([
            'to_emails' => $to_users,
            'subject' => '网读云'."新增作品统计",
            'body' => $html,
            'delay_times' => 0,
        ]);
        $notify->addAttach($storage_file);
        $notify->notify();

        //SendStatsEmailService::SendHtmlEmailWithAcce($to_user,['subject'=>'追书云'."新增作品统计",'body'=>$html],storage_path('app/new_books'.date('Y-m-d').'.xlsx'));

    }
}
