| @GeneratedValue(strategy = GenerationType.IDENTITY) | @GeneratedValue(strategy = GenerationType.IDENTITY) | ||||
| private Long id; | private Long id; | ||||
| @Column(name = "tb_crop_id", insertable = false, updatable = false) | |||||
| private Long tbCropId; | |||||
| @JsonIgnore | @JsonIgnore | ||||
| @OneToOne | @OneToOne | ||||
| @MapsId | |||||
| @JoinColumn(name = "tb_crop_id") | @JoinColumn(name = "tb_crop_id") | ||||
| private TBCrop tbCrop; | private TBCrop tbCrop; | ||||
| public void setTbCrop(TBCrop tbCrop) { | public void setTbCrop(TBCrop tbCrop) { | ||||
| this.tbCrop = tbCrop; | this.tbCrop = tbCrop; | ||||
| } | } | ||||
| public Long getTbCropId() { | |||||
| return tbCropId; | |||||
| } | |||||
| public void setTbCropId(Long tbCropId) { | |||||
| this.tbCropId = tbCropId; | |||||
| } | |||||
| } | } |
| @GeneratedValue(strategy = GenerationType.IDENTITY) | @GeneratedValue(strategy = GenerationType.IDENTITY) | ||||
| private Long id; | private Long id; | ||||
| @Column(name = "tb_code_id", insertable = false, updatable = false) | |||||
| private Long tbCodeId; | |||||
| @ManyToOne(fetch = FetchType.LAZY) | @ManyToOne(fetch = FetchType.LAZY) | ||||
| @JsonIgnoreProperties("") | @JsonIgnoreProperties("") | ||||
| @JsonIgnore | @JsonIgnore | ||||
| public void setLastUpdatedDate(Instant lastUpdatedDate) { | public void setLastUpdatedDate(Instant lastUpdatedDate) { | ||||
| this.lastUpdatedDate = lastUpdatedDate; | this.lastUpdatedDate = lastUpdatedDate; | ||||
| } | } | ||||
| public Long getTbCodeId() { | |||||
| return tbCodeId; | |||||
| } | |||||
| public void setTbCodeId(Long tbCodeId) { | |||||
| this.tbCodeId = tbCodeId; | |||||
| } | |||||
| } | } |
| } | } | ||||
| @Override | @Override | ||||
| @Transactional | |||||
| public List<TBCodeDetailsDTO> saveAll(List<TBCodeDetailsDTO> tBCodeDetailsDTOs) { | public List<TBCodeDetailsDTO> saveAll(List<TBCodeDetailsDTO> tBCodeDetailsDTOs) { | ||||
| List<TBCodeDetails> tbCodes = new ArrayList<>(); | List<TBCodeDetails> tbCodes = new ArrayList<>(); | ||||
| for (TBCodeDetailsDTO tBCodeDetailsDTO : tBCodeDetailsDTOs) { | for (TBCodeDetailsDTO tBCodeDetailsDTO : tBCodeDetailsDTOs) { |
| import vn.azteam.tpf.repository.TBCodeDetailsRepository; | import vn.azteam.tpf.repository.TBCodeDetailsRepository; | ||||
| import vn.azteam.tpf.repository.TBCodeRepository; | import vn.azteam.tpf.repository.TBCodeRepository; | ||||
| import vn.azteam.tpf.repository.search.TBCodeSearchRepository; | import vn.azteam.tpf.repository.search.TBCodeSearchRepository; | ||||
| import vn.azteam.tpf.service.TBCodeDetailsService; | |||||
| import vn.azteam.tpf.service.TBCodeService; | import vn.azteam.tpf.service.TBCodeService; | ||||
| import vn.azteam.tpf.service.dto.TBCodeDTO; | 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.service.mapper.TBCodeMapper; | ||||
| import vn.azteam.tpf.web.rest.errors.BadRequestAlertException; | 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; | import java.util.stream.Collectors; | ||||
| /** | /** | ||||
| private final TBCodeDetailsRepository tbCodeDetailsRepository; | private final TBCodeDetailsRepository tbCodeDetailsRepository; | ||||
| private final TBCodeDetailsService tbCodeDetailsService; | |||||
| private final TBCodeMapper tBCodeMapper; | private final TBCodeMapper tBCodeMapper; | ||||
| private final TBCodeSearchRepository tBCodeSearchRepository; | private final TBCodeSearchRepository tBCodeSearchRepository; | ||||
| public TBCodeServiceImpl(TBCodeRepository tbCodeRepository, | public TBCodeServiceImpl(TBCodeRepository tbCodeRepository, | ||||
| TBCodeDetailsRepository tbCodeDetailsRepository, TBCodeMapper tBCodeMapper, | |||||
| TBCodeDetailsRepository tbCodeDetailsRepository, TBCodeDetailsService tbCodeDetailsService, TBCodeMapper tBCodeMapper, | |||||
| TBCodeSearchRepository tBCodeSearchRepository) { | TBCodeSearchRepository tBCodeSearchRepository) { | ||||
| this.tbCodeRepository = tbCodeRepository; | this.tbCodeRepository = tbCodeRepository; | ||||
| this.tbCodeDetailsRepository = tbCodeDetailsRepository; | this.tbCodeDetailsRepository = tbCodeDetailsRepository; | ||||
| this.tbCodeDetailsService = tbCodeDetailsService; | |||||
| this.tBCodeMapper = tBCodeMapper; | this.tBCodeMapper = tBCodeMapper; | ||||
| this.tBCodeSearchRepository = tBCodeSearchRepository; | this.tBCodeSearchRepository = tBCodeSearchRepository; | ||||
| } | } | ||||
| @Override | @Override | ||||
| @Transactional | |||||
| public TBCodeDTO save(TBCodeDTO tBCodeDTO) { | public TBCodeDTO save(TBCodeDTO tBCodeDTO) { | ||||
| log.debug("Request to save TBCodeDTO : {}", tBCodeDTO); | log.debug("Request to save TBCodeDTO : {}", tBCodeDTO); | ||||
| tbCode = tbCodeRepository.save(tbCode); | tbCode = tbCodeRepository.save(tbCode); | ||||
| TBCodeDTO result = tBCodeMapper.toDto(tbCode); | TBCodeDTO result = tBCodeMapper.toDto(tbCode); | ||||
| tBCodeSearchRepository.save(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; | return result; | ||||
| } | } | ||||
| log.debug("REST request to save TBCode : {}", tBCodeCreationDTO); | log.debug("REST request to save TBCode : {}", tBCodeCreationDTO); | ||||
| //Set created by to current user login | //Set created by to current user login | ||||
| UserDTO currentUser = userService.getCurrentUserDTO().get(); | UserDTO currentUser = userService.getCurrentUserDTO().get(); | ||||
| if (tBCodeCreationDTO.gettBCropId() != null) { | |||||
| if (tBCodeCreationDTO.gettBCropId() == null) { | |||||
| throw new BadRequestAlertException("1047", ENTITY_NAME, "1047"); | throw new BadRequestAlertException("1047", ENTITY_NAME, "1047"); | ||||
| } | } | ||||
| if (tBCodeCreationDTO.getQuantity() != null && tBCodeCreationDTO.getQuantity() <= 0) { | if (tBCodeCreationDTO.getQuantity() != null && tBCodeCreationDTO.getQuantity() <= 0) { | ||||
| } | } | ||||
| while (hasViolationException); | 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())) | return ResponseEntity.created(new URI("/api/tb-codes/" + result.getId())) | ||||
| .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString())) | .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString())) |