<?php

use Illuminate\Database\Seeder;

class MiniSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        // 创建角色、用户
        $total = 20;
        \App\Models\Role::truncate();
        \App\Models\User::truncate();
        for($i = 1; $i < $total; ++$i) {
            $role = \App\Models\Role::create([
                'name' => '用户角色' . $i,
                'sort' => $i,
                'key' => 'role-' . $i
            ]);

            \App\Models\User::create([
                'role_id' => $role->id,
                'name' => '用户' . $i,
                'password' => bcrypt(123456),
                'phone' => $i < 10 ? '1234567890' . $i : '123456789' . $i
            ]);
        }

        // 创建项目角色
        $this->call([
            ProjectRoleSeeder::class
        ]);

        // 创建项目，为项目分配用户和项目角色
        \App\Models\Project::truncate();
        \App\Models\ProjectUser::truncate();
        $users = \App\Models\User::limit(7)->get();
        $project_roles = \App\Models\ProjectRole::limit(7)->get();
        for($i = 1; $i < $total; ++$i) {
            $project = \App\Models\Project::create([
                'name' => '项目' . ($i + 2)
            ]);
            if($i == 1) {
                for($j = 0; $j < count($users); ++$j) {
                    \App\Models\ProjectUser::create([
                        'project_id' => $project->id,
                        'user_id' => $users[$j]->id,
                        'project_role_id' => $project_roles[$j]->id
                    ]);
                }
            }

        }

        \App\Models\Device::truncate();
        \App\Models\DeviceName::truncate();
        \App\Models\Spec::truncate();
        // 创建设备类型
        $device_types = ['起重机械', '隧道掘进机'];
        $cnt = 0;
        foreach($device_types as $key => $value) {
            $cnt = $cnt + 1;
            $device = \App\Models\Device::create([
                'name' => $value,
                'sort' => $cnt
            ]);
            for($i = 1; $i < 3; ++$i) {
                $cnt = $cnt + 1;
                $device_name = \App\Models\DeviceName::create([
                    'device_id' => $device->id,
                    'name' => $value . $cnt,
                    'sort' => $cnt
                ]);

                for($j = 1; $j < 3; ++$j) {
                    $cnt = $cnt + 1;
                    \App\Models\Spec::create([
                        'device_id' => $device->id,
                        'device_name_id' => $device_name->id,
                        'name' => '规格' . $cnt,
                        'sort' => $cnt,
                    ]);
                }
            }
        }

        \App\Models\RentType::truncate();
        $rent_names = ['按小时', '按天'];
        foreach($rent_names as $key => $rent_name) {
            \App\Models\RentType::create([
                'name' => $rent_name,
                'sort' => ($key + 1)
            ]);
        }

        // 创建需求工点
        \App\Models\WorkPoint::truncate();
        for($i = 1; $i < $total; ++$i) {
            \App\Models\WorkPoint::create([
                'name' => '工点' . $i
            ]);
        }

        // 创建订单状态
        $this->call([
            OptionSeeder::class
        ]);

        // 创建订单
        \App\Models\Order::truncate();
        \App\Models\OrderDevice::truncate();
        $project = \App\Models\Project::first();
        $work_points = \App\Models\WorkPoint::all();
        $user = \App\Models\User::first();
        $devices = \App\Models\Device::all();

        // 草稿
        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_roles->first()->id,
                'type' => 1
            ]);

            $this->createOrderDevice($order, $devices);
        }

        // 待审核，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' => 1
            ]);

            $this->createOrderDevice($order, $devices);
        }

        // 已审核，checked
        for($i = 1; $i < $total; ++$i) {
            $last_project_role = $project_roles->whereIn('key', ['machine', 'assist', 'manager'])->random();
            $project_role = $project_roles->where('key', 'work')->first();
            if($last_project_role->key != 'manager') {
                $project_role = $project_roles->where('need_check', 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' => 1
            ]);

            $this->createOrderDevice($order, $devices);
        }

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

            $last_project_role = $project_roles->where('key', 'manager')->first();
            $project_role = $project_roles->where('key', 'work')->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' => 1
            ]);

            $this->createOrderDevice($order, $devices);
        }

        // 已驳回，pass
        for($i = 1; $i < $total; ++$i) {
            $last_project_role = $project_roles->whereIn('key', ['machine', 'assist', 'manager'])->random();
            $project_role = $project_roles->where('key', 'work')->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' => 1
            ]);

            $this->createOrderDevice($order, $devices);
        }

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

    public function createOrderDevice(\App\Models\Order $order, \Illuminate\Support\Collection $devices)
    {
        $money = 0;

        $specs = \App\Models\Spec::all();
        $rent_types = \App\Models\RentType::all();
        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();
            $quantity = mt_rand(1, 5);
            $price = mt_rand(1, 10) * 1000;
            $money = $money + $quantity * $price;
            $spec = $specs->random();
            $rent_type = $rent_types->random();
            \App\Models\OrderDevice::create([
                'name' => '设备' . $j,
                'order_id' => $order['id'],
                'project_id' => $order['project_id'],
                'device_id' => $spec->device_id,
                'device_name_id' => $spec->device_name_id,
                'spec_id' => $spec->id,
                'rent_type_id' => $rent_type->id,
                'quantity' => $quantity,
                'price' => $price,
                'start_date' => $start_date,
                'end_date' => $end_date
            ]);
        }
        $date_at = \Carbon\Carbon::now()->subDay(mt_rand(1, 20))->toDateTimeString();
        $order->update(['money' => $money, 'created_at' => $date_at, 'updated_at' => $date_at]);
    }
}
