<?php

namespace app\common\model;

use addons\faqueue\library\QueueApi;
use think\Model;

/**
 * 赠送礼物模型
 */
class GiftUserParty extends Model
{

    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';

    protected static function init()
    {
        GiftUserParty::afterInsert(function ($giftUserParty) {
            if ($giftUserParty->party_id > 0) {
                $giftNotice = 100;
                $bigGiftNotice = 200;
                $partyInfo = $giftUserParty->party;
                $money = $giftUserParty->value / 100;

                $type = 0;
                if ($money >= $bigGiftNotice) {
                    $type = 72;
                }elseif ($money >= $giftNotice){
                    $type = 71;
                }

                $type > 0 and QueueApi::sendGroupMessage($type, $giftUserParty->user->nickname, $giftUserParty->toUser->nickname, $partyInfo, $giftUserParty);
            }
        });
    }

    public function user()
    {
        return $this->belongsTo('app\common\model\User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function toUser()
    {
        return $this->belongsTo('app\common\model\User', 'user_to_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function party()
    {
        return $this->belongsTo('app\common\model\Party', 'party_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}