<?php

namespace common\models;

use Yii;
use yii\db\Expression;

/**
 * This is the model class for table "fx_user_qrcode".
 *
 * @property integer $id
 * @property integer $fx_uid
 * @property integer $qr_id
 */
class FxUserQrcode extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'fx_user_qrcode';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['fx_uid', 'qr_id'], 'integer'],
            [['qr_id'], 'unique'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'fx_uid' => 'Fx Uid',
            'qr_id' => 'Qr ID',
        ];
    }

    /**
     * Des:根据qrId获取详细
     * Name: getInfoByQrId
     * @param $qrCodeId
     * @return array|null|\yii\db\ActiveRecord
     * @author 倪宗锋
     */
    public function getInfoByQrId($qrCodeId)
    {
        $where = ['=', 'qr_id', $qrCodeId];
        $select = ['fx_uid', 'qr_id'];
        $getInfo = self::find()->select($select)
            ->where($where)
            ->asArray()
            ->one();
        return $getInfo;
    }

    /**
     * Des:添加新记录
     * Name: addNew
     * @param $qrCodeId
     * @return bool
     * @author 倪宗锋
     */
    public function addNew($qrCodeId)
    {
        $params = [
            'qr_id' => $qrCodeId,
            'fx_uid' => 0
        ];
        $this->setAttributes($params);
        $return = $this->insert(false);
        return $return;
    }

    /**
     * Des:获取所有列表数据
     * Name: getList
     * @return array|\yii\db\ActiveRecord[]
     * @author 倪宗锋
     */
    public function getList()
    {
        $select = [
            'a.fx_uid',
            'a.qr_id',
            'img_url' => new Expression("concat('/web/fx/images/FxQrCode/',a.qr_id,'.jpg')"),
            'nickname' => new Expression("if(b.nickname is null,'未设置',concat(b.nickname,' - ',b.phone))")
        ];
        $getList = self::find()->select($select)
            ->from(static::tableName().' a')
            ->leftJoin(FxUser::tableName().' b','a.fx_uid = b.uid')
            ->asArray()
            ->all();
        return $getList;
    }

}
