package com.yc.education.controller.admin;

import com.yc.education.util.AjaxMessage;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
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 java.io.File;
import java.io.IOException;
import java.util.*;

/**
 * @ClassName FindskillAdminController
 * @Description TODO  找技术
 * @Author QuZhangJing
 * @Date 2018/9/10 11:53
 * @Version 1.0
 */
@Controller
/*@RequestMapping("/admin/")*/
public class FindskillAdminController{

   /* @Autowired
    private FindskillService findskillService;*/

    private static Random rand = new Random();

    private static String[] str = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
    /**
     * 找技术列表
     * @param type
     * @param title
     * @param pageNum
     * @param pageSize
     * @return
     */
 /*   @RequestMapping("findskill_list.html")
    public ModelAndView findskill_list(
            @RequestParam(required = false, defaultValue = "0") int type,
            @RequestParam(required = false, defaultValue = "") String title,
            @RequestParam(required = false, defaultValue = "0") Integer pageNum,
            @RequestParam(required = false, defaultValue = "10") Integer pageSize
    ){
        ModelAndView  result =new ModelAndView();
        List<Findskill> findskills =  findskillService.findSkill(type,title,pageNum,pageSize);
        result.addObject("pageInfo",new PageInfo<Findskill>(findskills));
        result.addObject("type",type);
        result.addObject("title",title);
        result.addObject("pageNum",pageNum);
        result.addObject("pageSize",pageSize);
        return result;
    }


    *//**
     * 添加找技术
     * @return
     *//*
    @RequestMapping("findskill_add.html")
    public ModelAndView findskill_add(){return new ModelAndView();}


    *//**
     * 编辑找技术
     * @param id
     * @return
     *//*
    @RequestMapping("findskill_edit.html")
    public ModelAndView findskill_add(long id){
        ModelAndView result = new ModelAndView();

       Findskill findskill =  findskillService.selectByKey(id);

        *//*List<String>  imgurls =  new ArrayList<String>();
        try {
            String [] imgurl = findskill.getBanner().split(",");
            for (int i = 0; i < imgurl.length; i++) {
                imgurls.add(imgurl[i]);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }*//*
        *//*findskill.setImages(imgurls);*//*
        findskill.setAdddate(DateUtils.getSpecifyDate(findskill.getAddtime(),DateUtils.FORMAT_YYYY_MM_DD));
        result.addObject("findskill",findskill);

        return result;
    }

    @RequestMapping("findskill_add")
    public ModelAndView findskill_add(Findskill findskill,
                                          @RequestParam(required = false, defaultValue = "")String [] imgsurl
    ){
        *//*String imgs ="";
        for (int i = 0; i < imgsurl.length; i++) {
            imgs+=imgsurl[i]+",";
        }
        findskill.setBanner(imgs);*//*
        findskill.setBanner(findskill.getImg());
        if(! "".equals(findskill.getAdddate())){
            try {
                findskill.setAddtime(DateUtils.getDateByPattern(findskill.getAdddate(),DateUtils.FORMAT_YYYY_MM_DD));
            }catch (Exception e){
            }
        }else{
            findskill.setAddtime(new Date());
        }
        findskill.setMakenum(0);
        findskill.setCollectnum(0);
        int rows = findskillService.save(findskill);
        return ViewUtil.returnview(rows,"findskill_list.html","找技术");
    }


    @RequestMapping("findskill_edit")
    public ModelAndView findskill_edit(Findskill findskill,
                                          @RequestParam(required = false, defaultValue = "")String [] imgsurl
    ){
        *//*String imgs ="";
        for (int i = 0; i < imgsurl.length; i++) {
            imgs+=imgsurl[i]+",";
        }*//*
        findskill.setBanner(findskill.getImg());
        if(! "".equals(findskill.getAdddate())){
            try {
                findskill.setAddtime(DateUtils.getDateByPattern(findskill.getAdddate(),DateUtils.FORMAT_YYYY_MM_DD));
            }catch (Exception e){
            }
        }
        int rows = findskillService.updateNotNull(findskill);
            return ViewUtil.returnview(rows,"findskill_list.html","找技术");
    }

    *//**
     * 删除
     * @param id
     * @return
     *//*
    @ResponseBody
    @RequestMapping("delfindskill")
    public AjaxMessage<Object> delfindskill(String id) {
        String[] idlist = id.split(",");
        int num = idlist.length;
        int rows = 0;
        for (String string : idlist) {
           rows += findskillService.delete(Long.parseLong(string));
        }
        return ViewUtil.returnajax(rows, num);
    }*/

    @RequestMapping("admin/test-list.html")
    public ModelAndView test(){
        ModelAndView modelAndView=new ModelAndView("admin/findskill_add");
        return modelAndView;
    }
    /**
     * 上传找技术图片
     * @param request
     * @return
     * @throws IllegalStateException
     * @throws IOException
     */
   /* @ResponseBody
    @RequestMapping("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;
    }*/



}
