<?php

namespace wechat\models;

use Yii;

/**
 * This is the model class for table "bit_scan_qrcode_record".
 * 
 * @property int $id
 * @property int $wechat_group_id
 * @property string $session_id
 * @property int $create_time
 * @property int $last_scan_time
 */
class ScanQrcodeRecord extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'bit_scan_qrcode_record';
    }
    
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['session_id'], 'string'],
            [['wechat_group_id', 'create_time', 'last_scan_time'], 'number'],
            [['id'], 'safe'],
        ];
    }
    
    /**
     * 处理扫码记录
     * @param int $wechat_group_id
     * @param string $session_id
     * @return void
     */
    public static function doRecord($wechat_group_id, $session_id)
    {
        $time = time();
        $recordModel = static::find()->where(['wechat_group_id' => $wechat_group_id, 'session_id' => $session_id])->limit(1)->one();
        if (!$recordModel) {
            $recordModel = new static();
            $recordModel->wechat_group_id = $wechat_group_id;
            $recordModel->session_id = $session_id;
            $recordModel->create_time = $time;
            $recordModel->last_scan_time = $time;
            $recordModel->save();
        } else {
            /** @var ScanQrcodeRecord $recordModel */
            $recordModel->last_scan_time = $time;
            $recordModel->save();
        }
    }

}
