package cn.springboottestdemo.testdemo1.controller;
import cn.springboottestdemo.testdemo1.pojo.NewsComment;
import cn.springboottestdemo.testdemo1.pojo.NewsDetail;
import cn.springboottestdemo.testdemo1.service.CommentService;
import cn.springboottestdemo.testdemo1.service.NewsService;
import cn.springboottestdemo.testdemo1.util.DtoUtil;
import cn.springboottestdemo.testdemo1.util.Page;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@org.springframework.stereotype.Controller
@RequestMapping("/news")
public class Controller extends BaseController {
@Resource
private NewsService newsService;
@Resource
private CommentService commentService;
@RequestMapping("/get")
@ResponseBody
public Object get(){
return DtoUtil.returnSuccess(newsService.getlimit());
}
@RequestMapping("/get1")
@ResponseBody
public Object get1(Page page, @RequestParam(value = "title",required = false) String title){
NewsDetail newsDetail=new NewsDetail();
Map map=new HashMap();
if(title==null||title==""){
title="";
newsDetail.setTitle(title);
}else{
newsDetail.setTitle(title);
}
page.setCount(newsService.count(newsDetail));
if (page.getPageIndex()==0||page.getPageIndex()==null){
page.setPageIndex(1);
}
List<NewsDetail> list=newsService.getlimit1(page,newsDetail);
String pageinfo=this.getPageInfo(page,title);
map.put("list",list);
map.put("pageinfo",pageinfo);
map.put("page",page);
map.put("title",title);
return DtoUtil.returnSuccess(map);
}
@RequestMapping("/beforlookComment")
@ResponseBody
public Object beforlookComment(Integer id){
return DtoUtil.returnSuccess(id);
}
@RequestMapping("/lookComment")
@ResponseBody
public Object lookComment(Integer newsid){
List<NewsComment> l=commentService.getBynewsId(newsid);
return DtoUtil.returnSuccess(l);
}
@RequestMapping("/update")
@ResponseBody
public Object update(Integer newsid){
return DtoUtil.returnSuccess(newsid);
}
@RequestMapping("/doupdate")
@ResponseBody
public Object doupdate(NewsComment newsComment){
try{
newsComment.setCreatedate(new Date());
int a=commentService.add(newsComment);
if(a>0){
return DtoUtil.returnSuccess();
}
}catch (Exception e){
e.getStackTrace();
}
return DtoUtil.returnFaile();
}
@RequestMapping("/delete")
@ResponseBody
public Object delete(Integer id){
commentService.delete(id);
int a=newsService.delete(id);
if(a>0){
return DtoUtil.returnSuccess("删除成功!","");
}
return DtoUtil.returnFaile("删除失败!","");
}
private String getPageInfo(Page page, @RequestParam(required = false) String title){
StringBuffer pageCode = new StringBuffer();
if (page.getPageIndex()>=page.getTatoleCount()){
pageCode.append("<a href='list.html?title="+title+"&pageIndex=1'>首页</a>");
pageCode.append("<a href='list.html?title="+title+"&pageIndex="+(page.getPageIndex()-1)+"'>上一页</a>");
}else if (page.getPageIndex()==1){
pageCode.append("<a href='list.html?title="+title+"&pageIndex="+(page.getPageIndex()+1)+"'>下一页</a>");
pageCode.append("<a href='list.html?title="+title+"&pageIndex="+page.getTatoleCount()+"'>尾页</a>");
} else{
pageCode.append("<a href='list.html?title="+title+"&pageIndex=1'>首页</a>");
pageCode.append("<a href='list.html?title="+title+"&pageIndex="+(page.getPageIndex()-1)+"'>上一页</a>");
pageCode.append("<a href='list.html?title="+title+"&pageIndex="+(page.getPageIndex()+1)+"'>下一页</a>");
pageCode.append("<a href='list.html?title="+title+"&pageIndex="+page.getTatoleCount()+"'>尾页</a>");
}
return pageCode.toString();
}
}