diff --git a/src/main/java/com/peanut/config/Constants.java b/src/main/java/com/peanut/config/Constants.java index 969fa34..ebd2624 100644 --- a/src/main/java/com/peanut/config/Constants.java +++ b/src/main/java/com/peanut/config/Constants.java @@ -26,6 +26,10 @@ public class Constants { * 订单状态 - 交易失败 */ public static final String ORDER_STATUS_FAIL = "4"; + /** + * 订单状态 - 交易失败 + */ + public static final String ORDER_STATUS_REFUND = "6"; /** * 订单状态 - 全部 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 6d254ad..90471e6 100644 --- a/src/main/java/com/peanut/modules/book/controller/BuyOrderController.java +++ b/src/main/java/com/peanut/modules/book/controller/BuyOrderController.java @@ -118,6 +118,10 @@ public class BuyOrderController { private UserCourseBuyLogService userCourseBuyLogService; @Autowired private CourseCatalogueService courseCatalogueService; + @Autowired + private BuyOrderRefundService buyOrderRefundService; + @Autowired + private BuyOrderRefundLogService buyOrderRefundLogService; @RequestMapping(value = "/decomposeShipment", method = RequestMethod.POST) public R decomposeShipment(@RequestBody BuyOrderListRequestVo requestVo) { @@ -580,7 +584,109 @@ public class BuyOrderController { result.put("money", totalPrice); return R.ok(result); } + //订单退款进度 + @RequestMapping("/refundDetail") + public R refundOrderDetail(@RequestBody Map params){ + Object orderIdObj = params.get("orderId"); + if(orderIdObj == null || orderIdObj.toString().trim().equals("")){ + return R.error("orderId不能为空"); + } + int order_id = Integer.parseInt(orderIdObj.toString().trim()); + QueryWrapper wapper = new QueryWrapper<>(); + wapper.eq("order_id",order_id); + BuyOrderRefund refundInfo = buyOrderRefundService.getOne(wapper); + if(refundInfo==null){ + return R.error("退款信息不存在"); + } + QueryWrapper wapper2 = new QueryWrapper<>(); + wapper2.eq("refund_id",refundInfo.getId()).orderByDesc("create_time"); + List list = buyOrderRefundLogService.list(wapper2); + for (BuyOrderRefundLog log : list){ + if(log.getType()==1){ + log.setTitle("天医币已退还"); + log.setContent("天医币已退还到账户,可在我的->天医币中查看明细"); + }else if(log.getType()==2){ + log.setTitle("积分已退还"); + log.setContent("积分已退还到账户,可在我的->积分中查看明细"); + }else if(log.getType()==3){ + log.setTitle(log.getStatus()==1?"到账成功":"已退款,支付宝处理中"); + log.setContent(log.getStatus()==1?"已退到您的支付宝,到账时间以支付宝处理时间为准,可前往「支付宝-账单」查看":"已将退款资金提交给支付宝处理,通常情况下,退款会原路退回您的支付账户,预计会在1-7天内到账"); + }else if(log.getType()==4){ + log.setTitle(log.getStatus()==1?"到账成功":"已退款,微信处理中"); + log.setContent(log.getStatus()==1?"已退到您的微信,到账时间以微信处理时间为准,可前往「微信-账单」查看":"已将退款资金提交给微信处理,通常情况下,退款会原路退回您的支付账户,预计会在1-7天内到账"); + } + } + BuyOrderRefundLog log = new BuyOrderRefundLog(); + log.setId(0); + log.setRefundId(refundInfo.getId()); + log.setType(0); + log.setStatus(1); + log.setCreateTime(refundInfo.getCreateTime()); + log.setTitle("发起退款"); + log.setContent("系统会在1-2天内提交处理"); + list.add(log); + Map result = new HashMap<>(); + BigDecimal shippingMoney = refundInfo.getDeductShipping()==1?refundInfo.getShippingMoney():BigDecimal.ZERO; + result.put("orderMoney",refundInfo.getFee().add(refundInfo.getJfDeduction().add(shippingMoney))); + result.put("refundFee",refundInfo.getFee()); + result.put("payType",refundInfo.getPayType()); + result.put("refundJf",refundInfo.getJfDeduction()); + result.put("shippingMoney",shippingMoney); + result.put("list",list); + return R.ok().put("info",result); + } + @RequestMapping(value = "/refundOrder", method = RequestMethod.POST) + @Transactional + public R refundOrder(@RequestBody Map params) { + Object orderIdObject = params.get("orderId"); + if(orderIdObject == null || orderIdObject.toString().trim().equals("")){ + return R.error("orderId不能为空"); + } + int orderId = Integer.parseInt(orderIdObject.toString()); + QueryWrapper queryWapper = new QueryWrapper<>(); + queryWapper.eq("order_id",orderId); + BuyOrder buyOrder = buyOrderService.getOne(queryWapper); + if(buyOrder==null){ + return R.error("订单不存在"); + } + MyUserEntity user = myUserService.getById(buyOrder.getUserId()); + if (user == null) { + return R.error("订单对应用户不存在"); + } + + if (Constants.ORDER_STATUS_TO_BE_PAID.equals(buyOrder.getOrderStatus()) + || Constants.ORDER_STATUS_FAIL.equals(buyOrder.getOrderStatus()) + || Constants.ORDER_STATUS_REFUND.equals(buyOrder.getOrderStatus()) + || Constants.ORDER_STATUS_OUT_OF_TIME.equals(buyOrder.getOrderStatus())) { + return R.error("当前订单状态不支持退单"); + } + buyOrder.setOrderStatus(Constants.ORDER_STATUS_REFUND); + buyOrderService.updateById(buyOrder); + + BigDecimal refundFee = buyOrder.getRealMoney(); + BigDecimal shippingMoney = buyOrder.getShippingMoney()==null?BigDecimal.ZERO:buyOrder.getShippingMoney(); + int deductShipping = params.containsKey("deductShipping") && params.get("deductShipping")!=null?Integer.parseInt(params.get("deductShipping").toString()):0; + if(refundFee.compareTo(BigDecimal.ZERO)>0 && shippingMoney.compareTo(BigDecimal.ZERO)>0 && deductShipping==1){ + refundFee = refundFee.subtract(shippingMoney); + refundFee = refundFee.compareTo(BigDecimal.ZERO)>0?refundFee:BigDecimal.ZERO; + } + String remark = params.containsKey("remark") && params.get("remark").toString()!=null?params.get("remark").toString():""; + int refundId = buyOrderRefundService.insertBuyOrderRefund(buyOrder,refundFee,deductShipping,remark); + + if (Constants.PAYMENT_METHOD_VIRTUAL.equals(buyOrder.getPaymentMethod())) { + //BigDecimal refundPeanutCoin = buyOrder.getRealMoney() == null?BigDecimal.ZERO : buyOrder.getRealMoney(); + if(refundFee.compareTo(BigDecimal.ZERO)>0){ + transactionDetailsService.refundRecord(buyOrder,user,refundFee); + user.setPeanutCoin(user.getPeanutCoin().add(refundFee)); + myUserService.updateById(user); + buyOrderRefundLogService.insertRefundLog(refundId,1,1); + } + } + + buyOrderService.refundOrder(buyOrder,user,refundId); + return R.ok("ok"); + } @RequestMapping("/llll") public R ls(){ @@ -1166,7 +1272,7 @@ public class BuyOrderController { List collect = shopProductBookService.getBaseMapper().selectList(new LambdaQueryWrapper() .eq(ShopProductBookEntity::getProductId, productId) .eq(ShopProductBookEntity::getDelFlag, 0)).stream().map(ShopProductBookEntity::getBookId).collect(Collectors.toList()); - userEbookBuyService.addBookForUser(buyOrder.getUserId(), collect); + userEbookBuyService.addBookForUser(buyOrder, buyOrder.getUserId(), collect); } } diff --git a/src/main/java/com/peanut/modules/book/service/BuyOrderService.java b/src/main/java/com/peanut/modules/book/service/BuyOrderService.java index 53820e5..c9a9a65 100644 --- a/src/main/java/com/peanut/modules/book/service/BuyOrderService.java +++ b/src/main/java/com/peanut/modules/book/service/BuyOrderService.java @@ -9,9 +9,11 @@ import com.peanut.modules.book.to.UserOrderDto; import com.peanut.modules.book.vo.UserOrderVo; import com.peanut.modules.book.vo.request.BuyOrderListRequestVo; import com.peanut.modules.book.vo.response.BuyOrderResponseVo; +import com.peanut.modules.common.entity.MyUserEntity; import com.peanut.modules.common.entity.ShopProductCourseEntity; import java.io.IOException; +import java.math.BigDecimal; import java.util.List; import java.util.Map; @@ -68,4 +70,8 @@ public interface BuyOrderService extends IService { void addCourseToUser(String payType,BuyOrder orderEntity); boolean checkWlOrder(String orderSn); + + BigDecimal getRefundFee(Map params, BuyOrder buyOrder); + + void refundOrder(BuyOrder buyOrder, MyUserEntity user, int refundId); } \ No newline at end of file diff --git a/src/main/java/com/peanut/modules/book/service/MyUserService.java b/src/main/java/com/peanut/modules/book/service/MyUserService.java index 701a686..2ffa5d4 100644 --- a/src/main/java/com/peanut/modules/book/service/MyUserService.java +++ b/src/main/java/com/peanut/modules/book/service/MyUserService.java @@ -4,6 +4,8 @@ import com.baomidou.mybatisplus.extension.service.IService; import com.peanut.common.utils.PageUtils; import com.peanut.common.utils.R; import com.peanut.modules.common.entity.BookForumArticlesEntity; +import com.peanut.modules.common.entity.BuyOrder; +import com.peanut.modules.common.entity.BuyOrderProduct; import com.peanut.modules.common.entity.MyUserEntity; import java.util.List; import java.util.Map; @@ -33,5 +35,7 @@ public interface MyUserService extends IService { boolean checkUserTelOrEmail(MyUserEntity user); + void rollbackUserPowers(BuyOrder order, List orderProducts); + } diff --git a/src/main/java/com/peanut/modules/book/service/ShopProductService.java b/src/main/java/com/peanut/modules/book/service/ShopProductService.java index 03ee621..ffd9a61 100644 --- a/src/main/java/com/peanut/modules/book/service/ShopProductService.java +++ b/src/main/java/com/peanut/modules/book/service/ShopProductService.java @@ -2,6 +2,7 @@ package com.peanut.modules.book.service; import com.baomidou.mybatisplus.extension.service.IService; import com.peanut.common.utils.PageUtils; +import com.peanut.modules.common.entity.BuyOrder; import com.peanut.modules.common.entity.ShopProduct; import java.math.BigDecimal; @@ -37,6 +38,6 @@ public interface ShopProductService extends IService { PageUtils queryPageactivityprice(Map params); - + void rollbackStock(BuyOrder buyOrder); } diff --git a/src/main/java/com/peanut/modules/book/service/TransactionDetailsService.java b/src/main/java/com/peanut/modules/book/service/TransactionDetailsService.java index dffa638..20fa71e 100644 --- a/src/main/java/com/peanut/modules/book/service/TransactionDetailsService.java +++ b/src/main/java/com/peanut/modules/book/service/TransactionDetailsService.java @@ -27,6 +27,6 @@ public interface TransactionDetailsService extends IService { List getUserBookId(Integer userId); void addBookForUser(Integer userId, List bookIds); + + void addBookForUser(BuyOrder buyOrder, Integer userId, List bookIds); + + void rollbackUserEbooks(BuyOrder order, List orderProducts); + } diff --git a/src/main/java/com/peanut/modules/book/service/impl/BuyOrderServiceImpl.java b/src/main/java/com/peanut/modules/book/service/impl/BuyOrderServiceImpl.java index 793362d..e3ad7c2 100644 --- a/src/main/java/com/peanut/modules/book/service/impl/BuyOrderServiceImpl.java +++ b/src/main/java/com/peanut/modules/book/service/impl/BuyOrderServiceImpl.java @@ -11,6 +11,12 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.github.yulichang.wrapper.MPJLambdaWrapper; import com.peanut.common.utils.*; import com.peanut.config.Constants; +import com.peanut.modules.book.service.BuyOrderService; +import com.peanut.modules.book.service.CityService; +import com.peanut.modules.book.service.CountyService; +import com.peanut.modules.book.service.ExpressOrderService; +import com.peanut.modules.book.service.MyUserService; +import com.peanut.modules.book.service.ProvinceService; import com.peanut.modules.common.dao.*; import com.peanut.modules.book.service.*; import com.peanut.modules.book.to.UserOrderDto; @@ -19,8 +25,7 @@ import com.peanut.modules.book.vo.UserOrderVo; import com.peanut.modules.book.vo.request.BuyOrderListRequestVo; import com.peanut.modules.book.vo.response.*; import com.peanut.modules.common.entity.*; -import com.peanut.modules.common.service.UserVipService; -import com.peanut.modules.common.service.VipBuyConfigService; +import com.peanut.modules.common.service.*; import com.peanut.modules.common.vo.CourseCatalogueVo; import com.peanut.modules.oss.service.OssService; import lombok.extern.slf4j.Slf4j; @@ -35,6 +40,7 @@ import java.io.IOException; import java.math.BigDecimal; import java.math.RoundingMode; import java.text.SimpleDateFormat; +import java.time.Instant; import java.util.*; import java.util.stream.Collectors; @@ -92,8 +98,18 @@ public class BuyOrderServiceImpl extends ServiceImpl impl private UserCourseBuyDao userCourseBuyDao; @Autowired private UserCourseBuyLogDao userCourseBuyLogDao; + @Autowired + private CouponService couponService; + @Autowired + private UserEbookBuyService userEbookBuyService; protected Logger logger = LoggerFactory.getLogger(BuyOrderServiceImpl.class); + @Autowired + private JfTransactionDetailsService jfTransactionDetailsService; + @Autowired + private BuyOrderRefundLogService buyOrderRefundLogService; + @Autowired + private BuyOrderRefundService buyOrderRefundService; // TODO 新版本上线后删除 @Override @@ -412,20 +428,53 @@ public class BuyOrderServiceImpl extends ServiceImpl impl user.setUserVips(userVipService.list(new LambdaQueryWrapper() .eq(UserVip::getUserId,user.getId()).eq(UserVip::getState,0))); b.setUser(user); + boolean refundableStatus = false; + long timestamp = Instant.now().toEpochMilli(); + long paymentDateTime = b.getPaymentDate()==null?0: b.getPaymentDate().getTime(); if (b.getVipBuyConfigId()!=0){ + if(paymentDateTime > timestamp-7*24*60*60*1000){ + refundableStatus = true; + } b.setVipBuyConfigEntity(vipBuyConfigService.getById(b.getVipBuyConfigId())); } //添加商品信息 List buyOrderProducts = buyOrderProductDao.selectList(new LambdaQueryWrapper().eq(BuyOrderProduct::getOrderId, b.getOrderId())); - for (BuyOrderProduct b1:buyOrderProducts){ - MPJLambdaWrapper shopProductWrapper = new MPJLambdaWrapper<>(); - //关掉本次查询del_flg = 0的条件,查询删除商品 - shopProductWrapper.disableLogicDel().eq(ShopProduct::getProductId,b1.getProductId()); - ShopProduct byId = shopProductService.getOne(shopProductWrapper); - byId.setBooks(shopProductBookService.getBookByProductId(byId.getProductId())); - b1.setProduct(byId); - b1.setExpressOrder(expressOrderDao.selectById(b1.getExpressOrderId())); + + if(buyOrderProducts.size()>0){ + boolean[] refundableStatusArr = new boolean[buyOrderProducts.size()]; + for (int i=0;i shopProductWrapper = new MPJLambdaWrapper<>(); + //关掉本次查询del_flg = 0的条件,查询删除商品 + shopProductWrapper.disableLogicDel().eq(ShopProduct::getProductId,b1.getProductId()); + ShopProduct byId = shopProductService.getOne(shopProductWrapper); + byId.setBooks(shopProductBookService.getBookByProductId(byId.getProductId())); + b1.setProduct(byId); + b1.setExpressOrder(expressOrderDao.selectById(b1.getExpressOrderId())); + boolean refundableStatusProduct = false; + if(b1.getProduct().getGoodsType().equals("05") && (paymentDateTime > timestamp-7*24*60*60*1000)){ + refundableStatusProduct = true; + }else if(!b1.getProduct().getGoodsType().equals("05") && b.getOrderStatus().equals("1")){ + refundableStatusProduct = true; + } + refundableStatusArr[i] = refundableStatusProduct; + } + for (boolean rs : refundableStatusArr){ + if(!rs){ + refundableStatus = false; + break; + }else{ + refundableStatus = true; + } + } } + if(b.getOrderStatus().equals(Constants.ORDER_STATUS_REFUND)){ + QueryWrapper wrapperRefund = new QueryWrapper<>(); + wrapperRefund.eq("order_id",b.getOrderId()); + BuyOrderRefund refundInfo = buyOrderRefundService.getOne(wrapperRefund); + b.setRefundRemark(refundInfo.getRemark()); + } + b.setRefundableStatus(b.getOrderStatus().equals("6")?false:refundableStatus); b.setProductList(buyOrderProducts); //设置快递信息传递给前端 ConsigneeVo consigneeVo = new ConsigneeVo(); @@ -481,7 +530,7 @@ public class BuyOrderServiceImpl extends ServiceImpl impl wrapper.eq(BuyOrder::getUserId,userOrderDto.getUserId()); // wrapper.eq(BuyOrder::getOrderType,"order");//这里有点问题 if(userOrderDto.getOrderStatus()==null){ - Integer[] sts = {0,1,2,3}; + Integer[] sts = {0,1,2,3,6}; wrapper.in(BuyOrder::getOrderStatus,sts); }else{ wrapper.eq(BuyOrder::getOrderStatus,userOrderDto.getOrderStatus()); @@ -497,17 +546,40 @@ public class BuyOrderServiceImpl extends ServiceImpl impl //组装商品 List buyOrderProducts = buyOrderProductService.getBaseMapper().selectList(new LambdaQueryWrapper() .eq(BuyOrderProduct::getOrderId, b.getOrderId())); - for (BuyOrderProduct bb : buyOrderProducts){ - bb.setProduct(shopProductService.getOne(new LambdaQueryWrapper().select(ShopProduct::getProductId,ShopProduct::getProductName,ShopProduct::getProductImages,ShopProduct::getPrice,ShopProduct::getActivityPrice,ShopProduct::getIsVipPrice,ShopProduct::getGoodsType) - .eq(ShopProduct::getProductId,bb.getProductId()))); - UserRecord userRecord = userRecordDao.selectOne(new QueryWrapper() - .eq("userid", ShiroUtils.getUId()) - .eq("orderdid", b.getOrderId()) - .eq("product_id", bb.getProductId())); - if (userRecord!=null){ - bb.setRecordId(userRecord.getId()); + Boolean refundableStatus = false; + long paymentDateTime = b.getPaymentDate()==null?0:b.getPaymentDate().getTime(); + long timestamp = Instant.now().toEpochMilli(); + if (buyOrderProducts.size() > 0) { + boolean[] refundableStatusArr = new boolean[buyOrderProducts.size()]; + for (int i=0;i().select(ShopProduct::getProductId, ShopProduct::getProductName, ShopProduct::getProductImages, ShopProduct::getPrice, ShopProduct::getActivityPrice, ShopProduct::getIsVipPrice, ShopProduct::getGoodsType) + .eq(ShopProduct::getProductId, bb.getProductId()))); + UserRecord userRecord = userRecordDao.selectOne(new QueryWrapper() + .eq("userid", ShiroUtils.getUId()) + .eq("orderdid", b.getOrderId()) + .eq("product_id", bb.getProductId())); + if (userRecord != null) { + bb.setRecordId(userRecord.getId()); + } + boolean refundableStatusProduct = false; + if (bb.getProduct().getGoodsType().equals("05") && paymentDateTime > timestamp - 7 * 24 * 60 * 60 * 1000) { + refundableStatusProduct = true; + } else if (!bb.getProduct().getGoodsType().equals("05") && b.getOrderStatus().equals(Constants.ORDER_STATUS_TO_BE_SHIPPED)){ + refundableStatusProduct = true; + } + refundableStatusArr[i] = refundableStatusProduct; + } + for (boolean rs : refundableStatusArr){ + if(!rs){ + refundableStatus = false; + break; + }else{ + refundableStatus = true; + } } } + b.setRefundableStatus(b.getOrderStatus().equals("6")?false:refundableStatus); b.setProductList(buyOrderProducts); b.setTimestamp(b.getCreateTime().getTime()/1000); //充值订单填充充值商品信息 @@ -653,7 +725,20 @@ public class BuyOrderServiceImpl extends ServiceImpl impl List shopProductCourseEntities = this.getBaseMapper().selectJoinList(ShopProductCourseEntity.class, wrapper); return shopProductCourseEntities; } + public void removeCourseToUser(BuyOrder orderEntity){ + List orderCourse = getOrderCourse(orderEntity.getOrderSn()); + for (int i=0;i wrapper2 = new LambdaQueryWrapper<>(); + wrapper2.eq(UserCourseBuyLog::getOrderSn,orderEntity.getOrderSn()); + List buyCourseLogList = userCourseBuyLogDao.selectList(wrapper2); + for (UserCourseBuyLog courseLog : buyCourseLogList){ + userCourseBuyDao.deleteById(courseLog.getUserCourseBuyId()); + userCourseBuyLogDao.deleteById(courseLog.getId()); + } + } + } @Override public void addCourseToUser(String payType,BuyOrder orderEntity){ List orderCourse = getOrderCourse(orderEntity.getOrderSn()); @@ -951,5 +1036,53 @@ public class BuyOrderServiceImpl extends ServiceImpl impl return responseVo; } + public BigDecimal getRefundFee(Map params, BuyOrder buyOrder) { + Object userRefundFeeObj = params.get("refundFee"); + BigDecimal refundFee = new BigDecimal(0); + if(userRefundFeeObj!=null && !userRefundFeeObj.toString().trim().equals("")){ + refundFee = new BigDecimal(userRefundFeeObj.toString().trim()); + } + + BigDecimal realMoney = buyOrder.getRealMoney() == null ? BigDecimal.ZERO : buyOrder.getRealMoney(); + BigDecimal shippingMoney = buyOrder.getShippingMoney() == null ? BigDecimal.ZERO : buyOrder.getShippingMoney(); + BigDecimal maxRefundMoney = realMoney.subtract(shippingMoney); + if (maxRefundMoney.compareTo(BigDecimal.ZERO) < 0) { + maxRefundMoney = BigDecimal.ZERO; + } + + if(refundFee.compareTo(maxRefundMoney)>0){ + refundFee = maxRefundMoney; + } + + return refundFee; + } + @Override + public void refundOrder(BuyOrder buyOrder, MyUserEntity user, int refundId){ + if(buyOrder.getCouponId()!=null && buyOrder.getCouponId()!=0){ + couponService.rollbackCoupon(buyOrder.getCouponId()); + } + QueryWrapper productQueryWrapper = new QueryWrapper<>(); + productQueryWrapper.eq("order_id",buyOrder.getOrderId()); + List orderProducts = buyOrderProductService.list(productQueryWrapper); + + //积分恢复 + BigDecimal jf = buyOrder.getJfDeduction()==null?BigDecimal.ZERO : buyOrder.getJfDeduction(); + if(jf.compareTo(BigDecimal.ZERO)>0){ + jfTransactionDetailsService.refundJfTransaction(buyOrder,user); + user.setJf(jf.add(user.getJf())); + myUserService.updateById(user); + buyOrderRefundLogService.insertRefundLog(refundId,2,1); + } + //撤回本订单购买赠送的优惠券 + couponService.refundZSCouponHistoryByOrder(buyOrder); +// 恢复用户权益(点穴/时辰取穴/五运六气/肿瘤古方) +// myUserService.rollbackUserPowers(buyOrder, orderProducts); + //撤回电子书权限 + userEbookBuyService.rollbackUserEbooks(buyOrder, orderProducts); + //撤回课程权限 + removeCourseToUser(buyOrder); + //回滚库存 + shopProductService.rollbackStock(buyOrder); + } } \ No newline at end of file diff --git a/src/main/java/com/peanut/modules/book/service/impl/MyUserServiceImpl.java b/src/main/java/com/peanut/modules/book/service/impl/MyUserServiceImpl.java index 3ec3b1f..a4cf436 100644 --- a/src/main/java/com/peanut/modules/book/service/impl/MyUserServiceImpl.java +++ b/src/main/java/com/peanut/modules/book/service/impl/MyUserServiceImpl.java @@ -102,4 +102,27 @@ public class MyUserServiceImpl extends ServiceImpl impl MyUserEntity one = getOne(wrapper); return one == null; } + + public void rollbackUserPowers(BuyOrder order, List orderProducts) { + MyUserEntity user = getById(order.getUserId()); + if(user == null){ + return; + } + for (BuyOrderProduct buyOrderProduct : orderProducts){ + Integer productId = buyOrderProduct.getProductId(); + if(Arrays.asList(128, 129, 130, 131, 136, 137, 139, 1612).contains(productId)){ + user.setPointPower(0); + } + if(Arrays.asList(133, 134, 135).contains(productId)){ + user.setTgdzPower(0); + } + if(Arrays.asList(39, 62, 123, 127).contains(productId)){ + user.setWylqPower(0); + } + if(Arrays.asList(43, 62, 124).contains(productId)){ + user.setPrescriptBPower(0); + } + } + updateById(user); + } } \ No newline at end of file diff --git a/src/main/java/com/peanut/modules/book/service/impl/ShopProductServiceImpl.java b/src/main/java/com/peanut/modules/book/service/impl/ShopProductServiceImpl.java index 0f099a7..24a5b13 100644 --- a/src/main/java/com/peanut/modules/book/service/impl/ShopProductServiceImpl.java +++ b/src/main/java/com/peanut/modules/book/service/impl/ShopProductServiceImpl.java @@ -1,6 +1,9 @@ package com.peanut.modules.book.service.impl; import com.peanut.common.utils.ExcludeEmptyQueryWrapper; import com.peanut.common.utils.ShiroUtils; +import com.peanut.modules.book.service.BuyOrderProductService; +import com.peanut.modules.common.entity.BuyOrder; +import com.peanut.modules.common.entity.BuyOrderProduct; import com.peanut.modules.common.service.UserVipService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; @@ -24,6 +27,8 @@ public class ShopProductServiceImpl extends ServiceImpl params) { @@ -135,5 +140,17 @@ public class ShopProductServiceImpl extends ServiceImpl buyOrderProductQueryWrapper = new QueryWrapper<>(); + buyOrderProductQueryWrapper.eq("order_id", buyOrder.getOrderId()); + List buyOrderProductList = buyOrderProductService.list(buyOrderProductQueryWrapper); + for (BuyOrderProduct buyOrderProduct : buyOrderProductList) { + Integer productId = buyOrderProduct.getProductId(); + ShopProduct product = this.getById(productId); + product.setProductStock(product.getProductStock() + buyOrderProduct.getQuantity()); + this.updateById(product); + } + } } \ No newline at end of file diff --git a/src/main/java/com/peanut/modules/book/service/impl/TransactionDetailsServiceImpl.java b/src/main/java/com/peanut/modules/book/service/impl/TransactionDetailsServiceImpl.java index 94cabae..2318221 100644 --- a/src/main/java/com/peanut/modules/book/service/impl/TransactionDetailsServiceImpl.java +++ b/src/main/java/com/peanut/modules/book/service/impl/TransactionDetailsServiceImpl.java @@ -84,13 +84,18 @@ public class TransactionDetailsServiceImpl extends ServiceImpl params) { @@ -68,4 +75,34 @@ public class UserEbookBuyServiceImpl extends ServiceImpl bookIds) { + for (Integer i:bookIds){ + List userEbookBuyEntities = this.getBaseMapper().selectList(new LambdaQueryWrapper().eq(UserEbookBuyEntity::getUserId, userId).eq(UserEbookBuyEntity::getBookId, i)); + if(userEbookBuyEntities.size()==0){ + UserEbookBuyEntity userEbookBuyEntity = new UserEbookBuyEntity(); + userEbookBuyEntity.setUserId(userId); + userEbookBuyEntity.setBookId(i); + userEbookBuyEntity.setPayTime(new Date()); + userEbookBuyEntity.setOrdersn(buyOrder.getOrderSn()); + this.save(userEbookBuyEntity); + } + } + } + + @Override + public void rollbackUserEbooks(BuyOrder order, List orderProducts) { + for (BuyOrderProduct buyOrderProduct : orderProducts){ + List bookIds = shopProductBookService.getBaseMapper().selectList(new LambdaQueryWrapper() + .eq(ShopProductBookEntity::getProductId,buyOrderProduct.getProductId()) + .eq(ShopProductBookEntity::getDelFlag,0) + ).stream().map(ShopProductBookEntity::getBookId).collect(Collectors.toList()); + for (Integer bookId:bookIds){ + QueryWrapper removeWrapper = new QueryWrapper<>(); + removeWrapper.eq("user_id", order.getUserId()); + removeWrapper.eq("ordersn", order.getOrderSn()); + this.remove(removeWrapper); + } + } + } } \ No newline at end of file diff --git a/src/main/java/com/peanut/modules/common/controller/UserVipController.java b/src/main/java/com/peanut/modules/common/controller/UserVipController.java index 1859c02..923c014 100644 --- a/src/main/java/com/peanut/modules/common/controller/UserVipController.java +++ b/src/main/java/com/peanut/modules/common/controller/UserVipController.java @@ -380,6 +380,7 @@ public class UserVipController { MyUserEntity user = myUserDao.selectById(buyOrder.getUserId()); if (usePeanutCoin(user, totalPrice)&&useJfCoin(user,buyOrder.getJfDeduction())) { // 更新订单状态 + buyOrder.setPaymentDate(new Date()); buyOrderService.updateOrderStatus(user.getId(), buyOrder.getOrderSn(), "2"); //记录用户虚拟币消费 if(totalPrice.compareTo(BigDecimal.ZERO)>0){ diff --git a/src/main/java/com/peanut/modules/common/dao/BuyOrderRefundLogDao.java b/src/main/java/com/peanut/modules/common/dao/BuyOrderRefundLogDao.java new file mode 100644 index 0000000..23e2174 --- /dev/null +++ b/src/main/java/com/peanut/modules/common/dao/BuyOrderRefundLogDao.java @@ -0,0 +1,10 @@ +package com.peanut.modules.common.dao; + +import com.github.yulichang.base.MPJBaseMapper; +import com.peanut.modules.common.entity.BuyOrderRefund; +import com.peanut.modules.common.entity.BuyOrderRefundLog; +import org.apache.ibatis.annotations.Mapper; + +@Mapper +public interface BuyOrderRefundLogDao extends MPJBaseMapper { +} diff --git a/src/main/java/com/peanut/modules/common/entity/BuyOrder.java b/src/main/java/com/peanut/modules/common/entity/BuyOrder.java index 73cbb01..1145fe6 100644 --- a/src/main/java/com/peanut/modules/common/entity/BuyOrder.java +++ b/src/main/java/com/peanut/modules/common/entity/BuyOrder.java @@ -124,6 +124,7 @@ public class BuyOrder implements Serializable { * 3:已完成 * 4: 交易失败 * 5: 已过期 + * 6: 已退款 */ private String orderStatus; /** @@ -223,4 +224,9 @@ public class BuyOrder implements Serializable { private BookEntity bookEntity; @TableField(exist = false) private String trainingClassIdentity; + @TableField(exist = false) + private Boolean refundableStatus; + @TableField(exist = false) + private String refundRemark; + } diff --git a/src/main/java/com/peanut/modules/common/entity/BuyOrderRefund.java b/src/main/java/com/peanut/modules/common/entity/BuyOrderRefund.java index 0d4c334..3569fa4 100644 --- a/src/main/java/com/peanut/modules/common/entity/BuyOrderRefund.java +++ b/src/main/java/com/peanut/modules/common/entity/BuyOrderRefund.java @@ -23,6 +23,9 @@ public class BuyOrderRefund implements Serializable { private String orderSn; private String payType; private BigDecimal fee; + private BigDecimal jfDeduction; + private BigDecimal shippingMoney; + private int deductShipping; //商品名称 private String title; private String remark; diff --git a/src/main/java/com/peanut/modules/common/entity/BuyOrderRefundLog.java b/src/main/java/com/peanut/modules/common/entity/BuyOrderRefundLog.java new file mode 100644 index 0000000..19d8fce --- /dev/null +++ b/src/main/java/com/peanut/modules/common/entity/BuyOrderRefundLog.java @@ -0,0 +1,37 @@ +package com.peanut.modules.common.entity; + +import com.baomidou.mybatisplus.annotation.FieldFill; +import com.baomidou.mybatisplus.annotation.TableField; +import com.baomidou.mybatisplus.annotation.TableId; +import com.baomidou.mybatisplus.annotation.TableName; +import lombok.Data; + +import java.io.Serializable; +import java.util.Date; + +/** + * 订单退款进度日志表 + * + * @author yl + * @email yl328572838@163.com + * @date 2022-08-29 15:27:44 + */ +@Data +@TableName("buy_order_refund_log") +public class BuyOrderRefundLog implements Serializable { + private static final long serialVersionUID = 1L; + + @TableId + private Integer id; + private Integer refundId; + private Date createTime; + private int type; + private int status; + + @TableField(exist = false) + private String title; + + @TableField(exist = false) + private String content; + +} diff --git a/src/main/java/com/peanut/modules/common/service/BuyOrderRefundLogService.java b/src/main/java/com/peanut/modules/common/service/BuyOrderRefundLogService.java new file mode 100644 index 0000000..516a4b5 --- /dev/null +++ b/src/main/java/com/peanut/modules/common/service/BuyOrderRefundLogService.java @@ -0,0 +1,9 @@ +package com.peanut.modules.common.service; + +import com.baomidou.mybatisplus.extension.service.IService; +import com.peanut.modules.common.entity.BuyOrderRefund; +import com.peanut.modules.common.entity.BuyOrderRefundLog; + +public interface BuyOrderRefundLogService extends IService { + void insertRefundLog(int buyOrderRefundId, int type, int status); +} diff --git a/src/main/java/com/peanut/modules/common/service/BuyOrderRefundService.java b/src/main/java/com/peanut/modules/common/service/BuyOrderRefundService.java index 7a7f53e..1ffa530 100644 --- a/src/main/java/com/peanut/modules/common/service/BuyOrderRefundService.java +++ b/src/main/java/com/peanut/modules/common/service/BuyOrderRefundService.java @@ -1,7 +1,11 @@ package com.peanut.modules.common.service; import com.baomidou.mybatisplus.extension.service.IService; +import com.peanut.modules.common.entity.BuyOrder; import com.peanut.modules.common.entity.BuyOrderRefund; +import java.math.BigDecimal; + public interface BuyOrderRefundService extends IService { + int insertBuyOrderRefund(BuyOrder buyOrder, BigDecimal refundFee, int deductShipping,String remark); } diff --git a/src/main/java/com/peanut/modules/common/service/CouponService.java b/src/main/java/com/peanut/modules/common/service/CouponService.java index bfdc40d..28b1df2 100644 --- a/src/main/java/com/peanut/modules/common/service/CouponService.java +++ b/src/main/java/com/peanut/modules/common/service/CouponService.java @@ -28,6 +28,8 @@ public interface CouponService extends IService { //回滚优惠卷 void rollbackCoupon(int couponHistoryId); + void refundZSCouponHistoryByOrder(BuyOrder order); + //通过商品发放优惠卷 void insertCouponHistoryByProductId(BuyOrder order); diff --git a/src/main/java/com/peanut/modules/common/service/JfTransactionDetailsService.java b/src/main/java/com/peanut/modules/common/service/JfTransactionDetailsService.java index 30bf2dc..1b9a641 100644 --- a/src/main/java/com/peanut/modules/common/service/JfTransactionDetailsService.java +++ b/src/main/java/com/peanut/modules/common/service/JfTransactionDetailsService.java @@ -10,4 +10,7 @@ public interface JfTransactionDetailsService extends IService implements BuyOrderRefundLogService { + @Override + public void insertRefundLog(int refundId, int type, int status){ + BuyOrderRefundLog log = new BuyOrderRefundLog(); + log.setRefundId(refundId); + log.setType(type); + log.setStatus(status); + this.save(log); + } +} diff --git a/src/main/java/com/peanut/modules/common/service/impl/BuyOrderRefundServiceImpl.java b/src/main/java/com/peanut/modules/common/service/impl/BuyOrderRefundServiceImpl.java index 83d2070..09dab2f 100644 --- a/src/main/java/com/peanut/modules/common/service/impl/BuyOrderRefundServiceImpl.java +++ b/src/main/java/com/peanut/modules/common/service/impl/BuyOrderRefundServiceImpl.java @@ -2,12 +2,32 @@ package com.peanut.modules.common.service.impl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.peanut.modules.common.dao.BuyOrderRefundDao; +import com.peanut.modules.common.entity.BuyOrder; import com.peanut.modules.common.entity.BuyOrderRefund; import com.peanut.modules.common.service.BuyOrderRefundService; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; +import java.math.BigDecimal; + @Slf4j @Service("commonBuyOrderRefundService") public class BuyOrderRefundServiceImpl extends ServiceImpl implements BuyOrderRefundService { + + @Override + public int insertBuyOrderRefund(BuyOrder buyOrder, BigDecimal refundFee, int deductShipping,String remark){ + BuyOrderRefund buyOrderRefund = new BuyOrderRefund(); + buyOrderRefund.setOrderId(buyOrder.getOrderId()); + buyOrderRefund.setType("线上"); //id + buyOrderRefund.setUserId(buyOrder.getUserId()); + buyOrderRefund.setOrderSn(buyOrder.getOrderSn()); + buyOrderRefund.setPayType(buyOrder.getPaymentMethod()); + buyOrderRefund.setFee(refundFee); + buyOrderRefund.setJfDeduction(buyOrder.getJfDeduction()); + buyOrderRefund.setShippingMoney(deductShipping==1?buyOrder.getShippingMoney():BigDecimal.ZERO); + buyOrderRefund.setDeductShipping(deductShipping); + buyOrderRefund.setRemark(remark); + this.save(buyOrderRefund); + return buyOrderRefund.getId(); + } } diff --git a/src/main/java/com/peanut/modules/common/service/impl/BuyOrderServiceImpl.java b/src/main/java/com/peanut/modules/common/service/impl/BuyOrderServiceImpl.java index c9533aa..23f1e5c 100644 --- a/src/main/java/com/peanut/modules/common/service/impl/BuyOrderServiceImpl.java +++ b/src/main/java/com/peanut/modules/common/service/impl/BuyOrderServiceImpl.java @@ -16,6 +16,8 @@ import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; + +import java.time.Instant; import java.util.Date; import java.util.HashMap; import java.util.List; @@ -73,7 +75,7 @@ public class BuyOrderServiceImpl extends ServiceImpl impl wrapper.eq(BuyOrder::getUserId,params.get("userId")); wrapper.eq(BuyOrder::getCome,params.get("come")); if(StringUtils.isEmpty(params.get("orderStatus").toString())){ - Integer[] sts = {0,1,2,3}; + Integer[] sts = {0,1,2,3,6}; wrapper.in(BuyOrder::getOrderStatus,sts); }else{ wrapper.eq(BuyOrder::getOrderStatus,params.get("orderStatus").toString()); @@ -88,6 +90,7 @@ public class BuyOrderServiceImpl extends ServiceImpl impl } //组装购买人 b.setUser(userDao.selectById(b.getUserId())); + boolean refundableStatus = false; //组装充值配置详情 if (b.getProductId()!=null){ BookBuyConfigEntity bookBuyConfigEntity = bookBuyConfigDao.selectById(b.getProductId()); @@ -97,7 +100,12 @@ public class BuyOrderServiceImpl extends ServiceImpl impl } b.setBookBuyConfigEntity(bookBuyConfigEntity); } + long timestamp = Instant.now().toEpochMilli(); + long paymentDateTime = b.getPaymentDate()==null?0: b.getPaymentDate().getTime(); if (b.getVipBuyConfigId()!=0){ + if(paymentDateTime > timestamp-7*24*60*60*1000){ + refundableStatus = true; + } b.setVipBuyConfigEntity(vipBuyConfigDao.selectById(b.getVipBuyConfigId())); } if (b.getAiBuyConfigId()!=0){ @@ -107,10 +115,27 @@ public class BuyOrderServiceImpl extends ServiceImpl impl List buyOrderProducts = buyOrderProductDao.selectList( new LambdaQueryWrapper().eq(BuyOrderProduct::getOrderId, b.getOrderId())); if (buyOrderProducts.size() > 0) { - for (BuyOrderProduct bb : buyOrderProducts){ + boolean[] refundableStatusArr = new boolean[buyOrderProducts.size()]; + for (int i=0;i w = new MPJLambdaWrapper<>(); w.disableLogicDel().eq(ShopProduct::getProductId,bb.getProductId()); bb.setProduct(shopProductDao.selectOne(w)); + boolean refundableStatusProduct = false; + if(bb.getProduct().getGoodsType().equals("05") && (paymentDateTime > timestamp-7*24*60*60*1000)){ + refundableStatusProduct = true; + }else if(!bb.getProduct().getGoodsType().equals("05") && b.getOrderStatus().equals("1")){ + refundableStatusProduct = true; + } + refundableStatusArr[i] = refundableStatusProduct; + } + for (boolean rs : refundableStatusArr){ + if(!rs){ + refundableStatus = false; + break; + }else{ + refundableStatus = true; + } } b.setProductList(buyOrderProducts); b.setTimestamp(b.getCreateTime().getTime()/1000); @@ -122,6 +147,7 @@ public class BuyOrderServiceImpl extends ServiceImpl impl b.setExpressList(expressOrders); } } + b.setRefundableStatus(b.getOrderStatus().equals("6")?false:refundableStatus); } } return page; diff --git a/src/main/java/com/peanut/modules/common/service/impl/CouponServiceImpl.java b/src/main/java/com/peanut/modules/common/service/impl/CouponServiceImpl.java index 7e2941c..a57a93f 100644 --- a/src/main/java/com/peanut/modules/common/service/impl/CouponServiceImpl.java +++ b/src/main/java/com/peanut/modules/common/service/impl/CouponServiceImpl.java @@ -1,6 +1,7 @@ package com.peanut.modules.common.service.impl; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.github.yulichang.wrapper.MPJLambdaWrapper; import com.peanut.common.utils.DateUtils; @@ -8,7 +9,9 @@ import com.peanut.common.utils.R; import com.peanut.common.utils.ShiroUtils; import com.peanut.modules.common.dao.*; import com.peanut.modules.common.entity.*; +import com.peanut.modules.common.service.CouponHistoryService; import com.peanut.modules.common.service.CouponService; +import com.peanut.modules.common.service.JfTransactionDetailsService; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang.StringUtils; import org.springframework.beans.factory.annotation.Autowired; @@ -41,6 +44,10 @@ public class CouponServiceImpl extends ServiceImpl impl private ShopProductDao shopProductDao; @Autowired private JfTransactionDetailsDao jfTransactionDetailsDao; + @Autowired + private JfTransactionDetailsService jfTransactionDetailsService; + @Autowired + private CouponHistoryService couponHistoryService; @Override public CouponEntity getByIdSetRange(int id) { @@ -252,6 +259,59 @@ public class CouponServiceImpl extends ServiceImpl impl } } + @Override + public void refundZSCouponHistoryByOrder(BuyOrder order){ + MPJLambdaWrapper wrapper = new MPJLambdaWrapper(); + wrapper.leftJoin(BuyOrderProduct.class,BuyOrderProduct::getProductId,CouponToProduct::getProductId); + wrapper.leftJoin(CouponEntity.class,CouponEntity::getId,CouponToProduct::getCouponId); + wrapper.eq(CouponEntity::getCurrentState,0); + wrapper.eq(BuyOrderProduct::getOrderId,order.getOrderId()); + wrapper.select(CouponToProduct::getCouponId); + wrapper.select(BuyOrderProduct::getProductId); + wrapper.select(BuyOrderProduct::getQuantity); + List> buyOrderProducts = couponToProductDao.selectJoinMaps(wrapper); + + MyUserEntity userEntity = userDao.selectById(order.getUserId()); + List userVipList = userVipDao.selectList(new LambdaQueryWrapper() + .eq(UserVip::getUserId, userEntity.getId()) + .eq(UserVip::getState,0) + .lt(UserVip::getStartTime,order.getPaymentDate())); + boolean isVip = false; + if (userVipList.size() > 0) { + isVip = true; + } + BigDecimal refundAllJF = BigDecimal.ZERO; + Boolean isRefundCoupon = false; + for (Map map : buyOrderProducts) { + ShopProduct shopProduct = shopProductDao.selectById(map.get("product_id").toString()); + if ("03".equals(shopProduct.getGoodsType())){ + int couponId = Integer.parseInt(map.get("coupon_id").toString()); + CouponEntity couponEntity = couponDao.selectById(couponId); + //现金券 + if (couponEntity.getCouponType()==0 && isVip){ + BigDecimal quantity = new BigDecimal(map.get("quantity").toString()); + refundAllJF.add(couponEntity.getCouponAmount().multiply(quantity)); + }else{ + isRefundCoupon = true; + } + }else{ + isRefundCoupon = true; + } + } + if(refundAllJF.compareTo(BigDecimal.ZERO)>0 && userEntity.getJf().compareTo(refundAllJF)>=0){ + BigDecimal userJF = userEntity.getJf().subtract(refundAllJF); + userEntity.setJf(userJF.compareTo(BigDecimal.ZERO)>=0?userJF:BigDecimal.ZERO); + userDao.updateById(userEntity); + jfTransactionDetailsService.recordZsJfTransaction(order,userEntity, refundAllJF); + } + if(isRefundCoupon){ + QueryWrapper couponHistoryRemoveWrapper = new QueryWrapper<>(); + couponHistoryRemoveWrapper.eq("order_id",order.getOrderId()); + couponHistoryRemoveWrapper.eq("status",0); + couponHistoryService.remove(couponHistoryRemoveWrapper); + } + + } @Override public void insertCouponHistoryByProductId(BuyOrder order) { MPJLambdaWrapper wrapper = new MPJLambdaWrapper(); diff --git a/src/main/java/com/peanut/modules/common/service/impl/JfTransactionDetailsServiceImpl.java b/src/main/java/com/peanut/modules/common/service/impl/JfTransactionDetailsServiceImpl.java index 7ee2239..f223f7f 100644 --- a/src/main/java/com/peanut/modules/common/service/impl/JfTransactionDetailsServiceImpl.java +++ b/src/main/java/com/peanut/modules/common/service/impl/JfTransactionDetailsServiceImpl.java @@ -22,4 +22,26 @@ public class JfTransactionDetailsServiceImpl extends ServiceImpl