<?php

namespace App\Models\Common;

use App\Models\Model;

/**
 * App\Models\Common\FormidModel
 *
 * @property int $id 主键
 * @property int $uid 用户id
 * @property string|null $openid openid
 * @property string $form_id 交互id
 * @property \Illuminate\Support\Carbon $created_at 交互时间
 * @property \Illuminate\Support\Carbon $updated_at 更新时间
 * @property int $state 是否使用过0未使用1使用
 * @property string $appid
 * @property string $public_id 公众号id
 * @property int|null $action_id 动作id
 * @property string|null $extra 附加数据
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Common\FormidModel newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Common\FormidModel newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Common\FormidModel query()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Common\FormidModel whereActionId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Common\FormidModel whereAppid($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Common\FormidModel whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Common\FormidModel whereExtra($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Common\FormidModel whereFormId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Common\FormidModel whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Common\FormidModel whereOpenid($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Common\FormidModel wherePublicId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Common\FormidModel whereState($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Common\FormidModel whereUid($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Common\FormidModel whereUpdatedAt($value)
 * @mixin \Eloquent
 */
class FormidModel extends Model
{

    protected $table = 'kdgx_charge_fpdx_miniprogram_formid';

    protected $fillable = ['uid', 'form_id', 'public_id', 'openid', 'state', 'action_id', 'extra'];

    protected $dateFormat = 'U';

    /**
     * 获取某人可用的formid
     * @param int $uid 用户id
     * @param string $pubic_id
     * @return string|false formid
     */
    public static function getUseFormid(int $uid, string $pubic_id)
    {
        $form = self::where(array(
            ['uid', $uid],
            ['created_at', '>', time() - 80000 * 7],
            ['state', 0],
            ['public_id', $pubic_id]
        ))->orderBy('id', 'asc')->first();
        if (!$form) {
            return false;
        } else {
            $form->state = 1;
            $form->save();
            return $form->form_id;
        }
    }
}
