|
@@ -1,14 +1,10 @@
|
|
|
package com.caimei.service.impl;
|
|
|
|
|
|
-import com.caimei.mapper.OrderSubmitMapper;
|
|
|
-import com.caimei.mapper.ProductMapper;
|
|
|
-import com.caimei.mapper.ShoppingCartMapper;
|
|
|
+import com.caimei.mapper.*;
|
|
|
import com.caimei.model.ResponseJson;
|
|
|
import com.caimei.model.po.*;
|
|
|
-import com.caimei.model.vo.AddressVo;
|
|
|
-import com.caimei.model.vo.CartProductVo;
|
|
|
-import com.caimei.model.vo.LadderPriceVo;
|
|
|
-import com.caimei.model.vo.ShopVo;
|
|
|
+import com.caimei.model.vo.*;
|
|
|
+import com.caimei.service.OrderService;
|
|
|
import com.caimei.service.OrderSubmitService;
|
|
|
import com.caimei.service.ShoppingCartService;
|
|
|
import com.caimei.util.MathUtil;
|
|
@@ -16,6 +12,7 @@ import com.caimei.util.OrderNoUtils;
|
|
|
import com.caimei.util.ProductUtils;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
@@ -45,18 +42,32 @@ public class OrderSubmitServiceImpl implements OrderSubmitService {
|
|
|
private ProductMapper productMapper;
|
|
|
@Resource
|
|
|
private ShoppingCartMapper shoppingCartMapper;
|
|
|
+ @Resource
|
|
|
+ private UserMapper userMapper;
|
|
|
+ @Resource
|
|
|
+ private OrderMapper orderMapper;
|
|
|
+ @Resource
|
|
|
+ private PayOrderMapper payOrderMapper;
|
|
|
@Value("${caimei.oldapi}")
|
|
|
private String domain;
|
|
|
|
|
|
+ private OrderService orderService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ public void setOrderService(OrderService orderService) {
|
|
|
+ this.orderService = orderService;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
@Override
|
|
|
public ResponseJson<Map<String, Object>> orderConfirm(String productIds, Integer userId) {
|
|
|
Map<String, Object> confirmData = new HashMap<>(5);
|
|
|
if (userId != null) {
|
|
|
log.info("<<<<< 结算订单 >>>>>");
|
|
|
//商品总金额
|
|
|
- AtomicReference<BigDecimal> totalPrice = new AtomicReference<BigDecimal>(BigDecimal.ZERO);
|
|
|
+ AtomicReference<BigDecimal> totalPrice = new AtomicReference<>(BigDecimal.ZERO);
|
|
|
//促销满减
|
|
|
- AtomicReference<BigDecimal> totalFullReduction = new AtomicReference<BigDecimal>(BigDecimal.ZERO);
|
|
|
+ AtomicReference<BigDecimal> totalFullReduction = new AtomicReference<>(BigDecimal.ZERO);
|
|
|
String[] productId = productIds.split(",");
|
|
|
List<Integer> shopIds = new ArrayList<>();
|
|
|
List<CartProductVo> productGifts = new ArrayList<>();
|
|
@@ -123,6 +134,16 @@ public class OrderSubmitServiceImpl implements OrderSubmitService {
|
|
|
if (null == user) {
|
|
|
return ResponseJson.error("用户信息异常", null);
|
|
|
}
|
|
|
+ // 用户余额
|
|
|
+ BigDecimal ableUserMoney = user.getAbleUserMoney();
|
|
|
+ BigDecimal userMoney = user.getUserMoney();
|
|
|
+ Integer balancePayFlag = (Integer) payInfo.get("balancePayFlag");
|
|
|
+ if (1 == balancePayFlag && null != ableUserMoney) {
|
|
|
+ // 使用余额
|
|
|
+ if (MathUtil.compare(ableUserMoney, BigDecimal.ZERO) == 0) {
|
|
|
+ return ResponseJson.error("用户可用余额为0",null);
|
|
|
+ }
|
|
|
+ }
|
|
|
// 商品总数量
|
|
|
Integer productCount = 0;
|
|
|
// 赠品数量
|
|
@@ -137,6 +158,8 @@ public class OrderSubmitServiceImpl implements OrderSubmitService {
|
|
|
BigDecimal payTotalFee = BigDecimal.ZERO;
|
|
|
// 真实支付金额(订单总额减去抵扣的账户余额)
|
|
|
BigDecimal payableAmount = BigDecimal.ZERO;
|
|
|
+ // 余额支付金额
|
|
|
+ BigDecimal balancePayFee = BigDecimal.ZERO;
|
|
|
// 促销满减优惠
|
|
|
BigDecimal promotionFullReduction = BigDecimal.ZERO;
|
|
|
// 运费
|
|
@@ -405,9 +428,39 @@ public class OrderSubmitServiceImpl implements OrderSubmitService {
|
|
|
// 订单状态
|
|
|
order.setStatus("11");
|
|
|
order.setConfirmTime(curDateStr);
|
|
|
-
|
|
|
+ // 是否完成支付(默认不是,只有余额抵扣才算)
|
|
|
+ boolean isPaySuccessFlag = false;
|
|
|
+ // 余额支付标识,0不使用,1使用
|
|
|
+ if (1 == balancePayFlag) {
|
|
|
+ // 部分抵扣
|
|
|
+ if (MathUtil.compare(payTotalFee, ableUserMoney) > 0) {
|
|
|
+ balancePayFee = ableUserMoney;
|
|
|
+ payableAmount = MathUtil.sub(payTotalFee, balancePayFee);
|
|
|
+ // 余额抵扣用完
|
|
|
+ user.setAbleUserMoney(new BigDecimal(0));
|
|
|
+ // 部收款待发货
|
|
|
+ order.setStatus("21");
|
|
|
+ user.setUserMoney(MathUtil.sub(userMoney, balancePayFee));
|
|
|
+ order.setReceiptStatus("2");
|
|
|
+ } else {
|
|
|
+ // 全部用余额抵扣, 直接变成支付完成
|
|
|
+ balancePayFee = payTotalFee;
|
|
|
+ payableAmount = BigDecimal.ZERO;
|
|
|
+ user.setAbleUserMoney(MathUtil.sub(ableUserMoney, balancePayFee));
|
|
|
+ // 已收款待发货
|
|
|
+ order.setStatus("31");
|
|
|
+ user.setUserMoney(MathUtil.sub(userMoney, balancePayFee));
|
|
|
+ order.setReceiptStatus("3");
|
|
|
+ order.setPayFlag("1");
|
|
|
+ isPaySuccessFlag = true;
|
|
|
+ }
|
|
|
+ userMapper.updateMoney(user.getUserMoney(), user.getAbleUserMoney(), user.getUserID());
|
|
|
+ log.info(">>>>>更新余额抵扣:[userMoney:" + user.getUserMoney() + "] ,ableUserMoney:" + user.getAbleUserMoney());
|
|
|
+ // 支付时间
|
|
|
+ order.setPayTime(curDateStr);
|
|
|
+ }
|
|
|
// 余额支付金额
|
|
|
- order.setBalancePayFee(BigDecimal.ZERO);
|
|
|
+ order.setBalancePayFee(balancePayFee);
|
|
|
// 实际支付金额(商品金额+运费-余额抵扣)
|
|
|
order.setPayableAmount(payableAmount);
|
|
|
// 售后条款
|
|
@@ -542,6 +595,16 @@ public class OrderSubmitServiceImpl implements OrderSubmitService {
|
|
|
return ResponseJson.error("订单地址异常", null);
|
|
|
}
|
|
|
|
|
|
+ /*
|
|
|
+ *保存余额到余额收支记录
|
|
|
+ */
|
|
|
+ if (1 == balancePayFlag && MathUtil.compare(balancePayFee, 0) > 0) {
|
|
|
+ // 余额支付标识,0不使用,1使用
|
|
|
+ saveBalanceRecord(balancePayFee, order.getOrderID().intValue(), user.getUserID());
|
|
|
+ //保存余额到收款记录
|
|
|
+ saveDiscernReceipt(balancePayFee, order.getOrderID().intValue());
|
|
|
+ }
|
|
|
+
|
|
|
/*
|
|
|
* 保存 订单发票信息
|
|
|
*/
|
|
@@ -573,7 +636,40 @@ public class OrderSubmitServiceImpl implements OrderSubmitService {
|
|
|
info.put("payTotalFee", String.valueOf(order.getPayTotalFee()));
|
|
|
//真实需要付款金额
|
|
|
info.put("payableAmount", String.valueOf(order.getPayableAmount()));
|
|
|
- return ResponseJson.success(info);
|
|
|
+ if (isPaySuccessFlag) {
|
|
|
+ // 余额抵扣成功
|
|
|
+ // 1提交成功[且支付完成]
|
|
|
+ info.put("code", "1");
|
|
|
+ info.put("msg", "提交成功且已支付");
|
|
|
+ return ResponseJson.success(info);
|
|
|
+ } else {
|
|
|
+ info.put("code", "2");
|
|
|
+ info.put("msg", "提交成功但未支付");
|
|
|
+ return ResponseJson.success(info);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 新增余额抵扣收支记录
|
|
|
+ *
|
|
|
+ * @param balancePayFee 余额支付金额
|
|
|
+ * @param orderId 订单id
|
|
|
+ * @param userId 用户id
|
|
|
+ */
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ @Override
|
|
|
+ public void saveBalanceRecord(BigDecimal balancePayFee, Integer orderId, Integer userId) {
|
|
|
+ //保存余额到余额收支记录
|
|
|
+ BalanceRecordPo balanceRecord = new BalanceRecordPo();
|
|
|
+ balanceRecord.setUserId(userId.longValue());
|
|
|
+ balanceRecord.setType("2");
|
|
|
+ balanceRecord.setBalanceType("1");
|
|
|
+ balanceRecord.setAddDate(new Date());
|
|
|
+ balanceRecord.setAmount(balancePayFee);
|
|
|
+ balanceRecord.setOrderId(orderId.longValue());
|
|
|
+ balanceRecord.setDelFlag("0");
|
|
|
+ userMapper.insertBalanceRecord(balanceRecord);
|
|
|
+ log.info(">>>>>>>>>>>>>>>>>>>>>>>>>>新增用户余额收支记录(insert[cm_user_balance_record])id:" + balanceRecord.getId());
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -1056,4 +1152,96 @@ public class OrderSubmitServiceImpl implements OrderSubmitService {
|
|
|
shopOrderIds += (("".equals(shopOrderIds) ? "" : ",") + newShopOrder.getShopOrderID());
|
|
|
return shopOrderIds;
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public ResponseJson<Map<String, Object>> balanceDeduction(Integer orderId) {
|
|
|
+ OrderVo order = orderMapper.findOrder(orderId);
|
|
|
+ orderService.getDiscernReceipt(order);
|
|
|
+ UserPo user = orderSubmitMapper.findUser(order.getUserId().intValue());
|
|
|
+ //本次余额抵扣金额
|
|
|
+ BigDecimal balancePayFee = BigDecimal.ZERO;
|
|
|
+ if (MathUtil.compare(user.getAbleUserMoney(), 0) > 0) {
|
|
|
+ if (MathUtil.compare(user.getAbleUserMoney(), order.getPendingPayments()) < 0) {
|
|
|
+ if (!"0".equals(order.getStatus())) {
|
|
|
+ //待确认状态,不修改订单状态
|
|
|
+ if ("1".equals(order.getSendOutStatus())) {
|
|
|
+ order.setStatus("21");
|
|
|
+ } else if ("2".equals(order.getSendOutStatus())) {
|
|
|
+ order.setStatus("22");
|
|
|
+ } else {
|
|
|
+ order.setStatus("23");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ order.setReceiptStatus("2");
|
|
|
+ balancePayFee = user.getAbleUserMoney();
|
|
|
+ } else {
|
|
|
+ if (!"0".equals(order.getStatus())) {
|
|
|
+ if ("1".equals(order.getSendOutStatus())) {
|
|
|
+ order.setStatus("31");
|
|
|
+ } else if ("2".equals(order.getSendOutStatus())) {
|
|
|
+ order.setStatus("32");
|
|
|
+ } else {
|
|
|
+ order.setStatus("33");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ order.setReceiptStatus("3");
|
|
|
+ balancePayFee = order.getPendingPayments();
|
|
|
+ }
|
|
|
+ order.setBalancePayFee(MathUtil.add(order.getBalancePayFee(), balancePayFee));
|
|
|
+ order.setPayableAmount(MathUtil.sub(order.getPayableAmount(), balancePayFee));
|
|
|
+ payOrderMapper.updateSelective(order);
|
|
|
+ //剩余待付金额
|
|
|
+ order.setPendingPayments(MathUtil.sub(order.getPendingPayments(), balancePayFee));
|
|
|
+ //修改账户余额
|
|
|
+ if (!"0".equals(order.getStatus())) {
|
|
|
+ user.setUserMoney(MathUtil.sub(user.getUserMoney(), balancePayFee));
|
|
|
+ //保存收款记录
|
|
|
+ saveDiscernReceipt(balancePayFee, orderId);
|
|
|
+ }
|
|
|
+ user.setAbleUserMoney(MathUtil.sub(user.getAbleUserMoney(), balancePayFee));
|
|
|
+ userMapper.updateMoney(user.getUserMoney(), user.getAbleUserMoney(), user.getUserID());
|
|
|
+ //保存余额收支记录
|
|
|
+ saveBalanceRecord(balancePayFee, orderId, user.getUserID());
|
|
|
+ }
|
|
|
+ Map<String, Object> map = new HashMap<>(2);
|
|
|
+ map.put("order", order);
|
|
|
+ map.put("balancePayFee", balancePayFee);
|
|
|
+ return ResponseJson.success(map);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 保存余额到收款记录,余额收支记录
|
|
|
+ *
|
|
|
+ * @param balancePayFee 余额支付金额
|
|
|
+ * @param orderId 订单id
|
|
|
+ */
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ @Override
|
|
|
+ public void saveDiscernReceipt(BigDecimal balancePayFee, Integer orderId) {
|
|
|
+ String curDateStr = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(new Date());
|
|
|
+ CmDiscernReceiptPo discernReceipt = new CmDiscernReceiptPo();
|
|
|
+ discernReceipt.setPayWay("3");
|
|
|
+ discernReceipt.setPayType("16");
|
|
|
+ discernReceipt.setReceiptType("1");
|
|
|
+ discernReceipt.setReceiptStatus("3");
|
|
|
+ discernReceipt.setReceiptAmount(balancePayFee);
|
|
|
+ discernReceipt.setConfirmType("4");
|
|
|
+ discernReceipt.setReceiptDate(curDateStr);
|
|
|
+ discernReceipt.setConfirmDate(curDateStr);
|
|
|
+ discernReceipt.setReviewDate(curDateStr);
|
|
|
+ discernReceipt.setUpdateDate(curDateStr);
|
|
|
+ discernReceipt.setDelFlag("0");
|
|
|
+ payOrderMapper.insertDiscernReceipt(discernReceipt);
|
|
|
+ CmReceiptOrderRelationPo relation = new CmReceiptOrderRelationPo();
|
|
|
+ relation.setReceiptID(discernReceipt.getId().intValue());
|
|
|
+ relation.setOrderID(orderId);
|
|
|
+ relation.setAssociateAmount(balancePayFee);
|
|
|
+ relation.setRelationType("2");
|
|
|
+ relation.setDelFlag("0");
|
|
|
+ payOrderMapper.insertOrderRelation(relation);
|
|
|
+ log.info(">>>>>>>>>>>>>>>>>>>>>>>保存余额到收款记录," + balancePayFee);
|
|
|
+ }
|
|
|
}
|