Search in sources :

Example 11 with BusinessException

use of com.wayn.common.exception.BusinessException in project waynboot-mall by wayn111.

the class CartServiceImpl method changeNum.

@Override
public R changeNum(Long cartId, Integer number) {
    Cart cart = getById(cartId);
    Long productId = cart.getProductId();
    GoodsProduct goodsProduct = iGoodsProductService.getById(productId);
    Integer productNumber = goodsProduct.getNumber();
    if (number > productNumber) {
        throw new BusinessException(String.format("库存不足,该商品只剩%d件了", productNumber));
    }
    boolean update = lambdaUpdate().setSql("number = " + number).eq(Cart::getId, cartId).update();
    return R.result(update);
}
Also used : BusinessException(com.wayn.common.exception.BusinessException) GoodsProduct(com.wayn.common.core.domain.shop.GoodsProduct) Cart(com.wayn.mobile.api.domain.Cart)

Example 12 with BusinessException

use of com.wayn.common.exception.BusinessException in project waynboot-mall by wayn111.

the class OrderServiceImpl method submit.

@Override
@Transactional(rollbackFor = Exception.class)
public R submit(OrderDTO orderDTO) {
    Long userId = orderDTO.getUserId();
    // 获取用户地址
    Long addressId = orderDTO.getAddressId();
    Address checkedAddress;
    if (Objects.isNull(addressId)) {
        throw new BusinessException("收获地址为空,请求参数" + JSON.toJSONString(orderDTO));
    }
    checkedAddress = iAddressService.getById(addressId);
    // 获取用户订单商品,为空默认取购物车已选中商品
    List<Long> cartIdArr = orderDTO.getCartIdArr();
    List<Cart> checkedGoodsList;
    if (CollectionUtils.isEmpty(cartIdArr)) {
        checkedGoodsList = iCartService.list(new QueryWrapper<Cart>().eq("checked", true).eq("user_id", userId));
    } else {
        checkedGoodsList = iCartService.listByIds(cartIdArr);
    }
    // 商品费用
    BigDecimal checkedGoodsPrice = new BigDecimal("0.00");
    for (Cart checkGoods : checkedGoodsList) {
        checkedGoodsPrice = checkedGoodsPrice.add(checkGoods.getPrice().multiply(new BigDecimal(checkGoods.getNumber())));
    }
    // 根据订单商品总价计算运费,满足条件(例如88元)则免运费,否则需要支付运费(例如8元);
    BigDecimal freightPrice = new BigDecimal("0.00");
    /*if (checkedGoodsPrice.compareTo(SystemConfig.getFreightLimit()) < 0) {
            freightPrice = SystemConfig.getFreight();
        }*/
    // 可以使用的其他钱,例如用户积分
    BigDecimal integralPrice = new BigDecimal("0.00");
    // 优惠卷抵扣费用
    BigDecimal couponPrice = new BigDecimal("0.00");
    // 团购抵扣费用
    BigDecimal grouponPrice = new BigDecimal("0.00");
    // 订单费用
    BigDecimal orderTotalPrice = checkedGoodsPrice.add(freightPrice).subtract(couponPrice).max(new BigDecimal("0.00"));
    // 最终支付费用
    BigDecimal actualPrice = orderTotalPrice.subtract(integralPrice);
    // 组装订单数据
    Order order = new Order();
    order.setUserId(userId);
    order.setOrderSn(orderDTO.getOrderSn());
    order.setOrderStatus(OrderUtil.STATUS_CREATE);
    order.setConsignee(checkedAddress.getName());
    order.setMobile(checkedAddress.getTel());
    order.setMessage(orderDTO.getMessage());
    String detailedAddress = checkedAddress.getProvince() + checkedAddress.getCity() + checkedAddress.getCounty() + " " + checkedAddress.getAddressDetail();
    order.setAddress(detailedAddress);
    order.setFreightPrice(freightPrice);
    order.setCouponPrice(couponPrice);
    order.setGrouponPrice(grouponPrice);
    order.setIntegralPrice(integralPrice);
    order.setGoodsPrice(checkedGoodsPrice);
    order.setOrderPrice(orderTotalPrice);
    order.setActualPrice(actualPrice);
    order.setCreateTime(new Date());
    if (!save(order)) {
        throw new BusinessException("订单创建失败" + JSON.toJSONString(order));
    }
    Long orderId = order.getId();
    List<OrderGoods> orderGoodsList = new ArrayList<>();
    // 添加订单商品表项
    for (Cart cartGoods : checkedGoodsList) {
        // 订单商品
        OrderGoods orderGoods = new OrderGoods();
        orderGoods.setOrderId(orderId);
        orderGoods.setGoodsId(cartGoods.getGoodsId());
        orderGoods.setGoodsSn(cartGoods.getGoodsSn());
        orderGoods.setProductId(cartGoods.getProductId());
        orderGoods.setGoodsName(cartGoods.getGoodsName());
        orderGoods.setPicUrl(cartGoods.getPicUrl());
        orderGoods.setPrice(cartGoods.getPrice());
        orderGoods.setNumber(cartGoods.getNumber());
        orderGoods.setSpecifications(cartGoods.getSpecifications());
        orderGoods.setCreateTime(LocalDateTime.now());
        orderGoodsList.add(orderGoods);
    }
    if (!iOrderGoodsService.saveBatch(orderGoodsList)) {
        throw new BusinessException("添加订单商品表项失败" + JSON.toJSONString(orderGoodsList));
    }
    // 删除购物车里面的商品信息
    if (CollectionUtils.isEmpty(cartIdArr)) {
        iCartService.remove(new QueryWrapper<Cart>().eq("user_id", userId));
    } else {
        iCartService.removeByIds(cartIdArr);
    }
    // 下单60s内未支付自动取消订单
    long delay = 1000;
    redisCache.setCacheZset("order_zset", order.getId(), System.currentTimeMillis() + 60 * delay);
    taskService.addTask(new OrderUnpaidTask(order.getId(), delay * 60));
    return R.success().add("orderId", order.getId());
}
Also used : LambdaQueryWrapper(com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) OrderUnpaidTask(com.wayn.mobile.api.task.OrderUnpaidTask) BigDecimal(java.math.BigDecimal) BusinessException(com.wayn.common.exception.BusinessException) Cart(com.wayn.mobile.api.domain.Cart) Transactional(org.springframework.transaction.annotation.Transactional)

