jinpengyong
2022-02-23 15bba267b1992286f04be9db924af2786f3aac97
督办单修改
3 files modified
35 ■■■■ changed files
screen-api/src/main/java/com/moral/api/controller/SupervisionController.java 5 ●●●● patch | view | raw | blame | history
screen-api/src/main/java/com/moral/api/service/SupervisionService.java 2 ●●● patch | view | raw | blame | history
screen-api/src/main/java/com/moral/api/service/impl/SupervisionServiceImpl.java 28 ●●●● patch | view | raw | blame | history
screen-api/src/main/java/com/moral/api/controller/SupervisionController.java
@@ -36,7 +36,10 @@
    private SupervisionService supervisionService;
    @PostMapping("add")
    public ResultMessage add(MultipartFile[] files, Supervision supervision) {
    public ResultMessage add(HttpServletRequest request) {
        Map<String, Object> params = WebUtils.getParametersStartingWith(request, null);
        List<MultipartFile> files = ((MultipartHttpServletRequest) request).getFiles("files");
        Supervision supervision = JSONObject.parseObject(JSONObject.toJSONString(params), Supervision.class);
        Map<String, Object> response = supervisionService.add(files, supervision);
        if (!response.isEmpty()) {
            return ResultMessage.fail(Integer.parseInt(response.get("code").toString()), response.get("msg").toString());
screen-api/src/main/java/com/moral/api/service/SupervisionService.java
@@ -19,7 +19,7 @@
public interface SupervisionService extends IService<Supervision> {
    //新增督办单
    Map<String, Object> add(MultipartFile[] files, Supervision supervision);
    Map<String, Object> add(List<MultipartFile> files, Supervision supervision);
    //查询督办单
    Map<String, Object> selectSupervisions(Map<String, Object> params);
screen-api/src/main/java/com/moral/api/service/impl/SupervisionServiceImpl.java
@@ -51,7 +51,7 @@
    @Override
    @Transactional
    public Map<String, Object> add(MultipartFile[] files, Supervision supervision) {
    public Map<String, Object> add(List<MultipartFile> files, Supervision supervision) {
        //获取jar包所在目录
        ApplicationHome applicationHome = new ApplicationHome(getClass());
        //在jar包所在目录下生成一个upload文件夹用来存储上传的图片
@@ -69,6 +69,8 @@
        }
        List<String> images = new ArrayList<>();
        if (files.size() > 0) {
        for (MultipartFile file : files) {
            //判断上传文件格式
            String fileType = file.getContentType();
@@ -87,6 +89,7 @@
                result.put("code", ResponseCodeEnum.IMG_UPLOAD_FAIl.getCode());
                result.put("msg", ResponseCodeEnum.IMG_UPLOAD_FAIl.getMsg());
                return result;
                }
            }
        }
@@ -166,17 +169,19 @@
        List<String> images = new ArrayList<>();
        System.out.println(files.size());
        //如果上传了图片,就覆盖之前图片,删除服务器中图片
        if (files.size() > 0) {
        for (MultipartFile file : files) {
            //判断上传文件格式
            String fileType = file.getContentType();
            if ("image/jpg".equals(fileType) || "image/png".equals(fileType) || "image/jpeg".equals(fileType)) {
                //获取文件名
                String fileName = file.getOriginalFilename();
                    //获取文件类型
                    String contentType = file.getContentType();
                //获取文件后缀名
                String suffixName = fileName.substring(fileName.lastIndexOf("."));
                    String suffixName = contentType.substring(contentType.lastIndexOf("/")).replace("/", ".");
                //重新生成文件名
                fileName = UUID.randomUUID() + suffixName;
                    String fileName = UUID.randomUUID() + suffixName;
                //图片上传
                if (FileUtils.upload(file, path, fileName)) {
                    images.add(fileName);
@@ -187,7 +192,18 @@
                return result;
            }
        }
        }
        if (!ObjectUtils.isEmpty(images)) {
            //删除服务器中的原来督办单中图片
            for (String image : images) {
                String realPath = path + File.separator + image;
                File file = new File(realPath);
                if (file.exists() && file.isFile()) {
                    file.delete();
                }
            }
            String image = images.stream()
                    .map(String::valueOf)
                    .collect(Collectors.joining(","));