BE_Express_CreateOrder_SFTC.groovy 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import com.alibaba.fastjson2.JSON
  2. import com.dderp.common.api.BusinessExecutor
  3. import com.dderp.common.datas.ERPModule
  4. import com.dderp.common.entity.base.ProcessStringItem
  5. import com.dderp.common.entity.express.*
  6. import com.dderp.common.http.HttpTools
  7. import com.sweetfish.convert.json.JsonConvert
  8. import com.sweetfish.service.RetResult
  9. import groovy.json.JsonSlurper
  10. import org.apache.logging.log4j.LogManager
  11. import org.apache.logging.log4j.Logger
  12. import javax.annotation.Resource
  13. import java.nio.charset.StandardCharsets
  14. import java.time.LocalDateTime
  15. import java.time.ZoneOffset
  16. import java.util.concurrent.CompletableFuture
  17. import java.util.concurrent.ExecutionException
  18. import java.util.concurrent.TimeUnit
  19. import java.util.concurrent.TimeoutException
  20. @SuppressWarnings("unused")
  21. class BE_Express_CreateOrder_SFTC implements BusinessExecutor<ProcessStringItem, ProcessStringItem> {
  22. private final Logger logger = LogManager.getLogger(this.getClass().getSimpleName())
  23. @Resource(name = "property.sftc.appId")
  24. long sfAppId
  25. @Resource(name = "property.sftc.appKey")
  26. String sfAppKey
  27. @Resource(name = "property.sftc.apiUrl")
  28. String sfApiUrl
  29. @Resource
  30. JsonConvert jsonConvert
  31. @Override
  32. String scriptName() {
  33. return "顺丰同城创建订单"
  34. }
  35. @Override
  36. ERPModule module() {
  37. return ERPModule.EXPRESS_API
  38. }
  39. RetResult<ProcessStringItem> execute(ProcessStringItem source) {
  40. //秒级时间戳,groovy里面不让用system
  41. long currentTime = LocalDateTime.now().toEpochSecond(ZoneOffset.ofHours(8))
  42. long testTime = LocalDateTime.now().toInstant(ZoneOffset.ofHours(8)).toEpochMilli()
  43. SFOrder sfOrder = new SFOrder(
  44. dev_id: sfAppId,
  45. shop_id: "3243279847393",
  46. shop_type: 1,
  47. shop_order_id: "JY" + testTime + "L",
  48. order_sequence: "测试",
  49. lbs_type: 2,
  50. order_time: currentTime,
  51. push_time: currentTime,
  52. vehicle: 0,
  53. four_wheeler_type: 10,
  54. rider_pick_method: 1,
  55. version: 19,
  56. order_detail: new SFOrderDetail(
  57. total_price: 30100L,
  58. product_type: 6,
  59. weight_gram: 3000L,
  60. product_num: 15,
  61. product_type_num: 5,
  62. product_detail: [
  63. new SFOrderProductDetail(
  64. product_name: "测试商品1",
  65. product_num: 1
  66. ),
  67. new SFOrderProductDetail(
  68. product_name: "测试商品2",
  69. product_num: 3
  70. ),
  71. ]
  72. ),
  73. receive: new SFOrderReceive(
  74. user_name: "顺丰同城",
  75. user_phone: "18237171439",
  76. user_lng: "116.339392",
  77. user_lat: "40.002349",
  78. user_address: "北京市海淀区学清嘉创大厦A座15层"
  79. )
  80. )
  81. String postData = JSON.toJSONString(sfOrder)
  82. logger.info("请求数据: " + postData)
  83. String sign = ExpressApiSign.sfGenerateOpenSign(postData, sfAppId, sfAppKey)
  84. String url = sfApiUrl + "createorder?sign=" + sign
  85. CompletableFuture<String> apiResult = HttpTools.postHttpContentAsync(url,
  86. 5000,
  87. StandardCharsets.UTF_8,
  88. postData,
  89. ["Content-Type": "application/json;charset=utf-8"])
  90. try {
  91. String orderResult = apiResult.get(6000, TimeUnit.SECONDS)
  92. def jsonSlurper = new JsonSlurper()
  93. def sfOrderJson = jsonSlurper.parseText(orderResult)
  94. logger.info(sfOrderJson)
  95. SFCreateOrderResult createOrderResult = jsonConvert.convertFromO(SFCreateOrderResult.class, orderResult)
  96. logger.info(orderResult)
  97. logger.info(jsonConvert.convertTo(createOrderResult))
  98. return RetResult.<ProcessStringItem> successT().result(
  99. ProcessStringItem.newBuilder()
  100. .itemValue("")
  101. .build()
  102. )
  103. } catch (InterruptedException | ExecutionException | TimeoutException e) {
  104. logger.error(e.getMessage(), e)
  105. return RetResult.<ProcessStringItem> successT().result(
  106. ProcessStringItem.newBuilder()
  107. .itemValue("")
  108. .build()
  109. )
  110. }
  111. }
  112. }