메뉴 배너 관리
This commit is contained in:
@@ -0,0 +1,325 @@
|
||||
package com.caliverse.admin.domain.service;
|
||||
|
||||
import com.caliverse.admin.domain.dao.admin.MenuMapper;
|
||||
import com.caliverse.admin.domain.entity.*;
|
||||
import com.caliverse.admin.domain.request.MenuRequest;
|
||||
import com.caliverse.admin.domain.response.MenuResponse;
|
||||
import com.caliverse.admin.global.common.code.CommonCode;
|
||||
import com.caliverse.admin.global.common.code.ErrorCode;
|
||||
import com.caliverse.admin.global.common.code.SuccessCode;
|
||||
import com.caliverse.admin.global.common.constants.CommonConstants;
|
||||
import com.caliverse.admin.global.common.constants.MysqlConstants;
|
||||
import com.caliverse.admin.global.common.exception.RestApiException;
|
||||
import com.caliverse.admin.global.common.utils.CommonUtils;
|
||||
import com.caliverse.admin.global.common.utils.FileUtils;
|
||||
import com.caliverse.admin.history.service.MysqlHistoryLogService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.core.io.ResourceLoader;
|
||||
import org.springframework.core.io.UrlResource;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import software.amazon.awssdk.services.s3.model.S3Exception;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.net.MalformedURLException;
|
||||
import java.nio.file.Path;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class MenuService {
|
||||
|
||||
@Value("${excel.file-path}")
|
||||
private String filePath;
|
||||
@Value("${caliverse.file}")
|
||||
private String samplePath;
|
||||
private final FileUtils fileUtils;
|
||||
private final MenuMapper menuMapper;
|
||||
private final ResourceLoader resourceLoader;
|
||||
private final MysqlHistoryLogService mysqlHistoryLogService;
|
||||
private final S3Service s3Service;
|
||||
|
||||
public MenuResponse getList(Map requestParam){
|
||||
|
||||
//페이징 처리
|
||||
requestParam = CommonUtils.pageSetting(requestParam);
|
||||
|
||||
List<MenuBanner> list = menuMapper.getBannerList(requestParam);
|
||||
|
||||
return MenuResponse.builder()
|
||||
.status(CommonCode.SUCCESS.getHttpStatus())
|
||||
.result(CommonCode.SUCCESS.getResult())
|
||||
.resultData(MenuResponse.ResultData.builder()
|
||||
.bannerList(list)
|
||||
.total(menuMapper.getTotal())
|
||||
.totalAll(list.size())
|
||||
.pageNo(requestParam.get("page_no")!=null?
|
||||
Integer.valueOf(requestParam.get("page_no").toString()):1)
|
||||
.build()
|
||||
)
|
||||
.build();
|
||||
}
|
||||
|
||||
public MenuResponse getdetail(Long id){
|
||||
MenuBanner banner = menuMapper.getBannerDetail(id);
|
||||
|
||||
banner.setImageList(menuMapper.getMessage(id));
|
||||
|
||||
return MenuResponse.builder()
|
||||
.status(CommonCode.SUCCESS.getHttpStatus())
|
||||
.result(CommonCode.SUCCESS.getResult())
|
||||
.resultData(MenuResponse.ResultData.builder()
|
||||
.banner(banner)
|
||||
.build())
|
||||
.build();
|
||||
}
|
||||
|
||||
public MenuResponse imageUpload(MultipartFile file){
|
||||
if (file.isEmpty()) {
|
||||
throw new RestApiException(CommonCode.ERROR.getHttpStatus(), ErrorCode.NOT_EXIT_FILE.toString());
|
||||
}
|
||||
|
||||
// 파일을 서버에 저장
|
||||
String fileName = fileUtils.saveFile(file);
|
||||
|
||||
return MenuResponse.builder()
|
||||
.resultData(MenuResponse.ResultData.builder()
|
||||
.message(SuccessCode.EXCEL_UPLOAD.getMessage())
|
||||
.fileName(fileName)
|
||||
.build())
|
||||
.status(CommonCode.SUCCESS.getHttpStatus())
|
||||
.result(CommonCode.SUCCESS.getResult())
|
||||
.build();
|
||||
}
|
||||
|
||||
public MenuResponse imageDelete(String fileName){
|
||||
if (fileName.isEmpty()) {
|
||||
throw new RestApiException(CommonCode.ERROR.getHttpStatus(), ErrorCode.NOT_EXIT_FILE.getMessage() );
|
||||
}
|
||||
|
||||
// 서버 파일 삭제
|
||||
boolean is_delete = fileUtils.deleteFile(fileName);
|
||||
|
||||
if(is_delete){
|
||||
return MenuResponse.builder()
|
||||
.resultData(MenuResponse.ResultData.builder()
|
||||
.message(SuccessCode.FILE_DELETE.getMessage())
|
||||
.build())
|
||||
.status(CommonCode.SUCCESS.getHttpStatus())
|
||||
.result(CommonCode.SUCCESS.getResult())
|
||||
.build();
|
||||
}else{
|
||||
return MenuResponse.builder()
|
||||
.resultData(MenuResponse.ResultData.builder()
|
||||
.message(ErrorCode.ERROR_FILE_DELETE.getMessage())
|
||||
.build())
|
||||
.status(CommonCode.ERROR.getHttpStatus())
|
||||
.result(CommonCode.ERROR.getResult())
|
||||
.build();
|
||||
}
|
||||
}
|
||||
|
||||
public Resource fileDown(String fileName) {
|
||||
Path fileFullPath = Path.of(filePath+fileName);
|
||||
try {
|
||||
if(fileName.contains("sample")){
|
||||
return resourceLoader.getResource(samplePath + fileName);
|
||||
}
|
||||
Resource resource = new UrlResource(fileFullPath.toUri());
|
||||
if (resource.exists() && resource.isReadable()) {
|
||||
return resource;
|
||||
} else {
|
||||
throw new RestApiException(CommonCode.ERROR.getHttpStatus(), ErrorCode.NOT_EXIT_EXCEL.getMessage());
|
||||
}
|
||||
}catch (MalformedURLException e){
|
||||
throw new RestApiException(CommonCode.ERROR.getHttpStatus(), ErrorCode.NOT_EXIT_EXCEL.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Transactional(transactionManager = "transactionManager")
|
||||
public MenuResponse postBanner(MenuRequest menuRequest){
|
||||
menuRequest.setCreateBy(CommonUtils.getAdmin().getId());
|
||||
|
||||
menuMapper.insertBanner(menuRequest);
|
||||
|
||||
long banner_id = menuRequest.getId();
|
||||
HashMap<String,String> map = new HashMap<>();
|
||||
map.put("id",String.valueOf(banner_id));
|
||||
|
||||
//메시지 저장(title=이미지명, content=이미지 링크)
|
||||
if(menuRequest.getImageList()!= null && !menuRequest.getImageList().isEmpty()){
|
||||
menuRequest.getImageList().forEach(image -> {
|
||||
String temp_file = image.getContent();
|
||||
String contentType = fileUtils.getContentType(temp_file);
|
||||
File file = fileUtils.loadFileObject(temp_file);
|
||||
String fileUri = "";
|
||||
try{
|
||||
fileUri = s3Service.uploadFile(file, CommonConstants.S3_MENU_BANNER_DIRECTORY + banner_id, contentType);
|
||||
}catch (S3Exception e) {
|
||||
log.error("S3 오류: {}", e.getMessage());
|
||||
throw new RestApiException(CommonCode.ERROR.getHttpStatus(), ErrorCode.ERROR_FILE_S3_UPLOAD.getMessage());
|
||||
|
||||
} catch (IOException e) {
|
||||
log.error("파일 읽기 오류: {}", e.getMessage());
|
||||
throw new RestApiException(CommonCode.ERROR.getHttpStatus(), ErrorCode.ERROR_FILE_S3_UPLOAD.getMessage());
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("파일 업로드 중 예상치 못한 오류: {}", e.getMessage(), e);
|
||||
throw new RestApiException(CommonCode.ERROR.getHttpStatus(), ErrorCode.ERROR_FILE_S3_UPLOAD.getMessage());
|
||||
}
|
||||
|
||||
if(fileUri.isEmpty()){
|
||||
throw new RestApiException(CommonCode.ERROR.getHttpStatus(), ErrorCode.ERROR_FILE_S3_UPLOAD.getMessage());
|
||||
}
|
||||
|
||||
map.put("title", fileUri);
|
||||
map.put("language", image.getLanguage());
|
||||
if(menuRequest.isLink() && (menuRequest.getLinkList() != null && !menuRequest.getLinkList().isEmpty())){
|
||||
String link = menuRequest.getLinkList().stream().filter(data -> data.getLanguage().equals(image.getLanguage())).findFirst().get().getContent();
|
||||
map.put("content", link);
|
||||
}else{
|
||||
map.put("content", "");
|
||||
}
|
||||
menuMapper.insertMessage(map);
|
||||
|
||||
fileUtils.deleteFile(temp_file);
|
||||
});
|
||||
}
|
||||
|
||||
MenuBanner banner = menuMapper.getBannerDetail(menuRequest.getId());
|
||||
|
||||
mysqlHistoryLogService.insertHistoryLog(
|
||||
HISTORYTYPE.MENU_BANNER_ADD,
|
||||
MysqlConstants.TABLE_NAME_MENU_BANNER,
|
||||
HISTORYTYPE.MENU_BANNER_ADD.name(),
|
||||
banner,
|
||||
CommonUtils.getAdmin().getEmail(),
|
||||
CommonUtils.getClientIp()
|
||||
);
|
||||
|
||||
return MenuResponse.builder()
|
||||
.status(CommonCode.SUCCESS.getHttpStatus())
|
||||
.result(CommonCode.SUCCESS.getResult())
|
||||
.resultData(MenuResponse.ResultData.builder()
|
||||
.message(SuccessCode.SAVE.getMessage())
|
||||
.build())
|
||||
.build();
|
||||
}
|
||||
|
||||
@Transactional(transactionManager = "transactionManager")
|
||||
public MenuResponse updateBanner(Long id, MenuRequest menuRequest) {
|
||||
menuRequest.setId(id);
|
||||
menuRequest.setUpdateBy(CommonUtils.getAdmin().getId());
|
||||
menuRequest.setUpdateDt(LocalDateTime.now());
|
||||
|
||||
Long banner_id = menuRequest.getId();
|
||||
MenuBanner before_info = menuMapper.getBannerDetail(banner_id);
|
||||
List<Message> before_msg = menuMapper.getMessage(banner_id);
|
||||
before_info.setImageList(before_msg);
|
||||
|
||||
log.info("updateBanner Update Before MenuBanner: {}, Images: {}", before_info, before_msg);
|
||||
|
||||
menuMapper.updateBanner(menuRequest);
|
||||
|
||||
Map<String, String> map = new HashMap<>();
|
||||
map.put("id", String.valueOf(menuRequest.getId()));
|
||||
|
||||
menuMapper.deleteMessage(map);
|
||||
|
||||
if(menuRequest.getImageList()!= null && !menuRequest.getImageList().isEmpty()){
|
||||
menuRequest.getImageList().forEach(image -> {
|
||||
map.put("title", image.getContent());
|
||||
map.put("language", image.getLanguage());
|
||||
if(menuRequest.isLink() && menuRequest.getLinkList() != null && !menuRequest.getLinkList().isEmpty()){
|
||||
String link = menuRequest.getLinkList().stream().filter(data -> data.getLanguage().equals(image.getLanguage())).findFirst().get().getContent();
|
||||
map.put("content", link);
|
||||
}
|
||||
menuMapper.insertMessage(map);
|
||||
});
|
||||
}
|
||||
log.info("updateBanner Update After Banner: {}", menuRequest);
|
||||
|
||||
MenuBanner after_info = menuMapper.getBannerDetail(banner_id);
|
||||
after_info.setImageList(menuMapper.getMessage(banner_id));
|
||||
|
||||
mysqlHistoryLogService.updateHistoryLog(
|
||||
HISTORYTYPE.MENU_BANNER_UPDATE,
|
||||
MysqlConstants.TABLE_NAME_MENU_BANNER,
|
||||
HISTORYTYPE.MENU_BANNER_UPDATE.name(),
|
||||
before_info,
|
||||
after_info,
|
||||
CommonUtils.getAdmin().getEmail(),
|
||||
CommonUtils.getClientIp()
|
||||
);
|
||||
|
||||
return MenuResponse.builder()
|
||||
.status(CommonCode.SUCCESS.getHttpStatus())
|
||||
.result(CommonCode.SUCCESS.getResult())
|
||||
.resultData(MenuResponse.ResultData.builder()
|
||||
.message(SuccessCode.UPDATE.getMessage())
|
||||
.build())
|
||||
.build();
|
||||
}
|
||||
|
||||
@Transactional(transactionManager = "transactionManager")
|
||||
public MenuResponse deleteMail(MenuRequest menuRequest){
|
||||
Map<String,Object> map = new HashMap<>();
|
||||
|
||||
// mailRequest.getList().forEach(
|
||||
// item->{
|
||||
// map.put("id",item.getId());
|
||||
// mailMapper.deleteMail(map);
|
||||
//
|
||||
// // 스케줄에서 제거
|
||||
// scheduleService.closeTask("mail-" + item.getId());
|
||||
// log.info("deleteMail Mail: {}", item);
|
||||
//
|
||||
// //로그 기록
|
||||
// List<Message> message = mailMapper.getMessage(Long.valueOf(item.getId()));
|
||||
// map.put("adminId", CommonUtils.getAdmin().getId());
|
||||
// map.put("name", CommonUtils.getAdmin().getName());
|
||||
// map.put("mail", CommonUtils.getAdmin().getEmail());
|
||||
// map.put("type", HISTORYTYPE.MAIL_DELETE);
|
||||
// JSONObject jsonObject = new JSONObject();
|
||||
// if(!message.isEmpty()){
|
||||
// jsonObject.put("message",message.get(0).getTitle());
|
||||
// }
|
||||
// map.put("content",jsonObject.toString());
|
||||
// historyMapper.saveLog(map);
|
||||
// }
|
||||
// );
|
||||
|
||||
return MenuResponse.builder()
|
||||
.status(CommonCode.SUCCESS.getHttpStatus())
|
||||
.result(CommonCode.SUCCESS.getResult())
|
||||
.resultData(MenuResponse.ResultData.builder()
|
||||
.message(SuccessCode.DELETE.getMessage())
|
||||
.build())
|
||||
.build();
|
||||
}
|
||||
|
||||
public List<MenuBanner> getScheduleMailList(){
|
||||
return menuMapper.getScheduleBannerList();
|
||||
}
|
||||
|
||||
public List<Message> getMessageList(Long id){
|
||||
return menuMapper.getMessage(id);
|
||||
}
|
||||
|
||||
public void updateStatus(Long id, MenuBanner.BANNER_STATUS status){
|
||||
Map<String,Object> map = new HashMap<>();
|
||||
map.put("id", id);
|
||||
map.put("status", status.toString());
|
||||
menuMapper.updateBannerStatus(map);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -6,12 +6,16 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import software.amazon.awssdk.core.sync.RequestBody;
|
||||
import software.amazon.awssdk.services.s3.S3Client;
|
||||
import software.amazon.awssdk.services.s3.model.GetObjectAttributesRequest;
|
||||
import software.amazon.awssdk.services.s3.model.GetObjectAttributesResponse;
|
||||
import software.amazon.awssdk.services.s3.model.S3Exception;
|
||||
import software.amazon.awssdk.services.s3.model.*;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
@@ -24,6 +28,9 @@ public class S3Service {
|
||||
@Value("${amazon.s3.bucket-name}")
|
||||
private String bucketName;
|
||||
|
||||
@Value("${amazon.aws.region}")
|
||||
private String region;
|
||||
|
||||
public Optional<GetObjectAttributesResponse> getObjectMetadata(String key) {
|
||||
try {
|
||||
GetObjectAttributesRequest request = GetObjectAttributesRequest.builder()
|
||||
@@ -38,4 +45,42 @@ public class S3Service {
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
|
||||
public String uploadFile(File file, String directoryName, String contentType) throws IOException, S3Exception {
|
||||
String fileName = UUID.randomUUID().toString() + "-" + file.getName();
|
||||
|
||||
// S3 객체 키 (경로 + 파일명)
|
||||
String objectKey = directoryName + "/" + fileName;
|
||||
|
||||
try {
|
||||
|
||||
PutObjectRequest putObjectRequest = PutObjectRequest.builder()
|
||||
.bucket(bucketName)
|
||||
.key(objectKey)
|
||||
.contentType(contentType)
|
||||
.build();
|
||||
|
||||
// 파일 업로드
|
||||
PutObjectResponse response = s3Client.putObject(
|
||||
putObjectRequest,
|
||||
RequestBody.fromBytes(Files.readAllBytes(file.toPath()))
|
||||
);
|
||||
//https://metaverse-myhomeugc-test.s3.us-west-2.amazonaws.com/0002896883264fc9af5be62134939ce4/552ec8a4302348edb3fbf1496811d75f.ugcinfo
|
||||
return "https://" + bucketName + ".s3." +
|
||||
s3Client.serviceClientConfiguration().region().toString() +
|
||||
".amazonaws.com/" + objectKey;
|
||||
// return "s3://" + bucketName + objectKey;
|
||||
}catch (S3Exception | IOException e) {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
public void deleteFile(String fileUrl) {
|
||||
String objectKey = fileUrl.substring(fileUrl.indexOf(".com/") + 5);
|
||||
|
||||
s3Client.deleteObject(builder -> builder
|
||||
.bucket(bucketName)
|
||||
.key(objectKey)
|
||||
.build());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user