package com.yc.education.controller.admin;/**
 * huangpan
 * 2019/10/14
 * energy
 */

import com.yc.education.model.Job;
import com.yc.education.service.JobService;
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.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 *@ClassName JobController
 *@Description TODO
 *@Author QuZhangJing
 *@Date 2019/10/14 10:01
 *@Version 1.0
 */
@Controller
@RequestMapping("admin/")
public class JobController {
    @Autowired
    JobService jobService;
    /*查询所有*/
    @RequestMapping("job-list.html")
    public ModelAndView getJobs(){
        ModelAndView modelAndView=new ModelAndView();
        List<Job> jobs = jobService.getJobs();
        modelAndView.addObject("jobs", jobs);
        return modelAndView;
    }
    /*查询单条记录*/
    @RequestMapping("getJobById.html")
    public ModelAndView getJobById(Integer id){
        ModelAndView modelAndView=new ModelAndView("/admin/job-update");
        Job jobById = jobService.getJobById(id);
        modelAndView.addObject("jobById", jobById);
        return modelAndView;
    }
    /*修改信息*/
    @RequestMapping("job-update.html")
    public void updateJob(Job job , HttpServletResponse response)throws Exception{
 /*       SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String commentdatemin = df.format(new Date());
        job.setReleasedate(commentdatemin);*/
        jobService.updateNotNull(job);
        response.sendRedirect("job-list.html");
    }
    /*跳转新增*/
    @RequestMapping("job-add.html")
    public ModelAndView jobAdd(){
        return new ModelAndView();
    }
    /*执行新增*/
    @RequestMapping("job-save.html")
    public void saveJob(Job job , HttpServletResponse response)throws Exception{
        if (job.getReleasedate().equals("")){
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String commentdatemin = df.format(new Date());
            job.setReleasedate(commentdatemin);
        }
     jobService.save(job);
     response.sendRedirect("job-list.html");
    }
    /*删除*/
    @RequestMapping("deleJobById.html")
    @ResponseBody
    public void deleJob(@RequestParam( value = "id")Integer id){
        jobService.delJobById(id);
    }
}
