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

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

/**
 *@ClassName ProdutstController
 *@Description TODO
 *@Author QuZhangJing
 *@Date 2019/10/22 16:28
 *@Version 1.0
 */
@Controller
@RequestMapping("admin/")
public class ProdutstController {
    @Autowired
    ProductService productService;
    @Autowired
    ProductOneService productOneService;
    /*查询所有*/
    @RequestMapping("product-list.html")
    public ModelAndView getprod(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum ,
                                @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize,String tName){
        List<Product> products = productService.selproductAll(pageNum, pageSize, tName);
        List<Productclassify> product = productOneService.getProduct();
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("products", new PageInfo<>(products));
        modelAndView.addObject("product", product);
        modelAndView.addObject("tName", tName);
        return modelAndView;
    }
    /*编辑*/
    @RequestMapping("productById.html")
    public ModelAndView productById(Integer id, HttpSession session){
        ModelAndView modelAndView=new ModelAndView("admin/product-update");
        Product product = productService.selProductById(id);
        List<Productclassify> Productclassify = productOneService.getProductById(0);
        List<Productclassify> productById = productOneService.getProductById(product.getOneid());
        modelAndView.addObject("product", product);
        modelAndView.addObject("Productclassify", Productclassify);
        modelAndView.addObject("productById", productById);
        session.setAttribute("product", product);
        return modelAndView;
    }
   /*执行修改*/
    @RequestMapping("product-update.html")
    public void updateproduct(Product product, HttpSession session, HttpServletResponse response)throws Exception{
        Product product1 =(Product) session.getAttribute("product");
        int aNull = productService.updateNotNull(product);
        if (aNull>0){
            Productclassify productclassify=new Productclassify();
            if (product.getCfpid()!=product1.getCfpid()){
                productclassify.setId(product.getId());
                productclassify.setPfid(product.getCfpid());
                productOneService.updateNotNull(productclassify);
                if(product.getOneid()!=product1.getOneid()){
                    productclassify.setId(product.getCfpid());
                    productclassify.setPfid(product.getOneid());
                    productOneService.updateNotNull(productclassify);
                }
            }
        }
        response.sendRedirect("product-list.html");
    }
    /*新增*/
    @RequestMapping("product-add.html")
    public ModelAndView addProduct(@RequestParam(value = "produtId",defaultValue = "0")Integer produtId,@RequestParam(value = "pfid",defaultValue = "0")Integer pfid){
        ModelAndView modelAndView=new ModelAndView();
        if (produtId!=0||!produtId.equals(0)){
            List<Productclassify> productById = productOneService.getProductById(0);
            List<Productclassify> productByIdt = productOneService.getProductById(pfid);
            modelAndView.addObject("Productclassify", productById);
            modelAndView.addObject("productById", productByIdt);
            modelAndView.addObject("oneids", pfid);
            modelAndView.addObject("produtId", produtId);
        }else {
            List<Productclassify> Productclassify = productOneService.gerreemod();
            int ids = 0;
            ids = Productclassify.get(0).getId();
            List<Productclassify> productById = productOneService.getProductById(ids);
            modelAndView.addObject("Productclassify", Productclassify);
            modelAndView.addObject("productById", productById);
        }
        return modelAndView;
    }
    /*执行新增*/
    @RequestMapping("product-save.html")
    public void saveProduct(Product product, HttpSession session, HttpServletResponse response)throws Exception{
        Productclassify productclassify=new Productclassify();
        if (product.getReleasetime().equals("")){
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String commentdatemin = df.format(new Date());
            product.setReleasetime(commentdatemin);
        }
        productclassify.setPfid(product.getCfpid());
        productclassify.setCats(3);
        productOneService.save(productclassify);
        int pclassifyById = productOneService.getPclassifyById();
        product.setId(pclassifyById);
        int proCount= productService.save(product);
        if(proCount<1){
            productOneService.deletePclassifyById(pclassifyById);
    }
        response.sendRedirect("product-list.html");
    }
    /*查询二级信息*/
    @RequestMapping("producterji.html")
    @ResponseBody
    public List<Productclassify> producterji(Integer id){
        List<Productclassify> productById = productOneService.getProductById(id);
        return productById;
    }
    /*执行删除*/
    @RequestMapping("delproductById")
    @ResponseBody
    public void delProductById(@RequestParam( value = "id")Integer id){
        productService.deleteProductById(id);
        productOneService.deletePclassifyById(id);
    }
}
