| @@ -24,9 +24,11 @@ public class TBCode implements Serializable { | |||
| @GeneratedValue(strategy = GenerationType.IDENTITY) | |||
| private Long id; | |||
| @Column(name = "tb_crop_id", insertable = false, updatable = false) | |||
| private Long tbCropId; | |||
| @JsonIgnore | |||
| @OneToOne | |||
| @MapsId | |||
| @JoinColumn(name = "tb_crop_id") | |||
| private TBCrop tbCrop; | |||
| @@ -197,4 +199,12 @@ public class TBCode implements Serializable { | |||
| public void setTbCrop(TBCrop tbCrop) { | |||
| this.tbCrop = tbCrop; | |||
| } | |||
| public Long getTbCropId() { | |||
| return tbCropId; | |||
| } | |||
| public void setTbCropId(Long tbCropId) { | |||
| this.tbCropId = tbCropId; | |||
| } | |||
| } | |||
| @@ -22,6 +22,9 @@ public class TBCodeDetails implements Serializable { | |||
| @GeneratedValue(strategy = GenerationType.IDENTITY) | |||
| private Long id; | |||
| @Column(name = "tb_code_id", insertable = false, updatable = false) | |||
| private Long tbCodeId; | |||
| @ManyToOne(fetch = FetchType.LAZY) | |||
| @JsonIgnoreProperties("") | |||
| @JsonIgnore | |||
| @@ -161,4 +164,12 @@ public class TBCodeDetails implements Serializable { | |||
| public void setLastUpdatedDate(Instant lastUpdatedDate) { | |||
| this.lastUpdatedDate = lastUpdatedDate; | |||
| } | |||
| public Long getTbCodeId() { | |||
| return tbCodeId; | |||
| } | |||
| public void setTbCodeId(Long tbCodeId) { | |||
| this.tbCodeId = tbCodeId; | |||
| } | |||
| } | |||
| @@ -60,6 +60,7 @@ public class TBCodeDetailsImpl implements TBCodeDetailsService { | |||
| } | |||
| @Override | |||
| @Transactional | |||
| public List<TBCodeDetailsDTO> saveAll(List<TBCodeDetailsDTO> tBCodeDetailsDTOs) { | |||
| List<TBCodeDetails> tbCodes = new ArrayList<>(); | |||
| for (TBCodeDetailsDTO tBCodeDetailsDTO : tBCodeDetailsDTOs) { | |||
| @@ -10,14 +10,16 @@ import vn.azteam.tpf.domain.TBCodeStatusEnum; | |||
| import vn.azteam.tpf.repository.TBCodeDetailsRepository; | |||
| import vn.azteam.tpf.repository.TBCodeRepository; | |||
| import vn.azteam.tpf.repository.search.TBCodeSearchRepository; | |||
| import vn.azteam.tpf.service.TBCodeDetailsService; | |||
| import vn.azteam.tpf.service.TBCodeService; | |||
| import vn.azteam.tpf.service.dto.TBCodeDTO; | |||
| import vn.azteam.tpf.service.dto.TBCodeDetailsDTO; | |||
| import vn.azteam.tpf.service.mapper.TBCodeMapper; | |||
| import vn.azteam.tpf.web.rest.errors.BadRequestAlertException; | |||
| import vn.azteam.tpf.web.rest.util.RandomStringUtil; | |||
| import java.util.ArrayList; | |||
| import java.util.List; | |||
| import java.util.Optional; | |||
| import java.time.Instant; | |||
| import java.util.*; | |||
| import java.util.stream.Collectors; | |||
| /** | |||
| @@ -35,21 +37,25 @@ public class TBCodeServiceImpl implements TBCodeService { | |||
| private final TBCodeDetailsRepository tbCodeDetailsRepository; | |||
| private final TBCodeDetailsService tbCodeDetailsService; | |||
| private final TBCodeMapper tBCodeMapper; | |||
| private final TBCodeSearchRepository tBCodeSearchRepository; | |||
| public TBCodeServiceImpl(TBCodeRepository tbCodeRepository, | |||
| TBCodeDetailsRepository tbCodeDetailsRepository, TBCodeMapper tBCodeMapper, | |||
| TBCodeDetailsRepository tbCodeDetailsRepository, TBCodeDetailsService tbCodeDetailsService, TBCodeMapper tBCodeMapper, | |||
| TBCodeSearchRepository tBCodeSearchRepository) { | |||
| this.tbCodeRepository = tbCodeRepository; | |||
| this.tbCodeDetailsRepository = tbCodeDetailsRepository; | |||
| this.tbCodeDetailsService = tbCodeDetailsService; | |||
| this.tBCodeMapper = tBCodeMapper; | |||
| this.tBCodeSearchRepository = tBCodeSearchRepository; | |||
| } | |||
| @Override | |||
| @Transactional | |||
| public TBCodeDTO save(TBCodeDTO tBCodeDTO) { | |||
| log.debug("Request to save TBCodeDTO : {}", tBCodeDTO); | |||
| @@ -57,6 +63,23 @@ public class TBCodeServiceImpl implements TBCodeService { | |||
| tbCode = tbCodeRepository.save(tbCode); | |||
| TBCodeDTO result = tBCodeMapper.toDto(tbCode); | |||
| tBCodeSearchRepository.save(tbCode); | |||
| if (tbCode.getId() != null) { | |||
| List<TBCodeDetailsDTO> tbCodeDetailsDTOS = new ArrayList<>(); | |||
| for (int i = 1; i <= tbCode.getQuantity(); i++) { | |||
| TBCodeDetailsDTO codeDetailsDTO = new TBCodeDetailsDTO(); | |||
| codeDetailsDTO.setCode(String.valueOf(RandomStringUtil.getDigitCodeDetail())); | |||
| codeDetailsDTO.setTbCode(result); | |||
| codeDetailsDTO.setNumberScan(0); | |||
| codeDetailsDTO.setStatus(TBCodeStatusEnum.NEW); | |||
| codeDetailsDTO.setCreatedDate(Instant.now()); | |||
| tBCodeDTO.setCreatedById(tBCodeDTO.getCreatedById()); | |||
| tbCodeDetailsDTOS.add(codeDetailsDTO); | |||
| } | |||
| tbCodeDetailsDTOS = tbCodeDetailsService.saveAll(tbCodeDetailsDTOS); | |||
| result.setTbCodeDetails(new HashSet<>(tbCodeDetailsDTOS)); | |||
| } | |||
| return result; | |||
| } | |||
| @@ -122,7 +122,7 @@ public class TBCodeResource { | |||
| log.debug("REST request to save TBCode : {}", tBCodeCreationDTO); | |||
| //Set created by to current user login | |||
| UserDTO currentUser = userService.getCurrentUserDTO().get(); | |||
| if (tBCodeCreationDTO.gettBCropId() != null) { | |||
| if (tBCodeCreationDTO.gettBCropId() == null) { | |||
| throw new BadRequestAlertException("1047", ENTITY_NAME, "1047"); | |||
| } | |||
| if (tBCodeCreationDTO.getQuantity() != null && tBCodeCreationDTO.getQuantity() <= 0) { | |||
| @@ -162,20 +162,20 @@ public class TBCodeResource { | |||
| } | |||
| while (hasViolationException); | |||
| if (result.getId() != null) { | |||
| List<TBCodeDetailsDTO> tbCodeDetailsDTOS = new ArrayList<>(); | |||
| for (int i = 1; i <= tBCodeCreationDTO.getQuantity(); i++) { | |||
| TBCodeDetailsDTO codeDetailsDTO = new TBCodeDetailsDTO(); | |||
| codeDetailsDTO.setCode(String.valueOf(RandomStringUtil.getDigitCodeDetail())); | |||
| codeDetailsDTO.setTbCode(tBCodeDTO); | |||
| codeDetailsDTO.setNumberScan(0); | |||
| codeDetailsDTO.setStatus(TBCodeStatusEnum.NEW); | |||
| codeDetailsDTO.setCreatedDate(Instant.now()); | |||
| tBCodeDTO.setCreatedById(currentUser.getUserId()); | |||
| tbCodeDetailsDTOS.add(codeDetailsDTO); | |||
| } | |||
| tbCodeDetailsService.saveAll(tbCodeDetailsDTOS); | |||
| } | |||
| // if (result.getId() != null) { | |||
| // List<TBCodeDetailsDTO> tbCodeDetailsDTOS = new ArrayList<>(); | |||
| // for (int i = 1; i <= tBCodeCreationDTO.getQuantity(); i++) { | |||
| // TBCodeDetailsDTO codeDetailsDTO = new TBCodeDetailsDTO(); | |||
| // codeDetailsDTO.setCode(String.valueOf(RandomStringUtil.getDigitCodeDetail())); | |||
| // codeDetailsDTO.setTbCode(tBCodeDTO); | |||
| // codeDetailsDTO.setNumberScan(0); | |||
| // codeDetailsDTO.setStatus(TBCodeStatusEnum.NEW); | |||
| // codeDetailsDTO.setCreatedDate(Instant.now()); | |||
| // tBCodeDTO.setCreatedById(currentUser.getUserId()); | |||
| // tbCodeDetailsDTOS.add(codeDetailsDTO); | |||
| // } | |||
| // tbCodeDetailsService.saveAll(tbCodeDetailsDTOS); | |||
| // } | |||
| return ResponseEntity.created(new URI("/api/tb-codes/" + result.getId())) | |||
| .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString())) | |||