<?php

namespace App\Models\Friends;

use App\Models\User\UserModel;
use Illuminate\Database\Eloquent\Model;

/**
 * App\Models\Friends\FriendContact
 *
 * @property int $id
 * @property int $created_at 创建时间
 * @property int $last_at 最后交互时间
 * @property array $last_msg 最后一次交互的消息
 * @property int $list_unread_msg_cnt 列表未读数量
 * @property int $star_at 星标时间
 * @property int $uid 用户
 * @property int $friend_uid 好友id
 * @property int $friend_level 亲密度等级0陌生人1普通联系人2好友联系人
 * @property int $is_blacklist 是否拉黑0否1是
 * @property int $is_hide 是否解除好友并隐藏
 * @property int $is_friend 是否解除好友关系
 * @property int $unfriend_at 解除好友关系的时间
 * @property int $unread_cnt 未读消息数量
 * @property int $each_like 相互心动标签
 * @property int $pair 72h标签
 * @property int $apply 好友申请标签
 * @property int $gteam 开黑标签
 * @property int $goodnight 晚安标签
 * @property string $note 备注
 * @property-read \App\Models\User\UserModel $user
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Friends\FriendContact newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Friends\FriendContact newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Friends\FriendContact query()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Friends\FriendContact whereApply($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Friends\FriendContact whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Friends\FriendContact whereEachLike($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Friends\FriendContact whereFriendLevel($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Friends\FriendContact whereFriendUid($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Friends\FriendContact whereGoodnight($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Friends\FriendContact whereGteam($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Friends\FriendContact whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Friends\FriendContact whereIsBlacklist($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Friends\FriendContact whereIsFriend($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Friends\FriendContact whereIsHide($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Friends\FriendContact whereLastAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Friends\FriendContact whereLastMsg($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Friends\FriendContact whereListUnreadMsgCnt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Friends\FriendContact wherePair($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Friends\FriendContact whereStarAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Friends\FriendContact whereUid($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Friends\FriendContact whereUnfriendAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Friends\FriendContact whereUnreadCnt($value)
 * @mixin \Eloquent
 */
class FriendContact extends Model
{
    protected $table = "kdgx_fpdx_friends";

    public $timestamps = false;

    public static $unguarded = true;

    protected $casts = [
        'last_msg' => 'array',
    ];

    public function user()
    {
        return $this->belongsTo(UserModel::class, 'friend_uid', 'uid');
    }

    public function relationsToArray()
    {
        $results =  parent::relationsToArray();
        foreach ($results as $key => &$result) {
            if ('user' == $key) {
                $result['note'] = $this->note ?? "";
                if (2 != $this->friend_level) {
                    $result['qq'] = $result['qq'] ? substr($result['qq'], 0, 1) . "***" . substr($result['qq'], -1, 1) : $result['qq'];
                    $result['weixin'] = $result['weixin'] ? substr($result['weixin'], 0, 1) . "***" . substr($result['weixin'], -1, 1) : $result['weixin'];
                }
            }
        }
        return $results;
    }

    /**
     * 相互喜欢标签
     * @param  int  $uid
     * @param  int  $contactUid
     * @param  int  $switch
     */
    public function setEachLike(int $uid, int $contactUid, int $switch = 1)
    {
        $this->where([
            array('uid', $uid), array('friend_uid', $contactUid)
        ])->orWhere([
            array('uid', $contactUid), array('friend_uid', $uid)
        ])->update(['each_like' => $switch]);
    }

    /**
     * 72小时标签
     * @param  int  $uid
     * @param  int  $contactUid
     * @param  int  $switch
     */
    public function setPair(int $uid, int $contactUid, int $switch = 1)
    {
        $this->where([
            array('uid', $uid), array('friend_uid', $contactUid)
        ])->orWhere([
            array('uid', $contactUid), array('friend_uid', $uid)
        ])->update(['pair' => $switch]);
    }

    /**
     * 是否是好友联系人关系
     * @param  int  $uid
     * @param  int  $contactUid
     * @return bool
     */
    public function isFriend(int $uid, int $contactUid): bool
    {
        $contact = $this->where([
            array('uid', $uid), array('contactUid', $contactUid)
        ])->first();
        if ($contact && 2 == $contact->friend_level) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 是否是普通联系人关系
     * @param  int  $uid
     * @param  int  $contactUid
     * @return bool
     */
    public function isContact(int $uid, int $contactUid): bool
    {
        $contact = $this->where([
            array('uid', $uid), array('contactUid', $contactUid)
        ])->first();
        if ($contact && 1 == $contact->friend_level) {
            return true;
        } else {
            return false;
        }
    }
}
