<?php

namespace App\Models;

class Notification extends BaseModel
{
    public static function send($order_id, $overdue = false)
    {
        $order = Order::find($order_id);
        if(!$order) return false;
        if($order['is_draft'] == 1) return false;
        $checked_id = Option::get('orders', 'status', 'checked');
        $reject_id = Option::get('orders', 'status', 'reject');
        $pass_id = Option::get('orders', 'status', 'pass');
        $back_id = Option::get('orders', 'status', 'back');
        $last_role = ProjectRole::find($order['last_project_role_id']);
        $role = ProjectRole::find($order['project_role_id']);
        $status = 1;
        $user_ids = [];
        $project_role_ids = ProjectRole::where('level', $order['level'])->pluck('id');
        if($overdue) {
            // 调用时间到期
            $role = ProjectRole::getFirstRole($order);
            $project_role_ids = [$role['id']];
        } else if($order['status'] == $pass_id) {
            // 租赁完成，调用完成
            if($order['type'] == 1) {
                // 租赁完成
                $project_role_ids = ProjectRole::whereIn('key', ['manager'])->pluck('id');
            } else if($order['is_change' == 2]) {
                // 调用完成且未修改
                $project_role_ids = ProjectRole::whereIn('key', ['admin'])->pluck('id');
            } else {
                // 调用完成且修改
                $project_role_ids = ProjectRole::whereIn('key', ['admin'])->pluck('id');
            }
        } else if($order['status'] == $back_id) {
            $project_role_ids = ProjectRole::whereIn('key', ['admin'])->pluck('id');
        }

        if($overdue) {
            $project_role_ids = ProjectRole::whereIn('key', ['machine', 'admin'])->pluck('id');
        }
        if(count($project_role_ids) > 0) {
            $user_ids = ProjectUser::where([
                ['project_id', $order['project_id']]
            ])->whereIn('project_role_id', $project_role_ids)->pluck('user_id');
        }
        // 外部租赁
        if($order['type'] == 1) {
            // 审批成功
            if($order['status'] == $checked_id && $last_role && $last_role['key'] == 'manager') {
                $status = 1;
            } else if($role && in_array($role['key'], ['machine', 'assist', 'manager'])) {
                // 审批待处理
                $status = 2;
            } else if($order['status'] == $reject_id) {
                // 审批被驳回
                $status = 3;
            } else if($order['status'] == $pass_id) {
                // 租赁已完成
                $status = 5;
            }
        } else {
            // 调用
            // 调用成功
            if($order['status'] == $checked_id && $role && $last_role && in_array($last_role['key'], ['admin', 'sub'])) {
                $status = $order['is_change'] == 1 ? 7 : 1;
            } else if($order['status'] == $reject_id) {
                // 调用被驳回
                $status = 3;
            } else if($order['status'] == $pass_id) {
                // 调用已完成，主动退回
                $status = 6;
            } else if($order['status'] == $back_id) {
                // 调用已归还
                $status = 6;
            } else if($order['is_change'] == 1) {
                // 调用修改通知
                $status = 7;
            } else if($role && in_array($role['key'], ['assist', 'manager', 'sub', 'admin'])) {
                // 调用待处理
                $status = 2;
            }
        }
        // 调用时间到期，发送调用完成通知 status = 5
        if($overdue) {
            $status = 5;
        }
        foreach($user_ids as $user_id) {
            Notification::create([
                'user_id' => $user_id,
                'order_id' => $order['id'],
                'status' => $status,
                'type' => $order['type'],
                'is_read' => 2
            ]);
        }
        return true;
    }

    /**
     * 租赁时间到期 status = 4
     * @param $id
     * @return bool
     */
    public static function sendOverdue($id)
    {
        $order = self::find($id);
        if(!$order) return false;
        $role = $order->type == 1 ? $role = ProjectRole::getByKey('work') : ProjectRole::getByKey('machine');
        $user_ids = ProjectUser::where([
            ['project_id', $order['project_id']],
            ['project_role_id', $role['id']]
        ])->pluck('user_id');
        foreach($user_ids as $user_id) {
            Notification::create([
                'user_id' => $user_id,
                'order_id' => $order['id'],
                'status' => 4,
                'type' => $order['type'],
                'is_read' => 2
            ]);
        }
        return false;
    }

    public function getNameContent()
    {
        if($this['type'] == 1) {
            if($this['status'] == 1) {
                return [
                    'name' => '租赁审批成功通知',
                    'content' => '你的设备租赁申请已通过，请确认相关信息：'
                ];
            } else if($this['status'] == 2) {
                return [
                    'name' => '租赁审批待处理通知',
                    'content' => '你有一条设备租赁申请待审批，请确认相关信息：'
                ];
            } else if($this['status'] == 3) {
                return [
                    'name' => '租赁审批驳回通知',
                    'content' => '你有一条设备租赁申请被驳回，请确认相关信息：'
                ];
            } else if($this['status'] == 4) {
                return [
                    'name' => '租赁时间到期通知',
                    'content' => '你有一条设备租赁已到期消息，请确认相关信息：'
                ];
            } else if($this['status'] == 5) {
                return [
                    'name' => '租赁完成通知',
                    'content' => '你有一条设备租赁已完成消息，请确认相关信息：'
                ];
            }
        } else {
            if($this['status'] == 1) {
                return [
                    'name' => '调用审批成功通知',
                    'content' => '你的设备调用申请已通过，请确认相关信息：'
                ];
            } else if($this['status'] == 2) {
                return [
                    'name' => '调用审批待处理通知',
                    'content' => '你有一条设备调用申请待审批，请确认相关信息：'
                ];
            } else if($this['status'] == 3) {
                return [
                    'name' => '调用审批驳回通知',
                    'content' => '你有一条设备调用申请被驳回，请确认相关信息：'
                ];
            } else if($this['status'] == 4) {
                return [
                    'name' => '调用时间到期通知',
                    'content' => '你有一条设备调用已到期消息，请确认相关信息：'
                ];
            } else if($this['status'] == 5) {
                return [
                    'name' => '调用完成通知',
                    'content' => '你有一条设备调用已完成消息，请确认相关信息：'
                ];
            } else if($this['status'] == 6) {
                return [
                    'name' => '调用退回通知',
                    'content' => '你有一条设备调用已归还消息，请确认相关信息：'
                ];
            } else if($this['status'] == 7) {
                return [
                    'name' => '调用修改通知',
                    'content' => '你有一条设备调用被修改消息，请确认相关信息：'
                ];
            }
        }
        return ['name' => '', 'content' => ''];
    }
}
