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

namespace Open\Model;

use Think\Model;

class DocumentModel extends Model {

	public function getArticleListsByCategory($name,$p=1){

		$category = M("category")->where(['name'=>$name])->find();
		$map['category_id'] = $category['id'];
		return $this->getLists($map,$p);

	}

	public function getLists($map,$p){
		$page = intval($p);
		$page = $page ? $page : 1; //默认显示第一页数据
		$row = 10;
		$map['display'] = 1;
		$map['status'] = 1;
		$time = NOW_TIME;
		$map['_string'] = "deadline < {$time} or deadline = 0";
		$data['data'] = $this->field("id,title,description,create_time,cover_id as cover,category_id")
			->where($map)
			->order("level desc,create_time desc")
			->page($page,$row)
			->select();
		$data['count'] = $this->where($map)->count();
		return $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.description")
			->join("sys_document_article a on a.id = d.id")
			->where($map)
			->find();
		$data['cover_id'] = get_img_url($data['cover_id']);
		return $data;

	}


	/**
	 * 左侧文章列表
	 * @return mixed
	 * author: xmy 280564871@qq.com
	 */
	public function getLeftLists(){

		//开放PID
		$pid = M("category")->where(['name'=>"OPEN_PLATFORM"])->getField("id");
		//文章栏目
		$category_lists = M("category")->field("id,name,title,icon")->where(['pid'=>$pid])->select();
		$map['display'] = 1;
		$map['status'] = 1;
		$time = NOW_TIME;
		$map['_string'] = "deadline < {$time} or deadline = 0";
		$map['category_id'] = ['in',array_column($category_lists,'id')];
		//文章列表
		$data = $this->field("id,title,category_id")
			->where($map)
			->select();
		//文章归类
		foreach ($data as $key => $value){
			$result[$value['category_id']][] = $value;
		}
		foreach ($category_lists as $k => &$v){
			$v['icon'] = get_cover($v['icon'],'path');
			$v['_child'] = $result[$v['id']];
		}
		return $category_lists;
	}

	/**
	 * 获取文章内容
	 * @param $id
	 * @return mixed
	 * author: xmy 280564871@qq.com
	 */
	public function getContent($id){
		$map['d.id'] = $id;
		$data = $this->alias("d")->field("d.*,a.*")->join("left join sys_document_article a on a.id = d.id")->where($map)->find();
		return $data;
	}

	/**
	 * 平台公告所有文章
	 * @param $p
	 * @return mixed
	 * author: xmy 280564871@qq.com
	 */
	public function getNotice($p){
		$map['name'] = ['in',['open_notice','open_inform']];
		$category = M("category")->where($map)->select();
		$map = [];
		$map['category_id'] = ['in',array_column($category,'id')];
		return $this->getLists($map,$p);
	}

	/**
	 * 文章详情
	 * @param $id
	 * @return mixed
	 * author: xmy 280564871@qq.com
	 */
	public function getDetail($id){
		$map['d.id'] = $id;
		$data = $this->table("sys_document as d")
			->field("d.title,a.content,d.create_time,d.cover_id,d.description,c.name")
			->join("sys_document_article a on a.id = d.id")
            ->join("sys_category c on c.id=d.category_id")
			->where($map)
			->find();
		return $data;
	}

	/**
	 * 文章详情
	 * @param $name
	 * @return mixed
	 * author: xmy 280564871@qq.com
	 */
	public function getDetailByName($name){
		$map['d.name'] = $name;
		$data = $this->table("sys_document as d")
			->field("d.title,a.content,create_time,d.cover_id,d.description,d.id,d.category_id")
			->join("sys_document_article a on a.id = d.id")
			->where($map)
			->find();
		return $data;
	}

}