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

namespace App\Console\Commands\SendOrder;

use App\Modules\SendOrder\Models\SendOrderForceDayStat;
use App\Modules\SendOrder\Services\SendOrderForceDayStatService;
use App\Modules\SendOrder\Services\SendOrderStatService;
use Illuminate\Console\Command;
use Log;
use DB;

class GenerateDayByBooksStat 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_stats_by_bid';

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

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {

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

        $data = SendOrderForceDayStatService::getSendOrdersDayStatsByBid($date);
        $to_insert = json_decode(json_encode($data),true);
        \DB::table('book_promotion_day_stats')->insert($to_insert);
        $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['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);
    }
}