<?php
/**
 * Created by PhpStorm.
 * User: xmy 280564871@qq.com
 * Date: 2017/3/30
 * Time: 13:56
 */

namespace App\Model;

use Think\Model;
use App\Model\AdvModel;

class DocumentModel extends Model {

	public function getArticleListsByCategory($name,$p=1){
		$page = intval($p);
		$page = $page ? $page : 1; //默认显示第一页数据
		$row = 5;
		$category = M("category")->where(['name'=>$name])->find();
		$map['category_id'] = $category['id'];
		$map['display'] = 1;
		$map['status'] = 1;
		$time = NOW_TIME;
		$map['_string'] = "deadline < {$time} or deadline = 0";
		$data = $this->field("id,title,description,create_time,cover_id as cover")
			->where($map)
			->order("level desc,id desc")
			->page($page,$row)->select();				if(!empty($data)) {						foreach ($data as $key => $val) {
				$cover = get_cover($val['cover'],'path');
				if(strpos($cover, 'http')!==false){
					$cover = $cover;
				}else{
					$cover = 'http://'.$_SERVER['HTTP_HOST'].$cover;
				}
				$data[$key]['cover'] = $cover;
				$data[$key]['url'] = $this->generate_url($val['id']);
			}		} else {			$data = [];		}		

		switch ($name) {
			case 'APP_INFO':
			    $adv = 'wap_index_zx';
				break;
			case 'APP_NOTICE':
				$adv = 'wap_index_gg';
				break;
			default:
			    $adv = 'wap_index_activity';
				break;
		}
		$model = new AdvModel();
        $adv_data = $model->getAdv($adv,1);
        $new_data['data'] = $data;
        $new_data['adv_title'] = $adv_data[0]['title'];
        $new_data['adv_url'] = $adv_data[0]['url'];
        $new_data['adv_img'] = $adv_data[0]['data'];
		return $new_data;
	}

	/**
	 * 生成文章链接
	 * @param $id
	 * @return string
	 * author: xmy 280564871@qq.com
	 */
	public function generate_url($id){
		return U("Article/show",['id'=>$id],'',true);
	}

	public function getArticle($id){
		$map['d.id'] = $id;
		$data = $this->table("sys_document as d")
			->field("d.title,a.content,create_time,d.cover_id,d.cover_id2,d.description")
			->join("sys_document_article a on a.id = d.id")
			->where($map)
			->find();
		$data['cover_id'] = get_cover($data['cover_id2'],'path');
		return $data;

	}

}