<?php

namespace App\Console\Commands\ProductStats;

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

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

    /**
     * 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()
    {
        //
        $lastMonthBegin = date('Y-m-01',strtotime('-1 month'));
        $lastMonthEnd   = date('Y-m-d',strtotime(date('Y-m-01').' -1 day'));

        $data = SendStatsEmailService::getOrderStats($lastMonthBegin,$lastMonthEnd);
        //var_dump($data);
        $data = json_decode(json_encode($data),true);
        $page = view('email.monthStats',['datas'=>$data,'month'=>date('m月',strtotime('-1 month'))]);
        $html = response($page)->getContent();
        //\Log::info($html);
        $data_temp = array();
        foreach ($data as $key=>$value){
            $data_temp[] = array(
                $value['bid'],
                $value['book_name'],
                $value['order_num'],
                $value['UV'],
                $value['register_user_sum'],
                $value['recharge_sum'],
                $value['pid'],
                $value['order_num']>0?round($value['register_user_sum']/$value['order_num'],2):0,
                $value['order_num']>0?round($value['recharge_sum']/$value['order_num'],2):0,
                $value['register_user_sum']>0?round($value['recharge_sum']/$value['register_user_sum'],2):0
            );
        }
        $header = ['书籍ID','书籍名称','实际派单数','UV','注册用户数','充值','男女频','注册/派单','充值/派单','充值/注册'];
        saveExcelData($header,$data_temp,storage_path('app/last_month_send_order.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'=>'俞焰艳'],

        );
        SendStatsEmailService::SendHtmlEmailWithAcce($to_user,['subject'=>date('m月',strtotime('-1 month'))."外部派单作品分析报表(修订)",'body'=>$html],storage_path('app/last_month_send_order.xlsx'));
    }
}
