Skip to content

Commit

Permalink
Merge pull request #106 from NTOU-Auction/development
Browse files Browse the repository at this point in the history
準備發布1.0.4版本
  • Loading branch information
keke125 authored Jan 12, 2024
2 parents c32e31f + 34336a7 commit a3b1044
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,11 @@ List<OrderWithProductDetail> getDoneBySeller() {
Long userId = userService.findByUsername(userIdentity.getUsername()).getId();
return orderService.orderToOrderWithProductDetail(orderService.findDoneBySellerId(userId));
}

@GetMapping("/check/income")
List<Long> getIncomeBySeller() {
Long userId = userService.findByUsername(userIdentity.getUsername()).getId();
return orderService.checkIncome(userId);
}

@PostMapping("/create")
ResponseEntity<Map<String, String>> addOrder(@Valid @RequestBody AddOrderRequest request) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ public class OrderWithProductDetail {
@NotNull
private Long buyerid;

@NotNull
private String buyername;

@NotNull
private String sellername;

@NotNull
private Long sellerid;

Expand Down
71 changes: 51 additions & 20 deletions src/main/java/ntou/auction/spring/order/service/OrderService.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package ntou.auction.spring.order.service;

import ntou.auction.spring.account.entity.User;
import ntou.auction.spring.account.service.UserService;
import ntou.auction.spring.mail.EmailService;
import ntou.auction.spring.order.entity.Order;
import ntou.auction.spring.order.response.OrderWithProductDetail;
import ntou.auction.spring.order.repository.OrderRepository;
import ntou.auction.spring.product.entity.Product;
import ntou.auction.spring.shoppingcart.response.ProductAddAmount;
import ntou.auction.spring.product.service.ProductService;
import ntou.auction.spring.shoppingcart.service.ShoppingcartService;
Expand All @@ -12,6 +15,7 @@
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.Temporal;
import java.util.*;

@Service
Expand All @@ -20,20 +24,22 @@ public class OrderService {

private final ProductService productService;

private final ShoppingcartService shoppingcartService;
private final UserService userService;
private final EmailService emailService;

private final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

public OrderService(OrderRepository repository, ProductService productService, ShoppingcartService shoppingcartService, EmailService emailService) {
public OrderService(OrderRepository repository, ProductService productService, UserService userService, EmailService emailService) {
this.repository = repository;
this.productService = productService;
this.shoppingcartService = shoppingcartService;
this.userService = userService;
this.emailService = emailService;
}

public Order findOrderById(Long Id) {
return repository.findById(Id).orElse(null);
}

public List<Order> findAllByBuyerId(Long buyerId) {
return repository.findAllByBuyerid(buyerId);
}
Expand Down Expand Up @@ -66,9 +72,9 @@ public Long submitOrder(Long orderId, Long userId) {
// for status -> 0: reject, 1: waiting for submit, 2: submitted but not paid, 3: order done
// for return -> 0: orderNotFound, 1: statusError, 2: idError, 3: success, -1: expired
Order getorder = repository.findById(orderId).orElse(null);
if(getorder == null) return 0L;
if(!getorder.getStatus().equals(1L)) return 1L;
if(!Objects.equals(findOrderById(orderId).getSellerid(), userId)) return 2L;
if (getorder == null) return 0L;
if (!getorder.getStatus().equals(1L)) return 1L;
if (!Objects.equals(findOrderById(orderId).getSellerid(), userId)) return 2L;
getorder.setStatus(2L);
repository.save(getorder);
emailService.sendMailOrderUpdate(getorder.getBuyerid(),getorder);
Expand All @@ -79,9 +85,9 @@ public Long rejectOrder(Long orderId, Long userId) {
// 0: reject, 1: waiting for submit, 2: submitted but not paid, 3: order done
// for return -> 0: orderNotFound, 1: statusError, 2: idError, 3: success, -1: expired
Order getorder = repository.findById(orderId).orElse(null);
if(getorder == null) return 0L;
if(!getorder.getStatus().equals(1L)) return 1L;
if(!Objects.equals(findOrderById(orderId).getSellerid(), userId)) return 2L;
if (getorder == null) return 0L;
if (!getorder.getStatus().equals(1L)) return 1L;
if (!Objects.equals(findOrderById(orderId).getSellerid(), userId)) return 2L;
getorder.setStatus(0L);
repository.save(getorder);
emailService.sendMailOrderUpdate(getorder.getBuyerid(),getorder);
Expand All @@ -92,27 +98,48 @@ public Long cancelOrder(Long orderId, Long userId) {
// 0: reject, 1: waiting for submit, 2: submitted but not paid, 3: order done
// for return -> 0: orderNotFound, 1: statusError, 2: idError, 3: success, -1: expired
Order getorder = repository.findById(orderId).orElse(null);
if(getorder == null) return 0L;
if(getorder.getStatus().equals(3L) || getorder.getStatus().equals(0L)) return 1L;
if(!Objects.equals(findOrderById(orderId).getBuyerid(), userId)) return 2L;
if(Duration.between(getorder.getUpdateTime(), LocalDateTime.parse(LocalDateTime.now().format(formatter), formatter)).toSeconds()>(86400*7L)) return -1L;
if (getorder == null) return 0L;
if (getorder.getStatus().equals(3L) || getorder.getStatus().equals(0L)) return 1L;
if (!Objects.equals(findOrderById(orderId).getBuyerid(), userId)) return 2L;
if (Duration.between(getorder.getUpdateTime(), LocalDateTime.parse(LocalDateTime.now().format(formatter), formatter)).toSeconds() > (86400 * 7L))
return -1L;
getorder.setStatus(0L);
repository.save(getorder);
return 3L;
}

// make order be done
public Long doneOrder(Long orderId, Long userId) {
// 0: reject, 1: waiting for submit, 2: submitted but not paid, 3: order done
// for return -> 0: orderNotFound, 1: statusError, 2: idError, 3: success, -1: expired
Order getorder = repository.findById(orderId).orElse(null);
if(getorder == null) return 0L;
if(!getorder.getStatus().equals(2L)) return 1L;
if(!Objects.equals(findOrderById(orderId).getSellerid(), userId)) return 2L;
if (getorder == null) return 0L;
if (!getorder.getStatus().equals(2L)) return 1L;
if (!Objects.equals(findOrderById(orderId).getSellerid(), userId)) return 2L;
getorder.setStatus(3L);
repository.save(getorder);
return 3L;
}

public List<Long> checkIncome(Long userId) {
List<Order> doneOrder = findDoneBySellerId(userId);
List<Long> re = new ArrayList<>();
for (int i = 0; i < 8; i++) re.add(0L);
for (Order order : doneOrder) {
Temporal nowTime = LocalDateTime.parse(LocalDateTime.now().format(formatter), formatter);
long duringDay = Duration.between(order.getUpdateTime(), nowTime).toDays();
if (duringDay < 8L) {
Long total = 0L;
for(List<Long> product: order.getProductAddAmountList()) {
Product tempProduct = productService.getID(product.get(0));
total += tempProduct.getCurrentPrice()*product.get(1);
}
re.set((int)duringDay, re.get((int)duringDay)+total);
}
}
return re;
}

public void addOrder(Order order) {
repository.save(order);
}
Expand All @@ -122,18 +149,22 @@ public boolean checkIsSameSeller(List<List<Long>> list) {
for(List<Long> productAddAmount: list) {
check.add(productService.getID(productAddAmount.getFirst()).getSellerID());
}
return check.size()==1;
return check.size() == 1;
}

public List<OrderWithProductDetail> orderToOrderWithProductDetail(List<Order> getOrder) {
List<OrderWithProductDetail> result = new ArrayList<>();
for(Order order: getOrder) {
for (Order order : getOrder) {
OrderWithProductDetail addOrder = new OrderWithProductDetail();
addOrder.setSellerid(order.getSellerid());
addOrder.setBuyerid(order.getBuyerid());
addOrder.setUpdateTime(order.getUpdateTime());
addOrder.setStatus(order.getStatus());
addOrder.setOrderid(order.getId());
User buyer = userService.get(order.getBuyerid()).orElse(null);
User seller = userService.get(order.getSellerid()).orElse(null);
if(buyer!=null) addOrder.setBuyername(buyer.getUsername());
if(seller!=null) addOrder.setSellername(seller.getUsername());
List<ProductAddAmount> temp = new ArrayList<>();
for (List<Long> product : order.getProductAddAmountList()) {
temp.add(new ProductAddAmount(productService.getID(product.get(0)), product.get(1)));
Expand All @@ -146,9 +177,9 @@ public List<OrderWithProductDetail> orderToOrderWithProductDetail(List<Order> ge

public boolean addAmountToProduct(Order order) {
// (order == null) this may not be happened
if(order==null) return false;
if (order == null) return false;
// add product amount with amount
for(List<Long> eachProduct: order.getProductAddAmountList()) {
for (List<Long> eachProduct : order.getProductAddAmountList()) {
productService.productAmountIncrease(eachProduct.get(0), eachProduct.get(1));
}
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ ProductClassificatedBySeller getProduct() {
if(userShoppingcart==null) return null;
Map<String, List<ProductAddAmount>> result = new HashMap<>();
for(Map.Entry<Long, Long> product: userShoppingcart.getProductItems().entrySet()) {
System.out.println(product.getKey() + " " + product.getValue());
Product nowProduct = productService.getID(product.getKey());
Long sellerId = nowProduct.getSellerID();
Optional<User> sellerUser = userService.get(sellerId);
Expand Down

0 comments on commit a3b1044

Please sign in to comment.