<?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 SendMonthlyProductStats extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'SendMonthlyProductsStats';

    /**
     * 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-01',strtotime('-1 month'));
        $data = DB::table('product_stats')
            ->where([['date','=',$date],['type','=','month']])
            ->first();
        if($data) {
            $data = json_decode(json_encode($data),true);
            $data['date'] = date('Y-m',strtotime('-1 month'));
        }else{
            $data = ProductionStatsEmailService::getAll(date('Y-m-01 00:00:00',strtotime('-1 month')),date('Y-m-d 23:59:59',strtotime(date('Y-m-01').'-1 day')));
            $data['date'] = date('Y-m',strtotime('-1 month'));
        }
        $to_user = array(
            ['address'=>'zhangzg@iqiyoo.com','name'=>'张总'],
            //['address'=>'tushengxiang@yahoo.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,'title'=>date('m月',strtotime('-1 month')).' 作品数据报表']);
        $html = response($page)->getContent();

        SendStatsEmailService::SendHtmlEmailWithAcce($to_user,['subject'=>date('m月',strtotime('-1 month'))."作品数据报表",'body'=>$html]);

    }
}
