|
@@ -1,23 +1,39 @@
|
|
|
package com.ylx.massage.service.impl;
|
|
package com.ylx.massage.service.impl;
|
|
|
|
|
|
|
|
import cn.hutool.json.JSONUtil;
|
|
import cn.hutool.json.JSONUtil;
|
|
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
|
|
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.StringUtils;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
|
|
+import com.ylx.common.constant.MassageConstants;
|
|
|
|
|
+import com.ylx.common.core.domain.R;
|
|
|
import com.ylx.common.exception.ServiceException;
|
|
import com.ylx.common.exception.ServiceException;
|
|
|
-import com.ylx.massage.domain.Product;
|
|
|
|
|
-import com.ylx.massage.domain.ProductOrderInfo;
|
|
|
|
|
-import com.ylx.massage.domain.ProductOrderItem;
|
|
|
|
|
|
|
+import com.ylx.massage.domain.*;
|
|
|
import com.ylx.massage.domain.dto.ProductOrderCreateRequest;
|
|
import com.ylx.massage.domain.dto.ProductOrderCreateRequest;
|
|
|
|
|
+import com.ylx.massage.domain.dto.ProductOrderPayRequest;
|
|
|
|
|
+import com.ylx.massage.enums.BillTypeEnum;
|
|
|
|
|
+import com.ylx.massage.enums.OrderStatusEnum;
|
|
|
|
|
+import com.ylx.massage.enums.ProductOrderStatusEnum;
|
|
|
import com.ylx.massage.mapper.ProductMapper;
|
|
import com.ylx.massage.mapper.ProductMapper;
|
|
|
import com.ylx.massage.mapper.ProductOrderInfoMapper;
|
|
import com.ylx.massage.mapper.ProductOrderInfoMapper;
|
|
|
import com.ylx.massage.mapper.ProductOrderItemMapper;
|
|
import com.ylx.massage.mapper.ProductOrderItemMapper;
|
|
|
import com.ylx.massage.service.IProductOrderInfoService;
|
|
import com.ylx.massage.service.IProductOrderInfoService;
|
|
|
|
|
+import com.ylx.massage.service.TConsumptionLogService;
|
|
|
|
|
+import com.ylx.massage.service.TRechargeService;
|
|
|
|
|
+import com.ylx.massage.service.TWxUserService;
|
|
|
import com.ylx.massage.utils.OrderNumberGenerator;
|
|
import com.ylx.massage.utils.OrderNumberGenerator;
|
|
|
|
|
+import com.ylx.massage.utils.Sendvoice;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
|
|
|
|
+import javax.annotation.Resource;
|
|
|
|
|
+import java.math.BigDecimal;
|
|
|
import java.time.LocalDateTime;
|
|
import java.time.LocalDateTime;
|
|
|
|
|
+import java.util.Date;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 商品订单信息Service实现类
|
|
* 商品订单信息Service实现类
|
|
@@ -38,6 +54,14 @@ public class ProductOrderInfoServiceImpl extends ServiceImpl<ProductOrderInfoMap
|
|
|
@Autowired
|
|
@Autowired
|
|
|
private ProductMapper productMapper;
|
|
private ProductMapper productMapper;
|
|
|
|
|
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private TRechargeService rechargeService;
|
|
|
|
|
+
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private TWxUserService wxUserService;
|
|
|
|
|
+
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private TConsumptionLogService consumptionLogService;
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -111,10 +135,10 @@ public class ProductOrderInfoServiceImpl extends ServiceImpl<ProductOrderInfoMap
|
|
|
if (request.getPayType() == null || (request.getPayType() != 1 && request.getPayType() != 2)) {
|
|
if (request.getPayType() == null || (request.getPayType() != 1 && request.getPayType() != 2)) {
|
|
|
throw new ServiceException("支付方式不正确");
|
|
throw new ServiceException("支付方式不正确");
|
|
|
}
|
|
}
|
|
|
- if (request.getTotalAmount() == null || request.getTotalAmount() < 0) {
|
|
|
|
|
|
|
+ if (request.getTotalAmount() == null || request.getTotalAmount().compareTo(BigDecimal.ZERO) < 0) {
|
|
|
throw new ServiceException("总金额不能为空");
|
|
throw new ServiceException("总金额不能为空");
|
|
|
}
|
|
}
|
|
|
- if (request.getPayAmount() == null || request.getPayAmount() < 0) {
|
|
|
|
|
|
|
+ if (request.getPayAmount() == null || request.getPayAmount().compareTo(BigDecimal.ZERO) < 0) {
|
|
|
throw new ServiceException("支付金额不能为空");
|
|
throw new ServiceException("支付金额不能为空");
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -134,8 +158,8 @@ public class ProductOrderInfoServiceImpl extends ServiceImpl<ProductOrderInfoMap
|
|
|
orderInfo.setPayStatus(0); // 未支付
|
|
orderInfo.setPayStatus(0); // 未支付
|
|
|
orderInfo.setPayType(request.getPayType());
|
|
orderInfo.setPayType(request.getPayType());
|
|
|
orderInfo.setTotalAmount(request.getTotalAmount());
|
|
orderInfo.setTotalAmount(request.getTotalAmount());
|
|
|
- orderInfo.setDiscountAmount(0); // 优惠金额暂无
|
|
|
|
|
- orderInfo.setFreightAmount(request.getFreightAmount() != null ? request.getFreightAmount() : 0);
|
|
|
|
|
|
|
+ orderInfo.setDiscountAmount(BigDecimal.ZERO); // 优惠金额暂无
|
|
|
|
|
+ orderInfo.setFreightAmount(request.getFreightAmount() != null ? request.getFreightAmount() : BigDecimal.ZERO);
|
|
|
orderInfo.setPayAmount(request.getPayAmount());
|
|
orderInfo.setPayAmount(request.getPayAmount());
|
|
|
orderInfo.setPointsUsed(request.getPointsUsed() != null ? request.getPointsUsed() : 0);
|
|
orderInfo.setPointsUsed(request.getPointsUsed() != null ? request.getPointsUsed() : 0);
|
|
|
orderInfo.setAddressId(request.getAddressId());
|
|
orderInfo.setAddressId(request.getAddressId());
|
|
@@ -167,12 +191,98 @@ public class ProductOrderInfoServiceImpl extends ServiceImpl<ProductOrderInfoMap
|
|
|
}
|
|
}
|
|
|
orderItem.setSkuImage(request.getProductMainImage());
|
|
orderItem.setSkuImage(request.getProductMainImage());
|
|
|
// 单价 = 总金额 / 数量
|
|
// 单价 = 总金额 / 数量
|
|
|
- orderItem.setPrice(request.getTotalAmount() / request.getQuantity());
|
|
|
|
|
|
|
+ orderItem.setPrice(request.getTotalAmount().intValue() / request.getQuantity());
|
|
|
orderItem.setQuantity(request.getQuantity());
|
|
orderItem.setQuantity(request.getQuantity());
|
|
|
orderItem.setTotalAmount(request.getTotalAmount());
|
|
orderItem.setTotalAmount(request.getTotalAmount());
|
|
|
- orderItem.setDiscountAmount(0); // 优惠金额暂无
|
|
|
|
|
|
|
+ orderItem.setDiscountAmount(BigDecimal.ZERO); // 优惠金额暂无
|
|
|
orderItem.setActualAmount(request.getPayAmount());
|
|
orderItem.setActualAmount(request.getPayAmount());
|
|
|
orderItem.setRefundStatus(0); // 无退款
|
|
orderItem.setRefundStatus(0); // 无退款
|
|
|
return orderItem;
|
|
return orderItem;
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public R payOrder(ProductOrderPayRequest productOrderPayRequest) throws Exception {
|
|
|
|
|
+ // 1、校验订单是否存在
|
|
|
|
|
+ LambdaQueryWrapper<ProductOrderInfo> lambdaQueryWrapper = new LambdaQueryWrapper<ProductOrderInfo>().eq(ProductOrderInfo::getOrderNo, productOrderPayRequest.getOrderNo());
|
|
|
|
|
+ ProductOrderInfo orderInfo = productOrderInfoMapper.selectOne(lambdaQueryWrapper);
|
|
|
|
|
+ if (orderInfo == null) {
|
|
|
|
|
+ throw new ServiceException("订单不存在");
|
|
|
|
|
+ }
|
|
|
|
|
+ // 2、校验订单状态是否为待付款
|
|
|
|
|
+ if (orderInfo.getOrderStatus() != 0) {
|
|
|
|
|
+ throw new ServiceException("订单状态错误");
|
|
|
|
|
+ }
|
|
|
|
|
+ // 3、校验支付金额是否正确
|
|
|
|
|
+ if (orderInfo.getTotalAmount().compareTo(productOrderPayRequest.getTotalPrice()) != 0) {
|
|
|
|
|
+ throw new ServiceException("支付金额错误");
|
|
|
|
|
+ }
|
|
|
|
|
+ // 4、校验支付方式是否正确
|
|
|
|
|
+ if (orderInfo.getPayType() != productOrderPayRequest.getPayType()) {
|
|
|
|
|
+ throw new ServiceException("支付方式错误");
|
|
|
|
|
+ }
|
|
|
|
|
+ //判断支付方式
|
|
|
|
|
+ if (productOrderPayRequest.getPayType().equals(MassageConstants.INTEGER_ONE)) {
|
|
|
|
|
+ //微信支付
|
|
|
|
|
+ R resp = rechargeService.getPay(orderInfo.getOrderNo(), orderInfo.getTotalAmount(), orderInfo.getOpenId(), BillTypeEnum.WX_PAY.getInfo(), BillTypeEnum.WX_PAY.getCode().toString());
|
|
|
|
|
+ return resp;
|
|
|
|
|
+ }else{
|
|
|
|
|
+ //余额支付
|
|
|
|
|
+ TWxUser user = wxUserService.getByOpenId(orderInfo.getOpenId());
|
|
|
|
|
+ if (null == user) {
|
|
|
|
|
+ throw new ServiceException("用户不存在");
|
|
|
|
|
+ }
|
|
|
|
|
+ if (user.getdBalance().compareTo(orderInfo.getTotalAmount()) < MassageConstants.INTEGER_ZERO) {
|
|
|
|
|
+ throw new ServiceException("账户金额不够请充值");
|
|
|
|
|
+ } else {
|
|
|
|
|
+ orderPayManage(user, orderInfo);
|
|
|
|
|
+ return R.ok();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 订单支付管理
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param user
|
|
|
|
|
+ * @param orderInfo
|
|
|
|
|
+ */
|
|
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
|
|
+ public void orderPayManage(TWxUser user, ProductOrderInfo orderInfo) {
|
|
|
|
|
+ // 更新用户金额 及下单此时
|
|
|
|
|
+ TWxUser paramUser = new TWxUser();
|
|
|
|
|
+ paramUser.setcOpenid(user.getcOpenid());
|
|
|
|
|
+ // 余额支付
|
|
|
|
|
+ if (orderInfo.getPayType().equals(MassageConstants.INTEGER_TWO)) {
|
|
|
|
|
+ paramUser.setdBalance(user.getdBalance().subtract(orderInfo.getTotalAmount()));
|
|
|
|
|
+ }
|
|
|
|
|
+ paramUser.setdMoney(user.getdMoney().add(orderInfo.getTotalAmount()));
|
|
|
|
|
+ paramUser.setnNum(user.getnNum() + MassageConstants.INTEGER_ONE);
|
|
|
|
|
+ paramUser.setId(user.getId());
|
|
|
|
|
+ wxUserService.updateById(paramUser);
|
|
|
|
|
+ //增加消费记录
|
|
|
|
|
+ TConsumptionLog tConsumptionLog = new TConsumptionLog();
|
|
|
|
|
+ tConsumptionLog.setAmount(orderInfo.getTotalAmount().negate());
|
|
|
|
|
+ tConsumptionLog.setBillNo(orderInfo.getOrderNo());
|
|
|
|
|
+ tConsumptionLog.setOpenId(orderInfo.getOpenId());
|
|
|
|
|
+ if (orderInfo.getPayType().equals(MassageConstants.INTEGER_TWO)) {
|
|
|
|
|
+ tConsumptionLog.setBillType(BillTypeEnum.BALANCE_PAYMENT.getCode());
|
|
|
|
|
+ tConsumptionLog.setNote("余额支付");
|
|
|
|
|
+ } else if(orderInfo.getPayType().equals(MassageConstants.INTEGER_ONE)){
|
|
|
|
|
+ tConsumptionLog.setBillType(BillTypeEnum.WX_PAY.getCode());
|
|
|
|
|
+ tConsumptionLog.setNote("微信支付");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ consumptionLogService.save(tConsumptionLog);
|
|
|
|
|
+
|
|
|
|
|
+ ProductOrderInfo productOrderInfo = new ProductOrderInfo();
|
|
|
|
|
+ productOrderInfo.setId(orderInfo.getId());
|
|
|
|
|
+ productOrderInfo.setPayType(orderInfo.getPayType());
|
|
|
|
|
+ productOrderInfo.setOpenId(orderInfo.getOpenId());
|
|
|
|
|
+ //更新订单状态为待发货
|
|
|
|
|
+ productOrderInfo.setOrderStatus(ProductOrderStatusEnum.WAIT_DELIVERY.getCode());
|
|
|
|
|
+ productOrderInfo.setPayTime(LocalDateTime.now());
|
|
|
|
|
+ //更新订单表
|
|
|
|
|
+ updateById(productOrderInfo);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|