<?php

namespace App\Console\Commands\SendOrder;

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

    /**
     * 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()
    {
        //
        $this->getSuperiorBooks();
        return ;
        $beginLastweek = date('Y-m-d',mktime(0,0,0,date('m'),date('d')-date('w')+1-7,date('Y')));
        $endLastweek   = date('Y-m-d',mktime(23,59,59,date('m'),date('d')-date('w')+7-7,date('Y')));

        $data = SendStatsEmailService::getOrderStats($beginLastweek,$endLastweek);

        $page = view('email.weekOrderStats',['datas'=>$data]);
        $html = response($page)->getContent();
        //\Log::info($html);
        $data_temp = array();
        $header = ['书籍ID','书籍名称','UV','注册用户数','注册/UV','强关粉','强关率(强关/注册)','充值金额','付费人数','充值/UV','强关付费率(付费/强关)','UV付费率(付费/UV)','派单数','男女频'];
        foreach ($data as $k=>$item) {
            $title='';
            switch ($k){
                case 0:$title='注册UV比高于85%';break;
                case 1:$title='注册UV比介于30%到85%';break;
                case 2:$title='注册UV比低于30%';break;
            }
            $temp = array('header'=>$header,'title'=>$title,'data'=>array());
            foreach ($item as $key=>$value) {
                $temp['data'][] = array(
                    $value['bid'],
                    $value['book_name'],
                    $value['UV'],
                    $value['register_user_sum'],
                    $value['UV']>0?(round($value['register_user_sum']/$value['UV'],4)*100).'%':0,
                    $value['force_user_sum'],
                    $value['register_user_sum']>0?(round($value['force_user_sum']/$value['register_user_sum'],4)*100).'%':0,
                    $value['recharge_sum'],
                    $value['pay_user_num'],
                    $value['UV']>0?round($value['recharge_sum']/ $value['UV'],2):0,
                    $value['force_user_sum']>0?(round($value['pay_user_num']/$value['force_user_sum'],4)*100).'%':0,
                    $value['UV']>0?(round($value['pay_user_num']/$value['UV'],4)*100).'%':0,
                    $value['send_orders_num'],
                    $value['pid']
                );
            }
            $data_temp[] = $temp;
        }
        $storage_file = 'app/last_week_send_order.xlsx';
       saveExcelDataToMultiSheet($data_temp,storage_path($storage_file));

        $to_user = array(
            ['address'=>'songdb@iqiyoo.com','name'=>'songdb'],
            ['address'=>'zhaojp@yqsd.net','name'=>'赵君平'],
            ['address' => 'zhoulj@iqiyoo.com', 'name' => '周灵杰'],
        );

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


        $notify = MessageNotify::mail([
            'to_emails' => $to_users,
            'subject' => "上周( $beginLastweek 至 $endLastweek )外部派单作品分析报表",
            'body' => $html,
            'delay_times' => 0,
        ]);
        $notify->addAttach($storage_file);
        $notify->notify();
        //SendStatsEmailService::SendHtmlEmailWithAcce($to_user,['subject'=>"上周( $beginLastweek 至 $endLastweek )外部派单作品分析报表",'body'=>$html],storage_path('app/last_week_send_order.xlsx'));
    }

    private function getSuperiorBooks() {
        $data = SendStatsEmailService::getOrderStatsNew();

        $page = view('email.daySendOrderStats',['datas'=>$data]);
        $html = response($page)->getContent();
        //\Log::info($html);
        $data_temp = array();
        $header = ['书籍ID','书籍名称','是否下架','篇幅','24小时uv','24小时充值总额','72小时uv','72小时充值总额','uv','24~72小时充值增长','24小时充值比24小时UV','72小时充值比24小时UV','强关率'];
        foreach ($data as $k=>$item) {
            $title='';
            switch ($k){
                case 0:$title='注册UV比高于85%';break;
                //case 1:$title='注册UV比介于30%到85%';break;
                case 1:$title='注册UV比低于30%';break;
            }
            $temp = array('header'=>$header,'title'=>$title,'data'=>array());
            foreach ($item as $key=>$value) {
                $temp['data'][] = array(
                    $value['bid'],
                    $value['book_name'],
                    $value['is_on_shelf']==1?'内部上架':'外部上架',
                    $value['charge_type']=='BOOK'?'短篇':'长篇',
                    $value['uv_one_day_sum'],
                    $value['recharge_amount_in_one_day'],
                    $value['uv_three_day_sum'],
                    $value['recharge_amount_in_three_days'],
                    $value['UV'],
                    $value['recharge_amount_in_one_day']>0?round(($value['recharge_amount_in_three_days']-$value['recharge_amount_in_one_day'])/$value['recharge_amount_in_one_day'],2):0,
                    $value['uv_one_day_sum']>0?round(($value['recharge_amount_in_one_day']/$value['uv_one_day_sum']),2):0,
                    //$value['uv_three_day_sum']>0?round(($value['recharge_amount_in_three_days']/$value['uv_three_day_sum']),2):0,
                    $value['uv_one_day_sum']>0?round(($value['recharge_amount_in_three_days']/$value['uv_one_day_sum']),2):0,
                    $value['register_user_sum']>0?(round(($value['force_user_sum']/$value['register_user_sum']),4)*100).'%':0,

                );
            }
            $data_temp[] = $temp;
        }
        $storage_file = 'app/last_week_send_order.xlsx';
        saveExcelDataToMultiSheet($data_temp,storage_path($storage_file));

        $to_user = array(
            ['address'=>'zhangzg@iqiyoo.com','name'=>'张总'],
            ['address'=>'chensj@zw88.net','name'=>'陈帅君'],
            ['address'=>'zhaojp@yqsd.net','name'=>'赵君平'],
            ['address'=>'liur@iqiyoo.com','name'=>'刘嵘'],
            ['address'=>'panxp@zw88.net','name'=>'潘霞萍'],
            ['address'=>'2711046732@qq.com','name'=>'樊柯'],
//            ['address'=>'lisy@zw88.net','name'=>'test'],

        );


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


        $notify = MessageNotify::mail([
            'to_emails' => $to_users,
            'subject' => '追书云'.date('Y-m-d',strtotime('-1 day'))."优质书外部派单分析报表",
            'body' => $html,
            'delay_times' => 0,
        ]);
        $notify->addAttach($storage_file);
        $notify->notify();
        //SendStatsEmailService::SendHtmlEmailWithAcce($to_user,['subject'=>'追书云'.date('Y-m-d',strtotime('-1 day'))."优质书外部派单分析报表",'body'=>$html],storage_path('app/last_week_send_order.xlsx'));
    }
}