BE_Express_PreCreateOrder_SFTC.groovy 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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.api.StoreService
  5. import com.dderp.common.api.flycat.OrderSearchService
  6. import com.dderp.common.datas.ERPModule
  7. import com.dderp.common.datas.ReadOrderOption
  8. import com.dderp.common.datas.TokenUserFrom
  9. import com.dderp.common.entity.base.InvokeCallParams
  10. import com.dderp.common.entity.base.InvokeCallResult
  11. import com.dderp.common.entity.order.BusinessOrder
  12. import com.dderp.common.entity.store.StoreInfo
  13. import com.dderp.common.entity.store.StorePlatform
  14. import com.dderp.common.http.HttpTools
  15. import com.dderp.common.tool.ERPUtils
  16. import com.dySweetFishPlugin.sql.dao.OperatorWait
  17. import com.sweetfish.convert.json.JsonConvert
  18. import com.sweetfish.service.RetResult
  19. import groovy.json.JsonSlurper
  20. import org.apache.commons.lang3.StringUtils
  21. import org.apache.logging.log4j.LogManager
  22. import org.apache.logging.log4j.Logger
  23. import javax.annotation.Resource
  24. import java.nio.charset.StandardCharsets
  25. import java.time.LocalDateTime
  26. import java.time.ZoneOffset
  27. import java.util.concurrent.CompletableFuture
  28. import java.util.concurrent.ExecutionException
  29. import java.util.concurrent.TimeUnit
  30. import java.util.concurrent.TimeoutException
  31. @SuppressWarnings("unused")
  32. class BE_Express_PreCreateOrder_SFTC implements BusinessExecutor<InvokeCallParams, InvokeCallResult> {
  33. private final Logger logger = LogManager.getLogger(this.getClass().getSimpleName())
  34. @Resource(name = "property.sftc.appId")
  35. long sfAppId
  36. @Resource(name = "property.sftc.appKey")
  37. String sfAppKey
  38. @Resource(name = "property.sftc.apiUrl")
  39. String sfApiUrl
  40. @Resource
  41. JsonConvert jsonConvert
  42. @Resource
  43. ERPLockDataService lockDataService
  44. @Resource
  45. OrderSearchService orderSearchService
  46. @Resource
  47. StoreService storeService
  48. @Override
  49. String scriptName() {
  50. return "顺丰同城预创建订单--价格计算"
  51. }
  52. @Override
  53. ERPModule module() {
  54. return ERPModule.EXPRESS_API
  55. }
  56. @Override
  57. OperatorWait getAWait(InvokeCallParams source) {
  58. return OperatorWait.SYNC
  59. }
  60. @Override
  61. RetResult<InvokeCallParams> checkExecute(InvokeCallParams source) {
  62. //检查订单信息
  63. def jsonSlurper = new JsonSlurper()
  64. def invokeOrder = jsonSlurper.parseText(source.params)
  65. String idOrder = invokeOrder["idOrder"] as String
  66. if (source.currentUser == null) {
  67. return RetResult.<InvokeCallParams> errorT().retinfo("请登录")
  68. }
  69. RetResult<BusinessOrder> orderResult = orderSearchService.getBusinessOrder(Long.parseLong(idOrder), source.currentUser, source.dataSourceId, source.supplierCode)
  70. if (!orderResult.isSuccess()) {
  71. return RetResult.<InvokeCallParams> errorT().retinfo("无效的订单信息")
  72. }
  73. BusinessOrder businessOrder = orderResult.result
  74. if (source.currentUser.userFrom == TokenUserFrom.PC_STORE_ADMIN.value) {
  75. if (businessOrder.idStore != source.currentUser.idBindOrg) {
  76. return RetResult.<InvokeCallParams> errorT().retinfo("无效的订单信息")
  77. }
  78. }
  79. return RetResult.<InvokeCallParams> successT().result(source)
  80. }
  81. RetResult<InvokeCallResult> execute(InvokeCallParams source) {
  82. //预创建订单,并非真正发单;用来验证是否可以发单并在成功时返回时效、计价等信息,也可用来验证地址以及时间是否在顺丰的配送范围内
  83. def jsonSlurper = new JsonSlurper()
  84. def invokeOrder = jsonSlurper.parseText(source.params)
  85. String idOrder = invokeOrder["idOrder"] as String
  86. RetResult<BusinessOrder> orderResult = orderSearchService.getBusinessOrder(Long.parseLong(idOrder), source.currentUser, source.dataSourceId, source.supplierCode, ReadOrderOption.ORDER_FINANCES, ReadOrderOption.ORDER_DELIVERY)
  87. if (!orderResult.isSuccess()) {
  88. return RetResult.<InvokeCallResult> errorT().retinfo("无效的订单信息")
  89. }
  90. BusinessOrder businessOrder = orderResult.result
  91. StoreInfo storeInfo = storeService.getStoreInfo(businessOrder.idStore, source.supplierCode, false, false, true)
  92. StorePlatform storePlatform = storeInfo.platformList.find { StringUtils.equalsIgnoreCase("SFTC", it.platformCode) }
  93. if (storePlatform == null) {
  94. //统一返回,避免前端获取值不同
  95. def preDeliveryInfo = [
  96. errCode : 1001,
  97. errMsg : "商家未授权顺丰同城",
  98. //配送金额
  99. totalPrice : 0,
  100. //配送距离
  101. distanceMeter: 0
  102. ]
  103. return RetResult.<InvokeCallResult> successT().result(
  104. InvokeCallResult.success().data(JSON.toJSONString(preDeliveryInfo))
  105. )
  106. }
  107. //秒级时间戳,groovy里面不让用system
  108. long currentTime = LocalDateTime.now().toEpochSecond(ZoneOffset.ofHours(8))
  109. def sfOrder = [
  110. dev_id : sfAppId,
  111. shop_id : storePlatform.platformStoreId,
  112. product_type : 1,
  113. shop_type : 1,
  114. user_address : ERPUtils.convertNullToBlank(businessOrder.orderDeliveryInfo.deliverAddress),
  115. push_time : currentTime,
  116. return_flag : 511,
  117. multi_pickup_info: [
  118. [
  119. pickup_shop_address: storePlatform.platformShopAddress
  120. ]
  121. ]
  122. ]
  123. String postData = JSON.toJSONString(sfOrder)
  124. logger.info("请求数据: " + postData)
  125. String sign = ExpressApiSign.sfGenerateOpenSign(postData, sfAppId, sfAppKey)
  126. String url = sfApiUrl + "precreateorder?sign=" + sign
  127. CompletableFuture<String> apiResult = HttpTools.postHttpContentAsync(url,
  128. 5000,
  129. StandardCharsets.UTF_8,
  130. postData,
  131. ["Content-Type": "application/json;charset=utf-8"])
  132. try {
  133. String sfOrderResult = apiResult.get(6000, TimeUnit.SECONDS)
  134. def sfOrderJson = jsonSlurper.parseText(sfOrderResult)
  135. if (sfOrderJson["error_code"] == 0) {
  136. //统一返回,避免前端获取值不同
  137. def preDeliveryInfo = [
  138. errCode : 0,
  139. errMsg : "",
  140. //配送金额
  141. totalPrice : sfOrderJson["result"]["total_price"],
  142. //配送距离
  143. distanceMeter: sfOrderJson["result"]["delivery_distance_meter"]
  144. ]
  145. return RetResult.<InvokeCallResult> successT().result(
  146. InvokeCallResult.success().data(JSON.toJSONString(preDeliveryInfo))
  147. )
  148. } else {
  149. def preDeliveryInfo = [
  150. errCode : 1000,
  151. errMsg : sfOrderJson["error_msg"] as String,
  152. //配送金额
  153. totalPrice : 0,
  154. //配送距离
  155. distanceMeter: 0
  156. ]
  157. return RetResult.<InvokeCallResult> successT().result(
  158. InvokeCallResult.success().data(JSON.toJSONString(preDeliveryInfo))
  159. )
  160. }
  161. } catch (InterruptedException | ExecutionException | TimeoutException e) {
  162. logger.error(e.getMessage(), e)
  163. return RetResult.<InvokeCallResult> errorT().retinfo(e.getMessage())
  164. }
  165. }
  166. }