package cn.controller;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.fastjson.JSONArray;
import cn.pojo.Invitation;
import cn.pojo.Reply_detail;
import cn.service.InvitationService;
import cn.tools.PageSupport;
@Controller
public class InvitationController extends BaseController{
private Logger logger = Logger.getLogger(InvitationController.class);
@Resource
private InvitationService invitationService;
@RequestMapping("/getInvitationList")
public String getInvitationList(Model model,
@RequestParam(value = "title", required = false) String title,
@RequestParam(value="pageIndex",required=false) String pageIndex){
logger.info("getProviderList ---- > 帖子标题: " + title);
List<Invitation> invitationList = null;
//设置页面容量
int pageSize =4;
//当前页码
Integer currentPageNo = 1;
if(title == null){
title = "";
}
if(pageIndex != null){
try{
currentPageNo = Integer.valueOf(pageIndex);
}catch (NumberFormatException e) {
e.printStackTrace();
}
}
//总数量(表)
int totalCount = 0;
try {
totalCount = invitationService.getInvitationCount(title);
} catch (Exception e) {
e.printStackTrace();
}
//总页数
PageSupport pages=new PageSupport();
pages.setCurrentPageNo(currentPageNo);
pages.setPageSize(pageSize);
pages.setTotalCount(totalCount);
int totalPageCount = pages.getTotalPageCount();
//控制首页和尾页
if(currentPageNo < 1){
currentPageNo = 1;
}else if(currentPageNo > totalPageCount){
currentPageNo = totalPageCount;
}
try {
invitationList =invitationService.getInvitationList(title, currentPageNo, pageSize);
} catch (Exception e) {
e.printStackTrace();
}
model.addAttribute("invitationList", invitationList);
model.addAttribute("totalPageCount", totalPageCount);
model.addAttribute("totalCount", totalCount);
model.addAttribute("currentPageNo", currentPageNo);
return "invitationList";
}
@RequestMapping(value = "/del", method = RequestMethod.GET)
@ResponseBody
public Object deluser(@RequestParam String id) throws NumberFormatException, Exception {
HashMap<String, String> resultMap = new HashMap<String, String>();
try {
if(invitationService.delete(Integer.parseInt(id)))
resultMap.put("delResult", "true");
else
resultMap.put("delResult", "false");
} catch (Exception e) {
e.printStackTrace();
}
return JSONArray.toJSONString(resultMap);
}
@RequestMapping(value="/view/{id}",method=RequestMethod.GET)
public String view(@PathVariable String id,Model model,
@RequestParam(value="pageIndex",required=false) String pageIndex){
List<Reply_detail>replyList = null;
//设置页面容量
int pageSize =4;
//当前页码
Integer currentPageNo = 1;
if(pageIndex != null){
try{
currentPageNo = Integer.valueOf(pageIndex);
}catch (NumberFormatException e) {
e.printStackTrace();
}
}
//总数量(表)
int totalCount = 0;
try {
totalCount = invitationService.getVersionCount(Integer.parseInt(id));
} catch (Exception e) {
e.printStackTrace();
}
//总页数
PageSupport pages=new PageSupport();
pages.setCurrentPageNo(currentPageNo);
pages.setPageSize(pageSize);
pages.setTotalCount(totalCount);
int totalPageCount = pages.getTotalPageCount();
//控制首页和尾页
if(currentPageNo < 1){
currentPageNo = 1;
}else if(currentPageNo > totalPageCount){
currentPageNo = totalPageCount;
}
try {
replyList=invitationService.getVersionList(Integer.parseInt(id), currentPageNo, pageSize);
} catch (Exception e) {
e.printStackTrace();
}
model.addAttribute("id", id);
model.addAttribute("replyList", replyList);
model.addAttribute("totalPageCount", totalPageCount);
model.addAttribute("totalCount", totalCount);
model.addAttribute("currentPageNo", currentPageNo);
return "replyList";
}
@RequestMapping(value="/invitList")
public String invitList(){
return "invitationList";
}
@RequestMapping(value = "/add",method=RequestMethod.GET)
public String add(@RequestParam(value="id")String invid,Model model) {
model.addAttribute("invid",Integer.parseInt(invid));
return "replyadd";
}
@RequestMapping(value="/addsave",method=RequestMethod.POST)
public String addsave(Reply_detail rd,HttpSession session){
rd.setCreatedate(new Date());
try {
if(invitationService.rdadd(rd)){
return "redirect:/view/"+rd.getInvid();
}
} catch (Exception e) {
e.printStackTrace();
}
return "redirect:/add?id="+rd.getInvid();
}
}