<?php
/**
 * @link      http://www.u-bo.com
 * @copyright 南京友博网络科技有限公司 
 * @license   http://www.u-bo.com/license/
 */

namespace home\models;

use Yii;
use common\models\User;

/**
 * 标签库模型
 *
 * @property integer $tag_id  (标签ID)
 * @property string $name  (标签名称)
 * @property integer $sort  (排序)
 * @property smallint $status  (状态：0 禁用 1开启 99逻辑删除 默认开启)
 * @property integer $user_id  (创建人，关联表qc_user)
 * @property integer $create_time  (创建时间)
 * @property integer $update_time  (更新时间)
 * @property string $ip  (创建人IP)
 *
 * @author zealxd http://blog.u-bo.com
 * @since 0.1
 */
class Tag extends \yii\db\ActiveRecord
{

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%tag}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['name'], 'required'],
            ['name', 'unique'],
            [['sort', 'status', 'user_id', 'create_time', 'update_time'], 'integer'],
            [['name'], 'string', 'max' => 20],
            [['remark'], 'string', 'max' => 255],
            [['ip'], 'string', 'max' => 15]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'tag_id' => 'ID',
            'name' => '标签名称',
            'sort' => '排序',
            'status' => '状态',
            'user_id' => '创建人ID',
			'remark' => '备注',
            'create_time' => '创建时间',
            'update_time' => '更新时间',
            'ip' => '创建人IP',
        ];
    }
    
      /**
     * 查询tag
     * @param type $map
     * @param type $order
     * @param type $field
     * @return type
     */
    public static function getTag($map = [], $order = ['tag_id' => SORT_ASC],$field = '*')
    {
        $TagList = static::find()
                ->select($field)
                ->where($map)
                ->orderBy($order)
                ->asArray()
                ->all();
        return $TagList;
    }
    
	/**
     * 公共搜索所需信息
     */
    public function search()
    {
        return [
			'keywords' => [
				'name' => '标签名称',
			],
			'status' => User::getStatusList(),
        ];
    }

    /**
     * 插入更新前置操作
     */
    public function beforeSave($insert)
    {
        if (parent::beforeSave($insert)) {
            //判断是否是插入操作
            if ($this->isNewRecord) {
                $this->user_id =  Yii::$app->user->identity->user_id;
                $this->create_time = time();
                $this->ip = Yii::$app->getRequest()->getUserIP();
            } else {
                $this->update_time = time();
            }
            return true;
        } else {
            return false;
        }
    }

    /**
     * 检测标签并往数据库插入标签
     * @param type $tags
     * @return string
     */
    public static function insertTags($tags)
    {
        if (empty($tags)) {
            return '';
        }
        $tags = explode(',', $tags);
        //检测插入
        $TagIds ="";
        foreach ($tags as $tag) {
            $list = static::find()->where(['name'=>$tag])->one();
            if (empty($list)) { 
                $data = new Tag();
                $data->name =$tag;
                $data->save();
                $TagIds.=$data->tag_id.",";
            }else {
               $TagIds.=($list['tag_id']).",";
            }
        }
        return $TagIds;
    }
  
  
    /**
     * 获取所有的标签id和name值
     * 
     * @param type $map
     * @return type
     */
    public static function getTags($map = [])
    { 
        $TagList = static::getTag([], ['tag_id' => SORT_ASC], 'tag_id, name');
        foreach ($TagList as $result) {
            $data[$result['tag_id']] = $result['name'];
        }
        return $TagList;
    }
    
}
