package com.kri.contact.server.controller; import static com.kri.contact.server.common.util.ExcelResponseBuilder.buildExcelResponse; import com.kri.common.feature.datetime.DateTimeUtil; import com.kri.common.feature.web.RequestUserContext; import com.kri.contact.server.common.consts.ContactConstant; import com.kri.contact.server.dto.common.DataVerificationDto; import com.kri.contact.server.dto.excel.ExcelUploadResultDto; import com.kri.contact.server.dto.systemlog.UploadContentDto; import com.kri.contact.server.dto.user.UserInfoDto; import com.kri.contact.server.service.common.SystemEventLogService; import com.kri.contact.server.service.UserService; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; import jakarta.validation.Valid; import java.time.LocalDateTime; import java.util.Collections; import java.util.List; import lombok.RequiredArgsConstructor; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PatchMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; @RestController @RequestMapping("/api/v1/user") @RequiredArgsConstructor @Tag(name = "User", description = "사용자 관리 API") public class UserController { private final UserService userService; private final SystemEventLogService systemEventLogService; @Operation(summary = "ID 가용 여부 검사") @GetMapping("/check-user-id") public DataVerificationDto checkUserId(@RequestParam String userId) { return userService.checkUserId(userId); } @Operation(summary = "사용자 정보 조회") @GetMapping("/{userId}") public UserInfoDto getUser(@PathVariable String userId) { return userService.getUser(userId); } @Operation(summary = "사용자 목록 조회") @GetMapping() public List getUserList(RequestUserContext ctx) { String searchCmpn = ctx.getUserCmpn(); if (ctx.getUserRole() < ContactConstant.OUTSOURCE_USER_ROLE) { return userService.getUserList(); } else { return userService.getFieldworkUserListWithCmpn(searchCmpn); } } @Operation(summary = "컨택트 면접원 목록 조회") @GetMapping("fieldwork") public List getFiledoworkUserList(RequestUserContext ctx) { String searchCmpn = ctx.getUserCmpn(); if (ctx.getUserRole() < ContactConstant.OUTSOURCE_USER_ROLE) { searchCmpn = ""; } return userService.getFieldworkUserListWithCmpn(searchCmpn); } @Operation(summary = "외주 목록 조회") @GetMapping("outsource") List getOutsourceList() { return userService.getOutsourceList(); } @Operation(summary = "사용자 회사 목록 조회") @GetMapping("cmpn") public List getUserCmpnList(RequestUserContext ctx) { if (ctx.getUserRole() < ContactConstant.OUTSOURCE_USER_ROLE) { return userService.getUserCmpnList(); } return Collections.emptyList(); } @Operation(summary = "사용자 생성") @PutMapping public void createUser( @Valid @RequestBody UserInfoDto userInfo, RequestUserContext ctx ) { String regId = ctx.getUserId(); String clientIp = ctx.getClientIp(); final LocalDateTime now = DateTimeUtil.nowKstLocalDateTime(); userService.createUser(userInfo, regId, now); systemEventLogService.writeCreateUserLog(regId, now, clientIp, userInfo); } @Operation( summary = "사용자 일괄 생성(엑셀 업로드)", description = "성공하면 JSON, 실패가 있으면 실패 내역 엑셀(xlsx) 반환" ) @PostMapping(value = "/excel/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) public ExcelUploadResultDto createUserFromExcel( @RequestParam("file") MultipartFile file, RequestUserContext ctx ) { String regId = ctx.getUserId(); String clientIp = ctx.getClientIp(); final LocalDateTime now = DateTimeUtil.nowKstLocalDateTime(); String forceCmpn = ctx.getUserRole() < ContactConstant.OUTSOURCE_USER_ROLE ? "" : ctx.getUserCmpn(); ExcelUploadResultDto result = userService.createUsersFromExcel(file, regId, now, forceCmpn); systemEventLogService.writeUploadUserLog(regId, now, clientIp, UploadContentDto.from(file)); return result; } @Operation(summary = "사용자 일괄 생성 양식 다운로드") @PostMapping("/excel/template") public ResponseEntity exportUserTemplate() { byte[] bytes = userService.exportUserTemplate(); return buildExcelResponse("user_upload_template.xlsx", bytes); } @Operation(summary = "사용자 정보 수정") @PatchMapping() public void updateUser( @Valid @RequestBody UserInfoDto dto, RequestUserContext ctx ) { String modId = ctx.getUserId(); String clientIp = ctx.getClientIp(); final LocalDateTime now = DateTimeUtil.nowKstLocalDateTime(); userService.updateUser(dto, modId, now); systemEventLogService.writeUpdateUserLog(modId, now, clientIp, dto); } @Operation(summary = "사용자 삭제(소프트 삭제)") @DeleteMapping("/{userId}") public void deleteUser(@PathVariable String userId, RequestUserContext ctx) { String regId = ctx.getUserId(); String clientIp = ctx.getClientIp(); final LocalDateTime now = DateTimeUtil.nowKstLocalDateTime(); userService.deleteUser(userId, regId); systemEventLogService.writeDeleteUserLog(regId, now, clientIp, userId); } }