|
|
@ -4,11 +4,9 @@ import cn.edu.ecnu.stu.bookstore.component.AppException; |
|
|
|
import cn.edu.ecnu.stu.bookstore.component.Constants; |
|
|
|
import cn.edu.ecnu.stu.bookstore.mapper.BookMapper; |
|
|
|
import cn.edu.ecnu.stu.bookstore.mapper.OrderMapper; |
|
|
|
import cn.edu.ecnu.stu.bookstore.mapper.StoreMapper; |
|
|
|
import cn.edu.ecnu.stu.bookstore.mapper.UserMapper; |
|
|
|
import cn.edu.ecnu.stu.bookstore.pojo.Book; |
|
|
|
import cn.edu.ecnu.stu.bookstore.pojo.Order; |
|
|
|
import cn.edu.ecnu.stu.bookstore.pojo.OrderStatus; |
|
|
|
import cn.edu.ecnu.stu.bookstore.pojo.User; |
|
|
|
import cn.edu.ecnu.stu.bookstore.pojo.*; |
|
|
|
import cn.edu.ecnu.stu.bookstore.pojo.vo.NewOrderVO; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.context.annotation.Lazy; |
|
|
@ -34,6 +32,9 @@ public class BuyerService { |
|
|
|
private UserMapper userMapper; |
|
|
|
|
|
|
|
@Autowired |
|
|
|
private StoreMapper storeMapper; |
|
|
|
|
|
|
|
@Autowired |
|
|
|
private PasswordEncoder encoder; |
|
|
|
|
|
|
|
@Autowired |
|
|
@ -49,12 +50,16 @@ public class BuyerService { |
|
|
|
bookMap.put((String) map.get("id"), (Integer) map.get("count")); |
|
|
|
} |
|
|
|
String storeId = newOrderVO.getStoreId(); |
|
|
|
Store store = storeMapper.getStoreById(storeId); |
|
|
|
if(store == null) |
|
|
|
throw new AppException(Constants.CLIENT_ERROR, Constants.STORE_NON_EXIST_ERROR); |
|
|
|
|
|
|
|
buyerService.insertOrder(storeId, bookMap, bookList); |
|
|
|
buyerService.insertOrder(store, bookMap, bookList); |
|
|
|
} |
|
|
|
|
|
|
|
@Transactional |
|
|
|
public void insertOrder(String storeId, HashMap<String, Integer> bookMap, List<Map<String, Object>> bookList) { |
|
|
|
public void insertOrder(Store store, HashMap<String, Integer> bookMap, List<Map<String, Object>> bookList) { |
|
|
|
String storeId = store.getStoreId(); |
|
|
|
List<Book> books = bookMapper.batchSelect(storeId, bookMap.keySet()); |
|
|
|
if(books.size() != bookList.size()) |
|
|
|
throw new AppException(Constants.CLIENT_ERROR, Constants.BOOK_ERROR); |
|
|
@ -73,9 +78,11 @@ public class BuyerService { |
|
|
|
} |
|
|
|
User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); |
|
|
|
String orderId = UUID.randomUUID().toString(); |
|
|
|
Order order = new Order(orderId, user.getId(), null, null, sum, OrderStatus.WAIT_PAYMENT, storeId, null); |
|
|
|
Order order = Order.builder().orderId(orderId).buyerId(user.getId()).storeId(storeId) |
|
|
|
.price(sum).status(OrderStatus.WAIT_PAYMENT) |
|
|
|
.build(); |
|
|
|
orderMapper.insert(order); |
|
|
|
orderMapper.insertOrderBook(order.getOrderId(), list); |
|
|
|
orderMapper.insertOrderBook(orderId, list); |
|
|
|
for (Map<String, Object> map : list) { |
|
|
|
Book book = (Book)map.get("book"); |
|
|
|
bookMapper.minusStockLevel(book.getBookId(), (int)map.get("count")); |
|
|
@ -86,23 +93,29 @@ public class BuyerService { |
|
|
|
User user = userMapper.selectOneById(userId); |
|
|
|
if(!encoder.matches(password, user.getPassword())) |
|
|
|
throw new AppException(Constants.CLIENT_ERROR, Constants.PASSWORD_ERROR); |
|
|
|
BigDecimal price = orderMapper.selectPrice(orderId); |
|
|
|
if(price.compareTo(user.getBalance()) > 0) |
|
|
|
Order order = orderMapper.selectById(orderId); |
|
|
|
if(order == null) |
|
|
|
throw new AppException(Constants.CLIENT_ERROR, Constants.ORDER_NON_EXIST_ERROR); |
|
|
|
if(!order.getStatus().equals(OrderStatus.WAIT_PAYMENT)) |
|
|
|
throw new AppException(Constants.CLIENT_ERROR, Constants.ORDER_HAS_PAID_ERROR); |
|
|
|
if(order.getPrice().compareTo(user.getBalance()) > 0) |
|
|
|
throw new AppException(Constants.CLIENT_ERROR, Constants.BALANCE_ERROR); |
|
|
|
buyerService.updateBalanceAndOrder(orderId, userId, price); |
|
|
|
buyerService.updateBalanceAndOrder(storeMapper.getStoreById(order.getStoreId()).getSellerId(), order); |
|
|
|
} |
|
|
|
|
|
|
|
@Transactional |
|
|
|
public void updateBalanceAndOrder(String orderId, Integer userId, BigDecimal price) { |
|
|
|
orderMapper.updateOrderStatus(orderId, OrderStatus.WAIT_SEND); |
|
|
|
userMapper.minusBalance(userId, price); |
|
|
|
public void updateBalanceAndOrder(Integer sellerId, Order order) { |
|
|
|
orderMapper.updateOrderStatus(order.getOrderId(), OrderStatus.WAIT_SEND); |
|
|
|
BigDecimal price = order.getPrice(); |
|
|
|
userMapper.minusBalance(order.getBuyerId(), price); |
|
|
|
userMapper.addBalance(sellerId, price); |
|
|
|
} |
|
|
|
|
|
|
|
public void addFunds(String username, String password, BigDecimal addValue) { |
|
|
|
User user = userMapper.selectOneByName(username); |
|
|
|
if(!encoder.matches(password, user.getPassword())) |
|
|
|
throw new AppException(Constants.CLIENT_ERROR, Constants.PASSWORD_ERROR); |
|
|
|
userMapper.addFunds(user.getId(), addValue); |
|
|
|
userMapper.addBalance(user.getId(), addValue); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|