Skip to content

Commit

Permalink
chore: (#436) studyroom excel 신청 여러개인 경우 출력 오류
Browse files Browse the repository at this point in the history
  • Loading branch information
rlaisqls authored Apr 12, 2023
2 parents a3d5a8d + 250c00f commit 33bbc61
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ package team.aliens.dms.domain.studyroom.spi.vo
import java.util.UUID

class StudentSeatInfo(
val studentId: UUID,
val studentName: String,
val studentGrade: Int,
val studentClassRoom: Int,
val studentNumber: Int,
val seatFullName: String?,
val timeSlotId: UUID?
)
val studentName: String,
val seats: List<SeatInfo>?
) {
class SeatInfo(
val seatFullName: String?,
val timeSlotId: UUID?
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ import team.aliens.dms.domain.studyroom.spi.StudyRoomQuerySchoolPort
import team.aliens.dms.domain.studyroom.spi.StudyRoomQueryStudentPort
import team.aliens.dms.domain.studyroom.spi.StudyRoomQueryUserPort
import team.aliens.dms.domain.studyroom.spi.StudyRoomSecurityPort
import team.aliens.dms.domain.studyroom.spi.vo.StudentSeatApplicationVO
import team.aliens.dms.domain.studyroom.spi.vo.StudentSeatInfo
import team.aliens.dms.domain.user.exception.UserNotFoundException
import java.time.LocalDateTime
import java.util.UUID

@ReadOnlyUseCase
class ExportStudyRoomApplicationStatusUseCase(
Expand All @@ -33,23 +35,21 @@ class ExportStudyRoomApplicationStatusUseCase(
val manager = queryUserPort.queryUserById(currentUserId) ?: throw UserNotFoundException

val students = queryStudentPort.queryStudentsBySchoolId(manager.schoolId)
val studentSeatApplicationsMap = queryStudyRoomPort.querySeatApplicationsByStudentIdIn(
studentIds = students.map { it.id }
).associateBy { it.studentId }
val studentSeatApplicationsMap = getStudentSeatApplicationsMap(students)

val studentSeats = students.map { student ->
val seat = studentSeatApplicationsMap[student.id]
StudentSeatInfo(
studentId = student.id,
studentName = student.name,
studentGrade = student.grade,
studentClassRoom = student.classRoom,
studentNumber = student.number,
seatFullName = seat?.let {
StudyRoom.precessName(it.studyRoomFloor, it.studyRoomName) +
" " + Seat.processName(it.seatNumber, it.seatTypeName)
},
timeSlotId = seat?.timeSlotId
studentName = student.name,
seats = studentSeatApplicationsMap[student.id]?.map {
StudentSeatInfo.SeatInfo(
seatFullName = StudyRoom.precessName(it.studyRoomFloor, it.studyRoomName) +
" " + Seat.processName(it.seatNumber, it.seatTypeName),
timeSlotId = it.timeSlotId
)
}
)
}

Expand All @@ -62,6 +62,16 @@ class ExportStudyRoomApplicationStatusUseCase(
)
}

private fun getStudentSeatApplicationsMap(students: List<Student>) =
mutableMapOf<UUID, MutableList<StudentSeatApplicationVO>>().apply {
queryStudyRoomPort.querySeatApplicationsByStudentIdIn(
studentIds = students.map { it.id }
).map {
get(it.studentId)
?.run { this.add(it) } ?: put(it.studentId, mutableListOf(it))
}
}

private fun getStudyRoomApplicationStatusFile(
file: java.io.File?,
timeSlots: List<TimeSlot>,
Expand All @@ -71,10 +81,7 @@ class ExportStudyRoomApplicationStatusUseCase(
baseFile = file,
timeSlots = timeSlots,
studentSeatsMap = studentSeats.associateBy {
Pair(
Student.processGcn(it.studentGrade, it.studentClassRoom, it.studentNumber),
it.studentName
)
Pair(Student.processGcn(it.studentGrade, it.studentClassRoom, it.studentNumber), it.studentName)
}
)
} ?: writeFilePort.writeStudyRoomApplicationStatusExcelFile(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package team.aliens.dms.domain.studyroom.usecase

import java.util.UUID
import team.aliens.dms.common.annotation.ReadOnlyUseCase
import team.aliens.dms.domain.school.validateSameSchool
import team.aliens.dms.domain.student.exception.StudentNotFoundException
Expand All @@ -13,6 +12,7 @@ import team.aliens.dms.domain.studyroom.spi.StudyRoomQueryStudentPort
import team.aliens.dms.domain.studyroom.spi.StudyRoomQueryUserPort
import team.aliens.dms.domain.studyroom.spi.StudyRoomSecurityPort
import team.aliens.dms.domain.user.exception.UserNotFoundException
import java.util.UUID

@ReadOnlyUseCase
class StudentQueryStudyRoomsUseCase(
Expand Down Expand Up @@ -41,10 +41,10 @@ class StudentQueryStudyRoomsUseCase(
availableGrade = it.availableGrade,
availableSex = it.availableSex,
inUseHeadcount = it.inUseHeadcount,
totalAvailableSeat = it.totalAvailableSeat,
isMine = appliedSeat?.studyRoomId == it.id
)
}
totalAvailableSeat = it.totalAvailableSeat,
isMine = appliedSeat?.studyRoomId == it.id
)
}

return StudentQueryStudyRoomsResponse(
studyRooms = studyRooms
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package team.aliens.dms.domain.studyroom.usecase
import io.mockk.every
import io.mockk.mockk
import io.mockk.slot
import java.io.File
import java.util.UUID
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertAll
Expand All @@ -21,8 +23,6 @@ import team.aliens.dms.domain.studyroom.spi.vo.StudentSeatInfo
import team.aliens.dms.domain.studyroom.stub.createTimeSlotStub
import team.aliens.dms.domain.user.exception.UserNotFoundException
import team.aliens.dms.domain.user.stub.createUserStub
import java.io.File
import java.util.UUID

class ExportStudyRoomStudentsApplicationStatusUseCaseTests {

Expand Down Expand Up @@ -115,7 +115,7 @@ class ExportStudyRoomStudentsApplicationStatusUseCaseTests {
{ assertEquals(studentSeatInfo.studentGrade, studentStub.grade) },
{ assertEquals(studentSeatInfo.studentClassRoom, studentStub.classRoom) },
{ assertEquals(studentSeatInfo.studentNumber, studentStub.number) },
{ assertEquals(studentSeatInfo.timeSlotId, studentSeatApplicationVOStub.timeSlotId) },
{ assertEquals(studentSeatInfo.seats?.get(0)?.timeSlotId, studentSeatApplicationVOStub.timeSlotId) },
{ assert(response.fileName.startsWith("${schoolStub.name.replace(" ", "")}_자습실_신청상태_")) }
)
}
Expand Down Expand Up @@ -152,7 +152,7 @@ class ExportStudyRoomStudentsApplicationStatusUseCaseTests {
{ assertEquals(studentSeatInfo.studentGrade, studentStub.grade) },
{ assertEquals(studentSeatInfo.studentClassRoom, studentStub.classRoom) },
{ assertEquals(studentSeatInfo.studentNumber, studentStub.number) },
{ assertEquals(studentSeatInfo.timeSlotId, studentSeatApplicationVOStub.timeSlotId) },
{ assertEquals(studentSeatInfo.seats?.get(0)?.timeSlotId, studentSeatApplicationVOStub.timeSlotId) },
{ assert(response.fileName.startsWith("${schoolStub.name.replace(" ", "")}_자습실_신청상태_")) }
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package team.aliens.dms.thirdparty.parser

import java.io.ByteArrayOutputStream
import java.io.File
import java.time.format.DateTimeFormatter
import java.util.UUID
import org.apache.poi.hssf.usermodel.HSSFWorkbook
import org.apache.poi.ss.usermodel.Cell
import org.apache.poi.ss.usermodel.CellStyle
Expand Down Expand Up @@ -27,9 +31,6 @@ import team.aliens.dms.domain.studyroom.model.TimeSlot
import team.aliens.dms.domain.studyroom.spi.vo.StudentSeatInfo
import team.aliens.dms.thirdparty.parser.exception.ExcelExtensionMismatchException
import team.aliens.dms.thirdparty.parser.exception.ExcelInvalidFileException
import java.io.ByteArrayOutputStream
import java.io.File
import java.time.format.DateTimeFormatter

@Component
class ExcelAdapter : ParseFilePort, WriteFilePort {
Expand All @@ -47,6 +48,7 @@ class ExcelAdapter : ParseFilePort, WriteFilePort {

val excelData = row.run {
VerifiedStudent(
id = UUID.randomUUID(),
schoolName = schoolName,
gcn = Student.processGcn(
grade = getIntValue(0),
Expand Down Expand Up @@ -157,7 +159,7 @@ class ExcelAdapter : ParseFilePort, WriteFilePort {
val row = worksheet.getRow(i)
if (row.isFirstCellBlank()) continue

val studentSeat = row.run {
val studentSeats = row.run {
try {
studentSeatsMap[
Pair(
Expand All @@ -169,16 +171,13 @@ class ExcelAdapter : ParseFilePort, WriteFilePort {
e.printStackTrace()
throw BadExcelFormatException
}
}
}?.seats

val timeSlotIdx = studentSeat?.timeSlotId?.let {
timeSlots.indexOfFirst { it.id == studentSeat.timeSlotId }
}
insertDatasAtRow(
row = row,
startIdx = columnCount,
datas = (timeSlots.indices).map {
if (it == timeSlotIdx) studentSeat.seatFullName else null
datas = timeSlots.map { timeSlot ->
studentSeats?.singleOrNull { it.timeSlotId == timeSlot.id }?.seatFullName
},
style = getDefaultCellStyle(workbook)
)
Expand Down Expand Up @@ -215,22 +214,21 @@ class ExcelAdapter : ParseFilePort, WriteFilePort {
): ByteArray {
val attributes = listOf("학년", "", "번호", "이름", *timeSlots.map { it.name }.toTypedArray())

val remainInfosList = studentSeats.map { studentSeat ->
val timeSlotIdx = studentSeat.timeSlotId?.let { timeSlots.indexOfFirst { ts -> ts.id == it } }
val seatInfosList = studentSeats.map { studentSeat ->
listOf(
studentSeat.studentGrade.toString(),
studentSeat.studentClassRoom.toString(),
studentSeat.studentNumber.toString(),
studentSeat.studentName,
*(timeSlots.indices)
.map { i -> if (i == timeSlotIdx) studentSeat.seatFullName else null }
.toTypedArray(),
*timeSlots.map { timeSlot ->
studentSeat.seats?.singleOrNull { it.timeSlotId == timeSlot.id }?.seatFullName
}.toTypedArray(),
)
}

return createExcelSheet(
attributes = attributes,
datasList = remainInfosList
datasList = seatInfosList
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package team.aliens.dms.thirdparty.parser
import io.mockk.every
import io.mockk.slot
import io.mockk.spyk
import java.time.LocalDateTime
import java.time.LocalTime
import java.util.UUID
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertAll
Expand All @@ -12,9 +15,6 @@ import team.aliens.dms.domain.remain.dto.StudentRemainInfo
import team.aliens.dms.domain.student.model.Sex
import team.aliens.dms.domain.studyroom.model.TimeSlot
import team.aliens.dms.domain.studyroom.spi.vo.StudentSeatInfo
import java.time.LocalDateTime
import java.time.LocalTime
import java.util.UUID

class ExcelAdapterTests {

Expand Down Expand Up @@ -119,13 +119,16 @@ class ExcelAdapterTests {
private val studentSeatInfosStub by lazy {
listOf(
StudentSeatInfo(
studentId = UUID.randomUUID(),
studentName = "",
studentGrade = 1,
studentClassRoom = 2,
studentNumber = 3,
seatFullName = "",
timeSlotId = timeSlotStub.id
seats = listOf(
StudentSeatInfo.SeatInfo(
seatFullName = "",
timeSlotId = UUID.randomUUID()
)
)
)
)
}
Expand Down

0 comments on commit 33bbc61

Please sign in to comment.