package cn.ftebox.controller;

import cn.ftebox.entity.Blog;
import cn.ftebox.entity.Blogger;
import cn.ftebox.entity.PageBean;
import cn.ftebox.service.BlogService;
import cn.ftebox.service.BloggerService;
import cn.ftebox.util.PageUtil;
import cn.ftebox.util.StringUtil;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 首页
 */
@Controller
public class IndexController {
    @Resource
    private BlogService blogService;

    @RequestMapping({"/index.do"})
    public ModelAndView index(@RequestParam(value = "page", required = false) String page, HttpServletRequest request) {
        ModelAndView mav = new ModelAndView();
        if (StringUtil.isEmpty(page)) {
            page = "1";
        }
        PageBean pageBean = new PageBean(Integer.parseInt(page), 10);
        Map<String, Object> map = new HashMap<>();
        map.put("start", pageBean.getStart());
        map.put("size", pageBean.getPageSize());
        List<Blog> list = blogService.list(map);
        String pageCode = PageUtil.genPagination(request.getContextPath()+"index.do",blogService.getTotal(map),Integer.parseInt(page),list.size());
        mav.addObject("blogList", list);
        mav.addObject("pageCode",pageCode);
        return mav;
    }

    @RequestMapping({"/article.do"})
    public ModelAndView article(@RequestParam(value = "page", required = false) String page, HttpServletRequest request) {
        ModelAndView mav = new ModelAndView();
        if (StringUtil.isEmpty(page)) {
            page = "1";
        }
        PageBean pageBean = new PageBean(Integer.parseInt(page), 10);
        Map<String, Object> map = new HashMap<>();
        map.put("start", pageBean.getStart());
        map.put("size", pageBean.getPageSize());
        List<Blog> list = blogService.list(map);
        String pageCode = PageUtil.genPagination(request.getContextPath()+"article.do",blogService.getTotal(map),Integer.parseInt(page),list.size());
        mav.addObject("blogList", list);
        mav.addObject("pageCode",pageCode);
        return mav;
    }
}
