2021-12-14 09:36:44 +00:00
|
|
|
package com.dbnt.kcgfilemanager.service;
|
|
|
|
|
|
2021-12-15 09:33:47 +00:00
|
|
|
import com.dbnt.kcgfilemanager.config.LogStatus;
|
2021-12-15 07:07:35 +00:00
|
|
|
import com.dbnt.kcgfilemanager.mapper.BoardMapper;
|
|
|
|
|
import com.dbnt.kcgfilemanager.model.*;
|
|
|
|
|
import com.dbnt.kcgfilemanager.repository.*;
|
2021-12-14 09:36:44 +00:00
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
2021-12-15 07:07:35 +00:00
|
|
|
import java.io.File;
|
|
|
|
|
import java.io.IOException;
|
2021-12-14 09:36:44 +00:00
|
|
|
import java.util.List;
|
2021-12-15 07:07:35 +00:00
|
|
|
import java.util.Objects;
|
|
|
|
|
import java.util.UUID;
|
2021-12-14 09:36:44 +00:00
|
|
|
|
|
|
|
|
@Service
|
|
|
|
|
@RequiredArgsConstructor
|
|
|
|
|
public class BoardService {
|
|
|
|
|
|
2021-12-15 07:07:35 +00:00
|
|
|
private final BoardRepository boardRepository;
|
|
|
|
|
private final BoardLogRepository boardLogRepository;
|
|
|
|
|
private final HashTagRepository hashTagRepository;
|
|
|
|
|
private final HashTagLinkRepository hashTagLinkRepository;
|
|
|
|
|
private final FileInfoRepository fileInfoRepository;
|
|
|
|
|
private final BoardCategoryRepository boardCategoryRepository;
|
|
|
|
|
private final BoardMapper boardMapper;
|
2021-12-14 09:36:44 +00:00
|
|
|
|
|
|
|
|
@Transactional
|
|
|
|
|
public Integer saveContent(Board content){
|
2021-12-15 07:07:35 +00:00
|
|
|
Integer contentSeq = boardRepository.save(content).getContentSeq();
|
2021-12-15 09:33:47 +00:00
|
|
|
saveBoardLog(content.getContentSeq(), LogStatus.WRITE, null, content.getCreateId());
|
2021-12-15 07:07:35 +00:00
|
|
|
saveHashTagLink(contentSeq, content.getHashTagStr());
|
2021-12-15 09:33:47 +00:00
|
|
|
saveUploadFiles(content);
|
2021-12-15 07:07:35 +00:00
|
|
|
return contentSeq;
|
2021-12-14 09:36:44 +00:00
|
|
|
}
|
|
|
|
|
|
2021-12-15 09:33:47 +00:00
|
|
|
private void saveBoardLog(Integer contentSeq, LogStatus status, String description, String createId){
|
2021-12-14 09:36:44 +00:00
|
|
|
BoardLog lastLog = boardLogRepository.findTopByContentSeqOrderByLogSeqDesc(contentSeq).orElse(null);
|
|
|
|
|
BoardLog log = new BoardLog();
|
|
|
|
|
log.setContentSeq(contentSeq);
|
|
|
|
|
log.setLogSeq(lastLog == null?1:(lastLog.getLogSeq()+1));
|
2021-12-15 09:33:47 +00:00
|
|
|
log.setLogStatus(status.getKey());
|
2021-12-14 09:36:44 +00:00
|
|
|
log.setDescription(description);
|
|
|
|
|
log.setCreateId(createId);
|
|
|
|
|
boardLogRepository.save(log);
|
|
|
|
|
}
|
2021-12-15 07:07:35 +00:00
|
|
|
|
|
|
|
|
private HashTag saveHashTag(String tagName){
|
2021-12-14 09:36:44 +00:00
|
|
|
HashTag tag = new HashTag();
|
|
|
|
|
tag.setTagName(tagName);
|
|
|
|
|
return hashTagRepository.save(tag);
|
|
|
|
|
}
|
2021-12-15 07:07:35 +00:00
|
|
|
|
|
|
|
|
private void saveHashTagLink(Integer contentSeq, String hashTagStr){
|
2021-12-14 09:36:44 +00:00
|
|
|
String[] hashTagAry = hashTagStr.trim().replaceAll(",", "").split("#");
|
|
|
|
|
for(String tagName : hashTagAry){
|
2021-12-15 07:07:35 +00:00
|
|
|
if(!tagName.equals("")){
|
|
|
|
|
HashTag tag = hashTagRepository.findByTagName(tagName).orElse(null);
|
|
|
|
|
if(tag==null){
|
|
|
|
|
tag = saveHashTag(tagName);
|
|
|
|
|
}
|
|
|
|
|
HashTagLink link = new HashTagLink();
|
|
|
|
|
link.setContentSeq(contentSeq);
|
|
|
|
|
link.setTagSeq(tag.getTagSeq());
|
|
|
|
|
hashTagLinkRepository.save(link);
|
2021-12-14 09:36:44 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-12-15 09:33:47 +00:00
|
|
|
private void saveUploadFiles(Board content){
|
|
|
|
|
FileInfo lastFileInfo = fileInfoRepository.findTopByContentSeqOrderByFileSeqDesc(content.getContentSeq()).orElse(null);
|
2021-12-15 07:07:35 +00:00
|
|
|
int fileSeq = lastFileInfo==null?1:(lastFileInfo.getFileSeq()+1);
|
2021-12-15 09:33:47 +00:00
|
|
|
for(MultipartFile file : content.getFileList()){
|
2021-12-15 07:07:35 +00:00
|
|
|
String saveName = UUID.randomUUID().toString();
|
2021-12-15 09:33:47 +00:00
|
|
|
String path = makeFilePath(content.getCategorySeq());
|
2021-12-15 07:07:35 +00:00
|
|
|
|
|
|
|
|
File saveFile = new File(path+"\\"+saveName);
|
|
|
|
|
if(file.getSize()!=0){ // 저장될 파일 확인
|
|
|
|
|
if(!saveFile.exists()){ // 저장될 경로 확인
|
|
|
|
|
if(saveFile.getParentFile().mkdirs()){
|
|
|
|
|
try{
|
|
|
|
|
saveFile.createNewFile();
|
|
|
|
|
}catch (IOException e){
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
file.transferTo(saveFile);
|
|
|
|
|
}catch (IllegalStateException | IOException e){
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-12-14 09:36:44 +00:00
|
|
|
|
2021-12-15 07:07:35 +00:00
|
|
|
String[] originalFilename = Objects.requireNonNull(file.getOriginalFilename()).split("\\.");
|
|
|
|
|
FileInfo fileInfo = new FileInfo();
|
2021-12-15 09:33:47 +00:00
|
|
|
fileInfo.setContentSeq(content.getContentSeq());
|
2021-12-15 07:07:35 +00:00
|
|
|
fileInfo.setFileSeq(fileSeq++);
|
|
|
|
|
fileInfo.setOriginalName(originalFilename[0]);
|
|
|
|
|
fileInfo.setExtention(originalFilename[1]);
|
|
|
|
|
fileInfo.setConversionName(saveName);
|
|
|
|
|
fileInfo.setSavePath(path);
|
|
|
|
|
fileInfoRepository.save(fileInfo);
|
2021-12-15 09:33:47 +00:00
|
|
|
saveBoardLog(content.getContentSeq(), LogStatus.FILE_ADD, file.getOriginalFilename(), content.getCreateId());
|
2021-12-15 07:07:35 +00:00
|
|
|
}
|
2021-12-14 09:36:44 +00:00
|
|
|
}
|
|
|
|
|
|
2021-12-15 07:07:35 +00:00
|
|
|
public String getPageTitle(Integer categorySeq) {
|
|
|
|
|
BoardCategory category = boardCategoryRepository.findById(categorySeq).orElse(null);
|
|
|
|
|
if(category.getParentSeq()==null){
|
|
|
|
|
return category.getCategoryName();
|
|
|
|
|
}
|
|
|
|
|
return getPageTitle(category.getParentSeq())+" > "+category.getCategoryName();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public String makeFilePath(Integer categorySeq){
|
|
|
|
|
BoardCategory category = boardCategoryRepository.findById(categorySeq).orElse(null);
|
|
|
|
|
if(category.getParentSeq()==null){
|
|
|
|
|
return "D:\\kcgFileManager\\"+category.getCategoryName();
|
|
|
|
|
}
|
|
|
|
|
return makeFilePath(category.getParentSeq())+"\\"+category.getCategoryName();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<Board> selectContentList(Board board) {
|
|
|
|
|
return boardMapper.selectContentList(board);
|
|
|
|
|
}
|
|
|
|
|
public Integer selectContentListCnt(Board board) {
|
|
|
|
|
return boardMapper.selectContentListCnt(board);
|
|
|
|
|
}
|
2021-12-15 09:33:47 +00:00
|
|
|
|
|
|
|
|
public Board selectContentDetail(Integer contentSeq) {
|
|
|
|
|
Board target = boardRepository.findById(contentSeq).orElse(null);
|
|
|
|
|
target.setHashTagList(boardMapper.selectHashTagListFromContentSeq(contentSeq));
|
|
|
|
|
target.setChildFileList(fileInfoRepository.findByContentSeqOrderByFileSeqAsc(contentSeq));
|
|
|
|
|
return target;
|
|
|
|
|
}
|
|
|
|
|
public List<BoardLog> selectContentLog(Integer contentSeq){
|
|
|
|
|
return boardMapper.selectBoardLogFromContentSeq(contentSeq);
|
|
|
|
|
}
|
2021-12-14 09:36:44 +00:00
|
|
|
}
|