1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
|
@Override @Transactional public Map<String, Object> createWxPayOrder(Integer customerId, Integer goodsId, Integer number) { Boolean isLegal = orderDao.checkCustomerLegality(customerId); if (isLegal == null || !isLegal) { log.warn("用户[{}]创建订单受限:当天未付款订单过多或退款订单过多", customerId); return null; } HashMap<String, Object> goods = goodsDao.selectGoodsWithRule(goodsId); if (goods == null) { throw new HisException("商品不存在"); }
BigDecimal currentPrice = new BigDecimal(goods.get("currentPrice").toString()); BigDecimal finalPrice = currentPrice; BigDecimal totalAmount = currentPrice.multiply(new BigDecimal(number)); Object ruleContent = goods.get("rule"); if (ruleContent != null && StringUtils.hasText(ruleContent.toString())) { try { ExpressRunner runner = new ExpressRunner(); DefaultContext<String, Object> context = new DefaultContext<>(); context.put("number", number); context.put("price", currentPrice.doubleValue()); context.put("totalPrice", totalAmount.doubleValue()); Object result = runner.execute(ruleContent.toString(), context, null, true, false); if (result != null) { double discountedTotalPrice = Double.parseDouble(result.toString()); totalAmount = BigDecimal.valueOf(discountedTotalPrice).setScale(2, RoundingMode.HALF_UP); finalPrice = totalAmount.divide(new BigDecimal(number), 2, RoundingMode.HALF_UP); log.info("商品[{}]应用折扣规则,原单价{},折后单价{},原总价{},折后总价{}", goodsId, currentPrice, finalPrice, currentPrice.multiply(new BigDecimal(number)), totalAmount); } } catch (Exception e) { log.error("计算折扣规则失败:{},将使用原价", e.getMessage(), e); totalAmount = currentPrice.multiply(new BigDecimal(number)); } } String outTradeNo = generateOrderNo(); String snapshotId; String md5 = (String) goods.get("md5"); if (md5 != null && !md5.isEmpty()) { String existingSnapshotId = goodsSnapshotDao.existsByMd5(md5); if (existingSnapshotId == null) { GoodsSnapshotEntity snapshotEntity = new GoodsSnapshotEntity(); snapshotEntity.setId(goodsId); snapshotEntity.setCode((String) goods.get("code")); snapshotEntity.setTitle((String) goods.get("title")); snapshotEntity.setDescription((String) goods.get("description")); if (goods.containsKey("checkup_1")) { snapshotEntity.setCheckup_1((String) goods.get("checkup_1")); } if (goods.containsKey("checkup_2")) { snapshotEntity.setCheckup_2((String) goods.get("checkup_2")); } if (goods.containsKey("checkup_3")) { snapshotEntity.setCheckup_3((String) goods.get("checkup_3")); } if (goods.containsKey("checkup_4")) { snapshotEntity.setCheckup_4((String) goods.get("checkup_4")); } if (goods.containsKey("checkup")) { snapshotEntity.setCheckup((String) goods.get("checkup_5")); } snapshotEntity.setImage((String) goods.get("image")); snapshotEntity.setInitialPrice(new BigDecimal(goods.get("initialPrice").toString())); snapshotEntity.setCurrentPrice(currentPrice); snapshotEntity.setType((String) goods.get("type")); if (goods.containsKey("tag")) { snapshotEntity.setTag((String) goods.get("tag")); } snapshotEntity.setRuleName((String) goods.get("ruleName")); snapshotEntity.setRule((String) goods.get("rule")); snapshotEntity.setMd5(md5); snapshotId = goodsSnapshotDao.insert(snapshotEntity); log.info("创建商品快照成功,商品ID={},MD5={},快照ID={}", goodsId, md5, snapshotId); } else { snapshotId = existingSnapshotId; log.info("使用已存在的商品快照,商品ID={},MD5={},快照ID={}", goodsId, md5, snapshotId); } } else { snapshotId = UUID.randomUUID().toString(); log.warn("商品没有MD5值,使用随机UUID作为快照ID:{}", snapshotId); } OrderEntity order = new OrderEntity(); order.setCustomerId(customerId); order.setGoodsId(goodsId); order.setSnapshotId(snapshotId); order.setGoodsTitle((String) goods.get("title")); order.setGoodsPrice(finalPrice); order.setNumber(number); order.setAmount(totalAmount); order.setGoodsImage((String) goods.get("image")); order.setGoodsDescription((String) goods.get("description")); order.setOutTradeNo(outTradeNo); order.setStatus(1); LocalDateTime now = LocalDateTime.now(); order.setCreateDate(now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"))); order.setCreateTime(now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))); orderDao.insert(order); Map<String, Object> logInfo = new HashMap<>(); logInfo.put("customerId", customerId); logInfo.put("goodsId", goodsId); logInfo.put("goodsTitle", goods.get("title")); logInfo.put("number", number); logInfo.put("amount", totalAmount.toString()); orderLogger.logOperation("CREATE_ORDER", outTradeNo, "wechat", logInfo); try { Map<String, Object> payResult = paymentService.unifiedOrder(order, now); QrConfig qrConfig = new QrConfig(); qrConfig.setWidth(230); qrConfig.setHeight(230); qrConfig.setMargin(2); String codeUrl = (String) payResult.get("codeUrl"); String qrCodeBase64 = QrCodeUtil.generateAsBase64(codeUrl, qrConfig, "jpg"); payResult.put(codeUrl, qrCodeBase64); return payResult; } catch (PayException e) { orderDao.updateOrderStatus(outTradeNo, 0, null); log.error("支付创建失败,订单已标记为失败状态,订单号:{}", outTradeNo); throw e; } }
|