From 7c25318835f571d09c5dac86e598779dd645e72f Mon Sep 17 00:00:00 2001 From: Cauchy Date: Sun, 8 Oct 2023 18:03:11 +0800 Subject: [PATCH 1/2] format code --- .../book/controller/BuyOrderController.java | 694 ++++++------------ .../book/entity/ShopProductEntity.java | 282 ++++--- .../pay/weChatPay/config/WechatPayConfig.java | 1 - .../controller/WeChatPayController.java | 3 +- .../modules/pay/weChatPay/util/WxPayUtil.java | 3 +- .../mapper/book/ShopProudictBookDao.xml | 2 +- src/main/resources/weChatConfig.properties | 2 +- 7 files changed, 349 insertions(+), 638 deletions(-) diff --git a/src/main/java/com/peanut/modules/book/controller/BuyOrderController.java b/src/main/java/com/peanut/modules/book/controller/BuyOrderController.java index 2b98138a..37620ffd 100644 --- a/src/main/java/com/peanut/modules/book/controller/BuyOrderController.java +++ b/src/main/java/com/peanut/modules/book/controller/BuyOrderController.java @@ -28,7 +28,6 @@ import com.peanut.common.utils.PageUtils; import com.peanut.common.utils.R; - /** * 订单表 * @@ -61,17 +60,17 @@ public class BuyOrderController { @Autowired private UserEbookBuyService userEbookBuyService; @Autowired - private BookService bookService; + private BookService bookService; @Autowired - private BookShelfService bookShelfService; + private BookShelfService bookShelfService; @Autowired - private UserRecordService userRecordService; + private UserRecordService userRecordService; @Autowired - private UserFollowUpService userFollowUpService; + private UserFollowUpService userFollowUpService; @Autowired - private PayWechatOrderService payWechatOrderService; + private PayWechatOrderService payWechatOrderService; @Autowired - private PayZfbOrderService payZfbOrderService; + private PayZfbOrderService payZfbOrderService; /** * 订单状态 - 待支付 @@ -85,6 +84,31 @@ public class BuyOrderController { * 订单状态 - 待收货 */ private static final String ORDER_STATUS_TO_BE_RECEIVED = "2"; + /** + * 支付方式 - 支付宝 + */ + private static final String PAYMENT_METHOD_ALI_PAY = "1"; + /** + * 支付方式 + */ + private static final String PAYMENT_METHOD_WECHAT_PAY = "2"; + /** + * 支付方式 + */ + private static final String PAYMENT_METHOD_IOS = "3"; + /** + * 支付方式 + */ + private static final String PAYMENT_METHOD_VIRTUAL = "4"; + /** + * 购买方式 - 直接购买 + */ + private static final String BUY_TYPE_REDIRECT = "0"; + /** + * 购买方式 - 购物车 + */ + private static final String BUY_TYPE_CART = "1"; + @Autowired private ShopProudictBookService shopProudictBookService; @@ -93,10 +117,9 @@ public class BuyOrderController { * 列表 */ @RequestMapping("/list") -// @RequiresPermissions("book:buyorder:list") public R list(@RequestParam Map params) throws Exception { - if("all".equals(params.get("orderStatus"))){ + if ("all".equals(params.get("orderStatus"))) { params.remove("orderStatus"); } PageUtils page = buyOrderService.queryPage(params); @@ -105,17 +128,15 @@ public class BuyOrderController { /** - * * @param params * @return 听书未购买页面展示 * (销量最多的书,最先放最前面展示。最新上线的书,预售的书,放最前面展示 * @throws Exception */ @RequestMapping("/lists") -// @RequiresPermissions("book:buyorder:list") public R lists(@RequestParam Map params) throws Exception { - if("all".equals(params.get("orderStatus"))){ + if ("all".equals(params.get("orderStatus"))) { params.remove("orderStatus"); } PageUtils page = buyOrderService.queryPages(params); @@ -123,17 +144,12 @@ public class BuyOrderController { } - - - - /** * 信息 */ @RequestMapping("/info/{orderId}") -// @RequiresPermissions("book:buyorder:info") - public R info(@PathVariable("orderId") Integer orderId){ - BuyOrderEntity buyOrder = buyOrderService.getById(orderId); + public R info(@PathVariable("orderId") Integer orderId) { + BuyOrderEntity buyOrder = buyOrderService.getById(orderId); return R.ok().put("buyOrder", buyOrder); } @@ -142,424 +158,144 @@ public class BuyOrderController { /** * 下单 */ - @RequestMapping("/buysave") + @RequestMapping("/buySave") @Transactional - public R buysave(@RequestBody BuyOrderEntity buyOrder){ - BigDecimal realMoney = new BigDecimal(0); - Lock l = new ReentrantLock(); - l.lock(); - try { - // 根据订单获取订单详情 - List products = buyOrder.getProducts(); - BigDecimal bigDecimal1 = new BigDecimal(0); - ArrayList list = new ArrayList<>(); - - // 遍历商品 查询价格 - for (BuyOrderDetailEntity buyOrderDetail : products) { - BuyOrderDetailEntity buyOrderDetailEntity = new BuyOrderDetailEntity(); - Integer productId = buyOrderDetail.getProductId(); - ShopProductEntity product = shopProductService.getById(productId); - BigDecimal activityPrice = product.getActivityPrice(); - BigDecimal big = new BigDecimal(0); - BigDecimal price=null; - - //activityPrice等于 0,则原价 - if (activityPrice.equals(big)||activityPrice==null) { - price = product.getPrice(); - }else { - price = product.getActivityPrice(); - } - - Integer quantity = buyOrderDetail.getQuantity(); - BigDecimal bigDecimal = new BigDecimal(price.doubleValue() * quantity); - System.out.println("bigDecimal=================bigDecimal======================"+bigDecimal); - bigDecimal1 = bigDecimal1.add(bigDecimal); - - if (product.getProductStock() - buyOrderDetail.getQuantity() < 0 ){ - return R.error(500,"库存不足"); - } - // 改写 商品库存 - product.setProductStock(product.getProductStock() - buyOrderDetail.getQuantity()); - product.setSumSales(product.getSumSales() + buyOrderDetail.getQuantity()); - shopProductService.updateById(product); - - BeanUtils.copyProperties(buyOrderDetail,buyOrderDetailEntity); - buyOrderDetailEntity.setProductName(product.getProductName()); - buyOrderDetailEntity.setProductPrice(product.getPrice()); - buyOrderDetailEntity.setAddressId(buyOrder.getAddressId()); - buyOrderDetailEntity.setProductUrl(product.getProductImages()); - buyOrderDetailEntity.setOrderStatus("0"); - list.add(buyOrderDetailEntity); + public R buySave(@RequestBody BuyOrderEntity buyOrder) { + BigDecimal realMoney; + // 根据订单获取订单详情 + List products = buyOrder.getProducts(); + // ? + BigDecimal totalPrice = new BigDecimal(0); + List list = new ArrayList<>(); + // 遍历商品 查询价格 + for (BuyOrderDetailEntity buyOrderDetail : products) { + BuyOrderDetailEntity buyOrderDetailEntity = new BuyOrderDetailEntity(); + Integer productId = buyOrderDetail.getProductId(); + ShopProductEntity product = shopProductService.getById(productId); + BigDecimal activityPrice = product.getActivityPrice(); + // 实际执行价格 + BigDecimal price; + if (activityPrice == null || activityPrice.equals(BigDecimal.ZERO)) { + price = product.getPrice(); + } else { + price = product.getActivityPrice(); } + int quantity = buyOrderDetail.getQuantity(); + totalPrice = totalPrice.add(price.multiply(BigDecimal.valueOf(quantity))); - Integer couponId = buyOrder.getCouponId(); - - if (couponId != null) { - CouponHistoryEntity byId = couponHistoryService.getById(couponId); - CouponEntity coupon = couponService.getById(byId.getCouponId()); - BigDecimal amount = coupon.getCouponAmount(); - bigDecimal1 = bigDecimal1.subtract(amount); - + if (product.getProductStock() - quantity < 0) { + return R.error(500, "库存不足"); } + // 更新商品库存 + product.setProductStock(product.getProductStock() - quantity); + product.setSumSales(product.getSumSales() + quantity); + shopProductService.updateById(product); - if (buyOrder.getShippingMoney() != null) { - bigDecimal1 = bigDecimal1.add(buyOrder.getShippingMoney()); - } - - // 减去优惠券金额 - realMoney = buyOrder.getRealMoney(); - if (bigDecimal1.compareTo(realMoney) == 0) { - //特定格式的时间ID - String timeId = IdWorker.getTimeId().substring(0,32); - buyOrder.setOrderSn(timeId); - if("4".equals(buyOrder.getPaymentMethod())){ - buyOrder.setOrderStatus(ORDER_STATUS_TO_BE_SHIPPED); - } - buyOrder.setPaymentDate(new Date()); - buyOrderService.save(buyOrder); - - for (BuyOrderDetailEntity buyOrderDetailEntity : list) { - buyOrderDetailEntity.setOrderId(buyOrder.getOrderId()); - buyOrderDetailEntity.setUserId(buyOrder.getUserId()); - - // 判断结算状态 下单 位置 0- 商品页直接下单 1- 购物车结算 - String buyType = buyOrder.getBuyType(); - - if (buyType.equals("1")) { - // 更改购物车 状态 - List list1 = orderCartService.getBaseMapper().selectList(new QueryWrapper() - .eq("user_id", buyOrder.getUserId()).eq("product_id", buyOrderDetailEntity.getProductId())); - if (list1.size() > 0){ - List collect = list1.stream().map(orderCartEntity -> { - Integer cartId = orderCartEntity.getCartId(); - return cartId; - }).collect(Collectors.toList()); - - orderCartService.removeByIds(collect); - } - } - - } - - buyOrderDetailService.saveBatch(list); - if(couponId!=null){ - //更改优惠券状态 - CouponHistoryEntity one = couponHistoryService.getById(couponId); - one.setUseStatus(1); - one.setUseTime(new Date()); - one.setOrderId(Long.valueOf(buyOrder.getOrderId())); - one.setOrderSn(buyOrder.getOrderSn()); - couponHistoryService.updateById(one); - } - if("4".equals(buyOrder.getPaymentMethod())){ - MyUserEntity user = this.myUserService.getById(buyOrder.getUserId()); - if(user.getPeanutCoin().compareTo(realMoney) >= 0){ - user.setPeanutCoin(user.getPeanutCoin().subtract(realMoney)); - this.myUserService.updateById(user); - // 添加消费信息 - TransactionDetailsEntity transactionDetailsEntity = new TransactionDetailsEntity(); - transactionDetailsEntity.setRemark("购买健康超市用品!订单编号为《 "+ buyOrder.getOrderSn() + "》"); - transactionDetailsEntity.setUserId(user.getId()); - transactionDetailsEntity.setUserName(user.getNickname()); - transactionDetailsEntity.setChangeAmount(realMoney.negate()); - transactionDetailsEntity.setUserBalance(user.getPeanutCoin()); - transactionDetailsEntity.setTel(user.getTel()); - transactionDetailsEntity.setOrderType("购买健康超市用品!"); - transactionDetailsService.save(transactionDetailsEntity); - - //购买成功后,添加书到个人表中 - List pros = products.stream().map(BuyOrderDetailEntity::getProductId).collect(Collectors.toList()); - for (Integer s : pros){ - List collect = shopProudictBookService.getBaseMapper().selectList(new LambdaQueryWrapper() - .eq(ShopProudictBookEntity::getProudictId, s) - .eq(ShopProudictBookEntity::getDelFlag, 0)).stream().map(ShopProudictBookEntity::getBookId).collect(Collectors.toList()); - userEbookBuyService.addBookForUser(buyOrder.getUserId(),collect); - } - - }else{ - return R.error("余额不足!"); - } - } - } - }catch (Exception e){ - e.printStackTrace(); - }finally { - l.unlock(); + BeanUtils.copyProperties(buyOrderDetail, buyOrderDetailEntity); + buyOrderDetailEntity.setProductName(product.getProductName()); + buyOrderDetailEntity.setProductPrice(product.getPrice()); + buyOrderDetailEntity.setAddressId(buyOrder.getAddressId()); + buyOrderDetailEntity.setProductUrl(product.getProductImages()); + buyOrderDetailEntity.setOrderStatus(ORDER_STATUS_TO_BE_PAID); + list.add(buyOrderDetailEntity); } - return R.ok().put("orderSn",buyOrder.getOrderSn()).put("money",realMoney); + Integer couponId = buyOrder.getCouponId(); + + if (couponId != null) { + CouponHistoryEntity couponHistory = couponHistoryService.getById(couponId); + CouponEntity coupon = couponService.getById(couponHistory.getCouponId()); + BigDecimal amount = coupon.getCouponAmount(); + totalPrice = totalPrice.subtract(amount); + } + + if (buyOrder.getShippingMoney() != null) { + totalPrice = totalPrice.add(buyOrder.getShippingMoney()); + } + + // 减去优惠券金额 + realMoney = buyOrder.getRealMoney(); + if (totalPrice.compareTo(realMoney) == 0) { + //特定格式的时间ID + String timeId = IdWorker.getTimeId().substring(0, 32); + buyOrder.setOrderSn(timeId); + if (PAYMENT_METHOD_VIRTUAL.equals(buyOrder.getPaymentMethod())) { + buyOrder.setOrderStatus(ORDER_STATUS_TO_BE_SHIPPED); + } + buyOrder.setPaymentDate(new Date()); + buyOrderService.save(buyOrder); + + for (BuyOrderDetailEntity buyOrderDetailEntity : list) { + buyOrderDetailEntity.setOrderId(buyOrder.getOrderId()); + buyOrderDetailEntity.setUserId(buyOrder.getUserId()); + // 判断结算状态 + String buyType = buyOrder.getBuyType(); + if (buyType.equals(BUY_TYPE_CART)) { + // 更改购物车 状态 + List orderCartList = orderCartService.getBaseMapper().selectList(new QueryWrapper() + .eq("user_id", buyOrder.getUserId()).eq("product_id", buyOrderDetailEntity.getProductId())); + if (orderCartList.size() > 0) { + List collect = orderCartList.stream().map(orderCartEntity -> { + Integer cartId = orderCartEntity.getCartId(); + return cartId; + }).collect(Collectors.toList()); + orderCartService.removeByIds(collect); + } + } + + } + + buyOrderDetailService.saveBatch(list); + if (couponId != null) { + //更改优惠券状态 + CouponHistoryEntity one = couponHistoryService.getById(couponId); + one.setUseStatus(1); + one.setUseTime(new Date()); + one.setOrderId(Long.valueOf(buyOrder.getOrderId())); + one.setOrderSn(buyOrder.getOrderSn()); + couponHistoryService.updateById(one); + } + if ("4".equals(buyOrder.getPaymentMethod())) { + MyUserEntity user = this.myUserService.getById(buyOrder.getUserId()); + if (user.getPeanutCoin().compareTo(realMoney) >= 0) { + user.setPeanutCoin(user.getPeanutCoin().subtract(realMoney)); + this.myUserService.updateById(user); + // 添加消费信息 + TransactionDetailsEntity transactionDetailsEntity = new TransactionDetailsEntity(); + transactionDetailsEntity.setRemark("购买健康超市用品!订单编号为《 " + buyOrder.getOrderSn() + "》"); + transactionDetailsEntity.setUserId(user.getId()); + transactionDetailsEntity.setUserName(user.getNickname()); + transactionDetailsEntity.setChangeAmount(realMoney.negate()); + transactionDetailsEntity.setUserBalance(user.getPeanutCoin()); + transactionDetailsEntity.setTel(user.getTel()); + transactionDetailsEntity.setOrderType("购买健康超市用品!"); + transactionDetailsService.save(transactionDetailsEntity); + + //购买成功后,添加书到个人表中 + List pros = products.stream().map(BuyOrderDetailEntity::getProductId).collect(Collectors.toList()); + for (Integer s : pros) { + List collect = shopProudictBookService.getBaseMapper().selectList(new LambdaQueryWrapper() + .eq(ShopProudictBookEntity::getProudictId, s) + .eq(ShopProudictBookEntity::getDelFlag, 0)).stream().map(ShopProudictBookEntity::getBookId).collect(Collectors.toList()); + userEbookBuyService.addBookForUser(buyOrder.getUserId(), collect); + } + + } else { + return R.error("余额不足!"); + } + } + } + return R.ok().put("orderSn", buyOrder.getOrderSn()).put("money", realMoney); } /** - * 修改购买书籍(赠送电子书,加鉴权) - * @param buyOrder 订单表 - * @return + * 修改 */ - @RequestMapping("/buysave1") - @Transactional - public R buysave1(@RequestBody BuyOrderEntity buyOrder) { - - BigDecimal realMoney = new BigDecimal(0); - - Lock l = new ReentrantLock(); - l.lock(); - try { - - List products = buyOrder.getProducts(); - - - BigDecimal bigDecimal1 = new BigDecimal(0); - - ArrayList list = new ArrayList<>(); - - // 遍历商品 查询价格 - for (BuyOrderDetailEntity buyOrderDetail : products) { - BuyOrderDetailEntity buyOrderDetailEntity = new BuyOrderDetailEntity(); - Integer productId = buyOrderDetail.getProductId(); - ShopProductEntity product = shopProductService.getById(productId); -// BigDecimal price = product.getPrice(); - Integer quantity = buyOrderDetail.getQuantity(); - BigDecimal activityPrice = product.getActivityPrice(); - BigDecimal big = new BigDecimal(0); - BigDecimal price=null; - //activityPrice等于0,则原价 - if (activityPrice == big) { - price = product.getPrice(); - - }else { - price = product.getActivityPrice(); - } - - - //价格*数量 = 单价*购买数量赋值给bigDecimal1 - BigDecimal bigDecimal = new BigDecimal(price.doubleValue() * quantity); - System.out.println("bigDecimal========bigDecimal=========bigDecimal======================"+bigDecimal); - bigDecimal1 = bigDecimal1.add(bigDecimal); - - if (product.getProductStock() - buyOrderDetail.getQuantity() < 0) { - return R.error(500, "库存不足"); - } - - // 改写 商品库存 - product.setProductStock(product.getProductStock() - buyOrderDetail.getQuantity()); - product.setSumSales(product.getSumSales() + buyOrderDetail.getQuantity()); - shopProductService.updateById(product); - //buyOrderDetail 对象中的属性值复制到 buyOrderDetailEntity 对象中的属性值中 - BeanUtils.copyProperties(buyOrderDetail, buyOrderDetailEntity); - buyOrderDetailEntity.setProductName(product.getProductName()); - buyOrderDetailEntity.setProductPrice(product.getPrice()); - buyOrderDetailEntity.setAddressId(buyOrder.getAddressId()); - buyOrderDetailEntity.setProductUrl(product.getProductImages()); - buyOrderDetailEntity.setOrderStatus("0"); - list.add(buyOrderDetailEntity); - } - //优惠券Id couponId - Integer couponId = buyOrder.getCouponId(); - - if (couponId != null) { - CouponHistoryEntity byId = couponHistoryService.getById(couponId); - CouponEntity coupon = couponService.getById(byId.getCouponId()); - BigDecimal amount = coupon.getCouponAmount(); - bigDecimal1 = bigDecimal1.subtract(amount); - } - if (buyOrder.getShippingMoney() != null) { - bigDecimal1 = bigDecimal1.add(buyOrder.getShippingMoney()); - } - // 减去优惠券金额 - realMoney = buyOrder.getRealMoney(); - if (bigDecimal1.compareTo(realMoney) == 0) { - //特定格式的时间ID - String timeId = IdWorker.getTimeId().substring(0, 32); - - buyOrder.setOrderSn(timeId); - if ("4".equals(buyOrder.getPaymentMethod())) { - buyOrder.setOrderStatus("1"); - - } - //todo 增加结束时间 - buyOrder.setPaymentDate(new Date()); - buyOrderService.save(buyOrder); - - for (BuyOrderDetailEntity buyetailEntity : list) { - - buyetailEntity.setOrderId(buyOrder.getOrderId()); - buyetailEntity.setUserId(buyOrder.getUserId()); - - // 判断结算状态 下单 位置 0- 商品页直接下单 1- 购物车结算 - String buyType = buyOrder.getBuyType(); - if (buyType.equals("1")) { - // 更改购物车 状态 - List list1 = orderCartService.getBaseMapper().selectList(new QueryWrapper() - .eq("user_id", buyOrder.getUserId()).eq("product_id", buyetailEntity.getProductId())); - //将购物车列表转换为流,并从中提取每个购物车的 cartId。removeByIds() 方法从数据库中删除这些购物车记录。 - if (list1.size() > 0) { - List collect = list1.stream().map(orderCartEntity -> { - Integer cartId = orderCartEntity.getCartId(); - return cartId; - }).collect(Collectors.toList()); - - orderCartService.removeByIds(collect); - } - } - - } - - buyOrderDetailService.saveBatch(list); - if (couponId != null) { - //更改优惠券状态 - CouponHistoryEntity one = couponHistoryService.getById(couponId); - one.setUseStatus(1); - one.setUseTime(new Date()); - one.setOrderId(Long.valueOf(buyOrder.getOrderId())); - one.setOrderSn(buyOrder.getOrderSn()); - couponHistoryService.updateById(one); - - } - // 购买书籍直接赠送电子书听书 - TransactionDetailsEntity transaction = new TransactionDetailsEntity(); - - //避免重复购买 - TransactionDetailsEntity entity = transactionDetailsService.getBaseMapper().selectOne(new QueryWrapper().eq("user_id", transaction.getUserId()) - .eq("relation_id", transaction.getRelationId())); - if (entity != null) { - return R.error("余额不足,请充值!!!!!!!!!!!!!!!!"); - } - - List produ = buyOrder.getProducts(); - // 遍历商品 查询价格 - for (BuyOrderDetailEntity buyOrdr : produ) { - Integer pId = buyOrdr.getProductId(); - ShopProductEntity product1 = shopProductService.getById(pId); - String productName = product1.getProductName(); - // 如果是虚拟币购买 减少用户的虚拟币数量 - if ("4".equals(buyOrder.getPaymentMethod())) { - MyUserEntity user = this.myUserService.getById(buyOrder.getUserId()); - if (user.getPeanutCoin().compareTo(realMoney) >= 0) { - user.setPeanutCoin(user.getPeanutCoin().subtract(realMoney)); - this.myUserService.updateById(user); - // 添加消费信息 - TransactionDetailsEntity transactionDetailsEntity = new TransactionDetailsEntity(); - transactionDetailsEntity.setRemark("购买健康超市用品!订单编号为《 " + buyOrder.getOrderSn() + "》"); - transactionDetailsEntity.setUserId(user.getId()); - transactionDetailsEntity.setUserName(user.getNickname()); - transactionDetailsEntity.setChangeAmount(realMoney.negate()); - transactionDetailsEntity.setUserBalance(user.getPeanutCoin()); - transactionDetailsEntity.setTel(user.getTel()); - transactionDetailsEntity.setOrderType("购买《"+productName+"》赠送本书听书功能"); - transactionDetailsService.save(transactionDetailsEntity); - } else { - return R.error("余额不足!"); - } - } - } -// } - - - for (BuyOrderDetailEntity buyOrderDetail : products) { - Integer productId = buyOrderDetail.getProductId(); - ShopProductEntity product = shopProductService.getById(productId); - // 如果不等于空 则进行往下走 - String authorName = ""; - String bookId = product.getBookId(); - if(null != bookId && !"".equals(bookId)){ - List bkids = new ArrayList(); - if(bookId.indexOf(",") == -1){ - bkids.add(bookId); - }else { - for(String idObj : bookId.split(",")){ - bkids.add(idObj); - } - } - for(String b_id : bkids){ - if (b_id != null) { - List book = bookService.getBaseMapper().selectList(new QueryWrapper().eq("id", b_id)); - for (BookEntity bo : book) { - Integer id = bo.getId(); - String name = bo.getName(); - String images = bo.getImages(); - String authorId = bo.getAuthorId(); - if (book != null) { - UserEbookBuyEntity userEbookBuyEntity = new UserEbookBuyEntity(); - userEbookBuyEntity.setUserId(Integer.valueOf(buyOrder.getUserId())); - userEbookBuyEntity.setBookId(Integer.valueOf(id)); - String[] authorIds = authorId.split(","); - List authorList = Arrays.asList(authorIds); - List authorEntities = authorService.getBaseMapper().selectList(new QueryWrapper().in("id", authorList)); - //购买书籍表 userId bookName 商品订单详情表 userId productName - List userId = userEbookBuyService.getBaseMapper().selectList(new QueryWrapper() - .eq("user_Id", buyOrder.getUserId()) - .eq("book_id", id)); - for (AuthorEntity authorEntity : authorEntities) { - authorName += "," + authorEntity.getAuthorName(); - } - - //查询dengyu,则往下执行赠送电子书详情 - if (userId != null && !userId.isEmpty()) { -// 如果userId不等于空则不赠送图书不执行任何方法 -// return R.ok("此书已存在于您的书架"); - } else { - authorName = authorName.startsWith(",") ? authorName.substring(1) : authorName; - userEbookBuyEntity.setAuthor(authorName); - userEbookBuyEntity.setImage(images); - userEbookBuyEntity.setPayStatus("赠送成功"); - //这里后期需要修改字段内容 - userEbookBuyEntity.setPayType("point"); - userEbookBuyEntity.setPayTime(new Date()); - userEbookBuyEntity.setBookName(name); - userEbookBuyService.save(userEbookBuyEntity); - //判断是否加入书架如果没有就加入听书书架 - Integer integer = bookShelfService.getBaseMapper().selectCount(new QueryWrapper() - .eq("book_id", id) - .eq("user_id", userId)); - //如果integer<0代表数据库没有这条数据,则添加,有就不执行下面操作 - if (integer < 0) { - //保存到书架表中 - BookShelfEntity bookShelfEntity = new BookShelfEntity(); - bookShelfEntity.setBookId(id); - bookShelfEntity.setBookName(name); - bookShelfEntity.setUserId(buyOrder.getUserId()); - bookShelfEntity.setCreateTime(new Date()); - bookShelfService.save(bookShelfEntity); - } -// } -// else { -// return R.error().put("赠送失败",buyOrder.getOrderSn()); -// } - - - } - - } - } - - } - } - } - } - - - - } - - }catch (Exception e){ - e.printStackTrace(); - }finally { - l.unlock(); - } - return R.ok().put("orderSn",buyOrder.getOrderSn()).put("money",realMoney); - } - - - - - /** - * 修改 - */ @RequestMapping("/update") // @RequiresPermissions("book:buyorder:update") - public R update(@RequestBody BuyOrderEntity buyOrder){ - buyOrderService.updateById(buyOrder); + public R update(@RequestBody BuyOrderEntity buyOrder) { + buyOrderService.updateById(buyOrder); return R.ok(); } @@ -569,8 +305,8 @@ public class BuyOrderController { */ @RequestMapping("/delete") // @RequiresPermissions("book:buyorder:delete") - public R delete(@RequestBody Integer[] orderIds){ - buyOrderService.removeByIds(Arrays.asList(orderIds)); + public R delete(@RequestBody Integer[] orderIds) { + buyOrderService.removeByIds(Arrays.asList(orderIds)); return R.ok(); } @@ -580,7 +316,7 @@ public class BuyOrderController { */ @RequestMapping("/appUserGetlist") // @RequiresPermissions("book:buyorder:list") - public R appUserGetlist(@RequestParam Map params){ + public R appUserGetlist(@RequestParam Map params) { PageUtils page = buyOrderService.queryPage1(params); return R.ok().put("page", page); @@ -593,7 +329,7 @@ public class BuyOrderController { @RequestMapping("/appDelete") // @RequiresPermissions("book:buyorder:delete") @Transactional - public R appDelete(@RequestParam("orderId") Integer orderId){ + public R appDelete(@RequestParam("orderId") Integer orderId) { //1. 判断订单状态 BuyOrderEntity byId = buyOrderService.getById(orderId); @@ -649,7 +385,7 @@ public class BuyOrderController { @RequestMapping("/randomOrderCode") @Transactional - public R randomOrderCode(@RequestBody BuyOrderEntity buyOrder){ + public R randomOrderCode(@RequestBody BuyOrderEntity buyOrder) { // String re= String.valueOf(buyOrderService.randomOrderCode(buyOrder)); @@ -659,37 +395,32 @@ public class BuyOrderController { } - /** * 充值专用订单生成接口 */ @RequestMapping("/rechargeSave") @Transactional - public R rechargeSave(@RequestBody BuyOrderEntity buyOrder){ - - - String timeId = IdWorker.getTimeId().substring(0,32); - buyOrder.setOrderSn(timeId); - buyOrderService.save(buyOrder); - return R.ok().put("orderSn",timeId); - } - - + public R rechargeSave(@RequestBody BuyOrderEntity buyOrder) { + String timeId = IdWorker.getTimeId().substring(0, 32); + buyOrder.setOrderSn(timeId); + buyOrderService.save(buyOrder); + return R.ok().put("orderSn", timeId); + } /** * 信息 */ @RequestMapping("/appGetOrderInfo/{type}") - public R appGetOrderInfo(@PathVariable String type , @RequestParam("orderId") Integer orderId){ + public R appGetOrderInfo(@PathVariable String type, @RequestParam("orderId") Integer orderId) { BuyOrderEntity buyOrder = buyOrderService.getById(orderId); List orderDetail = null; - if("1".equals(type)){ + if ("1".equals(type)) { orderDetail = buyOrderDetailService.getBaseMapper().selectList(new QueryWrapper() .eq("order_id", orderId)); - }else{ + } else { orderDetail = buyOrderDetailService.getBaseMapper().selectList(new QueryWrapper() .eq("order_id", orderId)); //TODO 根据shipping_sn快递单号分组,订单下无快递单号,我的订单同时无法显示,暂时注释 @@ -707,15 +438,13 @@ public class BuyOrderController { } - - List resultOrder = new ArrayList(); Set sn_no = new HashSet(); - for(BuyOrderDetailEntity buyOrderDetailEntity : orderDetail){ + for (BuyOrderDetailEntity buyOrderDetailEntity : orderDetail) { - resultOrder.add(buyOrderDetailEntity); - sn_no.add(buyOrderDetailEntity.getShippingSn()); + resultOrder.add(buyOrderDetailEntity); + sn_no.add(buyOrderDetailEntity.getShippingSn()); } @@ -723,38 +452,34 @@ public class BuyOrderController { UserRecordEntity userRecordEntity = userRecordService.getBaseMapper().selectOne(new QueryWrapper() .eq("orderSn", buyOrder.getOrderSn()) .eq("userid", buyOrder.getUserId()) - .eq("orderdid",buyOrder.getOrderId()) + .eq("orderdid", buyOrder.getOrderId()) .last("LIMIT 1")); - - Integer id =null; + Integer id = null; if (userRecordEntity != null) { - id = userRecordEntity.getId(); + id = userRecordEntity.getId(); } - - buyOrder.setProducts(resultOrder); Date createDate = buyOrder.getCreateTime(); - - return R.ok().put("buyOrder", buyOrder).put("CreateTime",createDate).put("userRecordid",id); + return R.ok().put("buyOrder", buyOrder).put("CreateTime", createDate).put("userRecordid", id); } /** * 计算快递费用 */ @RequestMapping("/getTransPrice/{area}") - public R getTransPrice(@PathVariable String area, @RequestParam Map productMap){ + public R getTransPrice(@PathVariable String area, @RequestParam Map productMap) { - Map params = new HashMap<>(); - params.put("kdCode","YD"); - params.put("area",area); + Map params = new HashMap<>(); + params.put("kdCode", "YD"); + params.put("area", area); - int price = this.buyOrderService.getProductGoodsType(params,productMap); + int price = this.buyOrderService.getProductGoodsType(params, productMap); return R.ok().put("price", price); @@ -763,97 +488,92 @@ public class BuyOrderController { /** * 后台发货按钮 + * * @Param shipperCode 快递公司编码 * @Param sendType 0:订单列表发货 1:商品列表发货 * @Param type 合并发货/拆分发货 * @Param ids 订单id串 */ @RequestMapping("/delivery/{shipperCode}") - public R delivery(@PathVariable("shipperCode") String shipperCode,@RequestParam("shipperName") String shipperName, @RequestBody Integer[] ids){ + public R delivery(@PathVariable("shipperCode") String shipperCode, @RequestParam("shipperName") String shipperName, @RequestBody Integer[] ids) { - buyOrderService.sendFMS(ids,shipperCode,shipperName); + buyOrderService.sendFMS(ids, shipperCode, shipperName); - return R.ok(); + return R.ok(); } - - - /** - * 及时查询快递信息 + * 及时查询快递信息 */ @RequestMapping("/queryFMS") - public R queryFMS(@RequestParam Map< String,String> params){ + public R queryFMS(@RequestParam Map params) { List detailList = this.buyOrderDetailService.getBaseMapper().selectList(new QueryWrapper().eq("order_id", params.get("orderId"))); List jsonList = new ArrayList<>(); - JSONObject jsonObj =null; + JSONObject jsonObj = null; for (BuyOrderDetailEntity detail : detailList) { - jsonObj = buyOrderService.queryFMS(detail.getShipperCode(), detail.getShippingSn()); - if(Objects.isNull(jsonObj)){ + jsonObj = buyOrderService.queryFMS(detail.getShipperCode(), detail.getShippingSn()); + if (Objects.isNull(jsonObj)) { return R.ok("暂未查到物流信息!"); } - jsonObj.put("ShipperName",detail.getShipperName()); + jsonObj.put("ShipperName", detail.getShipperName()); jsonList.add(jsonObj); } - return R.ok().put("rntStr",jsonList); + return R.ok().put("rntStr", jsonList); } /** * 检查可合并的订单信息 + * * @return */ @RequestMapping("/checkOrder") - public R checkOrder(@RequestParam Map params){ + public R checkOrder(@RequestParam Map params) { Page page = buyOrderService.checkOrder(params); - return R.ok().put("page",page); + return R.ok().put("page", page); } - /** - * * 检查传来的orderId 是否有可合并的其他订单信息 * * @param orderIds * @return */ @RequestMapping("/checkMerge") - public R checkMerge(@RequestBody Integer[] orderIds){ + public R checkMerge(@RequestBody Integer[] orderIds) { List list = buyOrderService.checkOrder(orderIds); - return R.ok().put("list",list); + return R.ok().put("list", list); } /** - * * 批量发货功能 * - * @param orderDetailIds 订单详情 + * @param orderDetailIds 订单详情 * @return */ @RequestMapping("/blendSendFMS/{shipperCode}") - public R blendSendFMS(@PathVariable("shipperCode") String shipperCode,@RequestParam("shipperName") String shipperName,@RequestBody Integer[] orderDetailIds){ + public R blendSendFMS(@PathVariable("shipperCode") String shipperCode, @RequestParam("shipperName") String shipperName, @RequestBody Integer[] orderDetailIds) { - buyOrderService.blendSendFMS(orderDetailIds,shipperCode,shipperName); + buyOrderService.blendSendFMS(orderDetailIds, shipperCode, shipperName); return R.ok(); } - /** * 后台取消订单接口 */ @RequestMapping("/cancelFMS") - public R cancelFMS(@RequestParam Map params){ + public R cancelFMS(@RequestParam Map params) { buyOrderService.cancelFMS(params.get("orderSn").toString(), params.get("shipperCode").toString(), params.get("expNo").toString()); // return R.ok() - return R.ok().put("paramsTEXT",params); + return R.ok().put("paramsTEXT", params); } /** @@ -863,7 +583,7 @@ public class BuyOrderController { * @return */ @RequestMapping("/querySheetPage") - public R querySheetPage(@RequestParam Map params){ + public R querySheetPage(@RequestParam Map params) { PageUtils page = buyOrderDetailService.querySheet(params); diff --git a/src/main/java/com/peanut/modules/book/entity/ShopProductEntity.java b/src/main/java/com/peanut/modules/book/entity/ShopProductEntity.java index 683f156c..1e120928 100644 --- a/src/main/java/com/peanut/modules/book/entity/ShopProductEntity.java +++ b/src/main/java/com/peanut/modules/book/entity/ShopProductEntity.java @@ -1,19 +1,17 @@ package com.peanut.modules.book.entity; import com.baomidou.mybatisplus.annotation.*; +import lombok.Data; -import java.math.BigDecimal; import java.io.Serializable; +import java.math.BigDecimal; +import java.util.ArrayList; import java.util.Date; import java.util.List; -import java.util.ArrayList; -import java.util.Map; - -import lombok.Data; /** * 商品表 - * + * * @author yl * @email yl328572838@163.com * @date 2022-11-30 10:30:56 @@ -21,154 +19,148 @@ import lombok.Data; @Data @TableName("shop_product") public class ShopProductEntity implements Serializable { - private static final long serialVersionUID = 1L; + private static final long serialVersionUID = 1L; - /** - * - */ - @TableId - private Integer productId; - /** - * 商品名称 - */ - private String productName; - /** - * 商品价格 - */ - private BigDecimal price; - /** - * 商品活动价格 - */ - private BigDecimal activityPrice; - /** - * 商品重量 - */ - private Float weight; - /** - * 上架状态 - */ - private Integer publishStatus; - /** - * 商品介绍 - */ - private String productDetails; - /** - * 商品分类id - */ - private Integer productPid; - /** - * 商品 类型 0 - 预售 1 - 在售 - */ - private String productType; - /** - * 商品库存 - */ - private Integer productStock; - /** - * 商品图首页 - */ - private String productImages; - /** - * 商品 多图 - */ - private String productImageList; - /** - * 商品销量 - */ - private String productSalesVolume; - /** - * 创建时间 - */ - private Date createTime; - /** - * 更新时间 - */ - private Date updateTime; - /** - * 删除标记 - */ - @TableLogic - private Integer delFlag; - /** - * 排序 - */ - private Integer sort; + /** + * + */ + @TableId + private Integer productId; + /** + * 商品名称 + */ + private String productName; + /** + * 商品价格 + */ + private BigDecimal price; + /** + * 商品活动价格 + */ + private BigDecimal activityPrice; + /** + * 商品重量 + */ + private Float weight; + /** + * 上架状态 + */ + private Integer publishStatus; + /** + * 商品介绍 + */ + private String productDetails; + /** + * 商品分类id + */ + private Integer productPid; + /** + * 商品 类型 0 - 预售 1 - 在售 + */ + private String productType; + /** + * 商品库存 + */ + private Integer productStock; + /** + * 商品图首页 + */ + private String productImages; + /** + * 商品 多图 + */ + private String productImageList; + /** + * 商品销量 + */ + private String productSalesVolume; + /** + * 创建时间 + */ + private Date createTime; + /** + * 更新时间 + */ + private Date updateTime; + /** + * 删除标记 + */ + @TableLogic + private Integer delFlag; + /** + * 排序 + */ + private Integer sort; - @TableField(exist = false) - private List poids; - /** - * 作者 - */ - private String author; - /** - * 出版方 - */ - private String publisher; - /** - * 出版时间 - */ - @TableField(value = "pub_date" , updateStrategy = FieldStrategy.IGNORED) - private String pubDate; - /** - * 开本 - */ - private String format; - /** - * 页数 - */ - @TableField(value = "page_num" , updateStrategy = FieldStrategy.IGNORED) - private Integer pageNum; - /** - * 内文用纸材质 - */ - private String quality; - /** - * 总销量 - */ - private Integer sumSales; - /** - * 商品类型 1: 画册 2:书 3:仪器,4:预售书 - */ - private String goodsType; - private String goodsTypeCode; + @TableField(exist = false) + private List poids; + /** + * 作者 + */ + private String author; + /** + * 出版方 + */ + private String publisher; + /** + * 出版时间 + */ + @TableField(value = "pub_date", updateStrategy = FieldStrategy.IGNORED) + private String pubDate; + /** + * 开本 + */ + private String format; + /** + * 页数 + */ + @TableField(value = "page_num", updateStrategy = FieldStrategy.IGNORED) + private Integer pageNum; + /** + * 内文用纸材质 + */ + private String quality; + /** + * 总销量 + */ + private Integer sumSales; + /** + * 商品类型 1: 画册 2:书 3:仪器,4:预售书 + */ + private String goodsType; + private String goodsTypeCode; - /** - * 是否包邮 0:包邮 ,1:不包邮 - */ - private Object isFreeMail; + /** + * 是否包邮 0:包邮 ,1:不包邮 + */ + private Object isFreeMail; - /** - * 绑定电子书id, - */ - @TableField("book_ids") - private String bookId; + /** + * 绑定电子书id, + */ + @TableField("book_ids") + private String bookId; - /** - * 多个电子书Id - */ - @TableField(exist = false) - private ArrayList bookids; + /** + * 多个电子书Id + */ + @TableField(exist = false) + private ArrayList bookids; - /** - * 多个电子书Id ArrayList> - */ - @TableField(exist = false) - private List bookidsimages; + /** + * 多个电子书Id ArrayList> + */ + @TableField(exist = false) + private List bookidsimages; - @TableField(exist = false) - private List shoproudBook; + @TableField(exist = false) + private List shoproudBook; - @TableField(exist = false) - private List shoproudIds; - - @TableField(exist = false) - private List shoproudLabels; - - -// private Object bookidsimages; -// -//e + @TableField(exist = false) + private List shoproudIds; + @TableField(exist = false) + private List shoproudLabels; } diff --git a/src/main/java/com/peanut/modules/pay/weChatPay/config/WechatPayConfig.java b/src/main/java/com/peanut/modules/pay/weChatPay/config/WechatPayConfig.java index 05914449..72636938 100644 --- a/src/main/java/com/peanut/modules/pay/weChatPay/config/WechatPayConfig.java +++ b/src/main/java/com/peanut/modules/pay/weChatPay/config/WechatPayConfig.java @@ -77,7 +77,6 @@ public class WechatPayConfig implements Serializable { /** * 支付通知回调地址 */ - private String notifyUrl; /** * 退款回调地址 diff --git a/src/main/java/com/peanut/modules/pay/weChatPay/controller/WeChatPayController.java b/src/main/java/com/peanut/modules/pay/weChatPay/controller/WeChatPayController.java index d09c09df..84043155 100644 --- a/src/main/java/com/peanut/modules/pay/weChatPay/controller/WeChatPayController.java +++ b/src/main/java/com/peanut/modules/pay/weChatPay/controller/WeChatPayController.java @@ -109,7 +109,8 @@ public class WeChatPayController { // 订单编号 paramMap.put("out_trade_no", order.getOrderSn()); // paramMap.put("attach",""); //自定义数据 支付完成后才能显示 在查询API和支付通知中原样返回,可作为自定义参数使用 - paramMap.put("notify_url", wechatPayConfig.getNotifyUrl()); + //paramMap.put("notify_url", wechatPayConfig.getNotifyUrl()); + paramMap.put("notify_url","https://testapi.nuttyreading.com/pay/payNotify"); // paramMap.put("time_expire",afterString); // 实收金额0.38乘100=38 diff --git a/src/main/java/com/peanut/modules/pay/weChatPay/util/WxPayUtil.java b/src/main/java/com/peanut/modules/pay/weChatPay/util/WxPayUtil.java index 52212dd2..d4a95126 100644 --- a/src/main/java/com/peanut/modules/pay/weChatPay/util/WxPayUtil.java +++ b/src/main/java/com/peanut/modules/pay/weChatPay/util/WxPayUtil.java @@ -1,7 +1,6 @@ package com.peanut.modules.pay.weChatPay.util; - - import com.alibaba.fastjson.JSONObject; +import com.alibaba.fastjson.JSONObject; import com.wechat.pay.contrib.apache.httpclient.WechatPayHttpClientBuilder; import com.wechat.pay.contrib.apache.httpclient.util.PemUtil; import lombok.Data; diff --git a/src/main/resources/mapper/book/ShopProudictBookDao.xml b/src/main/resources/mapper/book/ShopProudictBookDao.xml index b5b1f400..4b6dfc80 100644 --- a/src/main/resources/mapper/book/ShopProudictBookDao.xml +++ b/src/main/resources/mapper/book/ShopProudictBookDao.xml @@ -18,7 +18,7 @@ FROM shop_proudict_book spb LEFT JOIN buy_order_detail bod ON spb.proudict_id = bod.product_id LEFT JOIN buy_order bo ON bo.order_id = bod.order_id - WHERE bo.order_sn = #{orderSn} + WHERE bo.order_sn = #{orderSn} AND del_flag != -1 diff --git a/src/main/resources/weChatConfig.properties b/src/main/resources/weChatConfig.properties index c9dcee5a..3dea880d 100644 --- a/src/main/resources/weChatConfig.properties +++ b/src/main/resources/weChatConfig.properties @@ -10,7 +10,7 @@ wxpay.baseUrl: https://api.mch.weixin.qq.com/v3 #po9k1ezoyexk.ngrok.xiaomiqiu123.top # ???????? #wxpay.notifyUrl: http://101.201.146.165:9100 -wxpay.notifyUrl: http://59.110.212.44:9100/pb/pay/payNotify +wxpay.notifyUrl: http://59.110.212.44:9200/pb/pay/payNotify # ?????? wxpay.refundNotifyUrl: http://pjm6m9.natappfree.cc/pay/refundNotify From 0453bead38619fd3e2d25bd4afd4acb4d0012ad9 Mon Sep 17 00:00:00 2001 From: Cauchy Date: Mon, 9 Oct 2023 13:27:13 +0800 Subject: [PATCH 2/2] optimize the code --- .../book/controller/BuyOrderController.java | 373 ++++++++---------- .../controller/WeChatPayController.java | 1 + .../modules/pay/weChatPay/util/WxPayUtil.java | 8 +- src/main/resources/weChatConfig.properties | 4 +- 4 files changed, 177 insertions(+), 209 deletions(-) diff --git a/src/main/java/com/peanut/modules/book/controller/BuyOrderController.java b/src/main/java/com/peanut/modules/book/controller/BuyOrderController.java index 37620ffd..e422eec6 100644 --- a/src/main/java/com/peanut/modules/book/controller/BuyOrderController.java +++ b/src/main/java/com/peanut/modules/book/controller/BuyOrderController.java @@ -2,21 +2,16 @@ package com.peanut.modules.book.controller; import java.math.BigDecimal; import java.util.*; -import java.util.concurrent.locks.Lock; -import java.util.concurrent.locks.ReentrantLock; import java.util.stream.Collectors; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.toolkit.IdWorker; -import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.peanut.modules.book.entity.*; import com.peanut.modules.book.service.*; import lombok.extern.slf4j.Slf4j; -import lombok.var; -import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.bind.annotation.PathVariable; @@ -56,21 +51,9 @@ public class BuyOrderController { @Autowired private TransactionDetailsService transactionDetailsService; @Autowired - private AuthorService authorService; - @Autowired private UserEbookBuyService userEbookBuyService; @Autowired - private BookService bookService; - @Autowired - private BookShelfService bookShelfService; - @Autowired private UserRecordService userRecordService; - @Autowired - private UserFollowUpService userFollowUpService; - @Autowired - private PayWechatOrderService payWechatOrderService; - @Autowired - private PayZfbOrderService payZfbOrderService; /** * 订单状态 - 待支付 @@ -85,19 +68,19 @@ public class BuyOrderController { */ private static final String ORDER_STATUS_TO_BE_RECEIVED = "2"; /** - * 支付方式 - 支付宝 + * 支付方式 - 支付宝 - 支付宝支付 */ private static final String PAYMENT_METHOD_ALI_PAY = "1"; /** - * 支付方式 + * 支付方式 - 微信支付 */ private static final String PAYMENT_METHOD_WECHAT_PAY = "2"; /** - * 支付方式 + * 支付方式 - IOS 内购 */ private static final String PAYMENT_METHOD_IOS = "3"; /** - * 支付方式 + * 支付方式 - 虚拟币支付 */ private static final String PAYMENT_METHOD_VIRTUAL = "4"; /** @@ -143,149 +126,75 @@ public class BuyOrderController { return R.ok().put("page", page); } - /** * 信息 */ @RequestMapping("/info/{orderId}") public R info(@PathVariable("orderId") Integer orderId) { BuyOrderEntity buyOrder = buyOrderService.getById(orderId); - return R.ok().put("buyOrder", buyOrder); } /** * 下单 + * + * @param buyOrder 订单 + * @return R */ @RequestMapping("/buySave") @Transactional public R buySave(@RequestBody BuyOrderEntity buyOrder) { - BigDecimal realMoney; - // 根据订单获取订单详情 + // 获取订单详情 List products = buyOrder.getProducts(); - // ? + // 订单总金额 BigDecimal totalPrice = new BigDecimal(0); - List list = new ArrayList<>(); - // 遍历商品 查询价格 + // 遍历商品总价计算 for (BuyOrderDetailEntity buyOrderDetail : products) { - BuyOrderDetailEntity buyOrderDetailEntity = new BuyOrderDetailEntity(); Integer productId = buyOrderDetail.getProductId(); - ShopProductEntity product = shopProductService.getById(productId); - BigDecimal activityPrice = product.getActivityPrice(); - // 实际执行价格 - BigDecimal price; - if (activityPrice == null || activityPrice.equals(BigDecimal.ZERO)) { - price = product.getPrice(); - } else { - price = product.getActivityPrice(); - } int quantity = buyOrderDetail.getQuantity(); - totalPrice = totalPrice.add(price.multiply(BigDecimal.valueOf(quantity))); - - if (product.getProductStock() - quantity < 0) { + ShopProductEntity product = shopProductService.getById(productId); + BigDecimal price = getRealPrice(product); + if (!handleStock(buyOrderDetail, product)) { return R.error(500, "库存不足"); } - // 更新商品库存 - product.setProductStock(product.getProductStock() - quantity); - product.setSumSales(product.getSumSales() + quantity); - shopProductService.updateById(product); - - BeanUtils.copyProperties(buyOrderDetail, buyOrderDetailEntity); - buyOrderDetailEntity.setProductName(product.getProductName()); - buyOrderDetailEntity.setProductPrice(product.getPrice()); - buyOrderDetailEntity.setAddressId(buyOrder.getAddressId()); - buyOrderDetailEntity.setProductUrl(product.getProductImages()); - buyOrderDetailEntity.setOrderStatus(ORDER_STATUS_TO_BE_PAID); - list.add(buyOrderDetailEntity); + totalPrice = totalPrice.add(price.multiply(BigDecimal.valueOf(quantity))); + buyOrderDetail.setProductName(product.getProductName()); + buyOrderDetail.setProductPrice(product.getPrice()); + buyOrderDetail.setAddressId(buyOrder.getAddressId()); + buyOrderDetail.setProductUrl(product.getProductImages()); + buyOrderDetail.setOrderStatus(ORDER_STATUS_TO_BE_PAID); } - Integer couponId = buyOrder.getCouponId(); + totalPrice = totalPrice.subtract(useCouponAmount(buyOrder)); + totalPrice = totalPrice.add(getShoppingAmount(buyOrder)); + String orderSn = IdWorker.getTimeId().substring(0, 32); + buyOrder.setOrderSn(orderSn); + buyOrder.setPaymentDate(new Date()); + buyOrderService.save(buyOrder); - if (couponId != null) { - CouponHistoryEntity couponHistory = couponHistoryService.getById(couponId); - CouponEntity coupon = couponService.getById(couponHistory.getCouponId()); - BigDecimal amount = coupon.getCouponAmount(); - totalPrice = totalPrice.subtract(amount); - } - - if (buyOrder.getShippingMoney() != null) { - totalPrice = totalPrice.add(buyOrder.getShippingMoney()); - } - - // 减去优惠券金额 - realMoney = buyOrder.getRealMoney(); - if (totalPrice.compareTo(realMoney) == 0) { - //特定格式的时间ID - String timeId = IdWorker.getTimeId().substring(0, 32); - buyOrder.setOrderSn(timeId); - if (PAYMENT_METHOD_VIRTUAL.equals(buyOrder.getPaymentMethod())) { - buyOrder.setOrderStatus(ORDER_STATUS_TO_BE_SHIPPED); - } - buyOrder.setPaymentDate(new Date()); - buyOrderService.save(buyOrder); - - for (BuyOrderDetailEntity buyOrderDetailEntity : list) { - buyOrderDetailEntity.setOrderId(buyOrder.getOrderId()); - buyOrderDetailEntity.setUserId(buyOrder.getUserId()); - // 判断结算状态 - String buyType = buyOrder.getBuyType(); - if (buyType.equals(BUY_TYPE_CART)) { - // 更改购物车 状态 - List orderCartList = orderCartService.getBaseMapper().selectList(new QueryWrapper() - .eq("user_id", buyOrder.getUserId()).eq("product_id", buyOrderDetailEntity.getProductId())); - if (orderCartList.size() > 0) { - List collect = orderCartList.stream().map(orderCartEntity -> { - Integer cartId = orderCartEntity.getCartId(); - return cartId; - }).collect(Collectors.toList()); - orderCartService.removeByIds(collect); - } - } - - } - - buyOrderDetailService.saveBatch(list); - if (couponId != null) { - //更改优惠券状态 - CouponHistoryEntity one = couponHistoryService.getById(couponId); - one.setUseStatus(1); - one.setUseTime(new Date()); - one.setOrderId(Long.valueOf(buyOrder.getOrderId())); - one.setOrderSn(buyOrder.getOrderSn()); - couponHistoryService.updateById(one); - } - if ("4".equals(buyOrder.getPaymentMethod())) { - MyUserEntity user = this.myUserService.getById(buyOrder.getUserId()); - if (user.getPeanutCoin().compareTo(realMoney) >= 0) { - user.setPeanutCoin(user.getPeanutCoin().subtract(realMoney)); - this.myUserService.updateById(user); - // 添加消费信息 - TransactionDetailsEntity transactionDetailsEntity = new TransactionDetailsEntity(); - transactionDetailsEntity.setRemark("购买健康超市用品!订单编号为《 " + buyOrder.getOrderSn() + "》"); - transactionDetailsEntity.setUserId(user.getId()); - transactionDetailsEntity.setUserName(user.getNickname()); - transactionDetailsEntity.setChangeAmount(realMoney.negate()); - transactionDetailsEntity.setUserBalance(user.getPeanutCoin()); - transactionDetailsEntity.setTel(user.getTel()); - transactionDetailsEntity.setOrderType("购买健康超市用品!"); - transactionDetailsService.save(transactionDetailsEntity); - - //购买成功后,添加书到个人表中 - List pros = products.stream().map(BuyOrderDetailEntity::getProductId).collect(Collectors.toList()); - for (Integer s : pros) { - List collect = shopProudictBookService.getBaseMapper().selectList(new LambdaQueryWrapper() - .eq(ShopProudictBookEntity::getProudictId, s) - .eq(ShopProudictBookEntity::getDelFlag, 0)).stream().map(ShopProudictBookEntity::getBookId).collect(Collectors.toList()); - userEbookBuyService.addBookForUser(buyOrder.getUserId(), collect); - } - - } else { - return R.error("余额不足!"); - } + for (BuyOrderDetailEntity buyOrderDetail : products) { + buyOrderDetail.setOrderId(buyOrder.getOrderId()); + buyOrderDetail.setUserId(buyOrder.getUserId()); + if (BUY_TYPE_CART.equals(buyOrder.getBuyType())) { + handleBuyCart(buyOrder, buyOrderDetail); } } - return R.ok().put("orderSn", buyOrder.getOrderSn()).put("money", realMoney); + buyOrderDetailService.saveBatch(products); + if (PAYMENT_METHOD_VIRTUAL.equals(buyOrder.getPaymentMethod())) { + buyOrder.setOrderStatus(ORDER_STATUS_TO_BE_SHIPPED); + MyUserEntity user = this.myUserService.getById(buyOrder.getUserId()); + if (usePeanutCoin(user, totalPrice)) { + recordTransaction(buyOrder, user, totalPrice); + addEbookToUser(products, buyOrder); + } else { + return R.error(500, "花生币余额不足!"); + } + } + Map result = new HashMap<>(); + result.put("orderSn", buyOrder.getOrderSn()); + result.put("money", totalPrice); + return R.ok(result); } @@ -293,10 +202,8 @@ public class BuyOrderController { * 修改 */ @RequestMapping("/update") -// @RequiresPermissions("book:buyorder:update") public R update(@RequestBody BuyOrderEntity buyOrder) { buyOrderService.updateById(buyOrder); - return R.ok(); } @@ -304,10 +211,8 @@ public class BuyOrderController { * 删除 */ @RequestMapping("/delete") -// @RequiresPermissions("book:buyorder:delete") public R delete(@RequestBody Integer[] orderIds) { buyOrderService.removeByIds(Arrays.asList(orderIds)); - return R.ok(); } @@ -315,10 +220,8 @@ public class BuyOrderController { * 列表 */ @RequestMapping("/appUserGetlist") -// @RequiresPermissions("book:buyorder:list") public R appUserGetlist(@RequestParam Map params) { PageUtils page = buyOrderService.queryPage1(params); - return R.ok().put("page", page); } @@ -327,82 +230,46 @@ public class BuyOrderController { * app 端 取消订单 */ @RequestMapping("/appDelete") -// @RequiresPermissions("book:buyorder:delete") @Transactional public R appDelete(@RequestParam("orderId") Integer orderId) { //1. 判断订单状态 BuyOrderEntity byId = buyOrderService.getById(orderId); - if (byId != null) { //2. 判断当前订单是否存在优惠券 进行 回显 - Integer couponId = byId.getCouponId(); if (couponId != null) { CouponHistoryEntity byId1 = couponHistoryService.getById(couponId); byId1.setUseStatus(0); couponHistoryService.updateById(byId1); - } - // 库存回滚 List buyOrderDetailEntities = buyOrderDetailService.getBaseMapper().selectList(new QueryWrapper() .eq("order_id", byId.getOrderId())); - - for (BuyOrderDetailEntity buyOrderDetailEntity : buyOrderDetailEntities) { Integer productId = buyOrderDetailEntity.getProductId(); ShopProductEntity product = shopProductService.getById(productId); product.setProductStock(product.getProductStock() + buyOrderDetailEntity.getQuantity()); shopProductService.updateById(product); } - -// //3. 恢复当前订单 的 购物车商品 -// -// List products = buyOrderDetailService.getBaseMapper().selectList(new QueryWrapper() -// .eq("order_id", orderId)); -// -// for (BuyOrderDetailEntity product : products) { -// -// Integer productId = product.getProductId(); -// -// OrderCartEntity byId1 = orderCartService.getDeteleOrderCarts(byId.getUserId(),productId); -// -// byId1.setDelFlag(0); -// -// orderCartService.updateById(byId1); -// -// } - buyOrderService.removeById(orderId); - - } - return R.ok(); } @RequestMapping("/randomOrderCode") @Transactional public R randomOrderCode(@RequestBody BuyOrderEntity buyOrder) { - - -// String re= String.valueOf(buyOrderService.randomOrderCode(buyOrder)); -// buyOrderService.save(re); - return R.ok(); } - /** * 充值专用订单生成接口 */ @RequestMapping("/rechargeSave") @Transactional public R rechargeSave(@RequestBody BuyOrderEntity buyOrder) { - - String timeId = IdWorker.getTimeId().substring(0, 32); buyOrder.setOrderSn(timeId); buyOrderService.save(buyOrder); @@ -423,9 +290,6 @@ public class BuyOrderController { } else { orderDetail = buyOrderDetailService.getBaseMapper().selectList(new QueryWrapper() .eq("order_id", orderId)); - //TODO 根据shipping_sn快递单号分组,订单下无快递单号,我的订单同时无法显示,暂时注释 - //.groupBy("shipping_sn") - } for (BuyOrderDetailEntity buyOrderDetailEntity : orderDetail) { @@ -433,20 +297,13 @@ public class BuyOrderController { if (prod != null) { buyOrderDetailEntity.setImage(prod.getProductImages()); } - - } - List resultOrder = new ArrayList(); Set sn_no = new HashSet(); for (BuyOrderDetailEntity buyOrderDetailEntity : orderDetail) { - - resultOrder.add(buyOrderDetailEntity); sn_no.add(buyOrderDetailEntity.getShippingSn()); - - } UserRecordEntity userRecordEntity = userRecordService.getBaseMapper().selectOne(new QueryWrapper() @@ -454,18 +311,12 @@ public class BuyOrderController { .eq("userid", buyOrder.getUserId()) .eq("orderdid", buyOrder.getOrderId()) .last("LIMIT 1")); - - Integer id = null; if (userRecordEntity != null) { id = userRecordEntity.getId(); } - - buyOrder.setProducts(resultOrder); Date createDate = buyOrder.getCreateTime(); - - return R.ok().put("buyOrder", buyOrder).put("CreateTime", createDate).put("userRecordid", id); } @@ -474,15 +325,11 @@ public class BuyOrderController { */ @RequestMapping("/getTransPrice/{area}") public R getTransPrice(@PathVariable String area, @RequestParam Map productMap) { - Map params = new HashMap<>(); params.put("kdCode", "YD"); params.put("area", area); - int price = this.buyOrderService.getProductGoodsType(params, productMap); - return R.ok().put("price", price); - } @@ -523,7 +370,6 @@ public class BuyOrderController { return R.ok().put("rntStr", jsonList); } - /** * 检查可合并的订单信息 * @@ -535,7 +381,6 @@ public class BuyOrderController { return R.ok().put("page", page); } - /** * 检查传来的orderId 是否有可合并的其他订单信息 * @@ -548,7 +393,6 @@ public class BuyOrderController { return R.ok().put("list", list); } - /** * 批量发货功能 * @@ -564,7 +408,6 @@ public class BuyOrderController { } - /** * 后台取消订单接口 */ @@ -572,7 +415,6 @@ public class BuyOrderController { public R cancelFMS(@RequestParam Map params) { buyOrderService.cancelFMS(params.get("orderSn").toString(), params.get("shipperCode").toString(), params.get("expNo").toString()); -// return R.ok() return R.ok().put("paramsTEXT", params); } @@ -590,4 +432,129 @@ public class BuyOrderController { return R.ok().put("page", page); } + /** + * 获取商品实际价格 + * + * @param product 商品信息 + * @return 商品实际价格 + */ + private BigDecimal getRealPrice(ShopProductEntity product) { + BigDecimal activityPrice = product.getActivityPrice(); + return (activityPrice == null || activityPrice.equals(BigDecimal.ZERO)) ? product.getPrice() : activityPrice; + } + + /** + * 处理商品库存 + * + * @param buyOrderDetail + * @param product + * @return + */ + private boolean handleStock(BuyOrderDetailEntity buyOrderDetail, ShopProductEntity product) { + int quantity = buyOrderDetail.getQuantity(); + if (product.getProductStock() - quantity < 0) { + return false; + } + product.setProductStock(product.getProductStock() - quantity); + product.setSumSales(product.getSumSales() + quantity); + shopProductService.updateById(product); + return true; + } + + /** + * 使用优惠券 + * + * @param buyOrder + * @return + */ + private BigDecimal useCouponAmount(BuyOrderEntity buyOrder) { + Integer couponId = buyOrder.getCouponId(); + if (couponId != null) { + CouponHistoryEntity couponHistory = couponHistoryService.getById(couponId); + couponHistory.setUseStatus(1); + couponHistory.setUseTime(new Date()); + couponHistory.setOrderId(Long.valueOf(buyOrder.getOrderId())); + couponHistory.setOrderSn(buyOrder.getOrderSn()); + CouponEntity coupon = couponService.getById(couponHistory.getCouponId()); + + return coupon.getCouponAmount(); + } + return BigDecimal.ZERO; + } + + /** + * 计算运费 + * + * @param buyOrder + * @return + */ + private BigDecimal getShoppingAmount(BuyOrderEntity buyOrder) { + return buyOrder.getOrderMoney() == null ? BigDecimal.ZERO : buyOrder.getShippingMoney(); + } + + /** + * 使用虚拟币支付 + * + * @param user + * @param totalPrice + * @return + */ + private boolean usePeanutCoin(MyUserEntity user, BigDecimal totalPrice) { + if (user.getPeanutCoin().compareTo(totalPrice) >= 0) { + user.setPeanutCoin(user.getPeanutCoin().subtract(totalPrice)); + this.myUserService.updateById(user); + return true; + } + return false; + } + + /** + * 交易记录 + * + * @param buyOrder + * @param user + * @param totalPrice + */ + private void recordTransaction(BuyOrderEntity buyOrder, MyUserEntity user, BigDecimal totalPrice) { + TransactionDetailsEntity transactionDetailsEntity = new TransactionDetailsEntity(); + transactionDetailsEntity.setRemark("订单编号为 - " + buyOrder.getOrderSn()); + transactionDetailsEntity.setUserId(user.getId()); + transactionDetailsEntity.setUserName(user.getNickname()); + transactionDetailsEntity.setChangeAmount(totalPrice.negate()); + transactionDetailsEntity.setUserBalance(user.getPeanutCoin()); + transactionDetailsEntity.setTel(user.getTel()); + transactionDetailsEntity.setOrderType("购买商品"); + transactionDetailsService.save(transactionDetailsEntity); + } + + /** + * 给用户添加电子书 + * + * @param products + * @param buyOrder + */ + private void addEbookToUser(List products, BuyOrderEntity buyOrder) { + List productIds = products.stream().map(BuyOrderDetailEntity::getProductId).collect(Collectors.toList()); + for (Integer productId : productIds) { + List collect = shopProudictBookService.getBaseMapper().selectList(new LambdaQueryWrapper() + .eq(ShopProudictBookEntity::getProudictId, productId) + .eq(ShopProudictBookEntity::getDelFlag, 0)).stream().map(ShopProudictBookEntity::getBookId).collect(Collectors.toList()); + userEbookBuyService.addBookForUser(buyOrder.getUserId(), collect); + } + } + + /** + * 购物车 + * + * @param buyOrder + * @param buyOrderDetail + */ + private void handleBuyCart(BuyOrderEntity buyOrder, BuyOrderDetailEntity buyOrderDetail) { + List orderCartList = orderCartService.getBaseMapper().selectList(new QueryWrapper() + .eq("user_id", buyOrder.getUserId()).eq("product_id", buyOrderDetail.getProductId())); + if (orderCartList.size() > 0) { + List collect = orderCartList.stream().map(OrderCartEntity::getCartId).collect(Collectors.toList()); + orderCartService.removeByIds(collect); + } + } } diff --git a/src/main/java/com/peanut/modules/pay/weChatPay/controller/WeChatPayController.java b/src/main/java/com/peanut/modules/pay/weChatPay/controller/WeChatPayController.java index 84043155..e3877c81 100644 --- a/src/main/java/com/peanut/modules/pay/weChatPay/controller/WeChatPayController.java +++ b/src/main/java/com/peanut/modules/pay/weChatPay/controller/WeChatPayController.java @@ -284,6 +284,7 @@ public class WeChatPayController { System.out.println("============result========================" + b); if (wechatEntity.getTotalAmount().compareTo(realMoney) == 0) { buyOrderService.updateOrderStatus(order.getUserId(), order.getOrderSn(), "0"); + } } if ("point".equals(order.getOrderType())) { diff --git a/src/main/java/com/peanut/modules/pay/weChatPay/util/WxPayUtil.java b/src/main/java/com/peanut/modules/pay/weChatPay/util/WxPayUtil.java index d4a95126..f7beebde 100644 --- a/src/main/java/com/peanut/modules/pay/weChatPay/util/WxPayUtil.java +++ b/src/main/java/com/peanut/modules/pay/weChatPay/util/WxPayUtil.java @@ -31,12 +31,12 @@ import java.util.Base64; public static final String apiV3Key = "4aYFklzaULeGlr7oJPZ6rHWKcxjihZUF"; // apiV3秘钥 //商户私钥路径 - public static final String privateKeyUrl = "/usr/local/hs/peanut_book/target/classes/cent/apiclient_key.pem"; - //public static final String privateKeyUrl = "C:/Users/Cauchy/IdeaProjects/nuttyreading-java/src/main/resources/cent/apiclient_key.pem"; + //public static final String privateKeyUrl = "/usr/local/hs/peanut_book/target/classes/cent/apiclient_key.pem"; + public static final String privateKeyUrl = "C:/Users/Cauchy/IdeaProjects/nuttyreading-java/src/main/resources/cent/apiclient_key.pem"; //平台证书路径 - public static final String wechatPayCertificateUrl = "/usr/local/hs/peanut_book/target/classes/cent/wechatpay_7B5676E3CDF56680D0414A009CE501C844DBE2D6.pem"; - //public static final String wechatPayCertificateUrl = "C:/Users/Cauchy/IdeaProjects/nuttyreading-java/src/main/resources/cent/wechatpay_7B5676E3CDF56680D0414A009CE501C844DBE2D6.pem"; + //public static final String wechatPayCertificateUrl = "/usr/local/hs/peanut_book/target/classes/cent/wechatpay_7B5676E3CDF56680D0414A009CE501C844DBE2D6.pem"; + public static final String wechatPayCertificateUrl = "C:/Users/Cauchy/IdeaProjects/nuttyreading-java/src/main/resources/cent/wechatpay_7B5676E3CDF56680D0414A009CE501C844DBE2D6.pem"; //第一步申请完证书后,在API证书哪里点击管理证书就能看到 public static final String mchSerialNo = "679AECB2F7AC4183033F713828892BA640E4EEE3"; // 商户证书序列号 diff --git a/src/main/resources/weChatConfig.properties b/src/main/resources/weChatConfig.properties index 3dea880d..806c29c0 100644 --- a/src/main/resources/weChatConfig.properties +++ b/src/main/resources/weChatConfig.properties @@ -16,8 +16,8 @@ wxpay.notifyUrl: http://59.110.212.44:9200/pb/pay/payNotify wxpay.refundNotifyUrl: http://pjm6m9.natappfree.cc/pay/refundNotify # ???? /usr/local/hs/peanut_book/target/classes/cent/apiclient_key.pem -#wxpay.keyPemPath:C:/Users/Cauchy/IdeaProjects/nuttyreading-java/src/main/resources/cent/apiclient_key.pem -wxpay.keyPemPath:/usr/local/hs/peanut_book/target/classes/cent/apiclient_key.pem +wxpay.keyPemPath:C:/Users/Cauchy/IdeaProjects/nuttyreading-java/src/main/resources/cent/apiclient_key.pem +#wxpay.keyPemPath:/usr/local/hs/peanut_book/target/classes/cent/apiclient_key.pem #??????? wxpay.serialNo: 679AECB2F7AC4183033F713828892BA640E4EEE3 #???????