@@ -1,7 +1,11 @@ | |||||
package com.ffii.tsms.modules.data.entity; | package com.ffii.tsms.modules.data.entity; | ||||
import com.ffii.core.support.AbstractRepository; | import com.ffii.core.support.AbstractRepository; | ||||
import org.springframework.data.repository.query.Param; | |||||
import java.util.List; | |||||
public interface CustomerContactRepository extends AbstractRepository<CustomerContact, Long> { | public interface CustomerContactRepository extends AbstractRepository<CustomerContact, Long> { | ||||
List<CustomerContact> findAllByCustomerId(@Param("customerId") Long customerId); | |||||
} | } |
@@ -1,9 +1,13 @@ | |||||
package com.ffii.tsms.modules.data.entity; | package com.ffii.tsms.modules.data.entity; | ||||
import java.util.Optional; | import java.util.Optional; | ||||
import java.util.List; | |||||
import org.springframework.data.repository.query.Param; | |||||
import com.ffii.core.support.AbstractRepository; | import com.ffii.core.support.AbstractRepository; | ||||
public interface CustomerRepository extends AbstractRepository<Customer, Long> { | public interface CustomerRepository extends AbstractRepository<Customer, Long> { | ||||
Optional<Customer> findByCode(@Param("code") String code); | |||||
} | } |
@@ -0,0 +1,43 @@ | |||||
package com.ffii.tsms.modules.data.service | |||||
import com.ffii.tsms.modules.data.entity.CustomerContact | |||||
import com.ffii.tsms.modules.data.entity.CustomerContactRepository | |||||
import com.ffii.tsms.modules.data.entity.CustomerRepository | |||||
import org.springframework.stereotype.Service | |||||
@Service | |||||
class CustomerContactService( | |||||
private val customerContactRepository: CustomerContactRepository, | |||||
private val customerRepository: CustomerRepository | |||||
) { | |||||
fun findAllByCustomerId(customerId: Long): List<CustomerContact> { | |||||
return customerContactRepository.findAllByCustomerId(customerId) | |||||
} | |||||
fun saveContactsByCustomer(saveCustomerId: Long, saveCustomerContact: List<CustomerContact>) { | |||||
val customerContactList = mutableListOf<CustomerContact>() | |||||
val customerContactSize = saveCustomerContact.size | |||||
for (i in 0 until customerContactSize) { | |||||
val customer = customerRepository.findById(saveCustomerId).orElseThrow() | |||||
val customerContact = CustomerContact().apply { | |||||
id = if(saveCustomerContact[i].id!! > 0) saveCustomerContact[i].id else null | |||||
this.customer = customer | |||||
name = saveCustomerContact[i].name | |||||
phone = saveCustomerContact[i].phone | |||||
email = saveCustomerContact[i].email | |||||
} | |||||
customerContactList.add(customerContact) | |||||
} | |||||
customerContactRepository.saveAll(customerContactList) | |||||
} | |||||
fun deleteContactsByCustomer(deleteContactIds: List<Long>) { | |||||
customerContactRepository.deleteAllById(deleteContactIds.filter { it > 0 }) | |||||
} | |||||
} |
@@ -1,14 +1,22 @@ | |||||
package com.ffii.tsms.modules.data.service | package com.ffii.tsms.modules.data.service | ||||
import com.ffii.tsms.modules.data.entity.Customer | import com.ffii.tsms.modules.data.entity.Customer | ||||
import com.ffii.tsms.modules.data.entity.CustomerContact | |||||
import com.ffii.tsms.modules.data.entity.CustomerType | import com.ffii.tsms.modules.data.entity.CustomerType | ||||
import com.ffii.tsms.modules.data.entity.CustomerRepository | import com.ffii.tsms.modules.data.entity.CustomerRepository | ||||
import com.ffii.tsms.modules.data.entity.CustomerTypeRepository | import com.ffii.tsms.modules.data.entity.CustomerTypeRepository | ||||
import com.ffii.tsms.modules.data.web.models.NewCustomerResponse | |||||
import com.ffii.tsms.modules.project.web.models.SaveCustomerRequest | import com.ffii.tsms.modules.project.web.models.SaveCustomerRequest | ||||
import org.springframework.stereotype.Service | import org.springframework.stereotype.Service | ||||
import java.util.Optional | |||||
@Service | @Service | ||||
class CustomerService(private val customerRepository: CustomerRepository, private val customerTypeRepository: CustomerTypeRepository, private val customerSubsidiaryService: CustomerSubsidiaryService) { | |||||
class CustomerService( | |||||
private val customerRepository: CustomerRepository, | |||||
private val customerTypeRepository: CustomerTypeRepository, | |||||
private val customerSubsidiaryService: CustomerSubsidiaryService, | |||||
private val customerContactService: CustomerContactService, | |||||
) { | |||||
fun allCustomers(): List<Customer> { | fun allCustomers(): List<Customer> { | ||||
return customerRepository.findAll() | return customerRepository.findAll() | ||||
@@ -17,41 +25,63 @@ class CustomerService(private val customerRepository: CustomerRepository, privat | |||||
fun allCustomerTypes(): List<CustomerType> { | fun allCustomerTypes(): List<CustomerType> { | ||||
return customerTypeRepository.findAll() | return customerTypeRepository.findAll() | ||||
} | } | ||||
fun findCustomer(id: Long): Customer { | fun findCustomer(id: Long): Customer { | ||||
return customerRepository.findById(id).orElseThrow() | return customerRepository.findById(id).orElseThrow() | ||||
} | } | ||||
fun saveCustomer(saveCustomer: SaveCustomerRequest): Customer { | |||||
fun findCustomerByCode(code: String): Optional<Customer> { | |||||
return customerRepository.findByCode(code); | |||||
} | |||||
fun saveCustomer(saveCustomer: SaveCustomerRequest): NewCustomerResponse { | |||||
val duplicateCustomer = findCustomerByCode(saveCustomer.code) | |||||
//TODO: check duplicate customer | |||||
if (duplicateCustomer.isPresent()) { | |||||
return NewCustomerResponse( | |||||
customer = duplicateCustomer.get(), | |||||
message = "The customer code has already existed" | |||||
); | |||||
} | |||||
val customerType = customerTypeRepository.findById(saveCustomer.typeId).orElseThrow() | val customerType = customerTypeRepository.findById(saveCustomer.typeId).orElseThrow() | ||||
val customer = | val customer = | ||||
customerRepository.saveAndFlush( | |||||
Customer().apply { | |||||
id = saveCustomer.id | |||||
code = saveCustomer.code | |||||
name = saveCustomer.name | |||||
this.customerType = customerType | |||||
address = saveCustomer.address | |||||
district = saveCustomer.district | |||||
brNo = saveCustomer.brNo | |||||
} | |||||
) | |||||
customerRepository.saveAndFlush( | |||||
Customer().apply { | |||||
id = saveCustomer.id | |||||
code = saveCustomer.code | |||||
name = saveCustomer.name | |||||
this.customerType = customerType | |||||
address = saveCustomer.address | |||||
district = saveCustomer.district | |||||
brNo = saveCustomer.brNo | |||||
} | |||||
) | |||||
val customerId = customer.id | val customerId = customer.id | ||||
if (customerId != null && customerId > 0) { | if (customerId != null && customerId > 0) { | ||||
if (saveCustomer.deleteSubsidiaryIds.size > 0) { | |||||
if (saveCustomer.deleteSubsidiaryIds.isNotEmpty()) { | |||||
customerSubsidiaryService.deleteSubsidiariesByCustomer(customerId, saveCustomer.deleteSubsidiaryIds) | customerSubsidiaryService.deleteSubsidiariesByCustomer(customerId, saveCustomer.deleteSubsidiaryIds) | ||||
} | } | ||||
if (saveCustomer.addSubsidiaryIds.size > 0) { | |||||
if (saveCustomer.addSubsidiaryIds.isNotEmpty()) { | |||||
customerSubsidiaryService.saveSubsidiariesByCustomer(customerId, saveCustomer.addSubsidiaryIds) | customerSubsidiaryService.saveSubsidiariesByCustomer(customerId, saveCustomer.addSubsidiaryIds) | ||||
} | } | ||||
if (saveCustomer.deleteContactIds.isNotEmpty()) { | |||||
customerContactService.deleteContactsByCustomer( saveCustomer.deleteContactIds) | |||||
} | |||||
if (saveCustomer.addContacts.isNotEmpty()) { | |||||
customerContactService.saveContactsByCustomer(customerId, saveCustomer.addContacts) | |||||
} | |||||
} | } | ||||
return customer | |||||
return NewCustomerResponse(customer = customer, message = "Success"); | |||||
} | } | ||||
} | } |
@@ -18,12 +18,15 @@ class CustomerSubsidiaryService( | |||||
return customerSubsidiaryRepository.findAll() | return customerSubsidiaryRepository.findAll() | ||||
} | } | ||||
fun findAllByCustomerId(customerId : Long) : List<CustomerSubsidiary> { | |||||
return customerSubsidiaryRepository.findAllByCustomerId(customerId) | |||||
} | |||||
fun findAllSubsidiaryIdsByCustomerId(customerId : Long) : List<Long> { | fun findAllSubsidiaryIdsByCustomerId(customerId : Long) : List<Long> { | ||||
val customerSubsidiaryList: List<CustomerSubsidiary> = customerSubsidiaryRepository.findAllByCustomerId(customerId) | val customerSubsidiaryList: List<CustomerSubsidiary> = customerSubsidiaryRepository.findAllByCustomerId(customerId) | ||||
if (customerSubsidiaryList.size > 0) { | |||||
return customerSubsidiaryList.map { it.subsidiary.id }.filterNotNull() | |||||
if (customerSubsidiaryList.isNotEmpty()) { | |||||
return customerSubsidiaryList.mapNotNull { it.subsidiary.id } | |||||
} else return emptyList() | } else return emptyList() | ||||
} | } | ||||
@@ -49,19 +52,12 @@ class CustomerSubsidiaryService( | |||||
fun deleteSubsidiariesByCustomer(deleteCustomerId: Long, deleteSubsidiaryIds: List<Long>) { | fun deleteSubsidiariesByCustomer(deleteCustomerId: Long, deleteSubsidiaryIds: List<Long>) { | ||||
val customerSubsidiaryList = mutableListOf<CustomerSubsidiary>() | |||||
val subsidiaryIdsSize = deleteSubsidiaryIds.size | |||||
for (i in 0 until subsidiaryIdsSize) { | |||||
val customer = customerRepository.findById(deleteCustomerId).orElseThrow() | |||||
val subsidiary = subsidiaryRepository.findById(deleteSubsidiaryIds[i]).orElseThrow() | |||||
val customerSubsidiary = CustomerSubsidiary().apply { | |||||
this.customer = customer | |||||
this.subsidiary = subsidiary | |||||
} | |||||
val customerSubsidiaryList: List<CustomerSubsidiary> = findAllByCustomerId(deleteCustomerId) | |||||
customerSubsidiaryList.add(customerSubsidiary) | |||||
if (customerSubsidiaryList.isNotEmpty()) { | |||||
val filteredSubsidiaryIds: List<Long> = | |||||
customerSubsidiaryList.filter { deleteSubsidiaryIds.contains(it.subsidiary.id!!) }.mapNotNull { it.id } | |||||
customerSubsidiaryRepository.deleteAllById(filteredSubsidiaryIds) | |||||
} | } | ||||
} | } | ||||
} | } |
@@ -4,8 +4,9 @@ import com.ffii.tsms.modules.data.entity.Customer | |||||
import com.ffii.tsms.modules.data.entity.CustomerType | import com.ffii.tsms.modules.data.entity.CustomerType | ||||
import com.ffii.tsms.modules.data.service.CustomerService | import com.ffii.tsms.modules.data.service.CustomerService | ||||
import com.ffii.tsms.modules.data.service.CustomerSubsidiaryService | import com.ffii.tsms.modules.data.service.CustomerSubsidiaryService | ||||
import com.ffii.tsms.modules.data.web.models.CustomerResponse | |||||
import com.ffii.tsms.modules.data.web.models.NewCustomerResponse | |||||
import com.ffii.tsms.modules.project.web.models.SaveCustomerRequest | import com.ffii.tsms.modules.project.web.models.SaveCustomerRequest | ||||
import com.ffii.tsms.modules.project.web.models.CustomerResponse | |||||
import org.springframework.web.bind.annotation.GetMapping | import org.springframework.web.bind.annotation.GetMapping | ||||
import org.springframework.web.bind.annotation.RequestMapping | import org.springframework.web.bind.annotation.RequestMapping | ||||
import org.springframework.web.bind.annotation.RestController | import org.springframework.web.bind.annotation.RestController | ||||
@@ -37,7 +38,7 @@ class CustomerController(private val customerService: CustomerService, private v | |||||
} | } | ||||
@PostMapping("/save") | @PostMapping("/save") | ||||
fun saveCustomer(@Valid @RequestBody saveCustomer: SaveCustomerRequest): Customer { | |||||
fun saveCustomer(@Valid @RequestBody saveCustomer: SaveCustomerRequest): NewCustomerResponse { | |||||
return customerService.saveCustomer(saveCustomer) | return customerService.saveCustomer(saveCustomer) | ||||
} | } | ||||
} | } |
@@ -5,7 +5,7 @@ import com.ffii.tsms.modules.data.entity.CustomerSubsidiary | |||||
import com.ffii.tsms.modules.data.service.CustomerService | import com.ffii.tsms.modules.data.service.CustomerService | ||||
import com.ffii.tsms.modules.data.service.CustomerSubsidiaryService | import com.ffii.tsms.modules.data.service.CustomerSubsidiaryService | ||||
import com.ffii.tsms.modules.project.web.models.SaveCustomerRequest | import com.ffii.tsms.modules.project.web.models.SaveCustomerRequest | ||||
import com.ffii.tsms.modules.project.web.models.CustomerResponse | |||||
import com.ffii.tsms.modules.data.web.models.CustomerResponse | |||||
import org.springframework.web.bind.annotation.GetMapping | import org.springframework.web.bind.annotation.GetMapping | ||||
import org.springframework.web.bind.annotation.RequestMapping | import org.springframework.web.bind.annotation.RequestMapping | ||||
import org.springframework.web.bind.annotation.RestController | import org.springframework.web.bind.annotation.RestController | ||||
@@ -1,8 +1,9 @@ | |||||
package com.ffii.tsms.modules.project.web.models | |||||
package com.ffii.tsms.modules.data.web.models | |||||
import com.ffii.tsms.modules.data.entity.Customer | import com.ffii.tsms.modules.data.entity.Customer | ||||
data class CustomerResponse( | data class CustomerResponse( | ||||
val customer: Customer, | val customer: Customer, | ||||
val subsidiaryIds: List<Long>, | val subsidiaryIds: List<Long>, | ||||
) | |||||
) | |||||
@@ -0,0 +1,8 @@ | |||||
package com.ffii.tsms.modules.data.web.models | |||||
import com.ffii.tsms.modules.data.entity.Customer | |||||
data class NewCustomerResponse ( | |||||
val customer: Customer, | |||||
val message: String, | |||||
) |
@@ -1,5 +1,6 @@ | |||||
package com.ffii.tsms.modules.project.web.models | package com.ffii.tsms.modules.project.web.models | ||||
import com.ffii.tsms.modules.data.entity.CustomerContact | |||||
import jakarta.validation.constraints.NotBlank | import jakarta.validation.constraints.NotBlank | ||||
import jakarta.validation.constraints.NotNull | import jakarta.validation.constraints.NotNull | ||||
@@ -18,5 +19,8 @@ data class SaveCustomerRequest( | |||||
val deleteSubsidiaryIds: List<Long>, | val deleteSubsidiaryIds: List<Long>, | ||||
val addSubsidiaryIds: List<Long>, | val addSubsidiaryIds: List<Long>, | ||||
val deleteContactIds: List<Long>, | |||||
val addContacts: List<CustomerContact>, | |||||
val id: Long?, | val id: Long?, | ||||
) | ) |