<?php
namespace app\mainapp\controller;

use think\facade\Session;
use app\mainapp\BaseController;

use app\common\model\User as UserModel;

use app\common\model\Article as ArticleModel;
use app\common\model\ArticleCate as ArticleCateModel;
use app\common\model\ArticleComment as ArticleCommentModel;
use app\common\model\ArticleThumb as ArticleThumbModel;
use app\common\model\ArticleCollect as ArticleCollectModel;

use echowx\WxProgram;

class Article extends BaseController
{
	// 文章评论
	public function listComment()
	{
		$ppage = input('ppage/d', 1);
		$psize = input('psize/d', 20);
		$articleid = input('articleid/d', 0);
		$userid = input('userid/d', 0);
		$plist = ArticleCommentModel::with(['user','puser'])->where(['articleid'=>$articleid])
			->whereRaw(" status=:status1 OR ( status=:status2 AND userid=:userid ) ", ['status1' => 1, 'status2' => 2, 'userid' => $userid])
			->order('id','desc')->page($ppage)->limit($psize)->select();
		$count = ArticleCommentModel::where(['articleid'=>$articleid])
			->whereRaw(" status=:status1 OR ( status=:status2 AND userid=:userid ) ", ['status1' => 1, 'status2' => 2, 'userid' => $userid])
			->count();
		page_result(0, "", array(
			'plist' => $plist,
			'pstatus' => $psize > count($plist) ? 'noMore' : 'more',
			'count' => $count,
			));
	}
	
	public function sendComment()
	{
		$details = input('details/s', '', 'trim');
		if (empty($details)){
			page_result(1, "评论内容不能为空");
		}
		$wxprogram = new WxProgram();
		$res = $wxprogram->security_msg_sec_check($details);
		if ($res==false){
			page_result(1, "内容含有违法违规内容");
		}
		$articleid = input('articleid/d', 0);
		$article = ArticleModel::findOrEmpty($articleid);
		if ($article->isEmpty()){
			page_result(1, "文章资讯信息不存在");
		}
		$userid = input('userid/d', 0);
		$user = UserModel::findOrEmpty($userid);
		if ($user->isEmpty()){
			page_result(1, "用户信息不存在");
		}
		$puserid = input('puserid/d', 0);
		$newcomment = new ArticleCommentModel;
		$newcomment->save([
			'userid' 		=> $userid,
			'articleid'		=> $articleid,
			'puserid'		=> $puserid,
			'details'		=> $details,
			'createtime'	=> time(),
			'status'		=> 2
		]);
		$comment = ArticleCommentModel::with(['user','puser'])->where('id', '=', $newcomment->id)->select();
		page_result(0, "", array(
			'comment'	=> $comment
			));
	}
	

	// 详情
	public function getArticle()
	{
		$articleid = input('articleid/d', 0);
		$article = ArticleModel::with('articleCate')->withCount(['articleComment','articleThumb'])->append(['createtime_text'])->findOrEmpty($articleid);
		if ($article->isEmpty()){
			page_result(1, "文章资讯信息不存在");
		}
		$article->volume += 1;
		$article->save();
		
		$userid = input('userid/d', 0);
		$mythumb = ArticleThumbModel::where([['userid','=',$userid],['articleid','=',$articleid]])->count();
		$mycollect = ArticleCollectModel::where([['userid','=',$userid],['articleid','=',$articleid]])->count();

		$commentlist = ArticleCommentModel::with(['user','puser'])->where(['articleid'=>$articleid,'status'=>1])->order('id','desc')->limit(3)->select();
		$articlelist = ArticleModel::with('articleCate')->withCount(['articleComment','articleThumb'])->where([['status','=',1],['createtime','<=',time()]])->order(['priority'=>'desc','id'=>'desc'])->limit(3)->select();
		page_result(0, "", array(
			'article'			=> $article,
			'mythumb'			=> $mythumb,
			'mycollect'			=> $mycollect,
			'articlelist'		=> $articlelist,
			'commentlist'		=> $commentlist
			));
	}
	
	// 点赞
	public function setThumb()
	{
		$articleid = input('articleid/d', 0);
		$article = ArticleModel::with('articleCate')->withCount(['articleComment','articleThumb'])->findOrEmpty($articleid);
		if ($article->isEmpty()){
			page_result(1, "文章资讯信息不存在");
		}
		$userid = input('userid/d', 0);
		if ($userid==0){
			page_result(1, "你还未登录，不能点赞");
		}
		$thumb = ArticleThumbModel::where([['userid','=',$userid],['articleid','=',$articleid]])->findOrEmpty();
		$mythumb = 0;
		if ($thumb->isEmpty()){
			ArticleThumbModel::create([
				'userid' 		=> $userid,
				'articleid'		=> $articleid,
				'createtime'	=> time()
			]);
			$mythumb = 1;
		}else{
			$thumb->delete();
		}
		$thumbcount = ArticleThumbModel::where('articleid','=',$articleid)->count();
		page_result(0, "", array(
			'mythumb'		=> $mythumb,
			'thumbcount'	=> $thumbcount
			));
	}
	
	// 收藏
	public function setCollect()
	{
		$articleid = input('articleid/d', 0);
		$article = ArticleModel::with('articleCate')->withCount(['articleComment','articleThumb'])->findOrEmpty($articleid);
		if ($article->isEmpty()){
			page_result(1, "文章资讯信息不存在");
		}
		$userid = input('userid/d', 0);
		if ($userid==0){
			page_result(1, "你还未登录，不能点赞");
		}
		$collect = ArticleCollectModel::where([['userid','=',$userid],['articleid','=',$articleid]])->findOrEmpty();
		$mycollect = 0;
		if ($collect->isEmpty()){
			ArticleCollectModel::create([
				'userid' 		=> $userid,
				'articleid'		=> $articleid,
				'createtime'	=> time()
			]);
			$mycollect = 1;
		}else{
			$collect->delete();
		}
		page_result(0, "", array(
			'mycollect'		=> $mycollect
			));
	}
	 
	// 列表
	public function listArticle()
	{
		$ppage = input('ppage/d', 1);
		$psize = input('psize/d', 20);
		$map[] = ['status','=',1];
		$map[] = ['createtime','<=',time()];
		$cateid = input('cateid/d');
		if ($cateid!=0){
			$map[] = ['cateid', '=', $cateid];
		}
		$plist = ArticleModel::with('articleCate')->withCount(['articleComment','articleThumb'])->where($map)->order(['priority'=>'desc','id'=>'desc'])->page($ppage)->limit($psize)->append(['createtime_text'])->select();
		page_result(0, "", array(
			'plist' => $plist,
			'pstatus' => $psize > count($plist) ? 'noMore' : 'more'
			));
	}

	// 全部分类
	public function allCate()
	{
		$allcate = ArticleCateModel::where('status',1)->order(['priority'=>'desc','id'=>'desc'])->select()->toArray();
		array_unshift( $allcate, array('id'=>0,'title'=>'全部') );
		page_result(0, "", array('allcate'=>$allcate));
	}


}