package lingtao.net.controller;

import java.io.File;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
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 org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import lingtao.net.bean.Article;
import lingtao.net.bean.Msg;
import lingtao.net.service.ArticleService;

@Controller
public class ArticleController {

	@Autowired
	private ArticleService articleService;

	/**
	 * 获取所有的文章
	 * 
	 * @param article
	 * @return
	 */
	@RequestMapping("/getArticle")
	@ResponseBody
	public Msg articleList(Article article) {
		PageHelper.startPage(1, 10);
		List<Article> articles = articleService.getArticle(article);
		PageInfo<Article> pageInfo = new PageInfo<Article>(articles);
		return Msg.success().add("articles", pageInfo);
	}

	/**
	 * 新增文章
	 */
	@RequestMapping("/addArticle")
	@ResponseBody
	public Msg addArticle(Article article) {
		return articleService.addArticle(article);
	}

	/**
	 * 文章中的图片
	 */
	@RequestMapping(value = "/uploadconimage", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> uploadconimage(HttpServletRequest request, @RequestParam MultipartFile file) {
		Map<String, Object> mv = new HashMap<String, Object>();
		Map<String, String> mvv = new HashMap<String, String>();
		try {
			String rootPath = request.getSession().getServletContext().getRealPath("/image/");
			String contextPath = request.getContextPath();
			System.err.println(contextPath);
			System.out.println(rootPath);
			Calendar date = Calendar.getInstance(); // Calendar.getInstance()是获取一个Calendar对象并可以进行时间的计算，时区的指定
			String originalFile = file.getOriginalFilename(); // 获得文件最初的路径
			String uuid = UUID.randomUUID().toString(); // UUID转化为String对象
			String newfilename = date.get(Calendar.YEAR) + "" + (date.get(Calendar.MONTH) + 1) + ""
					+ date.get(Calendar.DATE) + uuid.replace("-", "") + originalFile;
			// 得到完整路径名
			File newFile = new File(rootPath + newfilename);
			/* 文件不存在就创建 */
			if (!newFile.getParentFile().exists()) {
				newFile.getParentFile().mkdirs();
			}
			String filename = originalFile.substring(0, originalFile.indexOf("."));
			System.out.println(originalFile);
			System.out.println(filename);
			file.transferTo(newFile);
			System.out.println("newFile : " + newFile);
			String urlpat = contextPath + "/image/" + newfilename;
			mvv.put("src", urlpat);
			mvv.put("title", newfilename);
			mv.put("code", 0);
			mv.put("msg", "上传成功");
			mv.put("data", mvv);
			return mv;
		} catch (Exception e) {
			e.printStackTrace();
			mv.put("success", 1);
			return mv;
		}
	}

	// 用户跳转页面
	@RequestMapping("/toUpdateArticle")
	public String index(HttpServletRequest request, HttpServletResponse response) throws Exception {
		 return "updateArticle";
		//response.sendRedirect(request.getContextPath() + "/views/updateArticle.jsp");
	}

	/**
	 * 修改文章
	 * 
	 */
	@RequestMapping("/updateArticle")
	@ResponseBody
	public Msg updateArticle(Article article) {
		return articleService.updateArticleById(article);
	}

	/**
	 * 文章详情
	 * 
	 */
	@RequestMapping("/articleInfo")
	@ResponseBody
	public Msg articleInfo(@RequestParam("id") Integer id) {
		Article article = articleService.articleInfo(id);
		return Msg.success().add("article", article);
	}

	/**
	 * 删除文章
	 */
	@RequestMapping("/deleteArticle")
	@ResponseBody
	public Msg delArticle(@RequestParam("id") Integer id) {
		return articleService.delArticleById(id);
	}
}
