MonthTotal.php 3.37 KB
<?php

namespace App\Console\Commands;

use App\Modules\Models\GuideRecord\GuideRecord;
use App\Modules\Models\Production\Production;
use App\Modules\Models\Settlement\Finance;
//use App\Modules\Models\Settlement\Settlement;
use App\Modules\Models\Spot\Spot;
use Illuminate\Console\Command;

class MonthTotal extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'MonthTotal {--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');
//        }


        $deal_time = date("Ym", strtotime("$time -1 month"));


        $start_time = date("Y-m-01 00:00:00", strtotime("$time -1 month"));
        $end_time = date("Y-m-01 00:00:00", strtotime("$time"));
        $orders = 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();


        $res=GuideRecord::select('guide_record.spot_id','guide_record.business_id','guide_record.real_total')
            ->where('guide_record.pay_time', '>=', $start_time)
            ->where('guide_record.pay_time','<=',$end_time)
            ->where('guide_record.is_pay',1)
            ->get()
            ->toArray();



        $income_data = [];
        $date = [ ];


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

        if(!empty($res)){
            for($i=0;$i<count($res);$i++){
                if(!isset($date[$res[$i]['spot_id']]['real_total'])){
                    $date[$res[$i]['spot_id']]['real_total'] = 0;
                }
                $date[$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]['business_id'] = (int)$v['business_id'];
            $insert_data[$k]['spot_id'] = (int)$v['id'];
            $insert_data[$k]['month'] = (int)$deal_time;
            $insert_data[$k]['total'] = empty($income_data[$v['id']]['real_total']) ? 0 : $income_data[$v['id']]['real_total'];
            $insert_data[$k]['guide_total'] = empty($date[$v['id']]['real_total']) ? 0 : $date[$v['id']]['real_total'];
            $insert_data[$k]['created_at'] =date("Y-m-d H:i:s");
            $insert_data[$k]['updated_at'] =date("Y-m-d H:i:s");
        }


        $res = Finance::insert($insert_data);

    }
    }