<?php

use Illuminate\Database\Seeder;

class InnerOrderSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $total = 20;

        $this->call([
            InnerDeviceStatusSeeder::class
        ]);

        $statuses = \App\Models\Option::get('inner_devices', 'status');
        \App\Models\InnerDevice::truncate();
        $specs = \App\Models\Spec::all();
        for($i = 0; $i < $total * 10; ++$i) {
            $spec = $specs->random();
            \App\Models\InnerDevice::create([
                'name' => '内部设备' . ($i + 1),
                'device_id' => $spec->device_id,
                'device_name_id' => $spec->device_name_id,
                'spec_id' => $spec->id,
                'status' => $statuses->whereIn('key', ['free', 'error'])->first()->id,
                'number' => mt_rand(10000, 99999),
                'produce_date' => '2020-01-01',
                'shape' => '180cm*180cm',
                'buy_origin' => '100',
                'manufacturer' => '机械制造厂'
            ]);
            \App\Models\InnerDevice::create([
                'name' => '内部设备' . ($i + 1),
                'device_id' => $spec->device_id,
                'device_name_id' => $spec->device_name_id,
                'spec_id' => $spec->id,
                'status' => $statuses->where('key', 'free')->first()->id,
                'number' => mt_rand(10000, 99999),
                'produce_date' => '2020-01-01',
                'shape' => '180cm*180cm',
                'buy_origin' => '100',
                'manufacturer' => '机械制造厂'
            ]);
        }

        $project = \App\Models\Project::first();
        $work_points = \App\Models\WorkPoint::all();
        $user = \App\Models\User::first();
        $project_roles = \App\Models\ProjectRole::limit(7)->get();
        $project_role = $project_roles->where('key', 'machine')->first();

        // 草稿
        for($i = 1; $i < $total; ++$i) {
            $order = \App\Models\Order::create([
                'work_point_id' => $work_points->random()->id,
                'is_draft' => 1,
                'status' => \App\Models\Option::get('orders', 'status', 'checking'),
                'order_number' => \App\Models\Order::createOrderNumber(),
                'project_id' => $project->id,
                'user_id' => $user->id,
                'project_role_id' => $project_role->id,
                'type' => 2
            ]);

            $this->createOrderDevice($order);
        }

        // 待审核，checking
        for($i = 1; $i < $total; ++$i) {
            $order = \App\Models\Order::create([
                'work_point_id' => $work_points->random()->id,
                'is_draft' => 2,
                'status' => \App\Models\Option::get('orders', 'status', 'checking'),
                'order_number' => \App\Models\Order::createOrderNumber(),
                'project_id' => $project->id,
                'user_id' => $user->id,
                'project_role_id' => $project_roles->first()->id,
                'type' => 2
            ]);

            $this->createOrderDevice($order);
        }

        // 已审核，checked
        for($i = 1; $i < $total; ++$i) {
            $last_project_role = $project_roles->whereIn('key', ['assist', 'manager', 'admin'])->random();
            $project_role = $project_roles->where('key', 'machine')->first();
            if($last_project_role->key != 'admin') {
                $project_role = $project_roles->where('need_check_inner', 1)->where('level', '>', $last_project_role->level)->first();
            }
            $order = \App\Models\Order::create([
                'work_point_id' => $work_points->random()->id,
                'is_draft' => 2,
                'status' => \App\Models\Option::get('orders', 'status', 'checked'),
                'order_number' => \App\Models\Order::createOrderNumber(),
                'project_id' => $project->id,
                'user_id' => $user->id,
                'project_role_id' => $project_role->id,
                'last_project_role_id' => $last_project_role->id,
                'type' => 2
            ]);

            $this->createOrderDevice($order);
        }

        // 已完成，pass
        for($i = 1; $i < $total; ++$i) {

            $last_project_role = $project_roles->where('key', 'admin')->first();
            $project_role = $project_roles->where('key', 'machine')->first();

            $order = \App\Models\Order::create([
                'work_point_id' => $work_points->random()->id,
                'is_draft' => 2,
                'status' => \App\Models\Option::get('orders', 'status', 'pass'),
                'order_number' => \App\Models\Order::createOrderNumber(),
                'project_id' => $project->id,
                'user_id' => $user->id,
                'project_role_id' => $project_role->id,
                'last_project_role_id' => $last_project_role->id,
                'type' => 2
            ]);

            $this->createOrderDevice($order);
        }

        // 已驳回，pass
        for($i = 1; $i < $total; ++$i) {
            $last_project_role = $project_roles->whereIn('key', ['assist', 'manager', 'admin'])->random();
            $project_role = $project_roles->where('key', 'machine')->first();
            $order = \App\Models\Order::create([
                'work_point_id' => $work_points->random()->id,
                'is_draft' => 2,
                'status' => \App\Models\Option::get('orders', 'status', 'reject'),
                'order_number' => \App\Models\Order::createOrderNumber(),
                'project_id' => $project->id,
                'user_id' => $user->id,
                'project_role_id' => $project_role->id,
                'last_project_role_id' => $last_project_role->id,
                'type' => 2
            ]);

            $this->createOrderDevice($order);
        }

        $this->call([
            NotificationSeeder::class
        ]);
    }

    public function createOrderDevice(\App\Models\Order $order)
    {
        $free_id = \App\Models\Option::get('inner_devices', 'status', 'free');
        $using_id = \App\Models\Option::get('inner_devices', 'status', 'using');
        for($j = 1; $j < 3; ++$j) {
            $start_date = \Carbon\Carbon::now()->addDay(mt_rand(1, 4))->toDateString();
            $end_date = \Carbon\Carbon::now()->addDay(mt_rand(5, 10))->toDateString();
            $device = \App\Models\InnerDevice::where('status', $free_id)->where('id', '>', 10)->first();
            \App\Models\OrderDevice::create([
                'order_id' => $order['id'],
                'project_id' => $order['project_id'],
                'inner_device_id' => $device->id,
                'start_date' => $start_date,
                'end_date' => $end_date
            ]);
//            \Illuminate\Support\Facades\Log::info($order['work_point_id']);
            $device->update([
                'project_id' => $order['project_id'],
                'status' => $using_id,
                'start_date' => $start_date,
                'end_date' => $end_date,
                'work_point_id' => $order['work_point_id']
            ]);
        }
    }
}
