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

import com.github.pagehelper.PageInfo;
import com.yc.education.model.Productclassify;
import com.yc.education.service.ProductOneService;
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.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Date;

/**
 *@ClassName ProdutTwoController
 *@Description TODO
 *@Author QuZhangJing
 *@Date 2019/10/22 15:54
 *@Version 1.0
 */
@Controller
public class ProdutTwoController {
    @Autowired
    ProductOneService producttwoService;

    /*查询所有二级信息*/
    @RequestMapping("admin/producttwo-list.html")
    public ModelAndView getproducttwo(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum ,
                                      @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize,String tName,@RequestParam(value = "pfid",defaultValue = "2")Integer pfid ){
        List<Productclassify> productclassifies = producttwoService.getcast(pfid, tName, pageNum, pageSize);
        List<Productclassify> product = producttwoService.getProduct();
        ModelAndView modelAndView=new ModelAndView("admin/produttwo-list");
        modelAndView.addObject("productclassifies", new PageInfo<>(productclassifies));
        modelAndView.addObject("product", product);
        modelAndView.addObject("tName", tName);
        return modelAndView;
    }
    /*新增二级分类*/
    @RequestMapping("admin/producttwo-add.html")
    public ModelAndView add(Integer fpid){
        ModelAndView modelAndView=new ModelAndView("admin/produttwo-add");
            List<Productclassify> product = producttwoService.gerreemod();
            modelAndView.addObject("product", product);
            modelAndView.addObject("ids", fpid);
        return modelAndView;
    }
    /*实现二级新增*/
    @RequestMapping("admin/producttwo-save.html")
    public void saveProduct(Productclassify productclassify, HttpServletResponse response, HttpServletRequest request)throws Exception{
        productclassify.setCats(2);
        producttwoService.save(productclassify);
        response.sendRedirect("producttwo-list.html");
    }
    /*修改二级分类*/
    @RequestMapping("admin/producttwoById.html")
    public ModelAndView producttwoById(Integer id){
        Productclassify productclassify = producttwoService.selectPclassifyById(id);
        ModelAndView modelAndView=new ModelAndView("admin/produttwo-update");
        List<Productclassify> product = producttwoService.getProductById(0);
        modelAndView.addObject("ids", productclassify.getPfid());
        modelAndView.addObject("product", product);
        modelAndView.addObject("productclassify", productclassify);
        return modelAndView;
    }
    /*执行二级分类修改*/
    @RequestMapping("admin/producttwo-update.html")
    public void updateproduct(Productclassify productclassify, HttpServletResponse response, HttpServletRequest request)throws Exception{
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String commentdatemin = df.format(new Date());
        productclassify.setDatechanged(commentdatemin);
        producttwoService.updateNotNull(productclassify);
        response.sendRedirect("producttwo-list.html");
    }
    /*执行删除*/
    @RequestMapping("admin/delproductt.html")
    @ResponseBody
    public boolean delproduct(Integer id){
        int i = producttwoService.countPclassify(id);
        System.err.println(i);
        if (i>=1){
            return false;
        }else{
            producttwoService.deletePclassifyById(id);
            return true;
        }
    }
}
