FAISP/src/main/java/com/dbnt/faisp/faRpt/service/FaRptService.java

136 lines
5.2 KiB
Java
Raw Normal View History

2022-10-20 09:27:13 +00:00
package com.dbnt.faisp.faRpt.service;
import com.dbnt.faisp.config.BaseService;
import com.dbnt.faisp.config.FileInfo;
import com.dbnt.faisp.faRpt.mapper.FaRptMapper;
import com.dbnt.faisp.faRpt.model.FaRptBoard;
2022-10-24 09:18:53 +00:00
import com.dbnt.faisp.faRpt.model.FaRptFile;
2022-10-20 09:27:13 +00:00
import com.dbnt.faisp.faRpt.model.FaRptReadUser;
2022-10-25 08:49:27 +00:00
import com.dbnt.faisp.faRpt.model.HashTagLinkFaRpt;
2022-10-20 09:27:13 +00:00
import com.dbnt.faisp.faRpt.repository.FaRptBoardRepository;
import com.dbnt.faisp.faRpt.repository.FaRptFileRepository;
import com.dbnt.faisp.faRpt.repository.FaRptReadUserRepository;
2022-10-25 08:49:27 +00:00
import com.dbnt.faisp.faRpt.repository.HashTagLinkFaRptRepository;
import com.dbnt.faisp.hashTag.service.HashTagService;
2022-10-20 09:27:13 +00:00
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.util.List;
import java.util.UUID;
@Service
@RequiredArgsConstructor
public class FaRptService extends BaseService {
2022-10-25 08:49:27 +00:00
private final HashTagService hashTagService;
2022-10-20 09:27:13 +00:00
private final FaRptBoardRepository faRptBoardRepository;
private final FaRptFileRepository faRptFileRepository;
private final FaRptReadUserRepository faRptReadUserRepository;
2022-10-25 08:49:27 +00:00
private final HashTagLinkFaRptRepository hashTagLinkFaRptRepository;
2022-10-20 09:27:13 +00:00
private final FaRptMapper faRptMapper;
public List<FaRptBoard> selectFaRptList(FaRptBoard faRptBoard) {
return faRptMapper.selectFaRptList(faRptBoard);
}
public Integer selectFaRptCnt(FaRptBoard faRptBoard) {
return faRptMapper.selectFaRptCnt(faRptBoard);
}
2022-10-24 09:18:53 +00:00
@Transactional
2022-10-20 09:27:13 +00:00
public Integer saveFaRptBoard(FaRptBoard faRptBoard, List<Integer> deleteFileSeq) {
2022-10-24 09:18:53 +00:00
Integer faRptKey = faRptBoardRepository.save(faRptBoard).getFaRptKey();
if(deleteFileSeq!=null && deleteFileSeq.size()>0){
deleteFaRptFile(faRptKey, deleteFileSeq);
}
if(faRptBoard.getMultipartFileList() != null){
saveUploadFiles(faRptKey, faRptBoard.getMultipartFileList());
}
if(faRptBoard.getReadUserList() != null){
saveFaRptReadUser(faRptKey, faRptBoard.getReadUserList());
}
2022-10-25 08:49:27 +00:00
if(!faRptBoard.getHashTags().isEmpty()){
saveHashTagLink(faRptKey, faRptBoard.getHashTags().split(" "));
}
2022-10-24 09:18:53 +00:00
return faRptKey;
2022-10-20 09:27:13 +00:00
}
2022-10-25 08:49:27 +00:00
@Transactional
public FaRptBoard selectFaRptBoard(Integer faRptKey, Integer userSeq) {
2022-10-20 09:27:13 +00:00
FaRptBoard faRptBoard = faRptBoardRepository.findById(faRptKey).orElse(null);
2022-10-25 08:49:27 +00:00
if(faRptBoard != null){
faRptBoard.setFileList(faRptFileRepository.findByFaRptKey(faRptKey));
faRptBoard.setHashTags(faRptMapper.selectHashTags(faRptKey));
faRptBoard.setReadUserList(faRptReadUserRepository.findByFaRptKey(faRptKey));
if(faRptBoard.getStatus().equals("DST007")){
for(FaRptReadUser readUser: faRptBoard.getReadUserList()){
if(readUser.getUserSeq().equals(userSeq)){
readUser.setReadYn("T");
faRptReadUserRepository.save(readUser);
}
}
}
}
2022-10-20 09:27:13 +00:00
return faRptBoard;
}
2022-10-24 09:18:53 +00:00
private void saveFaRptReadUser(Integer faRptKey, List<FaRptReadUser> readUserList) {
faRptReadUserRepository.deleteByFaRptKey(faRptKey);
for(FaRptReadUser readUser: readUserList){
readUser.setFaRptKey(faRptKey);
}
faRptReadUserRepository.saveAll(readUserList);
}
private void saveUploadFiles(Integer faRptKey, List<MultipartFile> multipartFileList) {
FaRptFile lastFile = faRptFileRepository.findTopByFaRptKeyOrderByFileSeq(faRptKey).orElse(null);
int fileSeq = lastFile==null?1:(lastFile.getFileSeq()+1);
for(MultipartFile file: multipartFileList){
String saveName = UUID.randomUUID().toString();
String path = locationPath+File.separator+"faRpt"+File.separator;
saveFile(file, new File(path+File.separator+saveName));
String originalFilename = file.getOriginalFilename();
int extnIdx = originalFilename.lastIndexOf(".");
FaRptFile fileInfo = new FaRptFile();
fileInfo.setFaRptKey(faRptKey);
fileInfo.setFileSeq(fileSeq++);
fileInfo.setOrigNm(originalFilename.substring(0, extnIdx));
fileInfo.setFileExtn(originalFilename.substring(extnIdx+1));
fileInfo.setConvNm(saveName);
fileInfo.setFileSize(calculationSize(file.getSize()));
fileInfo.setSavePath(path);
faRptFileRepository.save(fileInfo);
}
}
private void deleteFaRptFile(Integer faRptKey, List<Integer> deleteFileSeq) {
List<FaRptFile> fileList = faRptFileRepository.findByFaRptKey(faRptKey);
for(FaRptFile file: fileList){
if(deleteFileSeq.contains(file.getFileSeq())){
deleteStoredFile(new File(file.getSavePath(), file.getConvNm()));
faRptFileRepository.delete(file);
}
}
}
2022-10-25 08:49:27 +00:00
private void saveHashTagLink(Integer faRptKey, String[] hashTagAry){
hashTagLinkFaRptRepository.deleteByFaRptKey(faRptKey);
for(String tagNm: hashTagAry){
HashTagLinkFaRpt hashTagLink = new HashTagLinkFaRpt();
hashTagLink.setFaRptKey(faRptKey);
hashTagLink.setTagKey(hashTagService.selectTagKey(tagNm));
hashTagLinkFaRptRepository.save(hashTagLink);
}
}
public FileInfo selectFaRptFile(Integer faRptKey, Integer fileSeq) {
return faRptFileRepository.findById(new FaRptFile.FaRptFileId(faRptKey, fileSeq)).orElse(null);
}
2022-10-20 09:27:13 +00:00
}