<?php
/**
 * Created by PhpStorm.
 * User: sogndb
 * Date: 2018/02/05
 * Time: 下午5:26
 */

namespace App\Console\Commands\SendOrder;

use App\Modules\SendOrder\Services\SendOrderForceDayStatService;
use App\Modules\SendOrder\Services\SendOrderService;
use Illuminate\Console\Command;
use Log;
use DB;

class GenerateForceDayStat extends Command
{
    /**
     * 执行命令   php artisan send_order_generate_force_day_stat
     *
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'send_order:generate_force_day_stat';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '每日图书派单数据生成';

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        ini_set('memory_limit', '1280M');
        print_r("======每日图书派单数据生成 【任务执行开始】=====" . date("y-m-d H:i:s" . "\n"));
        Log::info("======每日图书派单数据生成 【任务执行开始】=====" . date("y-m-d H:i:s" . "\n"));

        $date = date("Y-m-d", strtotime(' -1 day'));
        try{
            SendOrderForceDayStatService::generateForceDayStat($date);
        }catch (\Exception $e){
            Log::info("======每日图书派单数据生成 =====".$e . date("y-m-d H:i:s" . "\n"));
        };


        Log::info("======每日图书派单数据生成 【任务执行结束】=====" . date("y-m-d H:i:s" . "\n"));
        print_r("======每日图书派单数据生成 【任务执行结束】=====" . date("y-m-d H:i:s" . "\n"));

        $i=1;
        $date = date("Y-m-d", strtotime(' -' . $i . ' day'));

        print_r("======每日男女频充值数据 【任务执行开始】=====" . date("y-m-d H:i:s" . "\n"));
        Log::info("======每日男女频充值数据 【任务执行开始】=====" . date("y-m-d H:i:s" . "\n"));

        //开始生成男女频充值数据
        $end_order_stats = DB::table('send_orders_force_day_stats')
            ->join('books','send_orders_force_day_stats.bid','=','books.id')
            ->join('book_categories','book_categories.id','=','books.category_id')
            ->where('send_orders_force_day_stats.date',$date)
            ->select([DB::raw('sum(send_orders_force_day_stats.recharge_amount) as total_recharge'),'pid'])
            ->groupBy('book_categories.pid')
            ->get();
        $male_recharge=$female_recharge=0;
        foreach ($end_order_stats as $end_order_stat){
            if($end_order_stat->pid==1) $male_recharge = $end_order_stat->total_recharge;
            if($end_order_stat->pid==2) $female_recharge = $end_order_stat->total_recharge;
        }
        $insert_data = compact('male_recharge','female_recharge');
        $insert_data['date'] = $date;
        $insert_data['month'] = date('Y-m',strtotime(' -'.$i.' day'));
        $insert_data['created_at']=date('Y-m-d H:i:s');
        $insert_data['updated_at']=date('Y-m-d H:i:s');
        DB::table('order_gender_stats')->insert($insert_data);

        Log::info("======每日男女频充值数据 【任务执行结束】=====" . date("y-m-d H:i:s" . "\n"));
        print_r("======每日男女频充值数据 【任务执行结束】=====" . date("y-m-d H:i:s" . "\n"));


        print_r("======书籍数据每日统计 【任务执行开始】=====" . date("y-m-d H:i:s" . "\n"));
        Log::info("======书籍数据每日统计 【任务执行开始】=====" . date("y-m-d H:i:s" . "\n"));

        $data = SendOrderForceDayStatService::getSendOrdersDayStatsByBid($date);

        $end_date = date('Y-m-d', strtotime($date) + 86400);
        $send_num_data = SendOrderService::getBookdSendNum($date, $end_date);

        $to_insert = json_decode(json_encode($data), true);
        $once_data = [];
        $once_num = 500;
        foreach ($to_insert as $_item) {
            $real_send_order_num = isset($send_num_data[$_item['bid']]) ? $send_num_data[$_item['bid']] : 0;
            $_item['real_send_order_num'] = $real_send_order_num;

            $once_data[] = $_item;

            if (count($once_data) >= $once_num) {
                DB::table('book_promotion_day_stats')->insert($once_data);
                $once_data = [];
            }
        }
        $once_data && DB::table('book_promotion_day_stats')->insert($once_data);

        Log::info("======书籍数据每日统计 【任务执行结束】=====" . date("y-m-d H:i:s" . "\n"));
        print_r("======书籍数据每日统计 【任务执行结束】=====" . date("y-m-d H:i:s" . "\n"));

    }
}