| @@ -8,6 +8,7 @@ import org.springframework.data.repository.query.Param; | |||
| import com.ffii.core.support.AbstractRepository; | |||
| public interface CustomerRepository extends AbstractRepository<Customer, Long> { | |||
| List<Customer> findAllByDeletedFalse(); | |||
| Optional<Customer> findByCode(@Param("code") String code); | |||
| } | |||
| @@ -11,4 +11,5 @@ public interface CustomerSubsidiaryRepository extends AbstractRepository<Custome | |||
| List<CustomerSubsidiary> findAllByCustomerId(@Param("customerId") Long customerId); | |||
| Optional<CustomerSubsidiary> findByCustomerIdAndSubsidiaryId(@Param("customerId") Long customerId, @Param("subsidiaryId") Long subsidiaryId); | |||
| } | |||
| @@ -23,13 +23,21 @@ class CustomerContactService( | |||
| 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 | |||
| } | |||
| val customerContactId = saveCustomerContact[i].id | |||
| val customerContact = | |||
| if (customerContactId != null && customerContactId > 0) customerContactRepository.findById( | |||
| customerContactId | |||
| ).orElseThrow().apply { | |||
| this.customer = customer | |||
| name = saveCustomerContact[i].name | |||
| phone = saveCustomerContact[i].phone | |||
| email = saveCustomerContact[i].email | |||
| } else CustomerContact().apply { | |||
| this.customer = customer | |||
| name = saveCustomerContact[i].name | |||
| phone = saveCustomerContact[i].phone | |||
| email = saveCustomerContact[i].email | |||
| } | |||
| customerContactList.add(customerContact) | |||
| } | |||
| @@ -1,5 +1,7 @@ | |||
| package com.ffii.tsms.modules.data.service | |||
| import com.ffii.core.support.AbstractBaseEntityService | |||
| import com.ffii.core.support.JdbcDao | |||
| import com.ffii.tsms.modules.data.entity.Customer | |||
| import com.ffii.tsms.modules.data.entity.CustomerContact | |||
| import com.ffii.tsms.modules.data.entity.CustomerType | |||
| @@ -12,30 +14,31 @@ import org.springframework.stereotype.Service | |||
| import java.util.Optional | |||
| @Service | |||
| class CustomerService( | |||
| open class CustomerService( | |||
| private val customerRepository: CustomerRepository, | |||
| private val customerTypeRepository: CustomerTypeRepository, | |||
| private val customerSubsidiaryService: CustomerSubsidiaryService, | |||
| private val customerContactService: CustomerContactService, | |||
| ) { | |||
| private val jdbcDao: JdbcDao | |||
| ) : AbstractBaseEntityService<Customer, Long, CustomerRepository>(jdbcDao, customerRepository){ | |||
| fun allCustomers(): List<Customer> { | |||
| return customerRepository.findAll() | |||
| open fun allCustomers(): List<Customer> { | |||
| return customerRepository.findAllByDeletedFalse() | |||
| } | |||
| fun allCustomerTypes(): List<CustomerType> { | |||
| open fun allCustomerTypes(): List<CustomerType> { | |||
| return customerTypeRepository.findAll() | |||
| } | |||
| fun findCustomer(id: Long): Customer { | |||
| open fun findCustomer(id: Long): Customer { | |||
| return customerRepository.findById(id).orElseThrow() | |||
| } | |||
| fun findCustomerByCode(code: String): Optional<Customer> { | |||
| open fun findCustomerByCode(code: String): Optional<Customer> { | |||
| return customerRepository.findByCode(code); | |||
| } | |||
| fun saveCustomer(saveCustomer: SaveCustomerRequest): NewCustomerResponse { | |||
| open fun saveCustomer(saveCustomer: SaveCustomerRequest): NewCustomerResponse { | |||
| val duplicateCustomer = findCustomerByCode(saveCustomer.code) | |||
| @@ -53,7 +56,10 @@ class CustomerService( | |||
| .orElseThrow() else Customer(); | |||
| BeanUtils.copyProperties(saveCustomer, instance) | |||
| instance.customerType = customerType | |||
| instance.apply { | |||
| this.customerType = customerType | |||
| } | |||
| val customer = | |||
| customerRepository.saveAndFlush(instance) | |||
| @@ -5,6 +5,8 @@ import com.ffii.tsms.modules.data.entity.CustomerSubsidiary | |||
| import com.ffii.tsms.modules.data.entity.CustomerSubsidiaryRepository | |||
| import com.ffii.tsms.modules.data.entity.SubsidiaryRepository | |||
| import org.springframework.stereotype.Service | |||
| import java.util.Optional | |||
| import kotlin.jvm.optionals.getOrNull | |||
| @Service | |||
| class CustomerSubsidiaryService( | |||
| @@ -14,13 +16,17 @@ class CustomerSubsidiaryService( | |||
| ) { | |||
| fun allCustomeriesSubsidiaries(): List<CustomerSubsidiary> { | |||
| fun allCustomersSubsidiaries(): List<CustomerSubsidiary> { | |||
| return customerSubsidiaryRepository.findAll() | |||
| } | |||
| fun findAllByCustomerId(customerId : Long) : List<CustomerSubsidiary> { | |||
| return customerSubsidiaryRepository.findAllByCustomerId(customerId) | |||
| } | |||
| fun findByCustomerIdAndSubsidiaryId(customerId : Long, subsidiaryId: Long) : CustomerSubsidiary? { | |||
| return customerSubsidiaryRepository.findByCustomerIdAndSubsidiaryId(customerId, subsidiaryId).getOrNull() | |||
| } | |||
| fun findAllSubsidiaryIdsByCustomerId(customerId : Long) : List<Long> { | |||
| val customerSubsidiaryList: List<CustomerSubsidiary> = customerSubsidiaryRepository.findAllByCustomerId(customerId) | |||
| @@ -37,14 +43,18 @@ class CustomerSubsidiaryService( | |||
| val subsidiaryIdsSize = saveSubsidiaryIds.size | |||
| for (i in 0 until subsidiaryIdsSize) { | |||
| val customer = customerRepository.findById(saveCustomerId).orElseThrow() | |||
| val subsidiary = subsidiaryRepository.findById(saveSubsidiaryIds[i]).orElseThrow() | |||
| val customerSubsidiary = CustomerSubsidiary().apply { | |||
| this.customer = customer | |||
| this.subsidiary = subsidiary | |||
| } | |||
| val checkExist = findByCustomerIdAndSubsidiaryId(saveCustomerId, saveSubsidiaryIds[i]) | |||
| customerSubsidiaryList.add(customerSubsidiary) | |||
| if (checkExist == null) { | |||
| val customer = customerRepository.findById(saveCustomerId).orElseThrow() | |||
| val subsidiary = subsidiaryRepository.findById(saveSubsidiaryIds[i]).orElseThrow() | |||
| val customerSubsidiary = CustomerSubsidiary().apply { | |||
| this.customer = customer | |||
| this.subsidiary = subsidiary | |||
| } | |||
| customerSubsidiaryList.add(customerSubsidiary) | |||
| } | |||
| } | |||
| customerSubsidiaryRepository.saveAll(customerSubsidiaryList) | |||
| @@ -1,7 +1,8 @@ | |||
| package com.ffii.tsms.modules.data.web | |||
| import com.ffii.tsms.modules.data.entity.Customer | |||
| import com.ffii.tsms.modules.data.entity.CustomerType | |||
| import com.ffii.core.support.AbstractBaseEntityService | |||
| import com.ffii.core.support.JdbcDao | |||
| import com.ffii.tsms.modules.data.entity.* | |||
| import com.ffii.tsms.modules.data.service.CustomerService | |||
| import com.ffii.tsms.modules.data.service.CustomerSubsidiaryService | |||
| import com.ffii.tsms.modules.data.web.models.CustomerResponse | |||
| @@ -14,10 +15,15 @@ import org.springframework.web.bind.annotation.RequestBody | |||
| import org.springframework.web.bind.annotation.PostMapping | |||
| import org.springframework.web.bind.annotation.PathVariable | |||
| import jakarta.validation.Valid | |||
| import org.springframework.http.HttpStatus | |||
| import org.springframework.web.bind.annotation.DeleteMapping | |||
| import org.springframework.web.bind.annotation.ResponseStatus | |||
| @RestController | |||
| @RequestMapping("/customer") | |||
| class CustomerController(private val customerService: CustomerService, private val customerSubsidiaryService: CustomerSubsidiaryService) { | |||
| class CustomerController(private val customerService: CustomerService, private val customerSubsidiaryService: CustomerSubsidiaryService, | |||
| private val customerContactRepository: CustomerContactRepository | |||
| ) { | |||
| @GetMapping | |||
| fun allCustomers(): List<Customer> { | |||
| return customerService.allCustomers() | |||
| @@ -28,13 +34,20 @@ class CustomerController(private val customerService: CustomerService, private v | |||
| return customerService.allCustomerTypes() | |||
| } | |||
| @DeleteMapping("/{id}") | |||
| @ResponseStatus(HttpStatus.NO_CONTENT) | |||
| fun deleteCustomer(@PathVariable id: Long) { | |||
| customerService.markDelete(id) | |||
| } | |||
| @GetMapping("/{id}") | |||
| fun findCustomer(@PathVariable id: Long): CustomerResponse { | |||
| val customer = customerService.findCustomer(id) | |||
| val subsidiaryIds = customerSubsidiaryService.findAllSubsidiaryIdsByCustomerId(id) | |||
| val contacts = customerContactRepository.findAllByCustomerId(id) | |||
| return CustomerResponse(customer, subsidiaryIds) | |||
| return CustomerResponse(customer, subsidiaryIds, contacts) | |||
| } | |||
| @PostMapping("/save") | |||
| @@ -19,9 +19,9 @@ import com.ffii.core.support.JdbcDao | |||
| @RequestMapping("/customer-subsidiary") | |||
| class CustomerSubsidiaryController(private val customerSubsidiaryService: CustomerSubsidiaryService) { | |||
| @GetMapping | |||
| fun allCustomeriesSubsidiaries(): List<CustomerSubsidiary> { | |||
| fun allCustomerSubsidiaries(): List<CustomerSubsidiary> { | |||
| // JdbcDao.queryForString(sql) | |||
| return customerSubsidiaryService.allCustomeriesSubsidiaries() | |||
| return customerSubsidiaryService.allCustomersSubsidiaries() | |||
| } | |||
| @GetMapping("/subsidiaries/{customerId}") | |||
| @@ -1,9 +1,11 @@ | |||
| package com.ffii.tsms.modules.data.web.models | |||
| import com.ffii.tsms.modules.data.entity.Customer | |||
| import com.ffii.tsms.modules.data.entity.CustomerContact | |||
| data class CustomerResponse( | |||
| val customer: Customer, | |||
| val subsidiaryIds: List<Long>, | |||
| val contacts: List<CustomerContact>, | |||
| ) | |||