OrderService.java 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  1. package com.izouma.nineth.service;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.alibaba.fastjson.serializer.SerializerFeature;
  5. import com.alipay.api.AlipayClient;
  6. import com.alipay.api.request.AlipayTradeWapPayRequest;
  7. import com.github.binarywang.wxpay.bean.order.WxPayMpOrderResult;
  8. import com.github.binarywang.wxpay.bean.order.WxPayMwebOrderResult;
  9. import com.github.binarywang.wxpay.bean.request.WxPayRefundRequest;
  10. import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderRequest;
  11. import com.github.binarywang.wxpay.constant.WxPayConstants;
  12. import com.github.binarywang.wxpay.exception.WxPayException;
  13. import com.github.binarywang.wxpay.service.WxPayService;
  14. import com.huifu.adapay.core.exception.BaseAdaPayException;
  15. import com.huifu.adapay.model.AdapayCommon;
  16. import com.huifu.adapay.model.Payment;
  17. import com.huifu.adapay.model.Refund;
  18. import com.izouma.nineth.config.*;
  19. import com.izouma.nineth.domain.Collection;
  20. import com.izouma.nineth.domain.*;
  21. import com.izouma.nineth.dto.PageQuery;
  22. import com.izouma.nineth.enums.*;
  23. import com.izouma.nineth.event.CreateAssetEvent;
  24. import com.izouma.nineth.event.CreateOrderEvent;
  25. import com.izouma.nineth.event.OrderNotifyEvent;
  26. import com.izouma.nineth.event.TransferAssetEvent;
  27. import com.izouma.nineth.exception.BusinessException;
  28. import com.izouma.nineth.repo.*;
  29. import com.izouma.nineth.security.Authority;
  30. import com.izouma.nineth.utils.JpaUtils;
  31. import com.izouma.nineth.utils.SecurityUtils;
  32. import com.izouma.nineth.utils.SnowflakeIdWorker;
  33. import lombok.AllArgsConstructor;
  34. import lombok.extern.slf4j.Slf4j;
  35. import org.apache.commons.codec.EncoderException;
  36. import org.apache.commons.codec.net.URLCodec;
  37. import org.apache.commons.collections.CollectionUtils;
  38. import org.apache.commons.collections.MapUtils;
  39. import org.apache.commons.lang3.StringUtils;
  40. import org.apache.rocketmq.client.producer.SendResult;
  41. import org.apache.rocketmq.spring.core.RocketMQTemplate;
  42. import org.springframework.cache.annotation.Cacheable;
  43. import org.springframework.context.event.EventListener;
  44. import org.springframework.core.env.Environment;
  45. import org.springframework.data.domain.Page;
  46. import org.springframework.data.redis.core.BoundSetOperations;
  47. import org.springframework.data.redis.core.BoundValueOperations;
  48. import org.springframework.data.redis.core.RedisTemplate;
  49. import org.springframework.scheduling.annotation.Scheduled;
  50. import org.springframework.stereotype.Service;
  51. import org.springframework.ui.Model;
  52. import javax.transaction.Transactional;
  53. import java.math.BigDecimal;
  54. import java.math.RoundingMode;
  55. import java.time.LocalDateTime;
  56. import java.time.format.DateTimeFormatter;
  57. import java.util.*;
  58. import java.util.concurrent.TimeUnit;
  59. @Service
  60. @AllArgsConstructor
  61. @Slf4j
  62. public class OrderService {
  63. private OrderRepo orderRepo;
  64. private CollectionRepo collectionRepo;
  65. private UserAddressRepo userAddressRepo;
  66. private UserRepo userRepo;
  67. private Environment env;
  68. private AlipayClient alipayClient;
  69. private AlipayProperties alipayProperties;
  70. private WxPayService wxPayService;
  71. private WxPayProperties wxPayProperties;
  72. private AssetService assetService;
  73. private SysConfigService sysConfigService;
  74. private BlindBoxItemRepo blindBoxItemRepo;
  75. private AssetRepo assetRepo;
  76. private UserCouponRepo userCouponRepo;
  77. private CollectionService collectionService;
  78. private RedisTemplate<String, Object> redisTemplate;
  79. private CommissionRecordRepo commissionRecordRepo;
  80. private AdapayProperties adapayProperties;
  81. private GeneralProperties generalProperties;
  82. private SnowflakeIdWorker snowflakeIdWorker;
  83. private RocketMQTemplate rocketMQTemplate;
  84. private ErrorOrderRepo errorOrderRepo;
  85. public Page<Order> all(PageQuery pageQuery) {
  86. return orderRepo.findAll(JpaUtils.toSpecification(pageQuery, Order.class), JpaUtils.toPageRequest(pageQuery));
  87. }
  88. public String mqCreate(Long userId, Long collectionId, int qty, Long addressId, Long userCouponId, Long invitor) {
  89. Long id = snowflakeIdWorker.nextId();
  90. SendResult result = rocketMQTemplate.syncSend(generalProperties.getCreateOrderTopic(),
  91. new CreateOrderEvent(id, userId, collectionId, qty, addressId, userCouponId, invitor), 100000);
  92. log.info("发送订单到队列: {}, result={}", id, result);
  93. return String.valueOf(id);
  94. }
  95. @Transactional
  96. public Order create(Long userId, Long collectionId, int qty, Long addressId, Long userCouponId, Long invitor, Long id) {
  97. qty = 1;
  98. long t = System.currentTimeMillis();
  99. int stock = Optional.ofNullable(collectionService.decreaseStock(collectionId, qty))
  100. .map(Math::toIntExact)
  101. .orElseThrow(new BusinessException("很遗憾,藏品已售罄"));
  102. try {
  103. if (stock < 0) {
  104. throw new BusinessException("很遗憾,藏品已售罄");
  105. }
  106. User user = userRepo.findByIdAndDelFalse(userId).orElseThrow(new BusinessException("用户不存在"));
  107. Collection collection = collectionRepo.findById(collectionId).orElseThrow(new BusinessException("藏品不存在"));
  108. User minter = userRepo.findById(collection.getMinterId()).orElseThrow(new BusinessException("铸造者不存在"));
  109. UserCoupon coupon = null;
  110. if (userCouponId != null) {
  111. coupon = userCouponRepo.findById(userCouponId).orElseThrow(new BusinessException("兑换券不存在"));
  112. if (coupon.isUsed()) {
  113. throw new BusinessException("该兑换券已使用");
  114. }
  115. if (coupon.isLimited() && !coupon.getCollectionIds().contains(collectionId)) {
  116. throw new BusinessException("该兑换券不可用");
  117. }
  118. }
  119. if (collection.isScheduleSale()) {
  120. if (collection.getStartTime().isAfter(LocalDateTime.now())) {
  121. throw new BusinessException("当前还未开售");
  122. }
  123. }
  124. if (!collection.isOnShelf()) {
  125. throw new BusinessException("藏品已下架");
  126. }
  127. if (qty > collection.getStock()) {
  128. throw new BusinessException("库存不足");
  129. }
  130. if (!collection.isSalable()) {
  131. throw new BusinessException("该藏品当前不可购买");
  132. }
  133. if (collection.getAssetId() != null) {
  134. Asset asset = assetRepo.findById(collection.getAssetId()).orElseThrow(new BusinessException("藏品不存在"));
  135. if (asset.getStatus() != AssetStatus.NORMAL) {
  136. throw new BusinessException("藏品已下架");
  137. }
  138. }
  139. UserAddress userAddress = null;
  140. if (addressId != null) {
  141. userAddress = userAddressRepo.findById(addressId).orElseThrow(new BusinessException("地址信息不存在"));
  142. }
  143. BigDecimal gasFee = sysConfigService.getBigDecimal("gas_fee");
  144. Order order = Order.builder()
  145. .id(Optional.ofNullable(id).orElse(snowflakeIdWorker.nextId()))
  146. .userId(userId)
  147. .collectionId(collectionId)
  148. .name(collection.getName())
  149. .pic(collection.getPic())
  150. .detail(collection.getDetail())
  151. .properties(collection.getProperties())
  152. .category(collection.getCategory())
  153. .canResale(collection.isCanResale())
  154. .royalties(collection.getRoyalties())
  155. .serviceCharge(collection.getServiceCharge())
  156. .type(collection.getType())
  157. .source(collection.getSource())
  158. .minterId(collection.getMinterId())
  159. .minter(minter.getNickname())
  160. .minterAvatar(minter.getAvatar())
  161. .qty(qty)
  162. .price(collection.getPrice())
  163. .gasPrice(gasFee)
  164. .totalPrice(collection.getPrice().multiply(BigDecimal.valueOf(qty)).add(gasFee))
  165. .contactName(Optional.ofNullable(userAddress).map(UserAddress::getName).orElse(null))
  166. .contactPhone(Optional.ofNullable(userAddress).map(UserAddress::getPhone).orElse(null))
  167. .address(Optional.ofNullable(userAddress).map(u ->
  168. u.getProvinceId() + " " + u.getCityId() + " " + u.getDistrictId() + " " + u.getAddress())
  169. .orElse(null))
  170. .status(OrderStatus.NOT_PAID)
  171. .assetId(collection.getAssetId())
  172. .couponId(userCouponId)
  173. .invitor(invitor)
  174. .projectId(collection.getProjectId())
  175. .build();
  176. if (coupon != null) {
  177. coupon.setUsed(true);
  178. coupon.setUseTime(LocalDateTime.now());
  179. if (coupon.isNeedGas()) {
  180. order.setTotalPrice(order.getGasPrice());
  181. } else {
  182. order.setTotalPrice(BigDecimal.ZERO);
  183. }
  184. userCouponRepo.save(coupon);
  185. }
  186. if (collection.getSource() == CollectionSource.TRANSFER) {
  187. Asset asset = assetRepo.findById(collection.getAssetId()).orElseThrow(new BusinessException("资产不存在"));
  188. asset.setStatus(AssetStatus.TRADING);
  189. assetRepo.save(asset);
  190. collectionRepo.setOnShelf(collection.getId(), false);
  191. }
  192. order = orderRepo.save(order);
  193. if (order.getTotalPrice().equals(BigDecimal.ZERO)) {
  194. notifyOrder(order.getId(), PayMethod.WEIXIN, null);
  195. }
  196. rocketMQTemplate.syncSend(generalProperties.getUpdateStockTopic(), collectionId, 10000);
  197. log.info("订单创建完成, id={}, {}ms", order.getId(), System.currentTimeMillis() - t);
  198. return order;
  199. } catch (Exception e) {
  200. collectionService.increaseStock(collectionId, qty);
  201. throw e;
  202. }
  203. }
  204. public void payOrderAlipay(Long id, Model model) {
  205. try {
  206. Order order = orderRepo.findByIdAndDelFalse(id).orElseThrow(new BusinessException("订单不存在"));
  207. if (order.getStatus() != OrderStatus.NOT_PAID) {
  208. throw new BusinessException("订单状态错误");
  209. }
  210. JSONObject bizContent = new JSONObject();
  211. bizContent.put("notifyUrl", alipayProperties.getNotifyUrl());
  212. bizContent.put("returnUrl", alipayProperties.getReturnUrl());
  213. bizContent.put("out_trade_no", String.valueOf(snowflakeIdWorker.nextId()));
  214. bizContent.put("total_amount", order.getTotalPrice().stripTrailingZeros().toPlainString());
  215. bizContent.put("disable_pay_channels", "pcredit,creditCard");
  216. if (Arrays.stream(env.getActiveProfiles()).noneMatch(s -> s.equals("prod"))) {
  217. // 测试环境设为1分
  218. bizContent.put("total_amount", "0.01");
  219. }
  220. bizContent.put("subject", order.getName());
  221. bizContent.put("product_code", "QUICK_WAP_PAY");
  222. JSONObject body = new JSONObject();
  223. body.put("action", "payOrder");
  224. body.put("userId", order.getUserId());
  225. body.put("orderId", order.getId());
  226. bizContent.put("body", body.toJSONString());
  227. AlipayTradeWapPayRequest alipayRequest = new AlipayTradeWapPayRequest();
  228. alipayRequest.setReturnUrl(alipayProperties.getReturnUrl());
  229. alipayRequest.setNotifyUrl(alipayProperties.getNotifyUrl());
  230. alipayRequest.setBizContent(JSON.toJSONString(bizContent));
  231. String form = alipayClient.pageExecute(alipayRequest).getBody();
  232. model.addAttribute("form", form);
  233. } catch (BusinessException err) {
  234. model.addAttribute("errMsg", err.getError());
  235. } catch (Exception e) {
  236. model.addAttribute("errMsg", e.getMessage());
  237. }
  238. }
  239. public Object payOrderWeixin(Long id, String tradeType, String openId) throws WxPayException, EncoderException {
  240. Order order = orderRepo.findByIdAndDelFalse(id).orElseThrow(new BusinessException("订单不存在"));
  241. if (order.getStatus() != OrderStatus.NOT_PAID) {
  242. throw new BusinessException("订单状态错误");
  243. }
  244. WxPayUnifiedOrderRequest request = new WxPayUnifiedOrderRequest();
  245. request.setBody(order.getName());
  246. request.setOutTradeNo(String.valueOf(snowflakeIdWorker.nextId()));
  247. request.setTotalFee(order.getTotalPrice().multiply(BigDecimal.valueOf(100)).intValue());
  248. if (Arrays.stream(env.getActiveProfiles()).noneMatch(s -> s.equals("prod"))) {
  249. // 测试环境设为1分
  250. // request.setTotalFee(1);
  251. }
  252. request.setSpbillCreateIp("180.102.110.170");
  253. request.setNotifyUrl(wxPayProperties.getNotifyUrl());
  254. request.setTradeType(tradeType);
  255. request.setOpenid(openId);
  256. request.setSignType("MD5");
  257. JSONObject body = new JSONObject();
  258. body.put("action", "payOrder");
  259. body.put("userId", order.getUserId());
  260. body.put("orderId", order.getId());
  261. request.setAttach(body.toJSONString());
  262. if (WxPayConstants.TradeType.MWEB.equals(tradeType)) {
  263. WxPayMwebOrderResult result = wxPayService.createOrder(request);
  264. return result.getMwebUrl() + "&redirect_url=" + new URLCodec().encode(wxPayProperties.getReturnUrl());
  265. } else if (WxPayConstants.TradeType.JSAPI.equals(tradeType)) {
  266. return wxPayService.<WxPayMpOrderResult>createOrder(request);
  267. }
  268. throw new BusinessException("不支持此付款方式");
  269. }
  270. @Cacheable(value = "adapay", key = "#id+'_'+#payChannel")
  271. public Object payAdapay(Long id, String payChannel, String openId) throws BaseAdaPayException {
  272. List<String> aliChannels = Arrays.asList("alipay", "alipay_qr", "alipay_wap");
  273. List<String> wxChannels = Arrays.asList("wx_pub", "wx_lite");
  274. if (!aliChannels.contains(payChannel) && !wxChannels.contains(payChannel)) {
  275. throw new BusinessException("不支持此渠道");
  276. }
  277. Order order = orderRepo.findByIdAndDelFalse(id).orElseThrow(new BusinessException("订单不存在"));
  278. Collection collection = collectionRepo.findById(order.getCollectionId())
  279. .orElseThrow(new BusinessException("藏品不存在"));
  280. User invitor = null;
  281. if (order.getInvitor() != null) {
  282. invitor = userRepo.findById(order.getInvitor()).orElse(null);
  283. }
  284. if (order.getStatus() != OrderStatus.NOT_PAID) {
  285. throw new BusinessException("订单状态错误");
  286. }
  287. Map<String, Object> paymentParams = new HashMap<>();
  288. paymentParams.put("order_no", String.valueOf(snowflakeIdWorker.nextId()));
  289. paymentParams.put("pay_amt", order.getTotalPrice().setScale(2, RoundingMode.HALF_UP).toPlainString());
  290. paymentParams.put("app_id", adapayProperties.getAppId());
  291. paymentParams.put("pay_channel", payChannel);
  292. paymentParams.put("goods_title", collection.getName());
  293. paymentParams.put("goods_desc", collection.getName());
  294. paymentParams.put("time_expire", DateTimeFormatter.ofPattern("yyyyMMddHHmmss")
  295. .format(LocalDateTime.now().plusMinutes(3)));
  296. paymentParams.put("notify_url", adapayProperties.getNotifyUrl() + "/order/" + adapayProperties.getMerchant() + "/" + order.getId());
  297. List<Map<String, Object>> divMembers = new ArrayList<>();
  298. BigDecimal totalAmount = order.getTotalPrice().subtract(order.getGasPrice());
  299. BigDecimal restAmount = order.getTotalPrice().multiply(BigDecimal.valueOf(1));
  300. if (collection.getSource().equals(CollectionSource.TRANSFER)) {
  301. Asset asset = assetRepo.findById(collection.getAssetId()).orElseThrow(new BusinessException("无记录"));
  302. User owner = userRepo.findById(asset.getUserId()).orElseThrow(new BusinessException("拥有者用户不存在"));
  303. if (collection.getServiceCharge() + collection.getRoyalties() > 0) {
  304. // 扣除手续费、服务费、GAS费
  305. restAmount = divMoney(totalAmount, restAmount, divMembers, owner.getMemberId(),
  306. 100 - (collection.getServiceCharge() + collection.getRoyalties()), false);
  307. }
  308. restAmount = divMoney(restAmount, divMembers, "0", restAmount, true);
  309. } else {
  310. if (invitor != null && invitor.getShareRatio() != null
  311. && invitor.getShareRatio().compareTo(BigDecimal.ZERO) > 0) {
  312. restAmount = divMoney(totalAmount, restAmount, divMembers, invitor.getMemberId(),
  313. invitor.getShareRatio().intValue(), false);
  314. }
  315. restAmount = divMoney(restAmount, divMembers, "0", restAmount, true);
  316. }
  317. if (restAmount.compareTo(BigDecimal.ZERO) != 0) {
  318. log.error("分账出错 {}", JSON.toJSONString(divMembers, SerializerFeature.PrettyFormat));
  319. throw new BusinessException("分账出错");
  320. }
  321. if (divMembers.size() > 1) {
  322. paymentParams.put("div_members", divMembers);
  323. }
  324. Map<String, Object> expend = new HashMap<>();
  325. paymentParams.put("expend", expend);
  326. if ("wx_pub".equals(payChannel)) {
  327. if (StringUtils.isBlank(openId)) {
  328. throw new BusinessException("缺少openId");
  329. }
  330. expend.put("open_id", openId);
  331. expend.put("limit_pay", "1");
  332. }
  333. Map<String, Object> response;
  334. if ("wx_lite".equals(payChannel)) {
  335. paymentParams.put("adapay_func_code", "wxpay.createOrder");
  336. paymentParams.put("callback_url", generalProperties.getHost() + "/9th/orders");
  337. response = AdapayCommon.requestAdapayUits(paymentParams);
  338. log.info("createOrderResponse {}", JSON.toJSONString(response, SerializerFeature.PrettyFormat));
  339. } else {
  340. response = Payment.create(paymentParams);
  341. log.info("createOrderResponse {}", JSON.toJSONString(response, SerializerFeature.PrettyFormat));
  342. AdapayService.checkSuccess(response);
  343. // 保存adapay的订单id,用于后续取消订单时的查询
  344. BoundSetOperations<String, Object> ops = redisTemplate.boundSetOps(RedisKeys.PAY_RECORD + order.getId());
  345. ops.add(MapUtils.getString(response, "id"));
  346. ops.expire(7, TimeUnit.DAYS);
  347. }
  348. switch (payChannel) {
  349. case "alipay_wap":
  350. case "alipay":
  351. return MapUtils.getString(MapUtils.getMap(response, "expend"), "pay_info");
  352. case "alipay_qr":
  353. return MapUtils.getString(MapUtils.getMap(response, "expend"), "qrcode_url");
  354. case "wx_pub":
  355. JSONObject payParams = JSON.parseObject(MapUtils.getString(MapUtils.getMap(response, "expend"), "pay_info"));
  356. payParams.put("timestamp", payParams.get("timeStamp"));
  357. payParams.remove("timeStamp");
  358. return payParams;
  359. default:
  360. return MapUtils.getMap(response, "expend");
  361. }
  362. }
  363. public static BigDecimal divMoney(BigDecimal totalAmount, BigDecimal restAmount, List<Map<String, Object>> divMembers,
  364. String memberId, int ratio, boolean feeFlag) {
  365. if (ratio == -1 || (ratio > 0 && ratio < 100)) {
  366. BigDecimal divAmount = ratio == -1 ? restAmount :
  367. totalAmount.multiply(BigDecimal.valueOf(ratio))
  368. .divide(BigDecimal.valueOf(100), 2, RoundingMode.HALF_UP);
  369. Map<String, Object> divMem = new HashMap<>();
  370. divMem.put("member_id", memberId);
  371. divMem.put("amount", divAmount.toPlainString());
  372. divMem.put("fee_flag", feeFlag ? "Y" : "N");
  373. divMembers.add(divMem);
  374. return restAmount.subtract(divAmount);
  375. } else {
  376. throw new BusinessException("分账比例错误");
  377. }
  378. }
  379. public static BigDecimal divMoney(BigDecimal restAmount, List<Map<String, Object>> divMembers,
  380. String memberId, BigDecimal divAmount, boolean feeFlag) {
  381. if (divAmount.compareTo(BigDecimal.ZERO) > 0) {
  382. Map<String, Object> divMem = new HashMap<>();
  383. divMem.put("member_id", memberId);
  384. divMem.put("amount", divAmount.toPlainString());
  385. divMem.put("fee_flag", feeFlag ? "Y" : "N");
  386. divMembers.add(divMem);
  387. }
  388. return restAmount.subtract(divAmount);
  389. }
  390. public void notifyOrder(Long orderId, PayMethod payMethod, String transactionId) {
  391. log.info("订单回调 orderId: {}, payMethod: {}, transactionId: {}", orderId, payMethod, transactionId);
  392. // 取消订单与订单回调不能同时进行,需要抢锁
  393. if (!getOrderLock(orderId)) {
  394. log.info("订单回调失败 orderId: {} redis锁定, 重新发送到队列", orderId);
  395. rocketMQTemplate.syncSend(generalProperties.getOrderNotifyTopic(),
  396. new OrderNotifyEvent(orderId, payMethod, transactionId, System.currentTimeMillis()));
  397. return;
  398. }
  399. try {
  400. Order order = orderRepo.findById(orderId).orElseThrow(new BusinessException("订单不存在"));
  401. Collection collection = collectionRepo.findById(order.getCollectionId())
  402. .orElseThrow(new BusinessException("藏品不存在"));
  403. User user = userRepo.findById(order.getUserId()).orElseThrow(new BusinessException("用户不存在"));
  404. if (order.getStatus() == OrderStatus.NOT_PAID) {
  405. order.setStatus(OrderStatus.PROCESSING);
  406. order.setPayTime(LocalDateTime.now());
  407. order.setTransactionId(transactionId);
  408. order.setPayMethod(payMethod);
  409. if (order.getType() == CollectionType.BLIND_BOX) {
  410. log.info("开始盲盒抽卡 orderId: {}, collectionId: {}", orderId, collection.getId());
  411. BlindBoxItem winItem = null;
  412. try {
  413. winItem = collectionService.draw(collection.getId());
  414. } catch (BusinessException ignored) {
  415. }
  416. if (winItem == null) {
  417. log.info("抽卡失败退款 orderId: {}", orderId);
  418. order.setStatus(OrderStatus.CANCELLED);
  419. order.setCancelTime(LocalDateTime.now());
  420. Map<String, Object> refundParams = new HashMap<>();
  421. refundParams.put("refund_amt", order.getTotalPrice().setScale(2, RoundingMode.HALF_UP)
  422. .toPlainString());
  423. refundParams.put("refund_order_no", String.valueOf(snowflakeIdWorker.nextId()));
  424. try {
  425. Map<String, Object> response = Refund.create(transactionId, refundParams);
  426. } catch (BaseAdaPayException e) {
  427. e.printStackTrace();
  428. }
  429. orderRepo.save(order);
  430. throw new BusinessException("抽卡失败, 已退款 " + orderId);
  431. }
  432. log.info("抽卡成功 orderId: {}, collectionId: {}, winCollectionId: {}", orderId, collection.getId(), winItem.getCollectionId());
  433. order.setWinCollectionId(winItem.getCollectionId());
  434. orderRepo.save(order);
  435. assetService.createAsset(winItem, user, order.getId(), order.getPrice(), "出售",
  436. winItem.getTotal() > 1 ? collectionService.getNextNumber(winItem.getCollectionId()) : null);
  437. } else {
  438. if (collection.getSource() == CollectionSource.TRANSFER) {
  439. orderRepo.save(order);
  440. Asset asset = assetRepo.findById(collection.getAssetId()).orElse(null);
  441. assetService.transfer(asset, order.getPrice(), user, "转让", order.getId());
  442. List<Long> collectionIds = collectionRepo.findAllByAssetId(collection.getAssetId());
  443. log.info("删除collection {}", collectionIds);
  444. if (CollectionUtils.isNotEmpty(collectionIds)) {
  445. collectionRepo.deleteAllByIdIn(collectionIds);
  446. } else {
  447. collectionRepo.delete(collection);
  448. }
  449. } else {
  450. orderRepo.save(order);
  451. assetService.createAsset(collection, user, order.getId(), order.getPrice(), "出售",
  452. collection.getTotal() > 1 ? collectionService.getNextNumber(order.getCollectionId()) : null);
  453. }
  454. }
  455. commission(order);
  456. if (collection.getAssetId() == null) {
  457. collectionService.increaseSale(order.getCollectionId(), order.getQty());
  458. }
  459. } else {
  460. throw new BusinessException("状态错误 " + order.getStatus());
  461. }
  462. } catch (Exception e) {
  463. ErrorOrder errorOrder = ErrorOrder.builder()
  464. .orderId(orderId)
  465. .transactionId(transactionId)
  466. .payMethod(payMethod)
  467. .build();
  468. if (e instanceof BusinessException) {
  469. log.error("订单回调出错 orderId: {} {}", orderId, e.getMessage());
  470. } else {
  471. log.error("订单回调出错 orderId: " + orderId, e);
  472. }
  473. errorOrder.setErrorMessage(e.getMessage());
  474. errorOrderRepo.save(errorOrder);
  475. }
  476. releaseOrderLock(orderId);
  477. }
  478. @EventListener
  479. public void onCreateAsset(CreateAssetEvent event) {
  480. Asset asset = event.getAsset();
  481. if (asset.getOrderId() != null) {
  482. Order order = orderRepo.findById(asset.getOrderId()).orElseThrow(new BusinessException("订单不存在"));
  483. if (event.isSuccess()) {
  484. order.setTxHash(asset.getTxHash());
  485. order.setGasUsed(asset.getGasUsed());
  486. order.setBlockNumber(asset.getBlockNumber());
  487. order.setStatus(OrderStatus.FINISH);
  488. orderRepo.save(order);
  489. } else {
  490. log.error("创建asset失败");
  491. }
  492. }
  493. }
  494. @EventListener
  495. public void onTransferAsset(TransferAssetEvent event) {
  496. log.info("藏品转让成功 assetId={}", event.getAsset().getId());
  497. Asset asset = event.getAsset();
  498. Order order = orderRepo.findById(asset.getOrderId()).orElseThrow(new BusinessException("订单不存在"));
  499. if (event.isSuccess()) {
  500. order.setTxHash(asset.getTxHash());
  501. order.setGasUsed(asset.getGasUsed());
  502. order.setBlockNumber(asset.getBlockNumber());
  503. order.setStatus(OrderStatus.FINISH);
  504. orderRepo.save(order);
  505. } else {
  506. log.error("创建asset失败");
  507. }
  508. }
  509. public void cancel(Long id) {
  510. Order order = orderRepo.findById(id).orElseThrow(new BusinessException("订单不存在"));
  511. cancel(order);
  512. }
  513. public void cancel(Order order) {
  514. if (!getOrderLock(order.getId())) {
  515. log.error("订单取消失败 {}, redis锁了", order.getId());
  516. return;
  517. }
  518. try {
  519. if (order.getStatus() != OrderStatus.NOT_PAID) {
  520. throw new BusinessException("已支付订单无法取消");
  521. }
  522. Collection collection = collectionRepo.findById(order.getCollectionId())
  523. .orElseThrow(new BusinessException("藏品不存在"));
  524. CollectionSource source = Optional.ofNullable(order.getSource()).orElseGet(() ->
  525. collectionRepo.findById(order.getCollectionId()).map(Collection::getSource).orElse(null));
  526. if (source == CollectionSource.TRANSFER) {
  527. Asset asset = assetRepo.findById(order.getAssetId()).orElse(null);
  528. if (asset != null) {
  529. log.info("set normal cancelOrder {}", order.getId());
  530. asset.setStatus(AssetStatus.NORMAL);
  531. assetRepo.save(asset);
  532. }
  533. collectionRepo.setOnShelf(order.getCollectionId(), true);
  534. }
  535. collectionService.increaseStock(order.getCollectionId(), order.getQty());
  536. order.setStatus(OrderStatus.CANCELLED);
  537. order.setCancelTime(LocalDateTime.now());
  538. orderRepo.save(order);
  539. if (order.getCouponId() != null) {
  540. userCouponRepo.findById(order.getCouponId()).ifPresent(coupon -> {
  541. coupon.setUsed(false);
  542. coupon.setUseTime(null);
  543. userCouponRepo.save(coupon);
  544. });
  545. }
  546. rocketMQTemplate.syncSend(generalProperties.getUpdateStockTopic(), order.getCollectionId(), 10000);
  547. log.info("取消订单{}", order.getId());
  548. } catch (Exception e) {
  549. if (e instanceof BusinessException) {
  550. log.error(e.getMessage());
  551. } else {
  552. log.error("订单取消错误 orderId: " + order.getId(), e);
  553. }
  554. }
  555. releaseOrderLock(order.getId());
  556. }
  557. @Scheduled(fixedRate = 30000)
  558. public void batchCancel() {
  559. if (Arrays.asList(env.getActiveProfiles()).contains("dev")) {
  560. return;
  561. }
  562. List<Order> orders = orderRepo.findByStatusAndCreatedAtBeforeAndDelFalse(OrderStatus.NOT_PAID,
  563. LocalDateTime.now().minusSeconds(210));
  564. orders.forEach(o -> {
  565. try {
  566. cancel(o);
  567. } catch (Exception ignored) {
  568. }
  569. });
  570. }
  571. public void refundCancelled(Order order) {
  572. }
  573. public synchronized void addSales(Long userId) {
  574. if (userId != null) {
  575. userRepo.findById(userId).ifPresent(user -> {
  576. user.setSales(user.getSales() + 1);
  577. userRepo.save(user);
  578. });
  579. }
  580. }
  581. public void setNumber() {
  582. for (Collection collection : collectionRepo.findAll()) {
  583. if (collection.getSource() != CollectionSource.OFFICIAL) continue;
  584. collection.setCurrentNumber(0);
  585. collectionRepo.save(collection);
  586. for (Asset asset : assetRepo.findByCollectionId(collection.getId())) {
  587. if (asset.getStatus() == AssetStatus.GIFTED || asset.getStatus() == AssetStatus.TRANSFERRED) {
  588. } else {
  589. asset.setNumber(collectionService.getNextNumber(collection.getId()));
  590. assetRepo.save(asset);
  591. }
  592. }
  593. }
  594. }
  595. public void setNumberRecursive(Asset asset) {
  596. }
  597. @Scheduled(fixedRate = 120000)
  598. public void setSales() {
  599. List<User> minters = userRepo.findByAuthoritiesContains(Authority.get(AuthorityName.ROLE_MINTER));
  600. for (User minter : minters) {
  601. userRepo.setSales(minter.getId(), (int) orderRepo.countSales(minter.getId()));
  602. }
  603. }
  604. public void commission(Order order) {
  605. if (order.getInvitor() != null) {
  606. userRepo.findById(order.getInvitor()).ifPresent(user -> {
  607. BigDecimal shareRatio = user.getShareRatio();
  608. if (shareRatio != null && shareRatio.compareTo(BigDecimal.ZERO) > 0) {
  609. BigDecimal totalPrice = order.getTotalPrice().subtract(order.getGasPrice());
  610. commissionRecordRepo.save(CommissionRecord.builder()
  611. .orderId(order.getId())
  612. .totalPrice(totalPrice)
  613. .nickname(user.getNickname())
  614. .userId(user.getId())
  615. .shareRatio(user.getShareRatio())
  616. .phone(user.getPhone())
  617. .shareAmount(totalPrice.multiply(shareRatio)
  618. .divide(BigDecimal.valueOf(100), 2, RoundingMode.HALF_UP))
  619. .build());
  620. }
  621. });
  622. }
  623. }
  624. public void refund(Long id) throws WxPayException {
  625. Order order = orderRepo.findById(id).orElseThrow(new BusinessException("无记录"));
  626. if (order.getStatus() != OrderStatus.FINISH) {
  627. throw new BusinessException("订单未付款");
  628. }
  629. WxPayRefundRequest request = new WxPayRefundRequest();
  630. request.setTransactionId(order.getTransactionId());
  631. request.setTotalFee(order.getTotalPrice().multiply(BigDecimal.valueOf(100)).intValue());
  632. request.setRefundFee(order.getTotalPrice().multiply(BigDecimal.valueOf(100)).intValue());
  633. request.setOutRefundNo(String.valueOf(snowflakeIdWorker.nextId()));
  634. wxPayService.refund(request);
  635. }
  636. public Object queryCreateOrder(String id) {
  637. Object res = redisTemplate.opsForValue().get(RedisKeys.CREATE_ORDER + id);
  638. if (res != null) {
  639. if (res instanceof Map) {
  640. if (MapUtils.getBooleanValue((Map) res, "success", false)) {
  641. Order order = (Order) MapUtils.getObject((Map) res, "data");
  642. if (!SecurityUtils.getAuthenticatedUser().getId().equals(order.getUserId())) {
  643. log.error("queryCreateOrder userId错误 requestUserId={} orderUserId={}",
  644. SecurityUtils.getAuthenticatedUser().getId(), order.getUserId());
  645. return null;
  646. }
  647. }
  648. }
  649. }
  650. return res;
  651. }
  652. // 获取订单锁,有效时间1小时
  653. public boolean getOrderLock(Long orderId) {
  654. BoundValueOperations<String, Object> ops = redisTemplate.boundValueOps(RedisKeys.ORDER_LOCK + orderId);
  655. Boolean flag = ops.setIfAbsent(1, 1, TimeUnit.HOURS);
  656. return Boolean.TRUE.equals(flag);
  657. }
  658. // 释放订单锁
  659. public void releaseOrderLock(Long orderId) {
  660. redisTemplate.delete(RedisKeys.ORDER_LOCK + orderId);
  661. }
  662. }