<?php

namespace App\Console\Commands;

use App\Models\Order;
use App\Models\OrderOverviewModel;
use App\Models\Project;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;

class GetOrdersOverview extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $name = 'ordersOverview';
    protected $signature = 'get:orders_overview';

    /**
     * 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 = strtotime('-1 month');
        $before_date = date('Y-m-01 00:00:00',$time);
        $date = date('Y-m-01 00:00:00');
        //获取到所有的通过审核的项目id
        $project_ids = Project::where('active',1)->where('id','>',1)->pluck('id')->toArray();
        foreach ($project_ids as $project_id)
        {
            $data = [
                'total_price' => 0,
            ];
            //获取到项目内已完成并且更新时间在上个月的订单id的数组
            $order_arr = Order::where('project_id',$project_id)
                                ->where('status',3)
                                ->where('type',1)
                                ->where('updated_at','>=',$before_date)
                                ->where('updated_at','<',$date)
                                ->get(['id','money','project_id'])->toArray();
            //如果订单数据为空那就添加一个空的数据条目到数据总览中
            if (empty($order_arr))
            {
                Log::info(Project::where('id',$project_id)->value('name').'项目'.date('Y-m',$time).'的数据总金额为空，默认写入空数据');
                $data['order_sn'] = Order::createOrderNumber();
                $data['date'] = $before_date;
                $data['project_id'] = $project_id;
                $data['status'] = 0;
                $data['confirmation_user_id'] = Project::where('id',$project_id)->value('user_id');
                $data['created_at'] = date('Y-m-d H:i:s',time());
                $data['updated_at'] = date('Y-m-d H:i:s',time());

                OrderOverviewModel::insert($data);
            }else
                {
                    Log::info(Project::where('id',$project_id)->value('name').'项目'.date('Y-m',$time).'的数据写入');
                    $num_arr = array_column($order_arr,'money');
                    $total = array_sum($num_arr);
                    $data['order_sn'] = Order::createOrderNumber();
                    $data['date'] = $before_date;
                    $data['project_id'] = $project_id;
                    $data['status'] = 0;
                    $data['confirmation_user_id'] = Project::where('id',$project_id)->value('user_id');
                    $data['total_price'] = $total;
                    $data['created_at'] = date('Y-m-d H:i:s',time());
                    $data['updated_at'] = date('Y-m-d H:i:s',time());
                    OrderOverviewModel::insert($data);
                }
        }

    }
}
