|
|
@@ -1,12 +1,164 @@
|
|
|
package com.ylx.giftCard.service.impl;
|
|
|
|
|
|
+import cn.hutool.core.collection.CollUtil;
|
|
|
+import cn.hutool.core.util.ObjectUtil;
|
|
|
+import cn.hutool.core.util.RandomUtil;
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.ylx.common.core.domain.model.WxLoginUser;
|
|
|
+import com.ylx.common.exception.ServiceException;
|
|
|
+import com.ylx.common.utils.DateUtils;
|
|
|
+import com.ylx.common.utils.SecurityUtils;
|
|
|
+import com.ylx.giftCard.domain.GiftCard;
|
|
|
import com.ylx.giftCard.domain.GiftCardOrder;
|
|
|
import com.ylx.giftCard.mapper.GiftCardOrderMapper;
|
|
|
import com.ylx.giftCard.service.IGiftCardOrderService;
|
|
|
+import com.ylx.massage.domain.TJs;
|
|
|
+import com.ylx.massage.service.TJsService;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
+import javax.annotation.Resource;
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.math.RoundingMode;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+@Slf4j
|
|
|
@Service
|
|
|
public class GiftCardOrderServiceImpl extends ServiceImpl<GiftCardOrderMapper, GiftCardOrder> implements IGiftCardOrderService {
|
|
|
|
|
|
-}
|
|
|
+ @Resource
|
|
|
+ private TJsService jsService;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public void buildOrders(GiftCard card, Integer quantity, String merchantId) {
|
|
|
+ // 1. 参数校验
|
|
|
+ if (ObjectUtil.isNull(card)) {
|
|
|
+ throw new IllegalArgumentException("购物卡信息不能为空");
|
|
|
+ }
|
|
|
+ if (quantity == null || quantity <= 0) {
|
|
|
+ throw new IllegalArgumentException("购买数量必须大于0");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 2. 获取用户信息
|
|
|
+ WxLoginUser wxLoginUser = SecurityUtils.getWxLoginUser();
|
|
|
+ if (ObjectUtil.isNull(wxLoginUser)) {
|
|
|
+ log.warn("用户未登录,无法创建订单");
|
|
|
+ throw new ServiceException("用户未登录");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 3. 查询商户信息(只查询一次,避免重复查询)
|
|
|
+ TJs merchant = null;
|
|
|
+ if (StrUtil.isNotEmpty(merchantId)) {
|
|
|
+ merchant = this.jsService.getById(merchantId);
|
|
|
+ if (ObjectUtil.isNull(merchant)) {
|
|
|
+ log.warn("商户信息不存在,ID: {}", merchantId);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 4. 批量创建订单
|
|
|
+ List<GiftCardOrder> orders = new ArrayList<>(quantity);
|
|
|
+ for (int i = 0; i < quantity; i++) {
|
|
|
+ GiftCardOrder order = createSingleOrder(card, wxLoginUser, merchant, i + 1, quantity);
|
|
|
+ orders.add(order);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 5. 批量插入订单
|
|
|
+ boolean insertResult = this.saveBatch(orders);
|
|
|
+ if (!insertResult) {
|
|
|
+ log.warn("批量创建购物卡订单失败,购物卡ID: {},下单人ID: {},总数量: {}", card.getId(), wxLoginUser.getId(), quantity);
|
|
|
+ throw new ServiceException("批量创建订单失败");
|
|
|
+ }
|
|
|
+
|
|
|
+ log.info("批量创建购物卡订单成功,购物卡ID: {},下单人ID: {},总数量: {}", card.getId(), wxLoginUser.getId(), quantity);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 创建单个订单
|
|
|
+ */
|
|
|
+ private GiftCardOrder createSingleOrder(GiftCard card, WxLoginUser wxLoginUser, TJs merchant, int currentSeq, int totalQuantity) {
|
|
|
+ GiftCardOrder order = new GiftCardOrder();
|
|
|
+
|
|
|
+ // 生成唯一订单号
|
|
|
+ String orderNo = generateUniqueOrderNo(currentSeq);
|
|
|
+ order.setOrderNo(orderNo);
|
|
|
+
|
|
|
+ // 设置购物卡信息
|
|
|
+ setGiftCardInfo(order, card);
|
|
|
+
|
|
|
+ // 设置用户信息
|
|
|
+ setUserInfo(order, wxLoginUser);
|
|
|
+
|
|
|
+ // 设置商户信息
|
|
|
+ setMerchantInfo(order, merchant);
|
|
|
+
|
|
|
+ // 计算金额(每张卡的金额)
|
|
|
+ calculateAmountPerCard(order, card);
|
|
|
+
|
|
|
+ // 设置订单状态和时间
|
|
|
+ order.setStatus(1); // 已支付
|
|
|
+ order.setCreateTime(DateUtils.getNowDate());
|
|
|
+ order.setUpdateTime(order.getCreateTime());
|
|
|
+
|
|
|
+ return order;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 生成唯一订单号(带序列号)
|
|
|
+ */
|
|
|
+ private String generateUniqueOrderNo(int sequence) {
|
|
|
+ long timestamp = System.currentTimeMillis();
|
|
|
+ String randomNum = RandomUtil.randomNumbers(6);
|
|
|
+ // 可以加入序列号信息,便于区分同批订单中的不同卡
|
|
|
+ return "GC" + timestamp + randomNum + String.format("%02d", sequence); // 序列号补零
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 设置购物卡信息
|
|
|
+ */
|
|
|
+ private void setGiftCardInfo(GiftCardOrder order, GiftCard card) {
|
|
|
+ order.setGiftCardId(card.getId());
|
|
|
+ order.setGiftCardName(card.getName());
|
|
|
+ order.setGiftCardAmount(card.getAmount());
|
|
|
+ order.setCommissionRate(card.getCommissionRate());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 设置用户信息
|
|
|
+ */
|
|
|
+ private void setUserInfo(GiftCardOrder order, WxLoginUser wxLoginUser) {
|
|
|
+ order.setUserId(wxLoginUser.getId());
|
|
|
+ order.setUserName(wxLoginUser.getUsername());
|
|
|
+ order.setUserPhone(wxLoginUser.getCPhone());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 设置商户信息
|
|
|
+ */
|
|
|
+ private void setMerchantInfo(GiftCardOrder order, TJs merchant) {
|
|
|
+ if (ObjectUtil.isNotNull(merchant)) {
|
|
|
+ order.setMerchantId(merchant.getId());
|
|
|
+ order.setMerchantName(merchant.getcName());
|
|
|
+ order.setMerchantNickName(merchant.getcNickName());
|
|
|
+ // order.setMerchantAccount(merchant.getAccount());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 计算单张卡的金额(每张卡的金额相同)
|
|
|
+ */
|
|
|
+ private void calculateAmountPerCard(GiftCardOrder order, GiftCard card) {
|
|
|
+ // 单张卡的支付金额 = 购物卡面额
|
|
|
+ BigDecimal payAmount = card.getAmount().setScale(2, RoundingMode.HALF_UP);
|
|
|
+ order.setPayAmount(payAmount);
|
|
|
+
|
|
|
+ // 单张卡的佣金金额 = 面额 * 佣金率 / 100
|
|
|
+ BigDecimal commissionAmount = payAmount
|
|
|
+ .multiply(card.getCommissionRate())
|
|
|
+ .divide(new BigDecimal(100), 2, RoundingMode.HALF_UP);
|
|
|
+ order.setCommissionAmount(commissionAmount);
|
|
|
+ }
|
|
|
+}
|