MintOrderService.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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 (mintActivity.getNum() > 0) {
  119. if (assetId.size() != mintActivity.getNum()) {
  120. throw new BusinessException("数量不正确,请重新选择");
  121. }
  122. }
  123. List<Asset> assets = assetRepo.findAllByIdInAndUserId(assetId, user.getId());
  124. // 资产产品是否符合铸造活动的名称
  125. assets = assets.stream()
  126. .filter(asset -> asset.getName()
  127. .contains(mintActivity.getCollectionName()) && AssetStatus.NORMAL.equals(asset.getStatus()))
  128. .collect(Collectors.toList());
  129. if (mintActivity.getNum() > 0 && (assets.size() != mintActivity.getNum())) {
  130. throw new BusinessException("有藏品不符合,请重新选择");
  131. }
  132. Map<Long, Long> privilegeIds = new HashMap<>();
  133. // 铸造特权
  134. if (!mintActivity.isConsume()) {
  135. assets.forEach(asset -> {
  136. List<Privilege> privileges = asset.getPrivileges()
  137. .stream()
  138. .filter(p -> p.getName().equals("铸造"))
  139. .collect(Collectors.toList());
  140. if (privileges.size() == 0) {
  141. throw new BusinessException("无铸造特权");
  142. } else {
  143. boolean flag = false;
  144. for (Privilege privilege : privileges) {
  145. // 打开多次 或者 可打开一次但未使用
  146. if (!privilege.isOnce() || (privilege.isOnce() && !privilege
  147. .isOpened())) {
  148. flag = true;
  149. privilegeIds.put(asset.getId(), privilege.getId());
  150. break;
  151. }
  152. }
  153. if (!flag) {
  154. throw new BusinessException("铸造特权已使用");
  155. }
  156. }
  157. });
  158. assets.forEach(asset -> {
  159. asset.getPrivileges()
  160. .stream()
  161. .filter(p -> p.getId().equals(privilegeIds.get(asset.getId())))
  162. .forEach(p -> {
  163. p.setOpened(true);
  164. p.setOpenTime(LocalDateTime.now());
  165. p.setOpenedBy(SecurityUtils.getAuthenticatedUser().getId());
  166. });
  167. assetRepo.save(asset);
  168. });
  169. } else {
  170. // 消耗改为转赠
  171. assets.forEach(asset -> {
  172. asset.setStatus(AssetStatus.MINTING);
  173. assetRepo.save(asset);
  174. });
  175. // 转让的用户
  176. userRepo.findByIdAndDelFalse(1435297L).orElseThrow(new BusinessException("无法铸造"));
  177. }
  178. // 铸造资产
  179. List<MintMaterial> materials = assets.stream().map(asset -> {
  180. MintMaterial material = new MintMaterial();
  181. material.setAssetId(asset.getId());
  182. material.setCollectionId(asset.getCollectionId());
  183. material.setName(asset.getName());
  184. material.setPrivilegeId(privilegeIds.get(asset.getId()));
  185. material.setNumber(asset.getNumber());
  186. material.setPic(asset.getPic());
  187. material.setCategory(asset.getCategory());
  188. return material;
  189. }).collect(Collectors.toList());
  190. UserAddress userAddress = null;
  191. if (addressId != null) {
  192. userAddress = userAddressRepo.findById(addressId).orElseThrow(new BusinessException("地址信息不存在"));
  193. }
  194. // 铸造订单
  195. mintOrderRepo.save(MintOrder.builder()
  196. .userId(user.getId())
  197. .phone(user.getPhone())
  198. .material(materials)
  199. .consume(mintActivity.isConsume())
  200. .status(MintOrderStatus.NOT_PAID)
  201. .airDrop(mintActivity.isAirDrop())
  202. .gasPrice(mintActivity.getGasPrice())
  203. .mintActivityId(mintActivityId)
  204. .contactName(Optional.ofNullable(userAddress).map(UserAddress::getName).orElse(null))
  205. .contactPhone(Optional.ofNullable(userAddress).map(UserAddress::getPhone).orElse(null))
  206. .address(Optional.ofNullable(userAddress).map(u ->
  207. u.getProvinceId() + " " + u.getCityId() + " " + u.getDistrictId() + " " + u.getAddress())
  208. .orElse(null))
  209. .build());
  210. //库存
  211. mintActivity.setStock(mintActivity.getStock() - 1);
  212. return mintActivityRepo.save(mintActivity).getId();
  213. }
  214. public Object payOrderWeixin(Long id, String tradeType, String openId) throws WxPayException, EncoderException {
  215. MintOrder order = mintOrderRepo.findById(id).orElseThrow(new BusinessException("订单不存在"));
  216. if (order.getStatus() != MintOrderStatus.NOT_PAID) {
  217. throw new BusinessException("订单状态错误");
  218. }
  219. WxPayUnifiedOrderRequest request = new WxPayUnifiedOrderRequest();
  220. request.setBody("铸造GAS费");
  221. request.setOutTradeNo(String.valueOf(new SnowflakeIdWorker(1, 1).nextId()));
  222. request.setTotalFee(order.getGasPrice().multiply(BigDecimal.valueOf(100)).intValue());
  223. if (Arrays.stream(env.getActiveProfiles()).noneMatch(s -> s.equals("prod"))) {
  224. // 测试环境设为1分
  225. // request.setTotalFee(1);
  226. }
  227. request.setSpbillCreateIp("180.102.110.170");
  228. request.setNotifyUrl(wxPayProperties.getNotifyUrl());
  229. request.setTradeType(tradeType);
  230. request.setOpenid(openId);
  231. request.setSignType("MD5");
  232. JSONObject body = new JSONObject();
  233. body.put("action", "payMintOrder");
  234. body.put("userId", order.getUserId());
  235. body.put("orderId", order.getId());
  236. request.setAttach(body.toJSONString());
  237. if (WxPayConstants.TradeType.MWEB.equals(tradeType)) {
  238. WxPayMwebOrderResult result = wxPayService.createOrder(request);
  239. return result.getMwebUrl() + "&redirect_url=" + new URLCodec().encode(wxPayProperties.getReturnUrl());
  240. } else if (WxPayConstants.TradeType.JSAPI.equals(tradeType)) {
  241. return wxPayService.<WxPayMpOrderResult>createOrder(request);
  242. }
  243. throw new BusinessException("不支持此付款方式");
  244. }
  245. public Object payAdapay(Long id, String payChannel, String openId) throws BaseAdaPayException {
  246. List<String> aliChannels = Arrays.asList("alipay", "alipay_qr", "alipay_wap");
  247. List<String> wxChannels = Arrays.asList("wx_pub", "wx_lite");
  248. if (!aliChannels.contains(payChannel) && !wxChannels.contains(payChannel)) {
  249. throw new BusinessException("不支持此渠道");
  250. }
  251. MintOrder order = mintOrderRepo.findById(id).orElseThrow(new BusinessException("订单不存在"));
  252. if (order.getStatus() != MintOrderStatus.NOT_PAID) {
  253. throw new BusinessException("订单状态错误");
  254. }
  255. Map<String, Object> paymentParams = new HashMap<>();
  256. paymentParams.put("order_no", String.valueOf(snowflakeIdWorker.nextId()));
  257. paymentParams.put("pay_amt", order.getGasPrice().setScale(2, RoundingMode.HALF_UP).toPlainString());
  258. paymentParams.put("app_id", adapayProperties.getAppId());
  259. paymentParams.put("pay_channel", payChannel);
  260. paymentParams.put("goods_title", "铸造GAS费");
  261. paymentParams.put("goods_desc", "铸造GAS费");
  262. paymentParams.put("time_expire", DateTimeFormatter.ofPattern("yyyyMMddHHmmss")
  263. .format(LocalDateTime.now().plusMinutes(5)));
  264. paymentParams.put("notify_url", adapayProperties.getNotifyUrl() + "/mintOrder/" + order.getId());
  265. Map<String, Object> expend = new HashMap<>();
  266. paymentParams.put("expend", expend);
  267. if ("wx_pub".equals(payChannel)) {
  268. if (StringUtils.isBlank(openId)) {
  269. throw new BusinessException("缺少openId");
  270. }
  271. expend.put("open_id", openId);
  272. expend.put("limit_pay", "1");
  273. }
  274. Map<String, Object> response;
  275. if ("wx_lite".equals(payChannel)) {
  276. paymentParams.put("adapay_func_code", "wxpay.createOrder");
  277. paymentParams.put("callback_url", generalProperties.getHost() + "/9th/orders");
  278. response = AdapayCommon.requestAdapayUits(paymentParams);
  279. log.info("createOrderResponse {}", JSON.toJSONString(response, SerializerFeature.PrettyFormat));
  280. } else {
  281. response = Payment.create(paymentParams);
  282. log.info("createOrderResponse {}", JSON.toJSONString(response, SerializerFeature.PrettyFormat));
  283. AdapayService.checkSuccess(response);
  284. }
  285. switch (payChannel) {
  286. case "alipay_wap":
  287. case "alipay":
  288. return MapUtils.getString(MapUtils.getMap(response, "expend"), "pay_info");
  289. case "alipay_qr":
  290. return MapUtils.getString(MapUtils.getMap(response, "expend"), "qrcode_url");
  291. case "wx_pub":
  292. JSONObject payParams = JSON.parseObject(MapUtils.getString(MapUtils.getMap(response, "expend"), "pay_info"));
  293. payParams.put("timestamp", payParams.get("timeStamp"));
  294. payParams.remove("timeStamp");
  295. return payParams;
  296. default:
  297. return MapUtils.getMap(response, "expend");
  298. }
  299. }
  300. @Transactional
  301. public void mintNotify(Long orderId, PayMethod payMethod, String transactionId) {
  302. MintOrder mintOrder = mintOrderRepo.findById(orderId).orElseThrow(new BusinessException("订单不存在"));
  303. List<MintMaterial> materials = mintOrder.getMaterial();
  304. List<Asset> assets = assetRepo.findAllById(materials
  305. .stream()
  306. .map(MintMaterial::getAssetId)
  307. .collect(Collectors.toList()));
  308. mintOrder.setPayMethod(payMethod);
  309. if (mintOrder.isAirDrop()) {
  310. mintOrder.setStatus(MintOrderStatus.AIR_DROP);
  311. } else {
  312. mintOrder.setStatus(MintOrderStatus.DELIVERY);
  313. }
  314. mintOrder.setTransactionId(transactionId);
  315. mintOrder.setPayAt(LocalDateTime.now());
  316. if (mintOrder.isConsume()) {
  317. User newOwner = userRepo.findByIdAndDelFalse(1435297L).orElseThrow(new BusinessException("无法铸造"));
  318. assets.forEach(asset -> assetService.transfer(asset, asset.getPrice(), newOwner, "转赠", null));
  319. }
  320. mintOrderRepo.save(mintOrder);
  321. }
  322. @Scheduled(fixedRate = 60000)
  323. public void batchCancel() {
  324. if (generalProperties.isNotifyServer()) {
  325. return;
  326. }
  327. if (Arrays.asList(env.getActiveProfiles()).contains("dev")) {
  328. return;
  329. }
  330. List<MintOrder> orders = mintOrderRepo.findByStatusAndCreatedAtBeforeAndDelFalse(MintOrderStatus.NOT_PAID,
  331. LocalDateTime.now().minusMinutes(5));
  332. orders.forEach(o -> {
  333. try {
  334. cancel(o);
  335. } catch (Exception ignored) {
  336. }
  337. });
  338. }
  339. public void cancel(MintOrder order) {
  340. if (order.getStatus() != MintOrderStatus.NOT_PAID) {
  341. throw new BusinessException("已支付订单无法取消");
  342. }
  343. List<MintMaterial> materials = order.getMaterial();
  344. List<Asset> assets = assetRepo.findAllById(materials.stream()
  345. .map(MintMaterial::getAssetId)
  346. .collect(Collectors.toList()));
  347. if (order.isConsume()) {
  348. assets.forEach(asset -> {
  349. asset.setStatus(AssetStatus.NORMAL);
  350. assetRepo.save(asset);
  351. });
  352. } else {
  353. Map<Long, Long> privilegeIds = materials.stream()
  354. .collect(Collectors.toMap(MintMaterial::getAssetId, MintMaterial::getPrivilegeId));
  355. assets.forEach(asset -> {
  356. asset.getPrivileges()
  357. .stream()
  358. .filter(p -> p.getId().equals(privilegeIds.get(asset.getId())))
  359. .forEach(p -> {
  360. p.setOpened(false);
  361. p.setOpenTime(null);
  362. p.setOpenedBy(null);
  363. });
  364. assetRepo.save(asset);
  365. });
  366. }
  367. log.info("set normal mintOrder {}", order.getId());
  368. order.setStatus(MintOrderStatus.CANCELLED);
  369. order.setCancelTime(LocalDateTime.now());
  370. mintOrderRepo.save(order);
  371. // 加库存
  372. mintActivityRepo.addStock(order.getMintActivityId());
  373. }
  374. }