Example 13 with BusinessException

use of com.wayn.common.exception.BusinessException in project waynboot-mall by wayn111.

the class FileUploadUtil method uploadFile.

/**
 * 上传文件
 *
 * @param file     spring MultipartFile文件对象
 * @param filePath 要保存的文件目录
 * @return 新文件名称
 * @throws IOException 上传异常
 */
public static String uploadFile(MultipartFile file, String filePath) throws IOException {
    int fileNameLength = Objects.requireNonNull(file.getOriginalFilename()).length();
    if (fileNameLength > 100) {
        throw new BusinessException("文件名称过长");
    }
    String fileName = file.getOriginalFilename();
    String extension = FilenameUtils.getExtension(fileName);
    if (StringUtils.isEmpty(extension)) {
        extension = MimeTypeUtils.getExtension(Objects.requireNonNull(file.getContentType()));
    }
    String encodingFilename = FileUtils.encodingFilename(fileName);
    fileName = genNewFilename(encodingFilename, extension);
    File desc = new File(filePath, fileName);
    if (!desc.getParentFile().exists()) {
        desc.getParentFile().mkdirs();
    }
    if (!desc.exists()) {
        desc.createNewFile();
    }
    file.transferTo(desc);
    return fileName;
}
Also used : BusinessException(com.wayn.common.exception.BusinessException) MultipartFile(org.springframework.web.multipart.MultipartFile) File(java.io.File)

Example 14 with BusinessException

use of com.wayn.common.exception.BusinessException in project waynboot-mall by wayn111.

the class LoginService method login.

@SneakyThrows
public String login(String mobile, String password) {
    // 用户验证
    Authentication authentication;
    try {
        // 该方法会去调用UserDetailsServiceImpl.loadUserByUsername
        authentication = authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(mobile, password));
    } catch (Exception e) {
        if (e instanceof BadCredentialsException) {
            throw new BadCredentialsException(e.getMessage(), e);
        } else {
            throw new BusinessException(e.getMessage());
        }
    }
    LoginUserDetail principal = (LoginUserDetail) authentication.getPrincipal();
    return tokenService.createToken(principal);
}
Also used : BusinessException(com.wayn.common.exception.BusinessException) Authentication(org.springframework.security.core.Authentication) LoginUserDetail(com.wayn.mobile.framework.security.LoginUserDetail) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) BadCredentialsException(org.springframework.security.authentication.BadCredentialsException) BadCredentialsException(org.springframework.security.authentication.BadCredentialsException) BusinessException(com.wayn.common.exception.BusinessException) SneakyThrows(lombok.SneakyThrows)

Aggregations

BusinessException (com.wayn.common.exception.BusinessException)14 Transactional (org.springframework.transaction.annotation.Transactional)4 LambdaQueryWrapper (com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper)3 QueryWrapper (com.baomidou.mybatisplus.core.conditions.query.QueryWrapper)3 LoginUserDetail (com.wayn.mobile.framework.security.LoginUserDetail)3 Member (com.wayn.common.core.domain.shop.Member)2 OrderHandleOption (com.wayn.common.core.util.OrderHandleOption)2 ReturnCodeEnum (com.wayn.common.enums.ReturnCodeEnum)2 Cart (com.wayn.mobile.api.domain.Cart)2 File (java.io.File)2 BeanUtil (cn.hutool.core.bean.BeanUtil)1 JSON (com.alibaba.fastjson.JSON)1 JSONObject (com.alibaba.fastjson.JSONObject)1 AlipayApiException (com.alipay.api.AlipayApiException)1 AlipayClient (com.alipay.api.AlipayClient)1 DefaultAlipayClient (com.alipay.api.DefaultAlipayClient)1 AlipaySignature (com.alipay.api.internal.util.AlipaySignature)1 AlipayTradeWapPayRequest (com.alipay.api.request.AlipayTradeWapPayRequest)1 IPage (com.baomidou.mybatisplus.core.metadata.IPage)1 Wrappers (com.baomidou.mybatisplus.core.toolkit.Wrappers)1