<?php
/**
 * Created by PhpStorm.
 * User: zilongs
 * Date: 20-10-30
 * Time: 下午4:43
 */

namespace App\Models;

class UserMessage extends BaseModel
{
    protected $appends = ['product_type'];

    public static function saveMessage($user_id, $type, $relation_id = 0, $param = [], $content = '')
    {
        if (empty($content)) {
            $content = config('config.user_message_map')[$type] ?? '';
            $content = vsprintf($content, $param);
        }
        UserMessage::create([
            'user_id' => $user_id,
            'type' => $type,
            'relation_id' => $relation_id,
            'content' => $content,
        ]);

        return true;
    }

    public function getProductTypeAttribute()
    {
        if (in_array($this->type, [1,2,4,5,6,7,8]) && !empty($this->relation_id)) {
            return Order::where('id', $this->relation_id)->value('product_type');
        }

        return 0;
    }
}
