package com.yc.education.controller.admin;

import com.yc.education.model.Technologyclassify;
import com.yc.education.service.TechnologyclassifyService;
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.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * huangpan
 * 2019/10/9
 * energy
 */
@Controller
public class TechnologyclassifyController {
    @Autowired
    TechnologyclassifyService technologyclassifyService;
   /* private static Random rand = new Random();
    private static String[] str = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };*/

    /*查询*/
    @RequestMapping("admin/tclassify-list.html")
    public ModelAndView getTclassify(String titleName){
        ModelAndView  modelAndView=new ModelAndView();
        List<Technologyclassify> technologyclassifies = technologyclassifyService.selectAllTclassify(titleName);
        modelAndView.addObject("technologyclassifies", technologyclassifies);
        return modelAndView;
    }
    /*修改分类信息*/
    @RequestMapping("admin/tclassifyByid.html")
    public ModelAndView selectTcById(@RequestParam( value = "id") int id){
        ModelAndView modelAndView =new ModelAndView("admin/tclassify-update");
        Technologyclassify technologyclassify = technologyclassifyService.selectTclassifyById(id);
        modelAndView.addObject("technologyclassify", technologyclassify);
        return modelAndView;
    }
    /*执行修改*/
    @RequestMapping("admin/tclassify-update.html")
    public void updateTclassify(Technologyclassify technologyclassify, HttpServletResponse response, HttpServletRequest request)throws Exception {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String commentdatemin = df.format(new Date());
        technologyclassify.setDatechanged(commentdatemin);
        technologyclassifyService.updateNotNull(technologyclassify);
        response.sendRedirect("tclassify-list.html");
    }
    /*新增*/
    @RequestMapping("admin/tclassify-add.html")
    public ModelAndView tclassifyAdd(){
       return  new ModelAndView();
    }
    /*执行新增*/
    @RequestMapping("admin/tclassify-save.html")
    public void saveTclassify(Technologyclassify technologyclassify, HttpServletResponse response, HttpServletRequest request)throws Exception {
       /* String rootPath = request.getSession().getServletContext().getRealPath("/");
        String contextPath = request.getSession().getServletContext().getContextPath();
        BaseState state = (BaseState) new ActionEnter(request, rootPath, contextPath, "img").invoke(ActionMap.UPLOAD_FILE);
        String url = state.getInfo("url");
        if (url!=null&&!url.equals("")){
            technologyclassify.setPicture(url);
        }*/
       if (technologyclassify.getDatechanged().equals("")){
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String commentdatemin = df.format(new Date());
        technologyclassify.setDatechanged(commentdatemin);}
        technologyclassifyService.save(technologyclassify);
        response.sendRedirect("tclassify-list.html");
    }
    /*删除*/
    @RequestMapping(value = ("admin/tclassifyDetele.html"),method = RequestMethod.POST)
    @ResponseBody
    public void deleteTclassify(@RequestParam( value = "id") int id){
        technologyclassifyService.deleteTclassifyById(id);
    }

   /* *//*ajax上传*//*
    @ResponseBody
    @RequestMapping("admin/uploadFindSkillImages")
    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;
    }*/
}
