수입/지출 데이터 삭제시 OCR의 경우 파일 실삭제
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
package app.module.core.config.application.util;
|
||||
|
||||
import app.module.core.usecase.file.getFile.adapters.persistence.GetFileMapper;
|
||||
import app.module.core.usecase.file.getFile.ports.application.domain.GetFile;
|
||||
import app.module.core.usecase.file.saveToStorage.ports.application.strategy.SftpConnector;
|
||||
import app.module.core.usecase.file.updateFile.adapters.persistence.UpdateFileMapper;
|
||||
import app.module.core.usecase.file.updateFile.ports.application.domain.UploadFileDto;
|
||||
import com.jcraft.jsch.ChannelSftp;
|
||||
import com.jcraft.jsch.JSchException;
|
||||
@@ -18,6 +21,8 @@ import java.nio.file.Paths;
|
||||
public class SftpFileDeleteUtil {
|
||||
|
||||
private final SftpConnector sftpConnector;
|
||||
private final GetFileMapper getFileMapper;
|
||||
private final UpdateFileMapper updateFileMapper;
|
||||
|
||||
@Value("${path.upload.sftp.public}")
|
||||
private String publicPath;
|
||||
@@ -64,6 +69,47 @@ public class SftpFileDeleteUtil {
|
||||
}
|
||||
}
|
||||
|
||||
public void deleteRealFile(Long deleteFileId) throws JSchException, SftpException {
|
||||
GetFile deleteFile = getFileMapper.findById(deleteFileId).orElseThrow();
|
||||
updateFileMapper.deleteReceiptFile(deleteFileId,CommonUtil.getLoginUsername());
|
||||
|
||||
if (deleteFile == null || deleteFile.getPath() == null || deleteFile.getName() == null) {
|
||||
log.error("Invalid file information: {}", deleteFile);
|
||||
throw new IllegalArgumentException("Invalid file information");
|
||||
}
|
||||
|
||||
// 경로 검증 및 생성
|
||||
String filePath = constructSafeFilePath(deleteFile.getPath(), deleteFile.getName());
|
||||
log.info("Attempting to delete file from SFTP: {}", filePath);
|
||||
|
||||
// SFTP 연결 확인 및 재연결
|
||||
if (!sftpConnector.isConnected()) {
|
||||
log.warn("SFTP connection lost, attempting to reconnect");
|
||||
sftpConnector.connect();
|
||||
}
|
||||
ChannelSftp channelSftp = sftpConnector.getChannelSftp();
|
||||
if (channelSftp == null) {
|
||||
log.error("SFTP channel is not initialized");
|
||||
throw new SftpException(ChannelSftp.SSH_FX_NO_CONNECTION, "SFTP channel is not initialized");
|
||||
}
|
||||
|
||||
// 파일 삭제
|
||||
try {
|
||||
channelSftp.stat(filePath); // 파일 존재 여부 확인
|
||||
channelSftp.rm(filePath);
|
||||
log.info("File deleted successfully from SFTP: {}", filePath);
|
||||
} catch (SftpException e) {
|
||||
if (e.id == ChannelSftp.SSH_FX_NO_SUCH_FILE) {
|
||||
log.warn("File does not exist on SFTP: {}", filePath);
|
||||
} else {
|
||||
log.error("Failed to delete file from SFTP: {}, error: {}", filePath, e.getMessage(), e);
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 경로 조작 방지를 위해 안전한 파일 경로 생성
|
||||
*/
|
||||
|
||||
@@ -3,6 +3,7 @@ package app.module.core.usecase.file.updateFile.adapters.persistence;
|
||||
import app.module.core.usecase.file.updateFile.ports.application.domain.UpdateUploadFileParam;
|
||||
import app.module.core.usecase.file.updateFile.ports.application.domain.UploadFileDto;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@@ -10,5 +11,6 @@ import java.util.List;
|
||||
public interface UpdateFileMapper {
|
||||
List<UploadFileDto> findDeleteFileList(UpdateUploadFileParam uploadFile);
|
||||
void deleteFile(UpdateUploadFileParam uploadFile);
|
||||
void deleteReceiptFile(@Param("fileId") Long fileId,@Param("deletedUserId") String deletedUserId);
|
||||
void updateFile(UploadFileDto uploadFile);
|
||||
}
|
||||
|
||||
@@ -36,6 +36,15 @@
|
||||
</if>
|
||||
</update>
|
||||
|
||||
<update id="deleteReceiptFile">
|
||||
UPDATE upload_file
|
||||
SET deleted_by = #{deletedUserId},
|
||||
deleted_at = now(),
|
||||
is_deleted = true
|
||||
WHERE is_deleted = false
|
||||
and id = #{fileId}
|
||||
</update>
|
||||
|
||||
<update id="updateFile" >
|
||||
UPDATE upload_file
|
||||
SET mapping_id = #{mappingId},
|
||||
|
||||
@@ -1,12 +1,19 @@
|
||||
package app.module.front.usecase.daily.expense.adapters.persistence;
|
||||
|
||||
import app.module.core.config.application.util.CommonUtil;
|
||||
import app.module.core.config.application.util.SftpFileDeleteUtil;
|
||||
import app.module.core.usecase.file.updateFile.adapters.persistence.UpdateFileMapper;
|
||||
import app.module.front.usecase.daily.expense.adapters.persistence.model.DailyExpenseDto;
|
||||
import app.module.front.usecase.daily.expense.adapters.persistence.model.DailyExpenseTypeCount;
|
||||
import app.module.front.usecase.daily.expense.ports.DailyExpensePortOut;
|
||||
import app.module.front.usecase.daily.expense.ports.application.domain.CreateDailyExpenseCmd;
|
||||
import app.module.front.usecase.daily.expense.ports.application.domain.GetDailyExpenseCmd;
|
||||
import app.module.front.usecase.daily.expense.ports.application.domain.RemoveDailyExpenseCmd;
|
||||
import com.jcraft.jsch.JSchException;
|
||||
import com.jcraft.jsch.SftpException;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.List;
|
||||
@@ -16,6 +23,10 @@ import java.util.List;
|
||||
@RequiredArgsConstructor
|
||||
public class DailyExpenseDao implements DailyExpensePortOut {
|
||||
private final DailyExpenseMapper dailyExpenseMapper;
|
||||
private final UpdateFileMapper updateFileMapper;
|
||||
@Value("${path.upload.storage}")
|
||||
private String uploadStorageType;
|
||||
private final SftpFileDeleteUtil sftpFileDeleteUtil;
|
||||
|
||||
@Override
|
||||
public List<DailyExpenseDto> list(GetDailyExpenseCmd command) {
|
||||
@@ -38,9 +49,26 @@ public class DailyExpenseDao implements DailyExpensePortOut {
|
||||
|
||||
@Override
|
||||
public void remove(GetDailyExpenseCmd findCmd, RemoveDailyExpenseCmd command) {
|
||||
Integer existCount = dailyExpenseMapper.getRecordExistCount(findCmd);
|
||||
if (existCount != 0) {
|
||||
DailyExpenseTypeCount existCount = dailyExpenseMapper.getRecordExistTypeCount(findCmd);
|
||||
if (existCount.getCount() != 0) {
|
||||
dailyExpenseMapper.removeDailyExpense(command);
|
||||
if("OCR".equals(existCount.getInputType().name())){
|
||||
dailyExpenseMapper.removeDailyExpenseReceipt(command);
|
||||
if(existCount.getFileIdCount() == 1){
|
||||
if("SFTP".equals(uploadStorageType)){
|
||||
try {
|
||||
sftpFileDeleteUtil.deleteRealFile(existCount.getFileId());
|
||||
} catch (JSchException | SftpException e) {
|
||||
log.error("SFTP 오류 e.getMessage={}", e.getMessage());
|
||||
throw new RuntimeException(e);
|
||||
} catch (Exception ignored){
|
||||
log.error(ignored.getMessage());
|
||||
}
|
||||
}else{
|
||||
updateFileMapper.deleteReceiptFile(existCount.getFileId(), CommonUtil.getLoginUsername());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
log.debug("[RemoveDailyExpense] record={}", command);
|
||||
}
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
package app.module.front.usecase.daily.expense.adapters.persistence;
|
||||
|
||||
import app.module.front.usecase.daily.expense.adapters.persistence.model.DailyExpenseTypeCount;
|
||||
import app.module.front.usecase.daily.expense.ports.application.domain.CreateDailyExpenseCmd;
|
||||
import app.module.front.usecase.daily.expense.adapters.persistence.model.DailyExpenseDto;
|
||||
import app.module.front.usecase.daily.expense.ports.application.domain.GetDailyExpenseCmd;
|
||||
import app.module.front.usecase.daily.expense.ports.application.domain.RemoveDailyExpenseCmd;
|
||||
import app.module.front.usecase.daily.expense.adapters.web.model.DailyExpenseUIReqVO;
|
||||
import app.module.front.usecase.daily.income.ports.application.domain.RemoveDailyIncomeCmd;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
import java.util.List;
|
||||
@@ -27,6 +29,13 @@ public interface DailyExpenseMapper {
|
||||
*/
|
||||
Integer getRecordExistCount(GetDailyExpenseCmd command);
|
||||
|
||||
/**
|
||||
* 일계부 지출 데이터 존재 여부
|
||||
*
|
||||
* @param command 검색 조건
|
||||
* @return 중복 개수
|
||||
*/
|
||||
DailyExpenseTypeCount getRecordExistTypeCount(GetDailyExpenseCmd command);
|
||||
/**
|
||||
* 일계부 지출 등록
|
||||
*
|
||||
@@ -47,4 +56,11 @@ public interface DailyExpenseMapper {
|
||||
* @param command 삭제 데이터
|
||||
*/
|
||||
void removeDailyExpense(RemoveDailyExpenseCmd command);
|
||||
|
||||
/**
|
||||
* 일계부 지출 영수증 삭제
|
||||
*
|
||||
* @param command 삭제 데이터
|
||||
*/
|
||||
void removeDailyExpenseReceipt(RemoveDailyExpenseCmd command);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
package app.module.front.usecase.daily.expense.adapters.persistence.model;
|
||||
|
||||
import app.module.front.usecase.daily.income.adapters.web.model.DailyInputType;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
public class DailyExpenseTypeCount {
|
||||
private Long count;
|
||||
private DailyInputType inputType;
|
||||
private Long fileId;
|
||||
private Long fileIdCount;
|
||||
|
||||
}
|
||||
@@ -1,12 +1,20 @@
|
||||
package app.module.front.usecase.daily.income.adapters.persistence;
|
||||
|
||||
import app.module.core.config.application.util.CommonUtil;
|
||||
import app.module.core.config.application.util.SftpFileDeleteUtil;
|
||||
import app.module.core.usecase.file.getFile.adapters.persistence.GetFileMapper;
|
||||
import app.module.core.usecase.file.updateFile.adapters.persistence.UpdateFileMapper;
|
||||
import app.module.front.usecase.daily.income.adapters.persistence.model.DailyIncomeDto;
|
||||
import app.module.front.usecase.daily.income.adapters.persistence.model.DailyIncomeTypeCount;
|
||||
import app.module.front.usecase.daily.income.ports.DailyIncomePortOut;
|
||||
import app.module.front.usecase.daily.income.ports.application.domain.CreateDailyIncomeCmd;
|
||||
import app.module.front.usecase.daily.income.ports.application.domain.GetDailyIncomeCmd;
|
||||
import app.module.front.usecase.daily.income.ports.application.domain.RemoveDailyIncomeCmd;
|
||||
import com.jcraft.jsch.JSchException;
|
||||
import com.jcraft.jsch.SftpException;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
@@ -16,6 +24,10 @@ import java.util.List;
|
||||
@RequiredArgsConstructor
|
||||
public class DailyIncomeDao implements DailyIncomePortOut {
|
||||
private final DailyIncomeMapper dailyIncomeMapper;
|
||||
private final UpdateFileMapper updateFileMapper;
|
||||
@Value("${path.upload.storage}")
|
||||
private String uploadStorageType;
|
||||
private final SftpFileDeleteUtil sftpFileDeleteUtil;
|
||||
|
||||
@Override
|
||||
public List<DailyIncomeDto> list(GetDailyIncomeCmd command) {
|
||||
@@ -38,10 +50,27 @@ public class DailyIncomeDao implements DailyIncomePortOut {
|
||||
|
||||
@Override
|
||||
public void remove(GetDailyIncomeCmd findCmd, RemoveDailyIncomeCmd command) {
|
||||
Integer existCount = dailyIncomeMapper.getRecordExistCount(findCmd);
|
||||
if (existCount != 0) {
|
||||
DailyIncomeTypeCount existCount = dailyIncomeMapper.getRecordExistTypeCount(findCmd);
|
||||
if (existCount.getCount() != 0) {
|
||||
dailyIncomeMapper.removeDailyIncome(command);
|
||||
|
||||
if("OCR".equals(existCount.getInputType().name())){
|
||||
dailyIncomeMapper.removeDailyIncomeReceipt(command);
|
||||
if(existCount.getFileIdCount() == 1){
|
||||
if("SFTP".equals(uploadStorageType)){
|
||||
try {
|
||||
sftpFileDeleteUtil.deleteRealFile(existCount.getFileId());
|
||||
} catch (JSchException | SftpException e) {
|
||||
log.error("SFTP 오류 e.getMessage={}", e.getMessage());
|
||||
throw new RuntimeException(e);
|
||||
} catch (Exception ignored){
|
||||
log.error(ignored.getMessage());
|
||||
}
|
||||
}else{
|
||||
updateFileMapper.deleteReceiptFile(existCount.getFileId(), CommonUtil.getLoginUsername());
|
||||
}
|
||||
}
|
||||
}
|
||||
log.debug("[RemoveDailyIncome] record={}", command);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package app.module.front.usecase.daily.income.adapters.persistence;
|
||||
|
||||
import app.module.front.usecase.daily.income.adapters.persistence.model.DailyIncomeTypeCount;
|
||||
import app.module.front.usecase.daily.income.ports.application.domain.CreateDailyIncomeCmd;
|
||||
import app.module.front.usecase.daily.income.adapters.persistence.model.DailyIncomeDto;
|
||||
import app.module.front.usecase.daily.income.ports.application.domain.GetDailyIncomeCmd;
|
||||
@@ -26,6 +27,13 @@ public interface DailyIncomeMapper {
|
||||
*/
|
||||
Integer getRecordExistCount(GetDailyIncomeCmd command);
|
||||
|
||||
/**
|
||||
* 일계부 수입 데이터 존재 여부, OCR 여부 , 매핑된 첨부파일 개수 확인
|
||||
*
|
||||
* @param command 검색 조건
|
||||
* @return 중복 개수
|
||||
*/
|
||||
DailyIncomeTypeCount getRecordExistTypeCount(GetDailyIncomeCmd command);
|
||||
/**
|
||||
* 일계부 수입 등록
|
||||
*
|
||||
@@ -46,4 +54,11 @@ public interface DailyIncomeMapper {
|
||||
* @param command 삭제 데이터
|
||||
*/
|
||||
void removeDailyIncome(RemoveDailyIncomeCmd command);
|
||||
|
||||
/**
|
||||
* 일계부 수입 영수증 삭제
|
||||
*
|
||||
* @param command 삭제 데이터
|
||||
*/
|
||||
void removeDailyIncomeReceipt(RemoveDailyIncomeCmd command);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
package app.module.front.usecase.daily.income.adapters.persistence.model;
|
||||
|
||||
import app.module.front.usecase.daily.income.adapters.web.model.DailyInputType;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
public class DailyIncomeTypeCount {
|
||||
private Long count;
|
||||
private DailyInputType inputType;
|
||||
private Long fileId;
|
||||
private Long fileIdCount;
|
||||
|
||||
}
|
||||
@@ -27,6 +27,27 @@
|
||||
</if>
|
||||
</select>
|
||||
|
||||
<select id="getRecordExistTypeCount">
|
||||
select count(1) as count,
|
||||
ds.input_type,
|
||||
rhr.file_id,
|
||||
COALESCE(fc.file_id_count, 0) as file_id_count
|
||||
from daily_expense ds
|
||||
<include refid="app.module.core.usecase.daily.record.adapters.persistence.DailyRecordMapper.InnerJoinByDailyRecord"/>
|
||||
left join
|
||||
receipt_has_record rhr on ds.id = rhr.receipt_type_id and rhr.is_deleted = false
|
||||
left join (
|
||||
select file_id,COUNT(*) AS file_id_count
|
||||
FROM receipt_has_record
|
||||
where is_deleted = false and type = 'EXPENSE'
|
||||
group by file_id
|
||||
) fc on fc.file_id = rhr.file_id
|
||||
where ds.is_deleted = false
|
||||
<if test="id != null and id != '0'">
|
||||
and ds.id = #{id}
|
||||
</if>
|
||||
group by ds.id,rhr.file_id,fc.file_id_count
|
||||
</select>
|
||||
<insert id="createDailyExpense" useGeneratedKeys="true" keyProperty="id">
|
||||
insert into daily_expense (
|
||||
daily_record_seq, input_type, category, income_mark
|
||||
@@ -69,4 +90,13 @@
|
||||
</if>
|
||||
</update>
|
||||
|
||||
<update id="removeDailyExpenseReceipt">
|
||||
update receipt_has_record set
|
||||
<include refid="common.fields.DeleteBaseSet"/>
|
||||
where is_deleted = false
|
||||
<if test="id != null and id != ''">
|
||||
and receipt_type_id = #{id}
|
||||
</if>
|
||||
</update>
|
||||
|
||||
</mapper>
|
||||
|
||||
@@ -27,6 +27,30 @@
|
||||
</if>
|
||||
</select>
|
||||
|
||||
<select id="getRecordExistTypeCount">
|
||||
select count(1) as count,
|
||||
ds.input_type,
|
||||
rhr.file_id,
|
||||
COALESCE(fc.file_id_count, 0) as file_id_count
|
||||
from daily_income ds
|
||||
<include refid="app.module.core.usecase.daily.record.adapters.persistence.DailyRecordMapper.InnerJoinByDailyRecord"/>
|
||||
left join
|
||||
receipt_has_record rhr on ds.id = rhr.receipt_type_id and rhr.is_deleted = false
|
||||
left join (
|
||||
select file_id,COUNT(*) AS file_id_count
|
||||
FROM receipt_has_record
|
||||
where is_deleted = false and type = 'INCOME'
|
||||
group by file_id
|
||||
) fc on fc.file_id = rhr.file_id
|
||||
where ds.is_deleted = false
|
||||
<if test="id != null and id != '0'">
|
||||
and ds.id = #{id}
|
||||
</if>
|
||||
group by ds.id,rhr.file_id,fc.file_id_count
|
||||
</select>
|
||||
|
||||
|
||||
|
||||
<insert id="createDailyIncome" useGeneratedKeys="true" keyProperty="id">
|
||||
insert into daily_income (
|
||||
daily_record_seq, input_type, category, expense_mark
|
||||
@@ -70,4 +94,13 @@
|
||||
</if>
|
||||
</update>
|
||||
|
||||
<update id="removeDailyIncomeReceipt">
|
||||
update receipt_has_record set
|
||||
<include refid="common.fields.DeleteBaseSet"/>
|
||||
where is_deleted = false
|
||||
<if test="id != null and id != ''">
|
||||
and receipt_type_id = #{id}
|
||||
</if>
|
||||
</update>
|
||||
|
||||
</mapper>
|
||||
|
||||
@@ -272,14 +272,17 @@
|
||||
);
|
||||
},
|
||||
addFileInput() {
|
||||
// 새로운 input type="file" 요소 생성
|
||||
// 여기에서 파일 첨부 몇개인지 확인하고 하루 3개,,,, 총 6개씩,,,
|
||||
|
||||
|
||||
|
||||
const fileInput = document.createElement('input');
|
||||
fileInput.type = 'file';
|
||||
fileInput.accept = 'image/*';
|
||||
fileInput.style.display = 'none';
|
||||
fileInput.name = 'file';
|
||||
|
||||
// 파일 선택 시 이벤트 처리
|
||||
// 파일 선택
|
||||
fileInput.addEventListener('change', (event) => {
|
||||
const files = event.target.files;
|
||||
const previewContainer = $('#file-upload-container');
|
||||
@@ -330,7 +333,6 @@
|
||||
`);
|
||||
previewContainer.append(fileItem);
|
||||
|
||||
// 파일 정보와 서버 응답 저장
|
||||
uploadedReceipt.push({
|
||||
fileId: fileItemId,
|
||||
file: file,
|
||||
|
||||
Reference in New Issue
Block a user