package com.cidic.design.controller;

import java.util.Date;
import java.util.List;
import java.util.Optional;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
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.cidic.design.DcController;
import com.cidic.design.exception.DCException;
import com.cidic.design.model.News;
import com.cidic.design.model.ResultModel;
import com.cidic.design.service.NewsService;

/**
 * 大赛新闻信息处理
 * @author dev
 *
 */
@Controller
@RequestMapping(value="/news")
public class NewsController  extends DcController{

	@Autowired
	@Qualifier(value = "newsServiceImpl")
	private NewsService newsServiceImpl;
	
	@RequestMapping(value = "/news")
	public String news(HttpServletRequest request, Model model) {
		return "frontend/news";
	}

	@RequestMapping(value = "/newsMgr")
    public String newsMgr(HttpServletRequest request, Model model) {
        return "backend/newsMgr";
    }
	
	@RequestMapping(value = "/newsCOU")
    public String newsCOU(HttpServletRequest request, Model model) {
        return "backend/newsCOU";
    }
	
	@ResponseBody
	@RequestMapping(value="/createNews", method = RequestMethod.POST)
	public ResultModel createNews(HttpServletRequest request, HttpServletResponse response,@RequestBody News news){
		resultModel = new ResultModel();
		try{
			news.setPublishTime(new Date());
			newsServiceImpl.createNews(news);
			resultModel.setResultCode(200);
			return resultModel;
		}
		catch(Exception e){
			throw new DCException(500, "创建出错");
		}
	}
	
	@ResponseBody
	@RequestMapping(value="/deleteNews", method = RequestMethod.POST)
	public ResultModel deleteNews(HttpServletRequest request, HttpServletResponse response,@PathVariable int id){
		resultModel = new ResultModel();
		try{
			newsServiceImpl.deleteNews(id);
			resultModel.setResultCode(200);
			return resultModel;
		}
		catch(Exception e){
			throw new DCException(500, "创建出错");
		}
	}
	
	@ResponseBody
	@RequestMapping(value="/updateNews", method = RequestMethod.POST)
	public ResultModel updateNews(HttpServletRequest request, HttpServletResponse response,@RequestBody News news){
		resultModel = new ResultModel();
		try{
			news.setPublishTime(new Date());
			newsServiceImpl.updateNews(news);
			resultModel.setResultCode(200);
			return resultModel;
		}
		catch(Exception e){
			throw new DCException(500, "创建出错");
		}
	}
	
	@ResponseBody
	@RequestMapping(value="/findNewsById/{id}", method = RequestMethod.GET)
	public ResultModel findNewsById(HttpServletRequest request, HttpServletResponse response,@PathVariable int id){
		
		resultModel = new ResultModel();
		try{
			News news = newsServiceImpl.findNewsById(id).get();
			
			resultModel.setObject(news);
			
			resultModel.setResultCode(200);
			return resultModel;
		}
		catch(Exception e){
			throw new DCException(500, "创建出错");
		}
	}
	
	@ResponseBody
	@RequestMapping(value="/findNewsByPage", method = RequestMethod.POST)
	public ResultModel findNewsByPage(HttpServletRequest request, HttpServletResponse response,@RequestParam int offset, @RequestParam int limit){

		resultModel = new ResultModel();
		try{
			List<News> list = newsServiceImpl.findNewsByPage(offset, limit);
			resultModel.setResultCode(200);
			resultModel.setObject(list);
			return resultModel;
		}
		catch(Exception e){
			throw new DCException(500, "创建出错");
		}
	}
}

@@ -779,16 +779,69 @@
 nseBody;
+%0Aimport org.springframework.web.servlet.ModelAndView;
 %0A%0Aimport
@@ -1687,24 +1687,409 @@
 U%22;%0A    %7D%0A%09%0A
+%09@RequestMapping(value = %22/newsCOU/%7Bid%7D%22,method = RequestMethod.GET)%0A    public ModelAndView updateCOU(HttpServletRequest request, @PathVariable int id) %7B%0A%09%09News news = null;%0A%09%09if (id %3E 0)%7B%0A%09%09%09news = newsServiceImpl.findNewsById(id).get();%0A%09%09%7D%0A%09%09%0A%09%09ModelAndView model = new ModelAndView();%0A%09%09model.setViewName(%22backend/newsCOU%22);%0A%09%09model.addObject(news);%0A        return model;%0A    %7D%0A%09%0A
 %09@ResponseBo

fix bugs