package com.ygj.yuemum.controller.wxmini; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.ygj.yuemum.component.Constant; import com.ygj.yuemum.domain.wxmini.WXDianPing; import com.ygj.yuemum.domain.wxmini.WXDianPingImages; import com.ygj.yuemum.service.wxmini.WXDianPingImagesService; import com.ygj.yuemum.service.wxmini.WXDianPingService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import java.io.File; import java.io.IOException; import java.util.List; import java.util.Map; import java.util.UUID; @RestController public class WXDianpingController { @Autowired private WXDianPingService wxDianPingService; @Autowired private WXDianPingImagesService wxDianPingImagesService; public static final String DIANPINGIMGURL = Constant.DIANPINGIMGURL; public static final String HTTPDIANPINGIMGURL = Constant.HTTPDIANPINGIMGURL; @GetMapping("/getWXDianPings") public List getWXDianPings() { List wxBanners = wxDianPingService.getWXDianPings(); return wxBanners; } @GetMapping("/getWXDianPing") public List getWXDianPing() { List wxBanners = wxDianPingService.getWXDianPing(); return wxBanners; } @RequestMapping(value = "/insertWXDianPing", method = RequestMethod.POST, produces = "application/json;charset=UTF-8") @ResponseBody public int insertWXDianPing(@RequestParam Map jsonParam) { WXDianPing wxDianPing = JSON.parseObject(JSON.toJSONString(jsonParam), WXDianPing.class); JSONArray imageLists = JSONArray.parseArray(wxDianPing.getImage_list()); int dianPingID = wxDianPingService.getDianPingID(); for (int i = 0; i < imageLists.size(); i++) { WXDianPingImages wxDianPingImages = new WXDianPingImages(); wxDianPingImages.setWd_id(dianPingID); wxDianPingImages.setWdi_image_path(imageLists.getString(i)); wxDianPingImagesService.insertWXDianPingImages(wxDianPingImages); } wxDianPingService.insertWXDianPing(wxDianPing); return 1; } @RequestMapping(value = "/updateWXDianPing", method = RequestMethod.POST, produces = "application/json;charset=UTF-8") @ResponseBody public int updateWXDianPing(@RequestParam Map jsonParam) { WXDianPing wxDianPing = JSON.parseObject(JSON.toJSONString(jsonParam), WXDianPing.class); JSONArray imageLists = JSONArray.parseArray(wxDianPing.getImage_list()); wxDianPingImagesService.deleteWXDianPingImage(wxDianPing.getId()); for (int i = 0; i < imageLists.size(); i++) { WXDianPingImages wxDianPingImages = new WXDianPingImages(); wxDianPingImages.setWd_id(wxDianPing.getId()); wxDianPingImages.setWdi_image_path(imageLists.getString(i)); wxDianPingImagesService.insertWXDianPingImages(wxDianPingImages); } wxDianPingService.updateWXDianPing(wxDianPing); return 1; } @PostMapping("/queryWXDianPings") public String queryWXDianPings(@ModelAttribute WXDianPing wxDianPing) { Map orderTemps = wxDianPingService.queryWXDianPings(wxDianPing); String jso = JSONObject.toJSONString(orderTemps); return jso; } @PostMapping("/deleteWXDianPing") public int deleteWXDianPing(@RequestParam("id") Integer id) { try { List wxDianPingImages = wxDianPingImagesService.getWXDianPingImages(id); for (WXDianPingImages img :wxDianPingImages) { String[] pathname = img.getWdi_image_path().split("/"); String name = pathname[pathname.length - 1]; String delPath = DIANPINGIMGURL + name; File delDest = new File(delPath); delDest.delete(); } wxDianPingImagesService.deleteWXDianPingImage(id); wxDianPingService.deleteWXDianPing(id); }catch (Exception ex){ ex.printStackTrace(); return 0; } return 1; } @RequestMapping(value = "/uploadDianPingImg", method = RequestMethod.POST) @ResponseBody public String uploadMiniImg(@RequestParam("file") MultipartFile file) { if (file.isEmpty()) { return null; } int dot = file.getOriginalFilename().lastIndexOf('.'); String originalFilename = file.getOriginalFilename().substring(dot); String name = UUID.randomUUID().toString().replace("-", ""); String path = DIANPINGIMGURL + name + originalFilename; try { File dest = new File(path); file.transferTo(dest); //保存文件 覆盖之前 } catch (IllegalStateException e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } catch (IOException e) { e.printStackTrace(); return null; } return HTTPDIANPINGIMGURL + name + originalFilename; } @PostMapping("/deleteWXDianPingImage") public int deleteWXDianPingImage(@RequestParam("filepath") String filepath) { try { String[] pathname = filepath.split("/"); String name = pathname[pathname.length - 1]; String delPath = DIANPINGIMGURL + name; File delDest = new File(delPath); delDest.delete(); } catch (Exception ex) { ex.printStackTrace(); return 0; } return 1; } @PostMapping("/getWXDianPingsByID") public WXDianPing getWXDianPingsByID(@RequestParam("id") Integer id) { return wxDianPingService.getWXDianPingsByID(id); } }