<?php

namespace App\Models\Friends;

use App\Models\Model;

/**
 * App\Models\Friends\FriendApplyModel
 *
 * @property int $id 主键
 * @property \Illuminate\Support\Carbon $created_at 创建时间
 * @property int $last_at 最近好友申请时间
 * @property int $uid 用户1
 * @property int $apply_uid 用户2
 * @property string $last_msg 最后一次交互的消息
 * @property int $unread_cnt 未读消息数
 * @property int $is_apply 是否发起申请过
 * @property int $is_receive 是否收到过申请过
 * @property int $is_hide 是否隐藏
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Friends\FriendApplyModel newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Friends\FriendApplyModel newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Friends\FriendApplyModel query()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Friends\FriendApplyModel whereApplyUid($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Friends\FriendApplyModel whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Friends\FriendApplyModel whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Friends\FriendApplyModel whereIsApply($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Friends\FriendApplyModel whereIsReceive($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Friends\FriendApplyModel whereLastAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Friends\FriendApplyModel whereLastMsg($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Friends\FriendApplyModel whereUid($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Friends\FriendApplyModel whereUnreadCnt($value)
 * @mixin \Eloquent
 */
class FriendApplyModel extends Model
{
    protected $table = "kdgx_fpdx_friend_apply";

    protected $dateFormat = "U";

    public const UPDATED_AT = null;

    protected $fillable = array(
        'last_at',
        'uid',
        'apply_uid',
        'last_at',
        'last_msg',
        'unread_cnt',
        'is_apply',
        'is_receive'
    );

    /**
     * 向某人发起好友申请
     * @param  int  $uid  申请者
     * @param  int  $to_uid  被申请者
     */
    public static function send(int $uid, int $to_uid)
    {
        /** @var FriendApplyModel $send */
        $send = self::firstOrNew(array('uid' => $uid, 'apply_uid' => $to_uid), ['uid' => $uid, 'apply_uid' => $to_uid]);
        $send->last_at = time();
        $send->is_apply = 1;
        $send->is_hide = 0;
        $send->last_msg = "向ta发出了好友申请";

        /** @var FriendApplyModel $apply */
        $apply = self::firstOrNew(
            array('uid' => $to_uid, 'apply_uid' => $uid),
            ['uid' => $to_uid, 'apply_uid' => $uid]
        );
        $apply->last_at = time();
        $apply->is_receive = 1;
        $send->is_hide = 0;
        $apply->last_msg = "收到ta发出的好友申请";
        $apply->unread_cnt = $apply->unread_cnt + 1;

        $send->save() && $apply->save();
    }
}
