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 com.yc.education.util.AjaxMessage;
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.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 *@ClassName ProdutOneController
 *@Description TODO
 *@Author QuZhangJing
 *@Date 2019/10/22 14:39
 *@Version 1.0
 */
@Controller
public class ProdutOneController {
    @Autowired
    ProductOneService productOneService;
    private static Random rand = new Random();
    private static String[] str = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };

    /*查询所有一级信息*/
    @RequestMapping("admin/productone-list.html")
    public ModelAndView getproductone(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum ,
                                      @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize,String tName,@RequestParam(value = "pfid",defaultValue = "0")Integer pfid ){
        List<Productclassify> productclassifies = productOneService.selAllPclassify(pfid, tName, pageNum, pageSize);
        ModelAndView modelAndView=new ModelAndView("admin/produtone-list");
        modelAndView.addObject("productclassifies", new PageInfo<>(productclassifies));
        modelAndView.addObject("tName", tName);
        return modelAndView;
    }
    /*新增一级分类*/
    @RequestMapping("admin/productone-add.html")
    public ModelAndView add(){return new ModelAndView("admin/produtone-add");}
    /*实现一级新增*/
    @RequestMapping("admin/productone-save.html")
    public void saveProduct(Productclassify productclassify, HttpServletResponse response)throws Exception{
        productclassify.setCats(1);
        productclassify.setPfid(0);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String commentdatemin = df.format(new Date());
        productclassify.setDatechanged(commentdatemin);
        productOneService.save(productclassify);
        response.sendRedirect("productone-list.html");
    }
    /*修改一级分类*/
    @RequestMapping("admin/productoneById.html")
    public ModelAndView productoneById(Integer id, HttpSession session){
        Productclassify productclassify = productOneService.selectPclassifyById(id);
        ModelAndView modelAndView=new ModelAndView("admin/produtone-update");
        modelAndView.addObject("productclassify", productclassify);
        session.setAttribute("productclassify", productclassify);
        return modelAndView;
    }
    /*执行一级分类修改*/
    @RequestMapping("admin/productone-update.html")
    public void updateproduct(Productclassify productclassify, HttpServletResponse response, HttpServletRequest request,HttpSession session)throws Exception{
       Productclassify productclassify1 = (Productclassify)session.getAttribute("productclassify");
       if (productclassify.getSurfaceplot()==null||productclassify.getSurfaceplot().equals("")){
           productclassify.setSurfaceplot(productclassify1.getSurfaceplot());
       }
        if (productclassify.getFlatpattern()==null||productclassify.getFlatpattern().equals("")){
            productclassify.setFlatpattern(productclassify1.getFlatpattern());
        }
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String commentdatemin = df.format(new Date());
        productclassify.setDatechanged(commentdatemin);
        productOneService.updateNotNull(productclassify);
        response.sendRedirect("productone-list.html");
    }
    /*执行删除*/
    @RequestMapping("admin/delproduct.html")
    @ResponseBody
    public boolean delproduct(Integer id){
        int i = productOneService.countPclassify(id);
        System.err.println(i);
        if (i>=1){
            return false;
        }else{
            productOneService.deletePclassifyById(id);
            return true;
        }
    }
    /*ajax上传*/
    @ResponseBody
    @RequestMapping("admin/uploadFindSkillImagesspf")
    public AjaxMessage<Object> uploadFindSkillImages(HttpServletRequest request)
            throws IllegalStateException, IOException
    {
        AjaxMessage<Object> ajax = new AjaxMessage<Object>();
        System.out.println("进入控制层");
        String pathString = "";
        String allpath = "";
        List<String> paths = new ArrayList<String>();

        CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(
                request.getSession().getServletContext());
        if (multipartResolver.isMultipart(request))
        {
            MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest)request;

            Iterator<String> iter = multiRequest.getFileNames();
            while (iter.hasNext())
            {
                MultipartFile file = multiRequest.getFile((String)iter.next());
                System.out.println("file:" + file.getSize());
                if (!file.isEmpty())
                {
                    String sms = "";

                    StringBuffer buf = new StringBuffer();
                    for (int i = 0; i < 3; i++) {
                        buf.append(str[rand.nextInt(10)]);
                    }
                    String originalFileName = file.getOriginalFilename();



                    ajax.setData(originalFileName);


                    String suffixString = originalFileName.substring(originalFileName.lastIndexOf(".") + 1);


                    String fileName = sms + new Date().getTime() + "." + suffixString;


                    String path = request.getSession().getServletContext().getRealPath("/");
                    String contextPath = request.getSession().getServletContext().getContextPath();

                    File localFile = new File(path + "findskill/" + fileName);
                    System.err.println(path + "findskill/" + fileName);
                    try
                    {
                        file.transferTo(localFile);

                        paths.add("/picat/" + fileName);
                        pathString = contextPath + "/findskill/" + fileName;
                        allpath = path + "findskill/" + fileName;
                    }
                    catch (IllegalStateException e)
                    {
                        e.printStackTrace();
                    }
                    catch (IOException e)
                    {
                        e.printStackTrace();
                    }
                }
            }
        }
        ajax.setMsg(pathString);
        return ajax;
    }
}
