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

namespace home\models;

use Yii;
use yii\helpers\Url;

/**
 * This is the model class for table "{{%attachment}}".
 * 
 * 附件表
 *
 * @property integer $attach_id  (ID)
 * @property string $route  (路由信息)
 * @property string $file_name  (附件名称)
 * @property string $file_path  (附件路径)
 * @property string $file_ext  (附件扩展名)
 * @property string $file_md5  (附件md5值)
 * @property smallint $is_image  (是否是图片)
 * @property smallint $is_thumb  (是否是缩略图)
 * @property smallint $status  (状态值)
 * @property integer $create_time  (上传时间)
 * @property integer $user_id  (上传用户id)
 * @property string $ip  (上传ip地址)
 *
 * @author legendjw http://blog.u-bo.com
 * @since 0.1
 */
class Attachment extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%attachment}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['file_ext'], 'required'],
            [['is_image', 'is_thumb', 'status', 'create_time', 'user_id'], 'integer'],
            [['route', 'ip'], 'string', 'max' => 50],
            [['file_name'], 'string', 'max' => 100],
            [['file_path'], 'string', 'max' => 255],
            [['file_ext'], 'string', 'max' => 10],
            [['file_md5'], 'string', 'max' => 32]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'attach_id' => 'ID',
            'route' => '路由信息',
            'file_name' => 'File Name',
            'file_path' => 'File Path',
            'file_ext' => 'File Ext',
            'file_md5' => 'File Md5',
            'is_image' => 'Is Image',
            'is_thumb' => 'Is Thumb',
            'status' => 'Status',
            'create_time' => 'Create Time',
            'user_id' => 'User ID',
            'ip' => 'Ip',
        ];
    }

    /**
     * 获取保存附件内容
     */
    public static function getSavePath()
    {
        $web = Yii::getAlias('@web');
        $path = './upload/' . date('Y') . '/' . date('m') . '/' . date('d') .'/';
        return $path;
    }

	/**
	 * 返回以','相隔的附件信息
	 * 
	 * @param type $str
	 * @return string
	 */
	public static function getAttachByDot($str)
	{
		if (empty($str)) {
			return '';
		}
		$data = self::find()
				->where(['attach_id' => explode(',', $str)])
				->indexBy('attach_id')
				->all();
		return $data;
	}

    /**
     * 生成附件下载列表
     * string $value 传递过来的附件值
     * @author yxd
     */
    public static function getDownList($value)
    {
        $downUrl = Url::to(['/attachment/download']);
        $filesStr = '';
		$files = self::getAttachByDot($value);
        if (!empty($files)) {
            $randId = mt_rand(1000, 9999);
            foreach ($files as $k => $file) {
                $fileDownUrl = $downUrl . ((false === strpos($downUrl, '?')) ? '?' : '&') . 'id='. $file['attach_id'] ;
                $filesStr .= "<li id='file{$randId}'> <input type='text' name='attach_ids' value= '{$file['file_name']}' style='width:300px;' disabled='disabled' class='input' data-id='{$file['attach_id']}' data-path='{$file['file_path']}' />&nbsp;&nbsp;<a href=\"{$fileDownUrl}\" >下载</a></li>";
            }
        }
        return $filesStr;
    }

}
