@@ -1,9 +1,8 @@ | |||||
package com.ffii.tsms.modules.data.entity; | package com.ffii.tsms.modules.data.entity; | ||||
import com.ffii.core.entity.BaseEntity; | import com.ffii.core.entity.BaseEntity; | ||||
import jakarta.persistence.Column; | |||||
import jakarta.persistence.Entity; | |||||
import jakarta.persistence.Table; | |||||
import com.ffii.tsms.modules.user.entity.User; | |||||
import jakarta.persistence.*; | |||||
import jakarta.validation.constraints.NotNull; | import jakarta.validation.constraints.NotNull; | ||||
@Entity | @Entity | ||||
@@ -20,6 +19,11 @@ public class Team extends BaseEntity<Long> { | |||||
@Column(name = "code", length = 30) | @Column(name = "code", length = 30) | ||||
private String code; | private String code; | ||||
@NotNull | |||||
@OneToOne | |||||
@JoinColumn(name = "teamLead", unique = true) | |||||
private Staff staff; | |||||
public String getDescription() { | public String getDescription() { | ||||
return description; | return description; | ||||
} | } | ||||
@@ -43,4 +47,8 @@ public class Team extends BaseEntity<Long> { | |||||
public void setCode(String code) { | public void setCode(String code) { | ||||
this.code = code; | this.code = code; | ||||
} | } | ||||
public Staff getStaff() { return staff;} | |||||
public void setStaff(Staff staff) { this.staff = staff; } | |||||
} | } |
@@ -80,13 +80,6 @@ open class StaffsService( | |||||
@Transactional(rollbackFor = [Exception::class]) | @Transactional(rollbackFor = [Exception::class]) | ||||
open fun saveStaff(req: NewStaffRequest): Staff { | open fun saveStaff(req: NewStaffRequest): Staff { | ||||
val user = userRepository.saveAndFlush( | |||||
User().apply { | |||||
username = req.name | |||||
password = passwordEncoder.encode("mms1234") | |||||
name = req.name | |||||
} | |||||
) | |||||
val currentPosition = positionRepository.findById(req.currentPositionId).orElseThrow() | val currentPosition = positionRepository.findById(req.currentPositionId).orElseThrow() | ||||
val joinPosition = positionRepository.findById(req.joinPositionId).orElseThrow() | val joinPosition = positionRepository.findById(req.joinPositionId).orElseThrow() | ||||
val company = companyRepository.findById(req.companyId).orElseThrow() | val company = companyRepository.findById(req.companyId).orElseThrow() | ||||
@@ -97,6 +90,17 @@ open class StaffsService( | |||||
// val salaryEffective = salaryEffectiveRepository.findById(req.salaryEffId).orElseThrow() | // val salaryEffective = salaryEffectiveRepository.findById(req.salaryEffId).orElseThrow() | ||||
val department = departmentRepository.findById(req.departmentId).orElseThrow() | val department = departmentRepository.findById(req.departmentId).orElseThrow() | ||||
val user = userRepository.saveAndFlush( | |||||
User().apply { | |||||
username = req.name | |||||
password = passwordEncoder.encode("mms1234") | |||||
name = req.name | |||||
phone1 = req.phone1 | |||||
// phone2 = req.phone2 ?: null | |||||
email = req.email ?: null | |||||
} | |||||
) | |||||
// // TODO: Add tasks, milestones, allocated | // // TODO: Add tasks, milestones, allocated | ||||
val staff = Staff().apply { | val staff = Staff().apply { | ||||
joinDate = req.joinDate | joinDate = req.joinDate | ||||
@@ -20,8 +20,20 @@ open class TeamService( | |||||
private val jdbcDao: JdbcDao, | private val jdbcDao: JdbcDao, | ||||
) : AbstractBaseEntityService<Team, Long, TeamRepository>(jdbcDao, teamRepository) { | ) : AbstractBaseEntityService<Team, Long, TeamRepository>(jdbcDao, teamRepository) { | ||||
open fun allTeam(): List<Team> { | |||||
return teamRepository.findByDeletedFalse(); | |||||
// open fun allTeam(): List<Team> { | |||||
// return teamRepository.findByDeletedFalse(); | |||||
// } | |||||
open fun allTeam(args: Map<String, Any>): List<Map<String, Any>> { | |||||
val sql = StringBuilder("select" | |||||
+ " t.*, " | |||||
+ " s.staffId, " | |||||
+ " s.name as staffName" | |||||
+ " from team t " | |||||
+ " left join staff s on t.teamLead = s.id " | |||||
+ " where t.deleted = false " | |||||
) | |||||
return jdbcDao.queryForList(sql.toString(), args) | |||||
} | } | ||||
@Transactional(rollbackFor = [Exception::class]) | @Transactional(rollbackFor = [Exception::class]) | ||||
@@ -53,21 +65,25 @@ open class TeamService( | |||||
open fun updateTeam(req: NewTeamRequest, team: Team): Team { | open fun updateTeam(req: NewTeamRequest, team: Team): Team { | ||||
val addIds = req.addStaffIds ?: listOf<Int>() | val addIds = req.addStaffIds ?: listOf<Int>() | ||||
val teamLead: Staff | |||||
val teamName: String | val teamName: String | ||||
val teamCode: String | val teamCode: String | ||||
if (addIds.isNotEmpty()) { | if (addIds.isNotEmpty()) { | ||||
val teamLead = staffRepository.findById(addIds[0].toLong()).orElseThrow() | |||||
teamName = "Team " + teamLead.name | |||||
val leader = staffRepository.findById(addIds[0].toLong()).orElseThrow() | |||||
teamName = "Team " + leader.name | |||||
teamLead = leader; | |||||
val initials = teamLead.name.split(" ").map { it.first() } | |||||
val initials = leader.name.split(" ").map { it.first() } | |||||
teamCode = initials.joinToString("") | teamCode = initials.joinToString("") | ||||
} else { | } else { | ||||
teamLead = team.staff | |||||
teamName = team.name | teamName = team.name | ||||
teamCode = team.code | teamCode = team.code | ||||
} | } | ||||
team.apply { | team.apply { | ||||
this.staff = teamLead | |||||
name = teamName | name = teamName | ||||
code = teamCode | code = teamCode | ||||
description = req.description | description = req.description | ||||
@@ -18,8 +18,8 @@ import org.springframework.web.bind.annotation.* | |||||
class TeamController(private val teamService: TeamService) { | class TeamController(private val teamService: TeamService) { | ||||
@GetMapping | @GetMapping | ||||
fun allStaff(): List<Team> { | |||||
return teamService.allTeam() | |||||
fun allStaff(args: Map<String, Any>): List<Map<String, Any>> { | |||||
return teamService.allTeam(args) | |||||
} | } | ||||
@PostMapping("/save") | @PostMapping("/save") | ||||
@@ -0,0 +1,5 @@ | |||||
-- liquibase formatted sql | |||||
-- changeset derek:add teamLead col in team | |||||
ALTER TABLE `team` | |||||
ADD COLUMN `teamLead` int(11) NULL AFTER `code`; |