package com.yc.education.controller.admin;

import com.github.pagehelper.PageInfo;
import com.yc.education.model.Culture;
import com.yc.education.model.History;
import com.yc.education.service.CultureService;
import com.yc.education.service.HistoryService;
import org.apache.ibatis.annotations.Param;
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.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * huangpan
 * 2019/9/26
 * energy
 */
@Controller
public class HistoryController {
    @Autowired
    HistoryService historyService;
    @Autowired
    CultureService cultureService;
    /*查询所有*/
    @RequestMapping("admin/history-list.html")
    public ModelAndView findHistory(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum ,
                                    @RequestParam(value = "pageSize",defaultValue = "12") Integer pageSize,@RequestParam(value = "pageSize",defaultValue = "2") Integer id){
        ModelAndView modelAndView=new ModelAndView();
        Culture cultures = cultureService.getCulture(id);
        modelAndView.addObject("cultures", cultures);
        List<History> list=historyService.getHistory(pageNum,pageSize);
        modelAndView.addObject("pageInfo", new PageInfo<>(list));
        return modelAndView;
    }
    /*修改*/
    @RequestMapping("admin/getHistoryId.html")
    public ModelAndView showHistory(@Param("id") int id){
        ModelAndView modelAndView=new ModelAndView("/admin/history-update");
        History classi= historyService.getHistoryId(id);
        modelAndView.addObject("classi", classi);
        return modelAndView;
    }
    /*新增*/
    @RequestMapping("admin/history-add.html")
    public ModelAndView reqHistory(){
        return  new ModelAndView();
    }
    /*执行修改*/
    @RequestMapping(value = "admin/history-update.html",method = RequestMethod.POST)
    public void  updateHistory(HttpServletRequest request, HttpServletResponse response, History history)
            throws Exception{
        historyService.updateNotNull(history);
        response.sendRedirect("history-list.html");
    }

    /*执行新增*/
    @RequestMapping(value = "admin/history-save.html")
    public void historySave(HttpServletRequest request, HttpServletResponse response, History history)
            throws Exception{
        history.setHfpid(2);
        historyService.save(history);
        response.sendRedirect("history-list.html");
    }

    @RequestMapping(value = "admin/historySave.html", method = RequestMethod.POST)
    public  void history_save(HttpServletRequest request, HttpServletResponse response, String htime,String hdescribe)
            throws Exception{
        System.out.println(htime+"124"+hdescribe);
      /*  history.setHfpid(2);
        historyService.save(history);
        response.sendRedirect("history-list.html");*/
    }
    /*删除*/
    @RequestMapping(value = "admin/deleteHistoryById.html",method = RequestMethod.POST)
    @ResponseBody
    public void deleteGetId(@Param("id") int id){
        historyService.deleteHistoryd(id);
    }
}
