From 87f99146cc28b071ebe351faf6dc28cacd7ef72a Mon Sep 17 00:00:00 2001 From: yby654 <67887676+yby654@users.noreply.github.com> Date: Sun, 15 Oct 2023 10:52:23 +0900 Subject: [PATCH] =?UTF-8?q?Chore:=20=ED=81=90=EC=8B=9C=ED=8A=B8=20?= =?UTF-8?q?=ED=8C=8C=EC=9D=BC=20=EC=97=85=EB=A1=9C=EB=93=9C=20filepath=20?= =?UTF-8?q?=EC=97=85=EB=8D=B0=EC=9D=B4=ED=8A=B8=20(#34)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Chore: 큐시트 파일 업로드 수정 --------- Co-authored-by: yby654 --- .../nw/controller/QsheetController.java | 24 +++- .../lab/cherry/nw/model/QsheetEntity.java | 2 + .../nw/service/Impl/QsheetServiceImpl.java | 103 +++++++++++++++--- .../lab/cherry/nw/service/QsheetService.java | 2 +- .../main/java/lab/cherry/nw/util/Common.java | 6 + 5 files changed, 114 insertions(+), 23 deletions(-) diff --git a/nw/src/main/java/lab/cherry/nw/controller/QsheetController.java b/nw/src/main/java/lab/cherry/nw/controller/QsheetController.java index 0f5f0fa..d0462a6 100644 --- a/nw/src/main/java/lab/cherry/nw/controller/QsheetController.java +++ b/nw/src/main/java/lab/cherry/nw/controller/QsheetController.java @@ -9,6 +9,7 @@ import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; +import org.springframework.util.CollectionUtils; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PatchMapping; @@ -106,8 +107,16 @@ public ResponseEntity findAllQsheets( @ApiResponse(responseCode = "400", description = "입력 값이 잘못 되었습니다.", content = @Content(schema = @Schema(implementation = ErrorResponse.class))), }) @Operation(summary = "Qsheet 생성", description = "Qsheet를 추가합니다.") - public ResponseEntity createQsheet(@Valid @RequestPart QsheetEntity.QsheetCreateDto qsheetCreateDto, @RequestPart List files) { + public ResponseEntity createQsheet(@RequestPart QsheetEntity.QsheetCreateDto qsheetCreateDto, @RequestPart(required = false) List files) { log.info("[QsheetController] createQsheet...!"); + log.error ("files : {}", files); + for(MultipartFile file:files){ + if(file.isEmpty()){ + files = null; + break; + } + } + qsheetService.createQsheet(qsheetCreateDto, files); final ResultResponse response = ResultResponse.of(SuccessCode.OK); @@ -129,11 +138,16 @@ public ResponseEntity createQsheet(@Valid @RequestPart QsheetEntity.QsheetCre @Operation(summary = "큐시트 업데이트", description = "특정 큐시트를 업데이트합니다.") public ResponseEntity updateById( @PathVariable("id") String id, - @RequestBody QsheetEntity.QsheetUpdateDto qsheetUpdateDto) { - + @RequestPart QsheetEntity.QsheetUpdateDto qsheetUpdateDto, @RequestPart(required = false) List files) { + log.info("[QsheetController] updateQsheet...!"); - - qsheetService.updateById(id, qsheetUpdateDto); + for(MultipartFile file:files){ + if(file.isEmpty()){ + files = null; + break; + } + } + qsheetService.updateById(id, qsheetUpdateDto, files); // final ResultResponse response = ResultResponse.of(SuccessCode.OK); return new ResponseEntity<>(qsheetService.findById(id), new HttpHeaders(), HttpStatus.OK); diff --git a/nw/src/main/java/lab/cherry/nw/model/QsheetEntity.java b/nw/src/main/java/lab/cherry/nw/model/QsheetEntity.java index 4573812..d229797 100644 --- a/nw/src/main/java/lab/cherry/nw/model/QsheetEntity.java +++ b/nw/src/main/java/lab/cherry/nw/model/QsheetEntity.java @@ -162,6 +162,7 @@ public void sortDataByOrderIndex() { data.sort(Comparator.comparingInt(ItemData::getOrderIndex)); } } + // public void sortDataByOrderIndex() { // if (data != null) { // data = data.entrySet() @@ -175,6 +176,7 @@ public void sortDataByOrderIndex() { // )); // } // } + @Getter @Builder @NoArgsConstructor @AllArgsConstructor diff --git a/nw/src/main/java/lab/cherry/nw/service/Impl/QsheetServiceImpl.java b/nw/src/main/java/lab/cherry/nw/service/Impl/QsheetServiceImpl.java index 5f82952..e1fb92a 100644 --- a/nw/src/main/java/lab/cherry/nw/service/Impl/QsheetServiceImpl.java +++ b/nw/src/main/java/lab/cherry/nw/service/Impl/QsheetServiceImpl.java @@ -6,6 +6,7 @@ import lab.cherry.nw.model.OrgEntity; import lab.cherry.nw.model.QsheetEntity; import lab.cherry.nw.model.UserEntity; +import lab.cherry.nw.model.QsheetEntity.ItemData; import lab.cherry.nw.repository.QsheetRepository; import lab.cherry.nw.service.FileService; import lab.cherry.nw.service.OrgService; @@ -17,8 +18,6 @@ import org.bson.types.ObjectId; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.io.ByteArrayOutputStream; @@ -96,18 +95,39 @@ public void createQsheet(QsheetEntity.QsheetCreateDto qsheetCreateDto, List newItemData = new ArrayList<>(); + if (files != null){ + Map info = new HashMap<>(); + info.put("type", "사용자"); + info.put("user", userEntity.getId()); + info.put("qsheetSeq", objectid.toString()); - log.error("name {}", qsheetCreateDto.getName()); - log.error("memo {}", qsheetCreateDto.getMemo()); - - Map info = new HashMap<>(); - info.put("type", "사용자"); - info.put("user", userEntity.getId()); - info.put("qsheetSeq", objectid.toString()); + List fileUrls = fileService.uploadFiles(info, files); + log.error("fileUrls {}", fileUrls); + //////////// + - List fileUrls = fileService.uploadFiles(info, files); - log.error("fileUrls {}", fileUrls); - //////////// + for (ItemData data : qsheetCreateDto.getData()) { + for (String filePath : fileUrls) { + if (filePath.contains(data.getFilePath())) { + ItemData tempData = ItemData.builder() + .orderIndex(data.getOrderIndex()) + .process(data.getProcess()) + .content(data.getContent()) + .actor(data.getActor()) + .note(data.getNote()) + .filePath(filePath) + .build(); + data = tempData; + break; + } + } + newItemData.add(data); + } + }else{ + newItemData = qsheetCreateDto.getData(); + } + QsheetEntity qsheetEntity = QsheetEntity.builder() @@ -115,7 +135,8 @@ public void createQsheet(QsheetEntity.QsheetCreateDto qsheetCreateDto, List files) { Instant instant = Instant.now(); QsheetEntity qsheetEntity = findById(id); + List newItemData =qsheetEntity.getData(); - if (qsheetEntity.getData() != null ) { + if (qsheetEntity != null ) { // qsheetEntity.updateFromDto(qsheetUpdateDto); // qsheetRepository.save(qsheetEntity); OrgEntity orgEntity = qsheetEntity.getOrgid(); @@ -156,16 +178,63 @@ public void updateById(String id, QsheetEntity.QsheetUpdateDto qsheetUpdateDto) }else{ log.error("[QsheetServiceImpl - udpateQsheet] org_approver와 isOrg_confirm 입력이 잘못되었습니다."); throw new CustomException(ErrorCode.INVALID_INPUT_VALUE); - } + } } + if(files!=null){ + newItemData= new ArrayList<>(); + Map info = new HashMap<>(); + info.put("type", "사용자"); + info.put("user", qsheetEntity.getUserid().getId()); + info.put("qsheetSeq", qsheetEntity.getId().toString()); + + List fileUrls = fileService.uploadFiles(info, files); + log.error("fileUrls {}", fileUrls); + //////////// + + for (ItemData data : qsheetUpdateDto.getData()) { + for (String filePath : fileUrls) { + if (filePath.contains(data.getFilePath())) { + ItemData tempData = ItemData.builder() + .orderIndex(data.getOrderIndex()) + .process(data.getProcess()) + .content(data.getContent()) + .actor(data.getActor()) + .note(data.getNote()) + .filePath(filePath) + .build(); + data = tempData; + break; + } + } + newItemData.add(data); + } + + + }else if(qsheetUpdateDto.getData()!=null && files==null){ + newItemData= new ArrayList<>(); + for(ItemData data : qsheetUpdateDto.getData()){ + ItemData tempData = ItemData.builder() + .orderIndex(data.getOrderIndex()) + .process(data.getProcess()) + .content(data.getContent()) + .actor(data.getActor()) + .note(data.getNote()) + .filePath(data.getFilePath()) + .build(); + newItemData.add(tempData); + } + } + + + qsheetEntity = QsheetEntity.builder() .id(qsheetEntity.getId()) .name(qsheetEntity.getName()) .orgid(orgEntity) .userid(qsheetEntity.getUserid()) .created_at(qsheetEntity.getCreated_at()) - .data(qsheetUpdateDto.getData()!=null?qsheetUpdateDto.getData():qsheetEntity.getData()) + .data(newItemData) .org_approver(orgUserEntity) .org_confirm(qsheetUpdateDto.isOrg_confirm()==!(qsheetEntity.isOrg_confirm())?qsheetUpdateDto.isOrg_confirm():qsheetEntity.isOrg_confirm()) .client_confirm(qsheetUpdateDto.isClient_confirm()==!(qsheetEntity.isClient_confirm())?qsheetUpdateDto.isClient_confirm():qsheetEntity.isClient_confirm()) diff --git a/nw/src/main/java/lab/cherry/nw/service/QsheetService.java b/nw/src/main/java/lab/cherry/nw/service/QsheetService.java index dbee2fa..5fe0873 100644 --- a/nw/src/main/java/lab/cherry/nw/service/QsheetService.java +++ b/nw/src/main/java/lab/cherry/nw/service/QsheetService.java @@ -22,7 +22,7 @@ public interface QsheetService { // QsheetEntity findByUserId(String userid); // QsheetEntity findByOrgId(String orgid); void createQsheet(QsheetEntity.QsheetCreateDto qsheetCreateDto, List files); - void updateById(String id, QsheetEntity.QsheetUpdateDto updateDto); + void updateById(String id, QsheetEntity.QsheetUpdateDto updateDto, List files); void deleteById(String id); Page findPageByUserId(String userid, Pageable pageable); Page findPageByOrgId(String orgid, Pageable pageable); diff --git a/nw/src/main/java/lab/cherry/nw/util/Common.java b/nw/src/main/java/lab/cherry/nw/util/Common.java index abe34b7..62146cb 100644 --- a/nw/src/main/java/lab/cherry/nw/util/Common.java +++ b/nw/src/main/java/lab/cherry/nw/util/Common.java @@ -90,4 +90,10 @@ public static String getFileExtension(MultipartFile file) { return ""; // 확장자가 없을 경우 빈 문자열 반환 } } + + public String getFileNameFromPath(String path) { + String[] parts = path.split("/"); + return parts[parts.length - 1]; + } + } \ No newline at end of file