TestCreate.php 3.03 KB
<?php

namespace App\Console\Commands;

use App\Modules\Models\DayIncome\DayIncome;
use App\Modules\Models\GuideRecord\GuideRecord;
use App\Modules\Models\Production\Production;
use App\Modules\Models\Spot\Spot;
use Illuminate\Console\Command;

class TestCreate extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'TestCreate {--day=}';

    /**
     * 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()
    {



        $time = $this->option('day');

        if( empty($time) ){
            $time = date('Y-m-d');
        }

        $day = date( "Y-m-d", strtotime( "$time -1 day" ) );

        $start_time = $day.' 00:00:00';
        $end_time = $day.' 23:59:59';


        //昨天的钱数讲解器租借
        $info=Production::select('rent.spot_id','rent.business_id','rent.real_total','production.return_time')
            ->leftJoin('rent', 'production.rent_id', '=', 'rent.id')
            ->where('production.return_time', '>=', $start_time)
            ->where('production.return_time','<=',$end_time)
            ->where('production.is_return',1)
            ->get()
            ->toArray();


        $data = [];
        $data1 =[];
        //每天同游共听的钱
        $where = [
            ['is_pay', '=', 1],
            ['pay_time', '>=', $start_time],
            ['pay_time', '<=', $end_time],
        ];
        $res=GuideRecord::where($where)->get();


        if(!empty($info)){
            for($i=0;$i<count($info);$i++){
                if(!isset( $data[ $info[$i]['spot_id'] ]['real_total'])){
                    $data[ $info[$i]['spot_id'] ]['real_total'] = 0;
                }
                $data[ $info[$i]['spot_id'] ]['real_total'] += $info[$i]['real_total'];
            }
        }

        if(!empty($res)){
            for($i=0;$i<count($res);$i++){
                if(!isset($data1[$res[$i]['spot_id']]['real_total'])){
                    $data1[$res[$i]['spot_id']]['real_total']=0;
                }
                $data1[$res[$i]['spot_id']]['real_total']+=$res[$i]['real_total'];
            }

        }

        $insert_data = [];
        $spots = Spot::select('*')->get()->toArray();
        foreach ( $spots as $k => $v ){
            $insert_data[$k]['day'] = $day;
            $insert_data[$k]['business_id'] =  (int)$v['business_id'];
            $insert_data[$k]['spot_id'] = (int)$v['id'];
            $insert_data[$k]['total'] = empty($data[$v['id']]['real_total']) ? 0 :$data[$v['id']]['real_total'];
            $insert_data[$k]['guide_total'] = empty($data1[$v['id']]['real_total'])? 0: $data1[$v['id']['real_total']];
        }

        $res = DayIncome::insert($insert_data);
        
    }
}