<?php

namespace Database;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Capsule\Manager as DB;

class Msgdirect extends Model
{
    protected $table = 'msgdirect';

    public static function hasMsg($chatid, $userid)
    {
        return DB::table('msgdirect')->where('chatid', $chatid)->where('userid', $userid)->exists();
    }

    public static function hasMsgById($id)
    {
        return DB::table('msgdirect')->where('id', $id)->exists();
    }

    public static function getMsg($chatid, $userid)
    {
        if (self::hasMsg($chatid, $userid) == false) {
            return false;
        }
        return DB::table('msgdirect')->where('chatid', $chatid)->where('userid', $userid)->first();
    }

    public static function getMsgById($id)
    {
        if (self::hasMsgById($id) == false) {
            return false;
        }
        return DB::table('msgdirect')->where('id', $id)->first();
    }

    public static function addMsg($chatid, $userid, $msg)
    {
        return DB::table('msgdirect')->insertGetId(['chatid' => $chatid, 'userid' => $userid, 'msg' => $msg]);
    }

    public static  function removeMsg($id)
    {
        return DB::table('msgdirect')->where('id', $id)->delete();
    }
}
