|
|
@@ -7,16 +7,14 @@ import org.apache.poi.ss.usermodel.Sheet |
|
|
import org.apache.poi.ss.usermodel.Workbook |
|
|
import org.apache.poi.ss.usermodel.Workbook |
|
|
import org.apache.poi.xssf.usermodel.XSSFWorkbook |
|
|
import org.apache.poi.xssf.usermodel.XSSFWorkbook |
|
|
import org.springframework.stereotype.Component |
|
|
import org.springframework.stereotype.Component |
|
|
import java.io.File |
|
|
|
|
|
import java.io.FileInputStream |
|
|
|
|
|
import java.io.IOException |
|
|
|
|
|
import com.ffii.fpsms.modules.user.entity.User |
|
|
|
|
|
import com.ffii.fpsms.modules.user.entity.UserRepository |
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired |
|
|
import org.springframework.beans.factory.annotation.Autowired |
|
|
import org.springframework.security.crypto.password.PasswordEncoder |
|
|
import org.springframework.security.crypto.password.PasswordEncoder |
|
|
import org.springframework.transaction.annotation.Transactional |
|
|
import org.springframework.transaction.annotation.Transactional |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import com.ffii.fpsms.modules.user.entity.User |
|
|
|
|
|
import com.ffii.fpsms.modules.user.entity.UserRepository |
|
|
|
|
|
import java.io.File |
|
|
|
|
|
import java.io.FileInputStream |
|
|
|
|
|
import java.io.IOException |
|
|
|
|
|
|
|
|
@Component |
|
|
@Component |
|
|
open class UsersSetup { |
|
|
open class UsersSetup { |
|
|
@@ -30,13 +28,13 @@ open class UsersSetup { |
|
|
data class UserData( |
|
|
data class UserData( |
|
|
val staffNo: String, |
|
|
val staffNo: String, |
|
|
val name: String, |
|
|
val name: String, |
|
|
val title: String |
|
|
|
|
|
|
|
|
val title: String, |
|
|
|
|
|
val authorityCodes: Set<String> |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
@Transactional(rollbackFor = [Exception::class]) |
|
|
@Transactional(rollbackFor = [Exception::class]) |
|
|
open fun importExcelFromLocal(filePath: String): Int { |
|
|
open fun importExcelFromLocal(filePath: String): Int { |
|
|
val file = File(filePath) |
|
|
val file = File(filePath) |
|
|
|
|
|
|
|
|
if (!file.exists()) { |
|
|
if (!file.exists()) { |
|
|
throw IOException("File not found: $filePath") |
|
|
throw IOException("File not found: $filePath") |
|
|
} |
|
|
} |
|
|
@@ -48,62 +46,113 @@ open class UsersSetup { |
|
|
val sheet: Sheet = workbook.getSheetAt(0) |
|
|
val sheet: Sheet = workbook.getSheetAt(0) |
|
|
val userDataList = mutableListOf<UserData>() |
|
|
val userDataList = mutableListOf<UserData>() |
|
|
|
|
|
|
|
|
for (rowIndex in 1..sheet.lastRowNum) { |
|
|
|
|
|
val row: Row? = sheet.getRow(rowIndex) |
|
|
|
|
|
if (row != null) { |
|
|
|
|
|
val staffNoCell: Cell? = row.getCell(1) // Column B |
|
|
|
|
|
val nameCell: Cell? = row.getCell(2) // Column C |
|
|
|
|
|
val titleCell: Cell? = row.getCell(3) // Column D |
|
|
|
|
|
|
|
|
|
|
|
val staffNo = if (staffNoCell != null) { |
|
|
|
|
|
ExcelUtils.getStringValue(staffNoCell).trim() |
|
|
|
|
|
} else { |
|
|
|
|
|
"" |
|
|
|
|
|
|
|
|
// ──────────────────────────────────────────────── |
|
|
|
|
|
// Read header row (index 1) → map column index to authority code |
|
|
|
|
|
// ──────────────────────────────────────────────── |
|
|
|
|
|
val headerRow = sheet.getRow(1) ?: throw IllegalStateException("Header row (index 1) not found") |
|
|
|
|
|
val columnToAuthority = mutableMapOf<Int, String>() |
|
|
|
|
|
|
|
|
|
|
|
for (col in 6..30) { // adjust upper limit if more columns exist |
|
|
|
|
|
val cell = headerRow.getCell(col) |
|
|
|
|
|
if (cell != null) { |
|
|
|
|
|
val headerValue = ExcelUtils.getStringValue(cell).trim() |
|
|
|
|
|
if (headerValue.isNotEmpty()) { |
|
|
|
|
|
val authCode = headerValue.uppercase() // normalize to uppercase |
|
|
|
|
|
columnToAuthority[col] = authCode |
|
|
} |
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
val name = if (nameCell != null) { |
|
|
|
|
|
ExcelUtils.getStringValue(nameCell).trim() |
|
|
|
|
|
} else { |
|
|
|
|
|
"" |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
println("Detected ${columnToAuthority.size} authority columns from header row:") |
|
|
|
|
|
columnToAuthority.forEach { (col, code) -> |
|
|
|
|
|
println(" Column $col → Authority code: '$code'") |
|
|
|
|
|
} |
|
|
|
|
|
println("") |
|
|
|
|
|
|
|
|
val title = if (titleCell != null) { |
|
|
|
|
|
ExcelUtils.getStringValue(titleCell).trim() |
|
|
|
|
|
|
|
|
// ──────────────────────────────────────────────── |
|
|
|
|
|
// Process data rows (starting from row index 2) |
|
|
|
|
|
// ──────────────────────────────────────────────── |
|
|
|
|
|
for (rowIndex in 2..sheet.lastRowNum) { |
|
|
|
|
|
val row = sheet.getRow(rowIndex) ?: continue |
|
|
|
|
|
|
|
|
|
|
|
val staffNo = ExcelUtils.getStringValue(row.getCell(1)).trim() |
|
|
|
|
|
if (staffNo.isEmpty()) continue |
|
|
|
|
|
|
|
|
|
|
|
val name = ExcelUtils.getStringValue(row.getCell(2)).trim() |
|
|
|
|
|
val title = ExcelUtils.getStringValue(row.getCell(3)).trim() |
|
|
|
|
|
|
|
|
|
|
|
val authorityCodes = mutableSetOf<String>() |
|
|
|
|
|
|
|
|
|
|
|
println("=== Processing row ${rowIndex + 1} (staffNo: $staffNo, name: $name) ===") |
|
|
|
|
|
|
|
|
|
|
|
// Header-based columns |
|
|
|
|
|
columnToAuthority.forEach { (colIndex, authCode) -> |
|
|
|
|
|
val cell = row.getCell(colIndex) |
|
|
|
|
|
val rawValue = if (cell == null) "" else ExcelUtils.getStringValue(cell) |
|
|
|
|
|
val value = rawValue.trim() |
|
|
|
|
|
|
|
|
|
|
|
println(" Col $colIndex | Raw: '${rawValue.take(40).replace("\n", "\\n")}' | Trimmed: '$value' | Len: ${value.length}") |
|
|
|
|
|
|
|
|
|
|
|
val isNotBlank = value.isNotBlank() |
|
|
|
|
|
println(" → non-blank? $isNotBlank") |
|
|
|
|
|
|
|
|
|
|
|
if (isNotBlank) { |
|
|
|
|
|
println(" → ADDING: $authCode") |
|
|
|
|
|
authorityCodes.add(authCode) |
|
|
} else { |
|
|
} else { |
|
|
"" |
|
|
|
|
|
|
|
|
println(" → skipped (empty/blank)") |
|
|
} |
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
if (staffNo.isNotEmpty()) { |
|
|
|
|
|
userDataList.add(UserData(staffNo, name, title)) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
println(" Final collected authorities: ${authorityCodes.joinToString(", ") { "'$it'" }}") |
|
|
|
|
|
println("=== End of row ${rowIndex + 1} ===\n") |
|
|
|
|
|
|
|
|
|
|
|
if (authorityCodes.isNotEmpty() || name.isNotEmpty() || title.isNotEmpty()) { |
|
|
|
|
|
userDataList.add(UserData(staffNo, name, title, authorityCodes)) |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Create users in database |
|
|
|
|
|
|
|
|
// ──────────────────────────────────────────────── |
|
|
|
|
|
// Create users + assign authorities |
|
|
|
|
|
// ──────────────────────────────────────────────── |
|
|
var createdCount = 0 |
|
|
var createdCount = 0 |
|
|
for (userData in userDataList) { |
|
|
|
|
|
// Check if username (staffNo) already exists |
|
|
|
|
|
val existingUser = userRepository.findByUsernameAndDeletedFalse(userData.staffNo) |
|
|
|
|
|
|
|
|
|
|
|
if (existingUser.isEmpty) { |
|
|
|
|
|
val user = User() |
|
|
|
|
|
user.username = userData.staffNo |
|
|
|
|
|
user.password = passwordEncoder.encode("Pings2026!") |
|
|
|
|
|
user.locked = false |
|
|
|
|
|
user.name = userData.name |
|
|
|
|
|
user.title = userData.title |
|
|
|
|
|
user.lotusNotesUser = false |
|
|
|
|
|
user.staffNo = userData.staffNo |
|
|
|
|
|
|
|
|
|
|
|
userRepository.save(user) |
|
|
|
|
|
createdCount++ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (data in userDataList) { |
|
|
|
|
|
val existing = userRepository.findByUsernameAndDeletedFalse(data.staffNo) |
|
|
|
|
|
if (existing.isPresent) { |
|
|
|
|
|
println("SKIP - User already exists: ${data.staffNo}") |
|
|
|
|
|
continue |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
val user = User().apply { |
|
|
|
|
|
username = data.staffNo |
|
|
|
|
|
password = passwordEncoder.encode("Pings2026!") |
|
|
|
|
|
locked = false |
|
|
|
|
|
this.name = data.name |
|
|
|
|
|
this.title = data.title |
|
|
|
|
|
lotusNotesUser = false |
|
|
|
|
|
staffNo = data.staffNo |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
val savedUser = userRepository.save(user) |
|
|
|
|
|
createdCount++ |
|
|
|
|
|
|
|
|
|
|
|
println("CREATED user: ${data.staffNo} → ID = ${savedUser.id}") |
|
|
|
|
|
|
|
|
|
|
|
data.authorityCodes.forEach { code -> |
|
|
|
|
|
println(" → Assigning authority: '$code'") |
|
|
|
|
|
userRepository.assignAuthorityByCodeIfNotExists(savedUser.id!!, code) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
println("") |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
println("Import finished. Created $createdCount new users.") |
|
|
return createdCount |
|
|
return createdCount |
|
|
|
|
|
|
|
|
} finally { |
|
|
} finally { |
|
|
workbook.close() |
|
|
workbook.close() |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
} |