package com.fjs.scenic.controller.news;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpSession;

import com.fjs.scenic.annotation.LoggerManage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.fjs.scenic.entity.news.News;
import com.fjs.scenic.service.news.NewsService;
import com.fjs.scenic.utils.ReturnResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Controller
@RequestMapping("/news/")
public class NewsController {

	@Autowired
	private NewsService newsService;
	@Autowired
    private RedisTemplate<String, Object> redisTemplate;
	
	/**
	 * 进入news页面
	 * 
	 * @param session
	 * @return
	 */
	@GetMapping("index")
	public ModelAndView index(HttpSession session) {
		return new ModelAndView("/news/News");
	}
	
	/**
	 * 查询所有订单
	 * 
	 * @param manage
	 *            条件
	 * @param currentPage
	 *            页码
	 * @param pageSize
	 *            每页显示数量
	 * @return
	 * @throws Exception 
	 */
	/**
	 * @param news
	 * @param currentPage
	 * @param pageSize
	 * @param startTime
	 * @param endTime
	 * @return
	 * @throws Exception
	 */
	@PostMapping("selectPage")
	@ResponseBody
	@LoggerManage(logDescription = "查询新闻列表")
	public ReturnResult selectPage(News news, int currentPage, int pageSize, 
			@RequestParam(required=false) String startTime, @RequestParam(required=false) String endTime) throws Exception {
		
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		
		pageSize = (pageSize == 0) ? 1 : pageSize;
		PageHelper.startPage(currentPage, pageSize);

		String str = "";
		//根据新闻标题模糊查询
		if(news.getTitle() != "" && news.getTitle() != null){
			str = "title like '%" + news.getTitle() + "%'";
		}else{
			//避免前端缓存 传值为 ""||null
			news.setTitle(null);
		}
		
		List<News> newsList = null;
		//根据时间区间查询
		if((startTime != null && !"".equals(startTime)) || (endTime != null  && !"".equals(endTime))){
			
			//开始时间为空 默认为一个月以前 -- 结束时间为空默认当前时间
			Calendar c = Calendar.getInstance();
			if(startTime == null || "".equals(startTime)){
				c.setTime(df.parse(endTime));
				c.add(Calendar.MONTH, -1);
				Date mouthLater = c.getTime();
				startTime = df.format(mouthLater);
			}
			if(endTime == null || "".equals(endTime)){
				endTime = df.format(new Date());
			}
			
			startTime = df.format(df.parse(startTime));
			endTime = df.format(df.parse(endTime));
			
			news.setParam(" time between '" + startTime + "' and '" + endTime + "'");
			
			if(!"".equals(str)){
				str += " and ";
			}

			str += " time between '" + startTime + "' and '" + endTime + "'";
		}
		
		if(!"".equals(str)){
			str += " order by time desc ";
			newsList = newsService.selectByParam(null, str);
		}else{
			newsList = newsService.select(news);
		}
		
		int i = 1;
		for(News news2 : newsList){
			news2.setNum(i);
			i++;
		}
		PageInfo<News> pageInfo = new PageInfo<>(newsList);
		return ReturnResult.ok(pageInfo);
	}
	
	/**
	 * 新增新闻
	 * @throws Exception 
	 */
	@PostMapping("add")
	@ResponseBody
	@LoggerManage(logDescription = "新增新闻")
	public ReturnResult add(News news) throws Exception {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		news.setTime(dateFormat.parse(dateFormat.format(new Date())));
		news.setMid(1); //发布人ID
		news.setStatus((byte) 1);
		newsService.insert(news);
		Set<String> keys = redisTemplate.keys("indexTopData:" + "*");
        redisTemplate.delete(keys);
		return ReturnResult.ok();
	}
	
	/**
	 * 根据ID查询新闻
	 * 
	 * @param id
	 * @return
	 */
	@PostMapping("get/{id}")
	@ResponseBody
	@LoggerManage(logDescription = "查询新闻")
	public ReturnResult get(@PathVariable("id") Integer id) {
		return ReturnResult.ok(newsService.select(new News(id)));
	}
	
	/**
	 * 根据ID删除新闻
	 * 
	 * @param id
	 * @return
	 */
	@PostMapping("del")
	@ResponseBody
	@LoggerManage(logDescription = "删除新闻")
	public ReturnResult del(Integer id) {
		newsService.delete(new News(id));
		Set<String> keys = redisTemplate.keys("indexTopData:" + "*");
        redisTemplate.delete(keys);
		return ReturnResult.ok();
	}
	
	/**
	 * 编辑新闻
	 * 
	 * @return
	 */
	@PostMapping("update")
	@ResponseBody
	@LoggerManage(logDescription = "编辑新闻")
	public ReturnResult update(News news) {
		newsService.update(news);
		Set<String> keys = redisTemplate.keys("indexTopData:" + "*");
        redisTemplate.delete(keys);
		return ReturnResult.ok();
	}
	
}
