package com.ygj.yuemum.controller.admin; import com.alibaba.fastjson.JSONObject; import com.ygj.yuemum.component.Constant; import com.ygj.yuemum.domain.admin.Photo; import com.ygj.yuemum.domain.admin.Resume; import com.ygj.yuemum.domain.maternitymatron.JlServiceUser; import com.ygj.yuemum.domain.pay.CustomerPayVoucher; import com.ygj.yuemum.service.admin.PhotoService; import com.ygj.yuemum.service.admin.ResumeService; import com.ygj.yuemum.service.maternitymatron.JlServiceUserService; import com.ygj.yuemum.service.pay.CustomerPayVoucherService; import io.swagger.annotations.Api; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.io.ResourceLoader; import org.springframework.util.ResourceUtils; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import java.io.*; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; @Api(tags = "图片、简历相关接口") @RestController public class ImageUploadController { @Autowired private PhotoService photoService; @Autowired private JlServiceUserService jlServiceUserService; @Autowired private ResumeService resumeService; @Autowired private CustomerPayVoucherService customerPayVoucherService; private ResourceLoader resourceLoader; public static final String ROOT = Constant.ROOT; public static final String ResumeFile = Constant.RESUMEFILE; public static final String httpRoot = Constant.HTTPROOT; public static final String httphtmlRoot = Constant.HTTPHTMLROOT; public static final String modulePath = Constant.MODULEPATH; public static final String payvoucher = Constant.PAYVOUCHER; public static final String httppayvoucher = Constant.HTTPPAYVOUCHER; public static final String promotionvoucher = Constant.PROMOTIONVOUCHER; public static final String httppromotionvoucher = Constant.HTTPPROMOTIONVOUCHER; @RequestMapping(value = "/upload", method = RequestMethod.GET) public String upload() { return "/fileupload"; } @RequestMapping(value = "/uploadImg", method = RequestMethod.POST) @ResponseBody public int uploadImg(@RequestParam("file") MultipartFile file, @RequestParam(value = "id") Integer id, @RequestParam(value = "type") Integer type) { // 照片类型 1:display 2:certificate 3:comment 4:service 5:meal // 删除display if (type == 1) { List photos = photoService.queryDisplays(id); for (Photo photo : photos) { photoService.deletePhoto(photo); } } String folder = ""; int dot = file.getOriginalFilename().lastIndexOf('.'); String newName = file.getOriginalFilename().substring(dot); String path = ROOT + getpathtype(type) + "Img"; Photo photo = new Photo(); if (file.isEmpty()) { return 0; } Date date = new Date(); DateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss"); File dest = new File(path + "/" + getpathtype(type) + id + type + sdf.format(date) + newName); try { file.transferTo(dest); //保存文件 photo.setServant_code(id); photo.setPhoto_type(type); photo.setPhoto_name(getpathtype(type) + id + type + sdf.format(date) + newName); photo.setPhoto_path(httpRoot + getpathtype(type) + "Img/" + dest.getName()); photoService.addPhoto(photo); return 1; } catch (IllegalStateException e) { // TODO Auto-generated catch block e.printStackTrace(); return 0; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); return 0; } } @PostMapping("/deletePhoto") public int deletePhoto(@ModelAttribute Photo photo) throws ParseException { try { String path = ROOT + getpathtype(photo.getPhoto_type()) + "Img/" + photo.getPhoto_name(); File dest = new File(path); dest.delete(); return photoService.deletePhoto(photo); } catch (Exception ex) { ex.printStackTrace(); return 0; } } @PostMapping("/queryPhoto") public String queryPhoto(@ModelAttribute Photo photo) throws ParseException { List photos = photoService.queryPhoto(photo); String jso = JSONObject.toJSONString(photos); jso = jso.replaceAll("photo_path", "url"); jso = jso.replaceAll("servant_code", "name"); return jso; } @PostMapping("/createResume") public String createResume(@ModelAttribute Photo photo) { String urlName = ""; try { JlServiceUser jlServiceUser = jlServiceUserService.getResume(photo.getServant_code()); urlName = String.valueOf(jlServiceUser.getId()); File file = ResourceUtils.getFile(modulePath); BufferedReader br = new BufferedReader(new FileReader(file)); Resume resume = new Resume(); Resume resumeold = new Resume(); StringBuilder stringHtml = new StringBuilder(); String str = ""; String photoDisplay = ""; String certificate = ""; String certificatehref = ""; String service = ""; String servicehref = ""; String meal = ""; String mealhref = ""; String comment = ""; String littcomm = ""; if (photo.getComment() == null) { photo.setComment(" "); } List photos = photoService.queryPhoto(photo); if (photos.size() > 0) { int stepcertificate = 0; int stepservice = 0; int stepmeal = 0; String certificatestart = "
" + "\r\n" + "相关证件" + "\r\n" + "
" + "\r\n" + "
" + "\r\n" + "
" + "\r\n" + "
" + "\r\n" + "
    " + "\r\n" + "
    " + "\r\n"; String certificateend = "
    " + "\r\n" + "
" + "\r\n" + "
" + "\r\n" + "
" + "\r\n" + "$cfstyle" + "\r\n" + "
" + "\r\n" + "
" + "\r\n" + "
" + "\r\n" + "
" + "\r\n" + "
" + "\r\n"; String mealstart = "
" + "\r\n" + "月子餐照片" + "\r\n" + "
" + "\r\n" + "
" + "\r\n" + "
" + "\r\n" + "
" + "\r\n" + "
    " + "\r\n" + "
    " + "\r\n"; String mealend = "
    " + "\r\n" + "
" + "\r\n" + "
" + "\r\n" + "
" + "\r\n" + "$smstyle" + "\r\n" + "
" + "\r\n" + "
" + "\r\n" + "
" + "\r\n" + "
" + "\r\n" + "
" + "\r\n"; String commentstart = "
" + "\r\n" + "最新评论" + "\r\n" + "
" + "\r\n" + "
" + "\r\n" + "
" + "\r\n" + "
" + "\r\n" + "
" + "\r\n"; String commentend = "
" + "
" + "
" + "\r\n" + "
" + "\r\n" + "
" + "\r\n" + "
" + "\r\n" + "
"; String servicestart = "
" + "\r\n" + "服务照片" + "\r\n" + "
" + "\r\n" + "
" + "\r\n" + "
" + "\r\n" + "
" + "\r\n" + "
    " + "\r\n" + "
    " + "\r\n"; String serviceend = "
    " + "\r\n" + "
" + "\r\n" + "
" + "\r\n" + "
" + "\r\n" + "$scstyle" + "\r\n" + "
" + "\r\n" + "
" + "\r\n" + "
" + "\r\n" + "
" + "\r\n" + "
" + "\r\n"; for (Photo onePhoto : photos) { if (onePhoto.getPhoto_type() == 1) { if (onePhoto.getPhoto_path() != null) { photoDisplay = "" + "\r\n"; } } if (onePhoto.getPhoto_type() == 2) { if (onePhoto.getPhoto_path() != null) { certificate = certificate + "
  • " + "\r\n" + "" + "\r\n" + "
  • " + "\r\n"; if (stepcertificate == 0) { certificatehref = certificatehref + "" + "\r\n"; } else { certificatehref = certificatehref + "" + "\r\n"; } stepcertificate++; } } if (onePhoto.getPhoto_type() == 4) { if (onePhoto.getPhoto_path() != null) { service = service + "
  • " + "\r\n" + "" + "\r\n" + "
  • " + "\r\n"; if (stepservice == 0) { servicehref = servicehref + " " + "\r\n"; } else { servicehref = servicehref + " " + "\r\n"; } stepservice++; } } if (onePhoto.getPhoto_type() == 5) { if (onePhoto.getPhoto_path() != null) { meal = meal + "
  • " + "\r\n" + "" + "\r\n" + "
  • " + "\r\n"; if (stepmeal == 0) { mealhref = mealhref + " " + "\r\n"; } else { mealhref = mealhref + " " + "\r\n"; } stepmeal++; } } if (onePhoto.getPhoto_type() == 3) { comment = comment + "" + "\r\n"; littcomm = littcomm + ""; } } if (comment != "") { comment = commentstart + comment + commentend; } if (meal != "") { meal = mealstart + meal + mealend; } if (service != "") { service = servicestart + service + serviceend; } if (certificate != "") { certificate = certificatestart + certificate + certificateend; } } while ((str = br.readLine()) != null) { str = str.replace("$name", jlServiceUser.getTruename()); str = str.replace("$np", jlServiceUser.getNp()); str = str.replace("$age", jlServiceUser.getAge() + "岁"); str = str.replace("$ethnicgroup", jlServiceUser.getEthnicgroup()); str = str.replace("$constellation", jlServiceUser.getConstellation()); str = str.replace("$seniority", jlServiceUser.getSeniority()); str = str.replace("$babynumber", jlServiceUser.getBabynumber()); str = str.replace("$education", jlServiceUser.getEducation()); str = str.replace("$weight", jlServiceUser.getWeight()); str = str.replace("$marriage", jlServiceUser.getMarriage()); str = str.replace("$highlight", jlServiceUser.getHighlight()); str = str.replace("$resumecomment", photo.getComment()); str = str.replace("$zodiac", "属" + jlServiceUser.getZodiac()); str = str.replace("$height", jlServiceUser.getHeight()); str = str.replace("$starts", String.valueOf(jlServiceUser.getGender())); str = str.replace("$display", photoDisplay); str = str.replace("$credentials", certificate); str = str.replace("$cfstyle", certificatehref); str = str.replace("$service", service); str = str.replace("$scstyle", servicehref); str = str.replace("$meal", meal); str = str.replace("$smstyle", mealhref); str = str.replace("$comment", comment); str = str.replace("$littcomm", littcomm); str = str.replace("$ystitle", "悦所-月嫂简历:" + jlServiceUser.getTruename()); str = str.replace("$yuesaohref", httphtmlRoot + urlName + ".html"); stringHtml.append(str + "\r\n"); } resumeold = resumeService.getResume(photo.getServant_code()); if (resumeold != null) { resumeService.deleteResume(resumeold.getId()); File fileDel = new File(resumeold.getRqcode_path()); if (file.exists()) { fileDel.delete(); } } resume.setServant_code(photo.getServant_code()); resume.setResume_comment(photo.getComment()); resume.setResume_path(httphtmlRoot + urlName + ".html"); resume.setRqcode_path(ResumeFile + urlName + ".html"); resumeService.addResume(resume); File newResume = new File(ResumeFile + urlName + ".html"); FileWriter fileWriter = new FileWriter(newResume); fileWriter.write(stringHtml.toString()); fileWriter.close(); } catch (Exception ex) { ex.printStackTrace(); return "error"; } return httphtmlRoot + urlName + ".html"; } public String getpathtype(int type) { switch (type) { case 1: return "display"; case 2: return "certificate"; case 3: return "comment"; case 4: return "service"; case 5: return "meal"; } return "error"; } @GetMapping("/reBulidResume") public int reBulidResume(@RequestParam("servant_code") String servant_code) { return photoService.reBuildResume(servant_code); } @RequestMapping(value = "/uploadPayImg", method = RequestMethod.POST) @ResponseBody public int uploadPayImg(@RequestParam("file") MultipartFile file, @RequestParam(value = "eh_code") String eh_code, @RequestParam(value = "cv_orderstaus") Integer cv_orderstaus) { int dot = file.getOriginalFilename().lastIndexOf('.'); String newName = file.getOriginalFilename().substring(dot); CustomerPayVoucher customerPayVoucher = new CustomerPayVoucher(); if (file.isEmpty()) { return 0; } Date date = new Date(); DateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss"); File dest = new File(payvoucher + "/" + eh_code + sdf.format(date) + newName); // 删除 try { file.transferTo(dest); //保存文件 customerPayVoucher.setEh_code(eh_code); customerPayVoucher.setCv_filename(eh_code + sdf.format(date) + newName); customerPayVoucher.setCv_orderstaus(cv_orderstaus); customerPayVoucher.setCv_filepath(httppayvoucher + "/" + eh_code + sdf.format(date) + newName); customerPayVoucherService.insertCustomerPayVoucher(customerPayVoucher); return 1; } catch (IllegalStateException e) { // TODO Auto-generated catch block e.printStackTrace(); return 0; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); return 0; } } @GetMapping("/deletePayPhoto") public int deletePayPhoto(@RequestParam("cv_filename") String cv_filename) throws ParseException { try { String path = payvoucher + "/" + cv_filename; File dest = new File(path); dest.delete(); return customerPayVoucherService.deleteCustomerPayVoucher(cv_filename); } catch (Exception ex) { ex.printStackTrace(); return 0; } } @GetMapping("/deleteCustomerPayVoucherAll") public int deleteCustomerPayVoucherAll(@RequestParam("eh_code") String eh_code) throws ParseException { try { List customerPays = customerPayVoucherService.selectCustomerPayVoucher(eh_code); for (CustomerPayVoucher cpv : customerPays) { String path = payvoucher + "/" + cpv.getCv_filename(); File dest = new File(path); dest.delete(); } return customerPayVoucherService.deleteCustomerPayVoucherAll(eh_code); } catch (Exception ex) { ex.printStackTrace(); return 0; } } @GetMapping("/queryPayPhoto") public String queryPayPhoto(@RequestParam("eh_code") String eh_code) throws ParseException { List customerPays = customerPayVoucherService.selectCustomerPayVoucher(eh_code); String jso = JSONObject.toJSONString(customerPays); jso = jso.replaceAll("cv_filepath", "url"); jso = jso.replaceAll("cv_filename", "name"); return jso; } @PostMapping("/queryOrderPayPhoto") public String queryOrderPayPhoto(@ModelAttribute CustomerPayVoucher customerPayVoucher) throws ParseException { List customerPays = customerPayVoucherService.queryOrderPayPhoto(customerPayVoucher); String jso = JSONObject.toJSONString(customerPays); jso = jso.replaceAll("cv_filepath", "url"); jso = jso.replaceAll("cv_filename", "name"); return jso; } @PostMapping("/deleteOrderPayPhoto") public int deleteOrderPayPhoto(@ModelAttribute CustomerPayVoucher customerPayVoucher) throws ParseException { try { List customerPays = customerPayVoucherService.queryOrderPayPhoto(customerPayVoucher); for (CustomerPayVoucher cpv : customerPays) { String path = payvoucher + "/" + cpv.getCv_filename(); File dest = new File(path); dest.delete(); } return customerPayVoucherService.deleteOrderPayPhoto(customerPayVoucher); } catch (Exception ex) { ex.printStackTrace(); return 0; } } @RequestMapping(value = "/uploadPromotionImg", method = RequestMethod.POST) @ResponseBody public String uploadPromotionImg(@RequestParam(value = "file", required = false) MultipartFile file) { int dot = file.getOriginalFilename().lastIndexOf('.'); String newName = file.getOriginalFilename().substring(dot); if (file.isEmpty()) { return ""; } Date date = new Date(); DateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss"); String fileUrl = promotionvoucher + "/" + sdf.format(date) + newName; String httpfileUrl = httppromotionvoucher + "/" + sdf.format(date) + newName; File dest = new File(fileUrl); // 删除 try { file.transferTo(dest); //保存文件 return httpfileUrl; } catch (Exception ex) { ex.printStackTrace(); return ""; } } @GetMapping("/deletePromotionImg") public int deletePromotionImg(@RequestParam("cv_filename") String cv_filename) throws ParseException { try { String path = promotionvoucher + "/" + cv_filename; File dest = new File(path); dest.delete(); return 1; } catch (Exception ex) { ex.printStackTrace(); return 0; } } }