123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- package com.ygj.yuemum.controller.admin;
- import com.alibaba.fastjson.JSONObject;
- 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.service.admin.PhotoService;
- import com.ygj.yuemum.service.admin.ResumeService;
- import com.ygj.yuemum.service.maternitymatron.JlServiceUserService;
- 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;
- @RestController
- public class ImageUploadController {
- @Autowired
- private PhotoService photoService;
- @Autowired
- private JlServiceUserService jlServiceUserService;
- @Autowired
- private ResumeService resumeService;
- private ResourceLoader resourceLoader;
- public static final String ROOT = "D:/apache-tomcat-9.0.6/webapps/resume/img/";
- public static final String ResumeFile = "D:/apache-tomcat-9.0.6/webapps/resume/";
- public static final String httpRoot = "http://localhost:8080/resume/img/";
- public static final String httphtmlRoot = "http://localhost:8080/resume/";
- @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
- 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<Photo> 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) {
- try {
- JlServiceUser jlServiceUser = jlServiceUserService.getResume(photo.getServant_code());
- File file = ResourceUtils.getFile("classpath:resume/resume.html");
- 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 comment ="";
- if(photo.getComment() == null){
- photo.setComment(" ");
- }
- List<Photo> photos = photoService.queryPhoto(photo);
- if(photos.size() > 0 ) {
- int stepcertificate = 0;
- int stepservice = 0;
- for(Photo onePhoto:photos){
- if(onePhoto.getPhoto_type() == 1){
- if(onePhoto.getPhoto_path() !=null){
- photoDisplay = "<image class=\"resume-header-left-image\" src='"+onePhoto.getPhoto_path()+"'></image>"+"\r\n";
- }
- }
- if(onePhoto.getPhoto_type() == 2){
- if(onePhoto.getPhoto_path() !=null){
- certificate = certificate +"<li class=\"poster-item zturn-item\">"+"\r\n"
- +"<image class=\"zturn-item-image-cer\" src='"+onePhoto.getPhoto_path()+"'></image>"+"\r\n"
- +"</li>"+"\r\n";
- if(stepcertificate == 0) {
- certificatehref = certificatehref + "<a href=\"javascript:void(0);\" value='"+stepcertificate+"' class=\"indicator-list-item selected\"></a>" + "\r\n";
- } else {
- certificatehref = certificatehref + "<a href=\"javascript:void(0);\" value='"+stepcertificate+"' class=\"indicator-list-item \"></a>" + "\r\n";
- }
- stepcertificate ++ ;
- }
- }
- if(onePhoto.getPhoto_type() == 4){
- if(onePhoto.getPhoto_path() !=null) {
- service = service+"<li class=\"poster-item zturn-item\">"+"\r\n"
- + "<image class=\"zturn-item-image\" src='"+onePhoto.getPhoto_path()+"'></image>"+"\r\n"
- +"</li>"+"\r\n";
- if(stepservice == 0) {
- servicehref = servicehref + " <a href=\"javascript:void(0);\" value='"+stepservice+"' class=\"indicator-list-item-service selected\"></a>" + "\r\n";
- }else {
- servicehref = servicehref + " <a href=\"javascript:void(0);\" value='"+stepservice+"' class=\"indicator-list-item-service\"></a>" + "\r\n";
- }
- stepservice++;
- }
- }
- if(onePhoto.getPhoto_type() == 3){
- comment = comment +"<a href=\"\"><img src='"+onePhoto.getPhoto_path()+"'/></a>"+"\r\n";
- }
- }
- }
- if(photoDisplay == ""){
- photoDisplay = "<image class=\"resume-header-left-image\" src=\"img/header.jpg\"></image>"+"\r\n";
- }
- if(certificate == ""){
- certificate = "<li class=\"poster-item zturn-item\">"+"\r\n"
- +"<image class=\"zturn-item-image-cer\" src=\"img/certificate01.jpg\"></image>"+"\r\n"
- +"</li>"+"\r\n"
- +"<li class=\"poster-item zturn-item\">"+"\r\n"
- +"<image class=\"zturn-item-image-cer\" src=\"img/certificate02.jpg\"></image>"+"\r\n"
- +"</li>"+"\r\n"
- +"<li class=\"poster-item zturn-item\">"+"\r\n"
- +"<image class=\"zturn-item-image-cer\" src=\"img/certificate03.jpg\"></image>"+"\r\n"
- +"</li>"+"\r\n";
- certificatehref = "<a href=\"javascript:void(0);\" value=\"0\" class=\"indicator-list-item selected\"></a>"+"\r\n"
- +"<a href=\"javascript:void(0);\" value=\"1\" class=\"indicator-list-item\"></a>"+"\r\n"
- +"<a href=\"javascript:void(0);\" value=\"2\" class=\"indicator-list-item\"></a>"+"\r\n";
- }
- if(service == ""){
- service = "<li class=\"poster-item zturn-item\">"+"\r\n"
- +"<image class=\"zturn-item-image\" src=\"img/service01.png\"></image>"+"\r\n"
- +"</li>"+"\r\n"
- +"<li class=\"poster-item zturn-item\">"+"\r\n"
- +"<image class=\"zturn-item-image\" src=\"img/service01.png\"></image>"+"\r\n"
- +"</li>"+"\r\n"
- +"<li class=\"poster-item zturn-item\">"+"\r\n"
- +"<image class=\"zturn-item-image\" src=\"img/service01.png\"></image>"+"\r\n"
- +"</li>"+"\r\n";
- servicehref = " <a href=\"javascript:void(0);\" value=\"0\" class=\"indicator-list-item-service selected\"></a>"+"\r\n"
- +"<a href=\"javascript:void(0);\" value=\"1\" class=\"indicator-list-item-service\"></a>"+"\r\n"
- +"<a href=\"javascript:void(0);\" value=\"2\" class=\"indicator-list-item-service\"></a>"+"\r\n";
- }
- if(comment == ""){
- comment = "<a href=\"\"><img src=\"img/comments01.jpg\"/></a>"+"\r\n"
- +"<a href=\"\"><img src=\"img/comments02.jpg\"/></a>"+"\r\n"
- +"<a href=\"\"><img src=\"img/comments03.jpg\"/></a>"+"\r\n";
- }
- 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",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("$comment",comment);
- stringHtml.append(str+"\r\n");
- }
- resumeold = resumeService.getResume(photo.getServant_code());
- if(resumeold != null) {
- resumeService.deleteResume(resumeold.getId());
- }
- resume.setServant_code(photo.getServant_code());
- resume.setResume_comment(photo.getComment());
- resume.setResume_path(httphtmlRoot+photo.getServant_code() + ".html");
- resumeService.addResume(resume);
- File newResume = new File(ResumeFile + photo.getServant_code() + ".html");
- FileWriter fileWriter = new FileWriter(newResume);
- fileWriter.write(stringHtml.toString());
- fileWriter.close();
- } catch (Exception ex){
- ex.printStackTrace();
- return "error";
- }
- return httphtmlRoot+photo.getServant_code() + ".html";
- }
- public String getpathtype(int type){
- switch (type) {
- case 1:
- return "display";
- case 2:
- return "certificate";
- case 3:
- return "comment";
- case 4:
- return "service";
- }
- return "error";
- }
- }
|