Search in sources :

Example 1 with BizException

use of com.github.lybgeek.orm.common.exception.BizException in project springboot-learning by lyb-geek.

the class BookServiceImpl method addBook.

@Override
@Transactional
public BookDTO addBook(BookDTO bookDTO) {
    Book book = dozerMapper.map(bookDTO, Book.class);
    boolean isExitBookByName = ObjectUtils.isNotEmpty(getBookByName(bookDTO.getBookName()));
    if (isExitBookByName) {
        throw new BizException("书名已经存在");
    }
    book.setCreateDate(new Date());
    book.setUpdateDate(new Date());
    baseMapper.insert(book);
    bookDTO = dozerMapper.map(book, BookDTO.class);
    return bookDTO;
}
Also used : BookDTO(com.github.lybgeek.orm.mybatisplus.dto.BookDTO) Book(com.github.lybgeek.orm.mybatisplus.model.Book) BizException(com.github.lybgeek.orm.common.exception.BizException) Date(java.util.Date) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with BizException

use of com.github.lybgeek.orm.common.exception.BizException in project springboot-learning by lyb-geek.

the class BookOrderServiceImpl method createBookOrder.

@Override
@Transactional
public BookOrderDTO createBookOrder(BookOrderDTO bookOrderDTO) {
    BookOrder bookOrder = dozerMapper.map(bookOrderDTO, BookOrder.class);
    OrderItemsDTO orderItemsDTO = OrderItemUtil.INSTANCE.getOrderItems(bookOrder.getBookOrderItems());
    Map<String, Integer> orderCountMap = orderItemsDTO.getItemContMap();
    boolean isCanCreate = true;
    String bookName = null;
    for (Map.Entry<String, Integer> entry : orderCountMap.entrySet()) {
        BookDTO bookDTO = bookService.getBookByName(entry.getKey());
        if (ObjectUtils.isEmpty(bookDTO)) {
            log.warn("不存在书目{}", entry.getKey());
            bookName = entry.getKey();
            isCanCreate = false;
            break;
        }
        int updateBookCount = bookService.updateStockById(bookDTO.getId(), entry.getValue());
        if (updateBookCount <= 0) {
            log.warn("当前书目:{},还有库存:{},欲购数目:{}", entry.getKey(), bookDTO.getStock(), entry.getValue());
            throw new BizException("当前书目->" + entry.getKey() + "已经售罄");
        }
    }
    if (!isCanCreate) {
        throw new BizException("不存在书目->" + bookName);
    }
    bookOrder.setTotal(orderItemsDTO.getTotalPrice());
    bookOrder.setOrderNo(String.valueOf(System.currentTimeMillis()));
    BookOrder dbBookOrder = bookOrderDao.createBookOrder(bookOrder);
    applicationEventPublisher.publishEvent(dbBookOrder);
    return dozerMapper.map(dbBookOrder, BookOrderDTO.class);
}
Also used : BookDTO(com.github.lybgeek.orm.mybatisplus.dto.BookDTO) OrderItemsDTO(com.github.lybgeek.orm.mybatis.dto.OrderItemsDTO) BizException(com.github.lybgeek.orm.common.exception.BizException) BookOrder(com.github.lybgeek.orm.mybatis.model.BookOrder) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

BizException (com.github.lybgeek.orm.common.exception.BizException)2 BookDTO (com.github.lybgeek.orm.mybatisplus.dto.BookDTO)2 Transactional (org.springframework.transaction.annotation.Transactional)2 OrderItemsDTO (com.github.lybgeek.orm.mybatis.dto.OrderItemsDTO)1 BookOrder (com.github.lybgeek.orm.mybatis.model.BookOrder)1 Book (com.github.lybgeek.orm.mybatisplus.model.Book)1 Date (java.util.Date)1