| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202 |
- import com.alibaba.fastjson2.JSON
- import com.dderp.common.api.BusinessExecutor
- import com.dderp.common.api.ERPLockDataService
- import com.dderp.common.api.StoreService
- import com.dderp.common.api.flycat.OrderSearchService
- import com.dderp.common.datas.ERPModule
- import com.dderp.common.datas.ReadOrderOption
- import com.dderp.common.datas.TokenUserFrom
- import com.dderp.common.entity.base.InvokeCallParams
- import com.dderp.common.entity.base.InvokeCallResult
- import com.dderp.common.entity.order.BusinessOrder
- import com.dderp.common.entity.store.StoreInfo
- import com.dderp.common.entity.store.StorePlatform
- import com.dderp.common.http.HttpTools
- import com.dderp.common.tool.ERPUtils
- import com.dySweetFishPlugin.sql.dao.OperatorWait
- import com.sweetfish.convert.json.JsonConvert
- import com.sweetfish.service.RetResult
- import groovy.json.JsonSlurper
- import org.apache.commons.lang3.StringUtils
- import org.apache.logging.log4j.LogManager
- import org.apache.logging.log4j.Logger
- import org.rex.RMap
- import javax.annotation.Resource
- import java.nio.charset.StandardCharsets
- import java.time.LocalDateTime
- import java.time.ZoneOffset
- import java.util.concurrent.CompletableFuture
- import java.util.concurrent.ExecutionException
- import java.util.concurrent.TimeUnit
- import java.util.concurrent.TimeoutException
- @SuppressWarnings("unused")
- class BE_Express_PreCreateOrder_SFTC implements BusinessExecutor<InvokeCallParams, InvokeCallResult> {
- private final Logger logger = LogManager.getLogger(this.getClass().getSimpleName())
- @Resource(name = "property.sftc.appId")
- long sfAppId
- @Resource(name = "property.sftc.appKey")
- String sfAppKey
- @Resource(name = "property.sftc.apiUrl")
- String sfApiUrl
- @Resource
- JsonConvert jsonConvert
- @Resource
- ERPLockDataService lockDataService
- @Resource
- OrderSearchService orderSearchService
- @Resource
- StoreService storeService
- @Override
- String scriptName() {
- return "顺丰同城预创建订单--价格计算"
- }
- @Override
- ERPModule module() {
- return ERPModule.EXPRESS_API
- }
- @Override
- OperatorWait getAWait(InvokeCallParams source) {
- return OperatorWait.SYNC
- }
- @Override
- RetResult<InvokeCallParams> checkExecute(InvokeCallParams source) {
- //检查订单信息
- def jsonSlurper = new JsonSlurper()
- def invokeOrder = jsonSlurper.parseText(source.params)
- String idOrder = invokeOrder["idOrder"] as String
- if (source.currentUser == null) {
- return RetResult.<InvokeCallParams> errorT().retinfo("请登录")
- }
- RetResult<BusinessOrder> orderResult = orderSearchService.getBusinessOrder(Long.parseLong(idOrder), source.currentUser, source.dataSourceId, source.supplierCode)
- if (!orderResult.isSuccess()) {
- return RetResult.<InvokeCallParams> errorT().retinfo("无效的订单信息")
- }
- BusinessOrder businessOrder = orderResult.result
- if ((source.currentUser.userFrom == TokenUserFrom.APP_STORE_ADMIN.value) || (source.currentUser.userFrom == TokenUserFrom.PC_STORE_ADMIN.value)) {
- if (businessOrder.idStore != source.currentUser.idBindOrg) {
- return RetResult.<InvokeCallParams> errorT().retinfo("无效的订单信息")
- }
- }
- return RetResult.<InvokeCallParams> successT().result(source)
- }
- RetResult<InvokeCallResult> execute(InvokeCallParams source) {
- //预创建订单,并非真正发单;用来验证是否可以发单并在成功时返回时效、计价等信息,也可用来验证地址以及时间是否在顺丰的配送范围内
- def jsonSlurper = new JsonSlurper()
- def invokeOrder = jsonSlurper.parseText(source.params)
- String idOrder = invokeOrder["idOrder"] as String
- RetResult<BusinessOrder> orderResult = orderSearchService.getBusinessOrder(Long.parseLong(idOrder), source.currentUser, source.dataSourceId, source.supplierCode, ReadOrderOption.ORDER_FINANCES, ReadOrderOption.ORDER_DELIVERY)
- if (!orderResult.isSuccess()) {
- return RetResult.<InvokeCallResult> errorT().retinfo("无效的订单信息")
- }
- BusinessOrder businessOrder = orderResult.result
- StoreInfo storeInfo = storeService.getStoreInfo(businessOrder.idStore, source.supplierCode, false, false, true)
- StorePlatform storePlatform = storeInfo.platformList.find { StringUtils.equalsIgnoreCase("SFTC", it.platformCode) }
- if (storePlatform == null) {
- //统一返回,避免前端获取值不同
- def preDeliveryInfo = [
- errCode : 1001,
- errMsg : "商家未授权顺丰同城",
- //配送金额
- totalPrice : 0,
- //配送距离
- distanceMeter: 0
- ]
- return RetResult.<InvokeCallResult> successT().result(
- InvokeCallResult.success().data(JSON.toJSONString(preDeliveryInfo))
- )
- }
- //秒级时间戳,groovy里面不让用system
- long currentTime = LocalDateTime.now().toEpochSecond(ZoneOffset.ofHours(8))
- def sfOrder = [
- dev_id : sfAppId,
- shop_id : storePlatform.platformStoreId,
- product_type : 1,
- shop_type : 1,
- user_address : ERPUtils.convertNullToBlank(businessOrder.orderDeliveryInfo.deliverAddress),
- push_time : currentTime,
- return_flag : 511,
- multi_pickup_info: [
- [
- pickup_shop_address: storePlatform.platformShopAddress
- ]
- ]
- ]
- String postData = JSON.toJSONString(sfOrder)
- logger.info("请求数据: " + postData)
- String sign = ExpressApiSign.sfGenerateOpenSign(postData, sfAppId, sfAppKey)
- String url = sfApiUrl + "precreateorder?sign=" + sign
- CompletableFuture<String> apiResult = HttpTools.postHttpContentAsync(url,
- 5000,
- StandardCharsets.UTF_8,
- postData,
- ["Content-Type": "application/json;charset=utf-8"])
- try {
- String sfOrderResult = apiResult.get(6000, TimeUnit.SECONDS)
- def sfOrderJson = jsonSlurper.parseText(sfOrderResult)
- if (sfOrderJson["error_code"] == 0) {
- //统一返回,避免前端获取值不同
- def preDeliveryInfo = [
- errCode : 0,
- errMsg : "",
- //配送金额
- totalPrice : sfOrderJson["result"]["total_price"],
- //配送距离
- distanceMeter: sfOrderJson["result"]["delivery_distance_meter"]
- ]
- return RetResult.<InvokeCallResult> successT().result(
- InvokeCallResult.success().data(JSON.toJSONString(preDeliveryInfo))
- )
- } else {
- def preDeliveryInfo = [
- errCode : 1000,
- errMsg : sfOrderJson["error_msg"] as String,
- //配送金额
- totalPrice : 0,
- //配送距离
- distanceMeter: 0
- ]
- return RetResult.<InvokeCallResult> successT().result(
- InvokeCallResult.success().data(JSON.toJSONString(preDeliveryInfo))
- )
- }
- } catch (InterruptedException | ExecutionException | TimeoutException e) {
- logger.error(e.getMessage(), e)
- return RetResult.<InvokeCallResult> errorT().retinfo(e.getMessage())
- }
- }
- }
|