From d786d7525776c10c09c8e6a394e14bc90646010b Mon Sep 17 00:00:00 2001 From: "cyril.tsui" Date: Tue, 30 Jul 2024 15:55:48 +0800 Subject: [PATCH] Add import OS Timesheets function --- .../timesheet/service/TimesheetsService.kt | 69 +++++++++++++++++++ .../timesheet/web/TimesheetsController.kt | 37 ++++++++-- 2 files changed, 100 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/ffii/tsms/modules/timesheet/service/TimesheetsService.kt b/src/main/java/com/ffii/tsms/modules/timesheet/service/TimesheetsService.kt index 96f7320..9abe991 100644 --- a/src/main/java/com/ffii/tsms/modules/timesheet/service/TimesheetsService.kt +++ b/src/main/java/com/ffii/tsms/modules/timesheet/service/TimesheetsService.kt @@ -249,6 +249,75 @@ open class TimesheetsService( return if (sheet.lastRowNum > 0) "Import Excel success btw " + notExistProjectList.distinct().joinToString(", ") else "Import Excel failure" } + @Transactional(rollbackFor = [Exception::class]) + open fun importOSFile(workbook: Workbook?): String { + val logger = LogFactory.getLog(javaClass) + + if (workbook == null) { + return "No Excel import" // if workbook is null + } + + val notExistStaffIdList = mutableListOf() + val sheet: Sheet = workbook.getSheetAt(0) + + logger.info("---------Start Import OS Timesheets-------") + val timesheetList = mutableListOf().toMutableList(); + for (i in 1.. 8.0) 8.0 else hours + val otHours = if (hours > 8.0) hours - 8.0 else 0.0 + + timesheetList += Timesheet().apply { + this.staff = staff + this.recordDate = recordDate + this.normalConsumed = normalHours + this.otConsumed = otHours + this.projectTask = projectTask + this.project = project + this.nonBillableTask = nonBillableTask + } + } + } + + timesheetRepository.saveAll(timesheetList) + logger.info("---------end-------") + + return if (sheet.lastRowNum > 0) "Import Excel success btw staffId:" + notExistStaffIdList.distinct().joinToString(", ") else "Import Excel failure" + } + @Transactional(rollbackFor = [Exception::class]) open fun rearrangeTimesheets(): String { val logger = LogFactory.getLog(javaClass) diff --git a/src/main/java/com/ffii/tsms/modules/timesheet/web/TimesheetsController.kt b/src/main/java/com/ffii/tsms/modules/timesheet/web/TimesheetsController.kt index 4aa049e..74ddeaa 100644 --- a/src/main/java/com/ffii/tsms/modules/timesheet/web/TimesheetsController.kt +++ b/src/main/java/com/ffii/tsms/modules/timesheet/web/TimesheetsController.kt @@ -3,6 +3,7 @@ package com.ffii.tsms.modules.timesheet.web import com.ffii.core.exception.BadRequestException import com.ffii.tsms.modules.timesheet.entity.LeaveType import com.ffii.tsms.modules.timesheet.service.LeaveService +import com.ffii.tsms.modules.timesheet.service.MailReminderService import com.ffii.tsms.modules.timesheet.service.TimesheetsService import com.ffii.tsms.modules.timesheet.web.models.* import jakarta.servlet.http.HttpServletRequest @@ -11,18 +12,16 @@ import org.apache.poi.ss.usermodel.Workbook import org.apache.poi.xssf.usermodel.XSSFWorkbook import org.springframework.http.ResponseEntity import org.springframework.web.bind.ServletRequestBindingException -import org.springframework.web.bind.annotation.GetMapping -import org.springframework.web.bind.annotation.PostMapping -import org.springframework.web.bind.annotation.RequestBody -import org.springframework.web.bind.annotation.RequestMapping -import org.springframework.web.bind.annotation.RestController +import org.springframework.web.bind.annotation.* import org.springframework.web.multipart.MultipartHttpServletRequest +import java.io.UnsupportedEncodingException +import java.text.ParseException import java.time.LocalDate import java.time.format.DateTimeFormatter @RestController @RequestMapping("/timesheets") -class TimesheetsController(private val timesheetsService: TimesheetsService, private val leaveService: LeaveService) { +class TimesheetsController(private val timesheetsService: TimesheetsService, private val leaveService: LeaveService, private val mailReminderService: MailReminderService) { @PostMapping("/save") fun newTimesheetEntry(@Valid @RequestBody recordTimesheet: Map>): Map> { val parsedEntries = kotlin.runCatching { @@ -137,6 +136,22 @@ class TimesheetsController(private val timesheetsService: TimesheetsService, pri return ResponseEntity.ok(timesheetsService.importFile(workbook)) } + @PostMapping("/import-OS") + @Throws(ServletRequestBindingException::class) + fun importOSFile(request: HttpServletRequest): ResponseEntity<*> { + var workbook: Workbook? = null + + try { + val multipartFile = (request as MultipartHttpServletRequest).getFile("multipartFileList") + workbook = XSSFWorkbook(multipartFile?.inputStream) + } catch (e: Exception) { + println("Excel Wrong") + println(e) + } + + return ResponseEntity.ok(timesheetsService.importOSFile(workbook)) + } + @PostMapping("/import-leave") @Throws(ServletRequestBindingException::class) fun importLeaveFile(request: HttpServletRequest): ResponseEntity<*> { @@ -159,4 +174,14 @@ class TimesheetsController(private val timesheetsService: TimesheetsService, pri return ResponseEntity.ok(timesheetsService.rearrangeTimesheets()) } + +// @GetMapping("/testReminder") +// @Throws( +// UnsupportedEncodingException::class, +// ParseException::class, +// ServletRequestBindingException::class +// ) +// fun testReminder(request: HttpServletRequest) { +// mailReminderService.sendTimesheetReminder() +// } } \ No newline at end of file