From 21dc83bacf135dbe491378daec4682d79dbf4a3d Mon Sep 17 00:00:00 2001 From: rivo Date: Tue, 3 Dec 2024 03:01:08 +0800 Subject: [PATCH] FEAT:fixing --- .../java/com/pos/app/entities/QrCode.java | 21 ++++++++++ .../com/pos/app/enums/OrderStatusEnum.java | 2 +- .../app/repositories/QrCodeRepository.java | 7 ++++ .../app/service/impl/OrderServiceImpl.java | 41 ++++++++++++++----- .../changelog/1.4.1_add_column_updated_by.xml | 14 +++++++ .../db/changelog/changelog-master.xml | 1 + 6 files changed, 75 insertions(+), 11 deletions(-) create mode 100644 src/main/java/com/pos/app/repositories/QrCodeRepository.java create mode 100644 src/main/resources/db/changelog/1.4.1_add_column_updated_by.xml diff --git a/src/main/java/com/pos/app/entities/QrCode.java b/src/main/java/com/pos/app/entities/QrCode.java index 76d430a..a5f5d6a 100644 --- a/src/main/java/com/pos/app/entities/QrCode.java +++ b/src/main/java/com/pos/app/entities/QrCode.java @@ -8,6 +8,9 @@ import lombok.NoArgsConstructor; import org.hibernate.type.format.jakartajson.JakartaJsonIntegration; +import java.util.Date; +import java.util.UUID; + @Data @Entity @Builder @@ -42,4 +45,22 @@ public class QrCode { @Column(name = "updated_by") private String updatedBy; + + @PrePersist + public void prePersist() { + if (this.id == null) { + this.id = UUID.randomUUID().toString(); + } + this.createdDate = new Date().getTime(); + this.updatedDate = new Date().getTime(); + } + + @PreUpdate + public void preUpdate() { + + + this.updatedDate = new Date().getTime(); + } + + } diff --git a/src/main/java/com/pos/app/enums/OrderStatusEnum.java b/src/main/java/com/pos/app/enums/OrderStatusEnum.java index 5212f5b..79938bd 100644 --- a/src/main/java/com/pos/app/enums/OrderStatusEnum.java +++ b/src/main/java/com/pos/app/enums/OrderStatusEnum.java @@ -1,5 +1,5 @@ package com.pos.app.enums; public enum OrderStatusEnum { - COMPLETED, CREATED, IN_PROGRESS, READY + COMPLETED, CREATED, IN_PROGRESS, READY, PENDING } diff --git a/src/main/java/com/pos/app/repositories/QrCodeRepository.java b/src/main/java/com/pos/app/repositories/QrCodeRepository.java new file mode 100644 index 0000000..22121b2 --- /dev/null +++ b/src/main/java/com/pos/app/repositories/QrCodeRepository.java @@ -0,0 +1,7 @@ +package com.pos.app.repositories; + +import com.pos.app.entities.QrCode; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface QrCodeRepository extends JpaRepository { +} diff --git a/src/main/java/com/pos/app/service/impl/OrderServiceImpl.java b/src/main/java/com/pos/app/service/impl/OrderServiceImpl.java index 3f1a329..39ff0e3 100644 --- a/src/main/java/com/pos/app/service/impl/OrderServiceImpl.java +++ b/src/main/java/com/pos/app/service/impl/OrderServiceImpl.java @@ -1,9 +1,7 @@ package com.pos.app.service.impl; -import com.pos.app.entities.Order; -import com.pos.app.entities.OrderProduct; -import com.pos.app.entities.Product; -import com.pos.app.entities.Transaction; +import com.pos.app.controller.OrderController; +import com.pos.app.entities.*; import com.pos.app.enums.OrderStatusEnum; import com.pos.app.enums.ResponseEnum; import com.pos.app.exception.BadRequestException; @@ -12,13 +10,9 @@ import com.pos.app.model.request.ReqCreateOrder; import com.pos.app.model.response.ResListOrder; import com.pos.app.model.response.ResponseIdQr; -import com.pos.app.repositories.OrderProductRepository; -import com.pos.app.repositories.OrderRepository; -import com.pos.app.repositories.ProductRepository; -import com.pos.app.repositories.TransactionRepository; +import com.pos.app.repositories.*; import com.pos.app.service.AccountService; import com.pos.app.service.OrderService; -import com.pos.app.service.WebSocketService; import com.pos.app.utils.NumberHelper; import com.pos.app.utils.UtilsHelper; import lombok.RequiredArgsConstructor; @@ -30,6 +24,7 @@ import java.util.ArrayList; import java.util.List; import java.util.Optional; +import java.util.UUID; @Service @RequiredArgsConstructor @@ -41,6 +36,8 @@ public class OrderServiceImpl implements OrderService { private final OrderProductRepository orderProductRepository; private final TransactionRepository transactionRepository; private final SimpMessagingTemplate simpMessagingTemplate; + private final ClientRepository clientRepository; + private final QrCodeRepository qrCodeRepository; @Override public ResponseEnum createOrder(ReqCreateOrder req) { @@ -198,8 +195,32 @@ public List getLiveOrderList(OrderStatusEnum status) { @Override public ResponseIdQr generateQROrder() { + String clientId = accountService.getCurrentClientIdOrNull(); + BigInteger getLatestCode = orderRepository.findLatestCode(clientId); + Optional client = clientRepository.findById(clientId); + String currentAccountId = accountService.getCurrentAccountId(); + if (client.isEmpty()) { + throw new NotFoundException(ResponseEnum.CLIENT_NOT_FOUND.name()); + } + try { - return null; + Order order = Order.builder() + .status(OrderStatusEnum.IN_PROGRESS) + .isPayment(false) + .orderCode(UtilsHelper.generateOrderCode(getLatestCode)) + .clientId(clientId) + .createdBy(accountService.getCurrentAccountId()) + .build(); + order = orderRepository.save(order); + QrCode qrCode = QrCode.builder() + .code(UUID.randomUUID().toString()) + .order(order) + .client(client.get()) + .createdBy(currentAccountId) + .updatedBy(currentAccountId) + .build(); + qrCode = qrCodeRepository.save(qrCode); + return ResponseIdQr.builder().id(qrCode.getCode()).build(); } catch (Exception e) { throw new RuntimeException(e); } diff --git a/src/main/resources/db/changelog/1.4.1_add_column_updated_by.xml b/src/main/resources/db/changelog/1.4.1_add_column_updated_by.xml new file mode 100644 index 0000000..1b4ad5c --- /dev/null +++ b/src/main/resources/db/changelog/1.4.1_add_column_updated_by.xml @@ -0,0 +1,14 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/resources/db/changelog/changelog-master.xml b/src/main/resources/db/changelog/changelog-master.xml index 1006660..5a785d9 100644 --- a/src/main/resources/db/changelog/changelog-master.xml +++ b/src/main/resources/db/changelog/changelog-master.xml @@ -20,5 +20,6 @@ + \ No newline at end of file