package com.ilovecl.demo.web;
import com.ilovecl.demo._const.RepairEnumCN;
import com.ilovecl.demo._const.StudentConst;
import com.ilovecl.demo._const.UrgentRepairEnum;
import com.ilovecl.demo.dto.LoginResult;
import com.ilovecl.demo.dto.ModifyRepairResult;
import com.ilovecl.demo.dto.StudentResult;
import com.ilovecl.demo.dto.StudentUrgentResult;
import com.ilovecl.demo.entity.Repair;
import com.ilovecl.demo.entity.RepairInfoVo;
import com.ilovecl.demo.entity.Student;
import com.ilovecl.demo.entity.UrgentRepair;
import com.ilovecl.demo.service.RepairService;
import com.ilovecl.demo.service.StudentService;
import com.ilovecl.demo.service.UrgentRepairService;
import com.ilovecl.demo.util.MD5;
import com.ilovecl.demo.util.Spider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
/**
* 用户的web层
*
*/
@Controller
@CrossOrigin(origins = {"http://localhost:8080", "null"})
@RequestMapping("/student")
public class StudentController {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
@Resource
private StudentService studentService;
@Resource
private RepairService repairService;
@Resource
private UrgentRepairService urgentRepairService;
@RequestMapping(value = "/introduce", method = RequestMethod.GET)
public String index() {
return "student/introduce";
}
/**
* 登录的GET方法
*
* @return
*/
@RequestMapping(value = "/login", method = RequestMethod.GET)
public String login() {
return "student/login";
}
/**
* 登录的POST方法
*
* @param id
* @param password
* @param httpSession
* @return
*/
@RequestMapping(value = "/login", method = RequestMethod.POST)
public
@ResponseBody
LoginResult login(String id, String password, HttpSession httpSession, HttpServletResponse httpServletResponse) {
LoginResult loginResult=null;
String stuName = null;
Student student = studentService.getStudentById(id);
Spider spider = new Spider();
try {
//调用service将数据存入数据库中
if(student==null){
stuName = spider.getStudentName(id, password);
student = new Student(id, stuName, password);
System.out.println(stuName);
studentService.addStudent(id, stuName, password, 0, "", "");
loginResult = new LoginResult(true);
}else{
stuName = student.getName();
// 密码正确
if (student.getPassword().equals(password)) {
loginResult = new LoginResult(true);
} else {
loginResult = new LoginResult(false);
loginResult.setReason("invalid user");
}
}
} catch (IOException e) {
e.printStackTrace();
}
if(loginResult.isSuccess()){
// 登录成功后,为该学生生成session
httpSession.setAttribute(StudentConst.STUDENT_ID, id);
httpServletResponse.addCookie(new Cookie(StudentConst.STUDENT_ID, id));
System.out.println("StudentName:"+StudentConst.STUDENT_NAME);
httpServletResponse.addCookie(new Cookie(StudentConst.STUDENT_NAME, stuName));
}
logger.info("***************************************************************************");
logger.info("登录: " + String.valueOf(loginResult.isSuccess()) + " id : " + id + " password : " + password);
logger.info("***************************************************************************");
return loginResult;
}
/**
* 退出的控制
*
* @param httpSession
* @return
*/
@RequestMapping(value = "/logout", method = RequestMethod.GET)
public String logout(HttpSession httpSession) {
httpSession.removeAttribute(StudentConst.STUDENT_EMAIL);
return "redirect:/student/login";
}
/**
* 提交报修单的POST方法
*
* @param model
* @return
*/
@RequestMapping(value = "/commit", method = RequestMethod.GET)
public String commit(Model model) {
return "/student/commit";
}
@RequestMapping(value = "/commit", method = RequestMethod.POST)
public String commit(@RequestParam("detail") String detail, @RequestParam("place") String place,
@RequestParam("file") MultipartFile file, HttpServletRequest httpServletRequest) {
String id = httpServletRequest.getSession().getAttribute(StudentConst.STUDENT_ID).toString();
Student student = studentService.getStudentById(id);
String picMD5 = "";
logger.info(detail);
logger.info(place);
logger.info(picMD5);
try {
logger.info(file.getInputStream().toString());
} catch (IOException e) {
e.printStackTrace();
}
if (file != null) {
try {
picMD5 = MD5.getMD5(id + String.valueOf(System.currentTimeMillis()) + file.getOriginalFilename());
} catch (Exception e) {
e.printStackTrace();
}
// 往数据库中插入维修单记录
repairService.submitRepair(detail, place, picMD5, student.getId());
// 保存现场照片
String path = httpServletRequest.getSession().getServletContext().getRealPath("/");
System.out.println("图片路径:" + path);
String fileName = picMD5;
File targetFile = new File(path, fileName);
try {
InputStream inputStream = file.getInputStream();
OutputStream outputStream = new FileOutputStream(targetFile);
byte[] buffer = new byte[2048];
int len = 0;
while ((len = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, len);
}
inputStream.close();
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
} else {
// 往数据库中插入维修单记录
repairService.submitRepair(detail, place, picMD5, student.getId());
}
return "redirect:/student/dashboard";
}
/**
* 获取显示的主页面
*
* @param model
* @return
*/
@RequestMapping(value = "/dashboard", method = RequestMethod.GET)
public String board(Model model, HttpServletRequest httpServletRequest) {
String id = httpServletRequest.getSession().getAttribute(StudentConst.STUDENT_ID).toString();
List<Repair> repairs = repairService.getRepqirByStudentId(id);
List<RepairInfoVo> repairInfoVos = new ArrayList<>();
for(Repair repair: repairs){
repair.setPicMD5("/"+repair.getPicMD5());
RepairInfoVo repairInfoVo = new RepairInfoVo(repair);
repairInfoVo.setStatesInfo(RepairEnumCN.stateOf(repair.getStatus()).getStateInfo());
repairInfoVos.add(repairInfoVo);
}
model.addAttribute("repairInfoVos", repairInfoVos);
return "/student/dashboard";
}
/**
* 报修单详情
*
* @param repairId
* @param model
* @return
*/
@RequestMapping(value = "/repair/{repairId}/detail", method = RequestMethod.GET)
public String detail(@PathVariable("repairId