MintOrderService.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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.github.binarywang.wxpay.bean.order.WxPayMpOrderResult;
  6. import com.github.binarywang.wxpay.bean.order.WxPayMwebOrderResult;
  7. import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderRequest;
  8. import com.github.binarywang.wxpay.constant.WxPayConstants;
  9. import com.github.binarywang.wxpay.exception.WxPayException;
  10. import com.github.binarywang.wxpay.service.WxPayService;
  11. import com.huifu.adapay.core.exception.BaseAdaPayException;
  12. import com.huifu.adapay.model.AdapayCommon;
  13. import com.huifu.adapay.model.Payment;
  14. import com.izouma.nineth.config.AdapayProperties;
  15. import com.izouma.nineth.config.GeneralProperties;
  16. import com.izouma.nineth.config.WxPayProperties;
  17. import com.izouma.nineth.domain.*;
  18. import com.izouma.nineth.dto.PageQuery;
  19. import com.izouma.nineth.enums.AssetStatus;
  20. import com.izouma.nineth.enums.MintOrderStatus;
  21. import com.izouma.nineth.enums.PayMethod;
  22. import com.izouma.nineth.exception.BusinessException;
  23. import com.izouma.nineth.repo.*;
  24. import com.izouma.nineth.utils.JpaUtils;
  25. import com.izouma.nineth.utils.SecurityUtils;
  26. import com.izouma.nineth.utils.SnowflakeIdWorker;
  27. import lombok.AllArgsConstructor;
  28. import lombok.extern.slf4j.Slf4j;
  29. import org.apache.commons.codec.EncoderException;
  30. import org.apache.commons.codec.net.URLCodec;
  31. import org.apache.commons.collections.MapUtils;
  32. import org.apache.commons.lang3.StringUtils;
  33. import org.springframework.core.env.Environment;
  34. import org.springframework.data.domain.Page;
  35. import org.springframework.scheduling.annotation.Scheduled;
  36. import org.springframework.stereotype.Service;
  37. import javax.transaction.Transactional;
  38. import java.math.BigDecimal;
  39. import java.math.RoundingMode;
  40. import java.time.LocalDateTime;
  41. import java.time.format.DateTimeFormatter;
  42. import java.util.*;
  43. import java.util.stream.Collectors;
  44. @Slf4j
  45. @Service
  46. @AllArgsConstructor
  47. public class MintOrderService {
  48. private MintOrderRepo mintOrderRepo;
  49. private UserRepo userRepo;
  50. private AssetService assetService;
  51. private AssetRepo assetRepo;
  52. private MintActivityRepo mintActivityRepo;
  53. private UserAddressRepo userAddressRepo;
  54. private GeneralProperties generalProperties;
  55. private Environment env;
  56. private AdapayProperties adapayProperties;
  57. private SnowflakeIdWorker snowflakeIdWorker;
  58. private WxPayProperties wxPayProperties;
  59. private WxPayService wxPayService;
  60. public Page<MintOrder> all(PageQuery pageQuery) {
  61. return mintOrderRepo.findAll(JpaUtils.toSpecification(pageQuery, MintOrder.class), JpaUtils.toPageRequest(pageQuery));
  62. }
  63. @Transactional
  64. public void create(Long userId, List<Long> assetIds) {
  65. User user = userRepo.findByIdAndDelFalse(userId).orElseThrow(new BusinessException("用户不存在"));
  66. User blackHole = userRepo.findByIdAndDelFalse(1435297L).orElseThrow(new BusinessException("无法铸造"));
  67. if (assetIds.size() != 3) {
  68. throw new BusinessException("数量不正确,请重新选择");
  69. }
  70. List<Asset> assets = assetRepo.findAllByIdInAndUserId(assetIds, userId);
  71. assets = assets.stream()
  72. .filter(asset -> asset.getName().contains("尼尔斯") && AssetStatus.NORMAL.equals(asset.getStatus()))
  73. .collect(Collectors.toList());
  74. if (assets.size() != 3) {
  75. throw new BusinessException("有藏品不符合,请重新选择");
  76. }
  77. // 铸造资产
  78. List<MintMaterial> materials = assets.stream().map(asset -> {
  79. MintMaterial material = new MintMaterial();
  80. material.setAssetId(asset.getId());
  81. material.setCollectionId(asset.getCollectionId());
  82. material.setName(asset.getName());
  83. material.setNumber(asset.getNumber());
  84. material.setPic(asset.getPic());
  85. material.setCategory(asset.getCategory());
  86. return material;
  87. }).collect(Collectors.toList());
  88. // 铸造订单
  89. mintOrderRepo.save(MintOrder.builder()
  90. .userId(userId)
  91. .phone(user.getPhone())
  92. .material(materials)
  93. .consume(true)
  94. .status(MintOrderStatus.AIR_DROP)
  95. .build());
  96. // 改为转赠
  97. assets.forEach(asset -> assetService.transfer(asset, asset.getPrice(), blackHole, "转赠", null));
  98. }
  99. public void finish(Long id) {
  100. MintOrder mintOrder = mintOrderRepo.findById(id).orElseThrow(new BusinessException("铸造订单不存在"));
  101. mintOrder.setStatus(MintOrderStatus.FINISH);
  102. mintOrderRepo.save(mintOrder);
  103. }
  104. /**
  105. * @param user 用户
  106. * @param assetId 资产
  107. * @param mintActivityId 铸造活动
  108. * @param addressId 地址
  109. */
  110. @Transactional
  111. public Long create(User user, List<Long> assetId, Long mintActivityId, Long addressId) {
  112. // 参加的活动
  113. MintActivity mintActivity = mintActivityRepo.findByIdAndDelFalse(mintActivityId)
  114. .orElseThrow(new BusinessException("无此铸造活动"));
  115. if (mintActivity.getStock() <= 0) {
  116. throw new BusinessException("铸造活动已无库存");
  117. }
  118. if (assetId.size() != mintActivity.getNum()) {
  119. throw new BusinessException("数量不正确,请重新选择");
  120. }
  121. List<Asset> assets = assetRepo.findAllByIdInAndUserId(assetId, user.getId());
  122. // 资产产品是否符合铸造活动的名称
  123. assets = assets.stream()
  124. .filter(asset -> asset.getName()
  125. .contains(mintActivity.getCollectionName()) && AssetStatus.NORMAL.equals(asset.getStatus()))
  126. .collect(Collectors.toList());
  127. if (assets.size() != mintActivity.getNum()) {
  128. throw new BusinessException("有藏品不符合,请重新选择");
  129. }
  130. Map<Long, Long> privilegeIds = new HashMap<>();
  131. // 铸造特权
  132. if (!mintActivity.isConsume()) {
  133. assets.forEach(asset -> {
  134. List<Privilege> privileges = asset.getPrivileges()
  135. .stream()
  136. .filter(p -> p.getName().equals("铸造"))
  137. .collect(Collectors.toList());
  138. if (privileges.size() == 0) {
  139. throw new BusinessException("无铸造特权");
  140. } else {
  141. boolean flag = false;
  142. for (Privilege privilege : privileges) {
  143. // 打开多次 或者 可打开一次但未使用
  144. if (!privilege.isOnce() || (privilege.isOnce() && !privilege
  145. .isOpened())) {
  146. flag = true;
  147. privilegeIds.put(asset.getId(), privilege.getId());
  148. break;
  149. }
  150. }
  151. if (!flag) {
  152. throw new BusinessException("铸造特权已使用");
  153. }
  154. }
  155. });
  156. assets.forEach(asset -> {
  157. asset.getPrivileges()
  158. .stream()
  159. .filter(p -> p.getId().equals(privilegeIds.get(asset.getId())))
  160. .forEach(p -> {
  161. p.setOpened(true);
  162. p.setOpenTime(LocalDateTime.now());
  163. p.setOpenedBy(SecurityUtils.getAuthenticatedUser().getId());
  164. });
  165. assetRepo.save(asset);
  166. });
  167. } else {
  168. // 消耗改为转赠
  169. assets.forEach(asset -> {
  170. asset.setStatus(AssetStatus.MINTING);
  171. assetRepo.save(asset);
  172. });
  173. // 转让的用户
  174. userRepo.findByIdAndDelFalse(1435297L).orElseThrow(new BusinessException("无法铸造"));
  175. }
  176. // 铸造资产
  177. List<MintMaterial> materials = assets.stream().map(asset -> {
  178. MintMaterial material = new MintMaterial();
  179. material.setAssetId(asset.getId());
  180. material.setCollectionId(asset.getCollectionId());
  181. material.setName(asset.getName());
  182. material.setPrivilegeId(privilegeIds.get(asset.getId()));
  183. material.setNumber(asset.getNumber());
  184. material.setPic(asset.getPic());
  185. material.setCategory(asset.getCategory());
  186. return material;
  187. }).collect(Collectors.toList());
  188. UserAddress userAddress = null;
  189. if (addressId != null) {
  190. userAddress = userAddressRepo.findById(addressId).orElseThrow(new BusinessException("地址信息不存在"));
  191. }
  192. // 铸造订单
  193. mintOrderRepo.save(MintOrder.builder()
  194. .userId(user.getId())
  195. .phone(user.getPhone())
  196. .material(materials)
  197. .consume(mintActivity.isConsume())
  198. .status(MintOrderStatus.NOT_PAID)
  199. .airDrop(mintActivity.isAirDrop())
  200. .gasPrice(mintActivity.getGasPrice())
  201. .mintActivityId(mintActivityId)
  202. .contactName(Optional.ofNullable(userAddress).map(UserAddress::getName).orElse(null))
  203. .contactPhone(Optional.ofNullable(userAddress).map(UserAddress::getPhone).orElse(null))
  204. .address(Optional.ofNullable(userAddress).map(u ->
  205. u.getProvinceId() + " " + u.getCityId() + " " + u.getDistrictId() + " " + u.getAddress())
  206. .orElse(null))
  207. .build());
  208. //库存
  209. mintActivity.setStock(mintActivity.getStock() - 1);
  210. return mintActivityRepo.save(mintActivity).getId();
  211. }
  212. public Object payOrderWeixin(Long id, String tradeType, String openId) throws WxPayException, EncoderException {
  213. MintOrder order = mintOrderRepo.findById(id).orElseThrow(new BusinessException("订单不存在"));
  214. if (order.getStatus() != MintOrderStatus.NOT_PAID) {
  215. throw new BusinessException("订单状态错误");
  216. }
  217. WxPayUnifiedOrderRequest request = new WxPayUnifiedOrderRequest();
  218. request.setBody("铸造GAS费");
  219. request.setOutTradeNo(String.valueOf(new SnowflakeIdWorker(1, 1).nextId()));
  220. request.setTotalFee(order.getGasPrice().multiply(BigDecimal.valueOf(100)).intValue());
  221. if (Arrays.stream(env.getActiveProfiles()).noneMatch(s -> s.equals("prod"))) {
  222. // 测试环境设为1分
  223. // request.setTotalFee(1);
  224. }
  225. request.setSpbillCreateIp("180.102.110.170");
  226. request.setNotifyUrl(wxPayProperties.getNotifyUrl());
  227. request.setTradeType(tradeType);
  228. request.setOpenid(openId);
  229. request.setSignType("MD5");
  230. JSONObject body = new JSONObject();
  231. body.put("action", "payMintOrder");
  232. body.put("userId", order.getUserId());
  233. body.put("orderId", order.getId());
  234. request.setAttach(body.toJSONString());
  235. if (WxPayConstants.TradeType.MWEB.equals(tradeType)) {
  236. WxPayMwebOrderResult result = wxPayService.createOrder(request);
  237. return result.getMwebUrl() + "&redirect_url=" + new URLCodec().encode(wxPayProperties.getReturnUrl());
  238. } else if (WxPayConstants.TradeType.JSAPI.equals(tradeType)) {
  239. return wxPayService.<WxPayMpOrderResult>createOrder(request);
  240. }
  241. throw new BusinessException("不支持此付款方式");
  242. }
  243. public Object payAdapay(Long id, String payChannel, String openId) throws BaseAdaPayException {
  244. List<String> aliChannels = Arrays.asList("alipay", "alipay_qr", "alipay_wap");
  245. List<String> wxChannels = Arrays.asList("wx_pub", "wx_lite");
  246. if (!aliChannels.contains(payChannel) && !wxChannels.contains(payChannel)) {
  247. throw new BusinessException("不支持此渠道");
  248. }
  249. MintOrder order = mintOrderRepo.findById(id).orElseThrow(new BusinessException("订单不存在"));
  250. if (order.getStatus() != MintOrderStatus.NOT_PAID) {
  251. throw new BusinessException("订单状态错误");
  252. }
  253. Map<String, Object> paymentParams = new HashMap<>();
  254. paymentParams.put("order_no", String.valueOf(snowflakeIdWorker.nextId()));
  255. paymentParams.put("pay_amt", order.getGasPrice().setScale(2, RoundingMode.HALF_UP).toPlainString());
  256. paymentParams.put("app_id", adapayProperties.getAppId());
  257. paymentParams.put("pay_channel", payChannel);
  258. paymentParams.put("goods_title", "铸造GAS费");
  259. paymentParams.put("goods_desc", "铸造GAS费");
  260. paymentParams.put("time_expire", DateTimeFormatter.ofPattern("yyyyMMddHHmmss")
  261. .format(LocalDateTime.now().plusMinutes(5)));
  262. paymentParams.put("notify_url", adapayProperties.getNotifyUrl() + "/mintOrder/" + order.getId());
  263. Map<String, Object> expend = new HashMap<>();
  264. paymentParams.put("expend", expend);
  265. if ("wx_pub".equals(payChannel)) {
  266. if (StringUtils.isBlank(openId)) {
  267. throw new BusinessException("缺少openId");
  268. }
  269. expend.put("open_id", openId);
  270. expend.put("limit_pay", "1");
  271. }
  272. Map<String, Object> response;
  273. if ("wx_lite".equals(payChannel)) {
  274. paymentParams.put("adapay_func_code", "wxpay.createOrder");
  275. paymentParams.put("callback_url", generalProperties.getHost() + "/9th/orders");
  276. response = AdapayCommon.requestAdapayUits(paymentParams);
  277. log.info("createOrderResponse {}", JSON.toJSONString(response, SerializerFeature.PrettyFormat));
  278. } else {
  279. response = Payment.create(paymentParams);
  280. log.info("createOrderResponse {}", JSON.toJSONString(response, SerializerFeature.PrettyFormat));
  281. AdapayService.checkSuccess(response);
  282. }
  283. switch (payChannel) {
  284. case "alipay_wap":
  285. case "alipay":
  286. return MapUtils.getString(MapUtils.getMap(response, "expend"), "pay_info");
  287. case "alipay_qr":
  288. return MapUtils.getString(MapUtils.getMap(response, "expend"), "qrcode_url");
  289. case "wx_pub":
  290. JSONObject payParams = JSON.parseObject(MapUtils.getString(MapUtils.getMap(response, "expend"), "pay_info"));
  291. payParams.put("timestamp", payParams.get("timeStamp"));
  292. payParams.remove("timeStamp");
  293. return payParams;
  294. default:
  295. return MapUtils.getMap(response, "expend");
  296. }
  297. }
  298. @Transactional
  299. public void mintNotify(Long orderId, PayMethod payMethod, String transactionId) {
  300. MintOrder mintOrder = mintOrderRepo.findById(orderId).orElseThrow(new BusinessException("订单不存在"));
  301. List<MintMaterial> materials = mintOrder.getMaterial();
  302. List<Asset> assets = assetRepo.findAllById(materials
  303. .stream()
  304. .map(MintMaterial::getAssetId)
  305. .collect(Collectors.toList()));
  306. mintOrder.setPayMethod(payMethod);
  307. if (mintOrder.isAirDrop()) {
  308. mintOrder.setStatus(MintOrderStatus.AIR_DROP);
  309. } else {
  310. mintOrder.setStatus(MintOrderStatus.DELIVERY);
  311. }
  312. mintOrder.setTransactionId(transactionId);
  313. mintOrder.setPayAt(LocalDateTime.now());
  314. if (mintOrder.isConsume()) {
  315. User newOwner = userRepo.findByIdAndDelFalse(1435297L).orElseThrow(new BusinessException("无法铸造"));
  316. assets.forEach(asset -> assetService.transfer(asset, asset.getPrice(), newOwner, "转赠", null));
  317. }
  318. mintOrderRepo.save(mintOrder);
  319. }
  320. @Scheduled(fixedRate = 60000)
  321. public void batchCancel() {
  322. if (generalProperties.isNotifyServer()) {
  323. return;
  324. }
  325. if (Arrays.asList(env.getActiveProfiles()).contains("dev")) {
  326. return;
  327. }
  328. List<MintOrder> orders = mintOrderRepo.findByStatusAndCreatedAtBeforeAndDelFalse(MintOrderStatus.NOT_PAID,
  329. LocalDateTime.now().minusMinutes(5));
  330. orders.forEach(o -> {
  331. try {
  332. cancel(o);
  333. } catch (Exception ignored) {
  334. }
  335. });
  336. }
  337. public void cancel(MintOrder order) {
  338. if (order.getStatus() != MintOrderStatus.NOT_PAID) {
  339. throw new BusinessException("已支付订单无法取消");
  340. }
  341. List<MintMaterial> materials = order.getMaterial();
  342. Map<Long, Long> privilegeIds = materials.stream()
  343. .collect(Collectors.toMap(MintMaterial::getAssetId, MintMaterial::getPrivilegeId));
  344. List<Asset> assets = assetRepo.findAllById(privilegeIds.keySet());
  345. assets.forEach(asset -> {
  346. if (order.isConsume()) {
  347. asset.setStatus(AssetStatus.NORMAL);
  348. } else {
  349. asset.getPrivileges()
  350. .stream()
  351. .filter(p -> p.getId().equals(privilegeIds.get(asset.getId())))
  352. .forEach(p -> {
  353. p.setOpened(false);
  354. p.setOpenTime(null);
  355. p.setOpenedBy(null);
  356. });
  357. }
  358. assetRepo.save(asset);
  359. });
  360. log.info("set normal mintOrder {}", order.getId());
  361. order.setStatus(MintOrderStatus.CANCELLED);
  362. order.setCancelTime(LocalDateTime.now());
  363. mintOrderRepo.save(order);
  364. // 加库存
  365. mintActivityRepo.addStock(order.getMintActivityId());
  366. }
  367. }