BE_Express_PreCreateOrder_SFTC.groovy 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import com.alibaba.fastjson2.JSON
  2. import com.dderp.common.api.BusinessExecutor
  3. import com.dderp.common.api.ERPLockDataService
  4. import com.dderp.common.datas.ERPModule
  5. import com.dderp.common.entity.base.InvokeCallParams
  6. import com.dderp.common.entity.base.InvokeCallResult
  7. import com.dderp.common.http.HttpTools
  8. import com.dySweetFishPlugin.sql.dao.OperatorWait
  9. import com.sweetfish.convert.json.JsonConvert
  10. import com.sweetfish.service.RetResult
  11. import groovy.json.JsonSlurper
  12. import org.apache.logging.log4j.LogManager
  13. import org.apache.logging.log4j.Logger
  14. import javax.annotation.Resource
  15. import java.nio.charset.StandardCharsets
  16. import java.time.LocalDateTime
  17. import java.time.ZoneOffset
  18. import java.util.concurrent.CompletableFuture
  19. import java.util.concurrent.ExecutionException
  20. import java.util.concurrent.TimeUnit
  21. import java.util.concurrent.TimeoutException
  22. @SuppressWarnings("unused")
  23. class BE_Express_PreCreateOrder_SFTC implements BusinessExecutor<InvokeCallParams, InvokeCallResult> {
  24. private final Logger logger = LogManager.getLogger(this.getClass().getSimpleName())
  25. @Resource(name = "property.sftc.appId")
  26. long sfAppId
  27. @Resource(name = "property.sftc.appKey")
  28. String sfAppKey
  29. @Resource(name = "property.sftc.apiUrl")
  30. String sfApiUrl
  31. @Resource
  32. JsonConvert jsonConvert
  33. @Resource
  34. ERPLockDataService lockDataService
  35. @Override
  36. String scriptName() {
  37. return "顺丰同城预创建订单--价格计算"
  38. }
  39. @Override
  40. ERPModule module() {
  41. return ERPModule.EXPRESS_API
  42. }
  43. @Override
  44. OperatorWait getAWait(InvokeCallParams source) {
  45. return OperatorWait.SYNC
  46. }
  47. @Override
  48. RetResult<InvokeCallParams> checkExecute(InvokeCallParams source) {
  49. //检查订单信息
  50. def jsonSlurper = new JsonSlurper()
  51. def invokeOrder = jsonSlurper.parseText(source.params)
  52. String orderId = invokeOrder["orderId"] as String
  53. //todo 获取订单信息
  54. return RetResult.<InvokeCallParams> successT().result(source)
  55. }
  56. RetResult<InvokeCallResult> execute(InvokeCallParams source) {
  57. //预创建订单,并非真正发单;用来验证是否可以发单并在成功时返回时效、计价等信息,也可用来验证地址以及时间是否在顺丰的配送范围内
  58. //秒级时间戳,groovy里面不让用system
  59. long currentTime = LocalDateTime.now().toEpochSecond(ZoneOffset.ofHours(8))
  60. def sfOrder = [
  61. dev_id : sfAppId,
  62. shop_type : 1,
  63. user_address: "北京市海淀区国泰大厦",
  64. push_time : currentTime
  65. ]
  66. String postData = JSON.toJSONString(sfOrder)
  67. logger.info("请求数据: " + postData)
  68. String sign = ExpressApiSign.sfGenerateOpenSign(postData, sfAppId, sfAppKey)
  69. String url = sfApiUrl + "precreateorder?sign=" + sign
  70. CompletableFuture<String> apiResult = HttpTools.postHttpContentAsync(url,
  71. 5000,
  72. StandardCharsets.UTF_8,
  73. postData,
  74. ["Content-Type": "application/json;charset=utf-8"])
  75. try {
  76. String orderResult = apiResult.get(6000, TimeUnit.SECONDS)
  77. def jsonSlurper = new JsonSlurper()
  78. def sfOrderJson = jsonSlurper.parseText(orderResult)
  79. logger.info(sfOrderJson)
  80. return RetResult.<InvokeCallResult> successT()
  81. } catch (InterruptedException | ExecutionException | TimeoutException e) {
  82. logger.error(e.getMessage(), e)
  83. return RetResult.<InvokeCallResult> errorT().retinfo(e.getMessage())
  84. }
  85. }
  86. }