package com.sgbg18.controller;

import com.sgbg18.entity.Employee_ztb18326;
import com.sgbg18.service.EmployeeService_ztb18326;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;
@Controller
public class EmployeeController_ztb18326 {
    @Autowired
    EmployeeService_ztb18326 employeeServiceZtb18326;
    @RequestMapping("/upload")
    public String fileupload(@Valid Employee_ztb18326 emp, BindingResult bindingResult, HttpServletRequest request,
                             List<MultipartFile> uploadFile, ModelMap map) {
        System.out.println(emp);
        List<FieldError> fieldErrors = bindingResult.getFieldErrors();
        if (!fieldErrors.isEmpty()) {
            for (FieldError fe : fieldErrors) {
                map.put("ERO_" + fe.getField(), fe.getField() + fe.getDefaultMessage());
            }
            System.out.println("有不合法参数");
            return "forward:register.jsp";
        } else {
            System.out.println(fieldErrors);
            if (!uploadFile.isEmpty() || uploadFile.size() > 0) {
                for (MultipartFile file : uploadFile) {
                    String originalFilename = file.getOriginalFilename();
                    String dirpath = request.getSession().getServletContext().getRealPath("/upload/");
                    File filepath = new File(dirpath);
                    if (!filepath.exists()) {
                        filepath.mkdirs();
                    }
                    String fileName = emp.getEmpName() + "_" + UUID.randomUUID() + originalFilename;
                    emp.setEmpPhoto(fileName);
                    map.put("emp", emp);
                    try {
                        file.transferTo(new File(dirpath + fileName));
                        emp.setEmpPhoto(fileName);
                        System.out.println(emp);
                        employeeServiceZtb18326.insert(emp);
                    } catch (IOException e) {
                        e.printStackTrace();
                        return "error";
                    }
                    return "forward:all";

                }
                return "forward:all";
            } else {
                return "error";
            }
        }
    }

    @RequestMapping("/all")
    public String all(ModelMap modelMap) {
        System.out.println(employeeServiceZtb18326.queryAllByLimit(0, 10));
        modelMap.put("emp", employeeServiceZtb18326.queryAllByLimit(0, 10));
        return "listEmployees";
    }
    @RequestMapping("/delete")
    public String deById(String id, ModelMap modelMap) {
        employeeServiceZtb18326.deleteById(Integer.valueOf(id));
        modelMap.put("emp", employeeServiceZtb18326.queryAllByLimit(0, 10));
        return "listEmployees";
    }
    @RequestMapping("/index")
    public String index() {
        return "forward:register.jsp";
    }
}

