관리자 엑셀 파일 업로드 추가 (모달창 퍼블 수정 필요,,,)
This commit is contained in:
@@ -17,7 +17,7 @@ class AdminIdDuplicateCheckPostController {
|
||||
private final CheckAdminIdPortIn checkAdminIdPortIn;
|
||||
|
||||
@GetMapping("/api/settings/admin/idCheck")
|
||||
ResponseEntity<?> updateMappingHousehold(@RequestParam String id ) {
|
||||
ResponseEntity<?> selectIdCheck(@RequestParam String id ) {
|
||||
return ResponseEntity.status(HttpStatus.OK).body(checkAdminIdPortIn.connect(id));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
package app.module.admin.usecase.admin.excelSaveAdmin.adapters.web;
|
||||
|
||||
import app.module.core.usecase.admin.excelSaveAdmin.ports.ExcelSaveAdminPortIn;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
@Slf4j
|
||||
@RestController
|
||||
@RequiredArgsConstructor
|
||||
class ExcelSaveAdminController {
|
||||
|
||||
private final ExcelSaveAdminPortIn excelSaveAdminPortIn;
|
||||
|
||||
@PostMapping("/api/settings/admin/excelUpload")
|
||||
ResponseEntity<?> excelUploadSaveAdmin(@RequestParam("file") MultipartFile file ) throws IOException {
|
||||
return ResponseEntity.status(HttpStatus.OK).body(excelSaveAdminPortIn.operate(file));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package app.module.admin.usecase.admin.excelSaveAdmin.adapters.web.model;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class ExcelAdminCreateVO {
|
||||
private String id;
|
||||
private String password;
|
||||
private String name;
|
||||
private String phone;
|
||||
private String regionMainCode;
|
||||
private String roleId;
|
||||
}
|
||||
@@ -7,7 +7,7 @@
|
||||
<form method="post" name="adminParamForm" onsubmit="goSearch();"
|
||||
th:action="@{/settings/admins}"
|
||||
th:object="${listStatus}">
|
||||
|
||||
<div th:replace="admin/modal/modal-admin-excel:: modalFragment"></div>
|
||||
<!-- hidden parameters -->
|
||||
<input name="page" th:value="${listOutput.pageable.pageNumber}" type="hidden"/>
|
||||
<input name="sort" th:value="${listOutput.pageable.sort}" type="hidden"/>
|
||||
@@ -128,6 +128,9 @@
|
||||
|
||||
<div class="component-btns bottom-btn-group">
|
||||
<div class="btns-row align-right">
|
||||
<button class="btn" onclick="showAdminExcelUploadModal();" type="button">
|
||||
<span class="btn-txt">엑셀 업로드</span>
|
||||
</button>
|
||||
<button class="btn" onclick="goExcel();" type="button"><span class="btn-txt">엑셀 다운로드</span></button>
|
||||
<button class="btn" onclick="location.href='/settings/admins/create';" type="button"><span class="btn-txt">등록</span></button>
|
||||
<button class="btn" onclick="goRemove();" type="button"><span class="btn-txt">삭제</span></button>
|
||||
@@ -135,15 +138,10 @@
|
||||
</div>
|
||||
</section>
|
||||
</form>
|
||||
|
||||
</section>
|
||||
|
||||
<section layout:fragment="script">
|
||||
<!-- <script th:inline="javascript">
|
||||
// 목록 정렬 표기 및 갱신 기능 적용
|
||||
uiUtil.applyListSortNotation([[${listOutput.pageable.pageSize}]],
|
||||
[[${listOutput.pageable.sort.toString()}]]);
|
||||
</script>-->
|
||||
|
||||
<script th:inline="javascript">
|
||||
// 검색
|
||||
function goSearch() {
|
||||
@@ -211,6 +209,51 @@
|
||||
f.method = 'post';
|
||||
f.submit();
|
||||
}
|
||||
|
||||
|
||||
function showAdminExcelUploadModal() {
|
||||
const modal = document.querySelector(".modal02");
|
||||
modal.ui.open();
|
||||
|
||||
}
|
||||
const uploadAdminExcel = () => {
|
||||
event.preventDefault(); // 폼 기본 제출 방지
|
||||
|
||||
const fileInput = document.getElementById('file');
|
||||
|
||||
if (fileInput.files.length === 0) {
|
||||
alert('업로드할 엑셀 파일을 선택해주세요.');
|
||||
return;
|
||||
}
|
||||
|
||||
let formData = new FormData();
|
||||
formData.append('file', fileInput.files[0]);
|
||||
|
||||
const csrfToken = $("meta[name='_csrf']").attr("content");
|
||||
const csrfHeader = $("meta[name='_csrf_header']").attr("content");
|
||||
|
||||
console.log('FormData:', formData);
|
||||
debugger
|
||||
$.ajax({
|
||||
method: 'POST',
|
||||
url: '/api/settings/admin/excelUpload',
|
||||
data: formData,
|
||||
contentType: false, // * 중요 *
|
||||
processData: false, // * 중요 *
|
||||
enctype: 'multipart/form-data', // * 중요 *
|
||||
beforeSend: function (xhr) {
|
||||
xhr.setRequestHeader(
|
||||
$("meta[name='_csrf_header']").attr("content"),
|
||||
$("meta[name='_csrf']").attr("content"));
|
||||
},
|
||||
success(res) {
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
const closeModal = () => {
|
||||
document.querySelector(".modal02").ui.close();
|
||||
};
|
||||
</script>
|
||||
</section>
|
||||
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
<!DOCTYPE html>
|
||||
<html xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" xmlns:th="http://www.thymeleaf.org"
|
||||
layout:decorate="~{layout/layout}">
|
||||
|
||||
<div class="layer-wrap" th:fragment="modalFragment">
|
||||
<div class="component-modal modal02" id="test" data-props-dimm-click="true" data-state="close">
|
||||
<div class="modal-dimm"></div>
|
||||
<div class="modal-frame">
|
||||
<div class="modal-container">
|
||||
<div class="modal-header">
|
||||
<h3 class="modal-tit" >엑셀 업로드</h3>
|
||||
</div>
|
||||
<div class="modal-content">
|
||||
<div class="modal-content-inner">
|
||||
<section>
|
||||
<form autocomplete="off" method="post" name="AdminExcelReqForm" id ="AdminExcelReqForm" enctype="multipart/form-data" action="/api/settings/admin/excelUpload">
|
||||
<label for="file" class="input-file-btn">
|
||||
<input type="file" name="file" id="file" accept=".xlsx, .xls">
|
||||
</label>
|
||||
<div class="component-btns bottom-btn-group" th:fragment="modalButtons">
|
||||
<div class="btns-row align-right">
|
||||
<button type="button" class="btn btn-primary" onclick="uploadAdminExcel()">
|
||||
<span class="btn-txt">등록</span>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
</section>
|
||||
</div>
|
||||
<button class="modal-close" onclick="closeModal()"></button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
<section layout:fragment="script">
|
||||
<script th:inline="javascript">
|
||||
|
||||
</script>
|
||||
</section>
|
||||
</div>
|
||||
</html>
|
||||
@@ -0,0 +1,110 @@
|
||||
package app.module.core.usecase.admin.excelSaveAdmin.adapters.persistence;
|
||||
|
||||
import app.module.core.config.application.util.AuditUtil;
|
||||
import app.module.core.usecase.admin.checkAdminId.persistence.CheckAdminIdMapper;
|
||||
import app.module.core.usecase.admin.excelSaveAdmin.adapters.persistence.model.ExcelAdminCreateVO;
|
||||
import app.module.core.usecase.admin.excelSaveAdmin.adapters.persistence.model.ExcelResultVO;
|
||||
import app.module.core.usecase.admin.excelSaveAdmin.ports.ExcelSaveAdminPortOut;
|
||||
import app.module.core.usecase.admin.saveAdmin.adapters.persistence.SaveAdminMapper;
|
||||
import app.module.core.usecase.admin.saveAdmin.adapters.persistence.model.SaveAdminHasRoleParam;
|
||||
import app.module.core.usecase.admin.saveAdmin.ports.application.domain.SaveAdminCmd;
|
||||
import app.module.core.usecase.uicode.list.adapters.persistence.UICodeListMapper;
|
||||
import app.module.core.usecase.uicode.list.ports.application.domain.UICodeElement;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.apache.poi.ss.usermodel.*;
|
||||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
class ExcelSaveAdminIdDao implements ExcelSaveAdminPortOut {
|
||||
|
||||
private final CheckAdminIdMapper checkAdminIdMapper;
|
||||
private final SaveAdminMapper saveAdminMapper;
|
||||
private final BCryptPasswordEncoder passwordEncoder;
|
||||
private final UICodeListMapper uiCodeListMapper;
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public ExcelResultVO operate(MultipartFile file) throws IOException {
|
||||
// 엑셀 파일 읽기
|
||||
Workbook workbook = WorkbookFactory.create(file.getInputStream());
|
||||
Sheet sheet = workbook.getSheetAt(0);
|
||||
Integer success = 0;
|
||||
List<UICodeElement> uiCodeElementList = uiCodeListMapper.findByParentId("code.region.main");
|
||||
// 첫 번째 행은 헤더로 간주하고 스킵
|
||||
for (int i = 1; i <= sheet.getLastRowNum(); i++) {
|
||||
Row row = sheet.getRow(i);
|
||||
if (row == null) continue;
|
||||
|
||||
ExcelAdminCreateVO admin = new ExcelAdminCreateVO();
|
||||
admin.setRoleId(getRoleId(row.getCell(0))); // 권한
|
||||
admin.setRegionMainCode(getRegionMainCode(row.getCell(1),uiCodeElementList)); // 실사
|
||||
admin.setId(getCellValue(row.getCell(2))); // ID
|
||||
admin.setPassword(getCellValue(row.getCell(3))); // 비밀번호
|
||||
admin.setName(getCellValue(row.getCell(4))); // 이름
|
||||
admin.setPhone(getCellValue(row.getCell(5))); // 전화번호
|
||||
|
||||
Integer duplicate = checkAdminIdMapper.checkAdminId(admin.getId());
|
||||
|
||||
if(duplicate == 0) {
|
||||
SaveAdminCmd cmd = new SaveAdminCmd(
|
||||
null,
|
||||
admin.getId(),
|
||||
passwordEncoder.encode(admin.getPassword()),
|
||||
LocalDateTime.now(),
|
||||
admin.getName(),
|
||||
admin.getPhone(),
|
||||
admin.getRegionMainCode(),
|
||||
false,
|
||||
0,
|
||||
admin.getRoleId()
|
||||
);
|
||||
success += saveAdminMapper.insertAdmin(AuditUtil.create(cmd));
|
||||
|
||||
if (!admin.getRoleId().isEmpty()) {
|
||||
saveAdminMapper.insertAdminHasRoles(
|
||||
AuditUtil.create(new SaveAdminHasRoleParam(cmd.getSeq(), admin.getRoleId())));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
workbook.close();
|
||||
return new ExcelResultVO(sheet.getLastRowNum()-1 , success);
|
||||
}
|
||||
|
||||
// 셀 값을 문자열로 변환
|
||||
private String getCellValue(Cell cell) {
|
||||
if (cell == null) return "";
|
||||
return switch (cell.getCellType()) {
|
||||
case STRING -> cell.getStringCellValue();
|
||||
case NUMERIC -> String.valueOf((long) cell.getNumericCellValue());
|
||||
default -> "";
|
||||
};
|
||||
}
|
||||
|
||||
private String getRoleId(Cell cell){
|
||||
if (cell == null) return "";
|
||||
return switch (cell.getStringCellValue().trim()) {
|
||||
case "관리자" -> "ROLE_ADMIN";
|
||||
case "지방실사" -> "ROLE_LOCAL_INSPECTOR";
|
||||
case "조사원" -> "ROLE_RESEARCHER";
|
||||
default -> "";
|
||||
};
|
||||
}
|
||||
|
||||
private String getRegionMainCode(Cell cell,List<UICodeElement> uiCodeElementList){
|
||||
if (cell == null) return "";
|
||||
|
||||
return uiCodeElementList.stream().filter(code -> code.getCodeText().equals(cell.getStringCellValue().trim()))
|
||||
.findFirst()
|
||||
.map(UICodeElement::getId).orElse("");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package app.module.core.usecase.admin.excelSaveAdmin.adapters.persistence.model;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class ExcelAdminCreateVO {
|
||||
private Long seq;
|
||||
private String id;
|
||||
private String password;
|
||||
private String name;
|
||||
private String phone;
|
||||
private String regionMainCode;
|
||||
private String roleId;
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package app.module.core.usecase.admin.excelSaveAdmin.adapters.persistence.model;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class ExcelResultVO {
|
||||
private Integer total;
|
||||
private Integer success;
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package app.module.core.usecase.admin.excelSaveAdmin.ports;
|
||||
|
||||
import app.module.core.usecase.admin.excelSaveAdmin.adapters.persistence.model.ExcelResultVO;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import java.io.IOException;
|
||||
|
||||
public interface ExcelSaveAdminPortIn {
|
||||
ExcelResultVO operate(MultipartFile file) throws IOException;
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package app.module.core.usecase.admin.excelSaveAdmin.ports;
|
||||
|
||||
import app.module.core.usecase.admin.excelSaveAdmin.adapters.persistence.model.ExcelResultVO;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public interface ExcelSaveAdminPortOut {
|
||||
|
||||
ExcelResultVO operate(MultipartFile file) throws IOException;
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package app.module.core.usecase.admin.excelSaveAdmin.ports.application;
|
||||
|
||||
import app.module.core.usecase.admin.excelSaveAdmin.adapters.persistence.model.ExcelResultVO;
|
||||
import app.module.core.usecase.admin.excelSaveAdmin.ports.ExcelSaveAdminPortIn;
|
||||
import app.module.core.usecase.admin.excelSaveAdmin.ports.ExcelSaveAdminPortOut;
|
||||
import app.module.core.usecase.admin.saveAdmin.ports.SaveAdminPortOut;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class ExcelSaveAdminService implements ExcelSaveAdminPortIn {
|
||||
|
||||
private final ExcelSaveAdminPortOut excelSaveAdminPortOut;
|
||||
|
||||
@Override
|
||||
public ExcelResultVO operate(MultipartFile file) throws IOException {
|
||||
|
||||
return excelSaveAdminPortOut.operate(file);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -7,7 +7,7 @@ import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
@Mapper
|
||||
public interface SaveAdminMapper {
|
||||
void insertAdmin(SaveAdminCmd cmd);
|
||||
Integer insertAdmin(SaveAdminCmd cmd);
|
||||
void updateAdmin(SaveAdminCmd cmd);
|
||||
void insertAdminHasRoles(SaveAdminHasRoleParam param);
|
||||
void deleteAdminRolesByAdminId(DeleteAdminHasRoleParam param);
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
|
||||
<mapper namespace="app.module.core.usecase.admin.saveAdmin.adapters.persistence.SaveAdminMapper">
|
||||
|
||||
<insert id="insertAdmin">
|
||||
<update id="insertAdmin">
|
||||
<selectKey keyProperty="seq" resultType="long" order="BEFORE">
|
||||
SELECT nextval('admin_seq')
|
||||
</selectKey>
|
||||
@@ -29,7 +29,7 @@
|
||||
#{accountDisabled},
|
||||
#{loginFailCnt},
|
||||
<include refid="common.fields.CreateUpdateBaseValues"/>)
|
||||
</insert>
|
||||
</update>
|
||||
|
||||
<update id="updateAdmin">
|
||||
/* updateAdmin : 관리자 정보 수정 */
|
||||
|
||||
Reference in New Issue
Block a user