<?php
/*
 +----------------------------------------------------------------------
 | TEMMOKUMVC [ NO BEST, ONLY BETTER ]
 +----------------------------------------------------------------------
 | Copyright (c) 2018~2019 https://www.temmoku.cn All rights reserved.
 +----------------------------------------------------------------------
 | Author: 张宗强 Email:webmaster@temmoku.cn QQ:158726877 516669373 TEL:17895221001 微信:temmokumvc
 +---------------------------------------------------------------------- 
*/
namespace admin\public_class;
use \temmoku\db;
class Comment {
	//删除评论的时候。连回复一起删除
	public function comment_del($id){
		$row=db::select('*')->from(jab."comment")->where("id= '$id'")->row();
		if(!$row){
			return json_encode(['Result'=>'0','content'=>'要删除的数据不存在']);
		}
		//先删除主评论
		db::delete(jab."comment")->where("id='$id'")->query();
		//再次删除回复的评论
		$this->comment_for_del($id);
		//判断是不是回复，如果不是回复，就要对数据减少一个
		if($row['reply_id']=='0'){
			//更新主表comment的值
			db::query("UPDATE `".jab."article` SET `comment_num` = `comment_num`-1 WHERE aid='$row[aid]'");
		}
		return json_encode(['Result'=>'1','content'=>'删除成功']);
	}
	
	//删除评论的时候。连回复一起删除
	private function comment_for_del($id){
		$get_comment_list=db::select('*')->from(jab."comment")->where("reply_id=$id")->orderByDESC(['id'])->query();
		if($get_comment_list){
			foreach($get_comment_list AS $r){
				$l=$this->comment_del($r['id']);
				db::delete(jab."comment")->where("id='$id'")->query();
			}
		}
	}

}
?>