|
|
@@ -0,0 +1,267 @@
|
|
|
+import com.dderp.business.dao.OrderDao
|
|
|
+import com.dderp.common.api.BusinessExecutor
|
|
|
+import com.dderp.common.api.StoreService
|
|
|
+import com.dderp.common.api.SupplierInitService
|
|
|
+import com.dderp.common.api.flycat.DouyinService
|
|
|
+import com.dderp.common.api.flycat.OrderSearchService
|
|
|
+import com.dderp.common.api.flycat.OrderService
|
|
|
+import com.dderp.common.api.flycat.OrderStepService
|
|
|
+import com.dderp.common.datas.ERPModule
|
|
|
+import com.dderp.common.datas.ESKeys
|
|
|
+import com.dderp.common.datas.ReadOrderOption
|
|
|
+import com.dderp.common.datas.flycat.BusinessOrderStatus
|
|
|
+import com.dderp.common.datas.flycat.PlatformType
|
|
|
+import com.dderp.common.entity.base.InvokeCallParams
|
|
|
+import com.dderp.common.entity.base.InvokeCallResult
|
|
|
+import com.dderp.common.entity.geo.GeoPoiLocation
|
|
|
+import com.dderp.common.entity.order.BusinessOrder
|
|
|
+import com.dderp.common.entity.order.OrderDeliveryInfo
|
|
|
+import com.dderp.common.entity.order.OrderStep
|
|
|
+import com.dderp.common.entity.site.ERPTokenUser
|
|
|
+import com.dderp.common.entity.store.StorePlatform
|
|
|
+import com.dySweetFishPlugin.elasticsearch.ESClient
|
|
|
+import com.dySweetFishPlugin.sql.TableIdService
|
|
|
+import com.dySweetFishPlugin.sql.dao.TunaService
|
|
|
+import com.sweetfish.convert.json.JsonConvert
|
|
|
+import com.sweetfish.service.RetResult
|
|
|
+import groovy.json.JsonSlurper
|
|
|
+import org.apache.logging.log4j.LogManager
|
|
|
+import org.apache.logging.log4j.Logger
|
|
|
+import org.elasticsearch.action.bulk.BulkRequestBuilder
|
|
|
+import org.elasticsearch.action.bulk.BulkResponse
|
|
|
+import org.elasticsearch.action.index.IndexRequestBuilder
|
|
|
+import org.elasticsearch.action.support.WriteRequest
|
|
|
+import org.elasticsearch.action.update.UpdateRequestBuilder
|
|
|
+import org.elasticsearch.common.xcontent.XContentType
|
|
|
+import org.rex.RMap
|
|
|
+
|
|
|
+import javax.annotation.Resource
|
|
|
+import java.math.RoundingMode
|
|
|
+import java.time.LocalDateTime
|
|
|
+import java.time.ZoneOffset
|
|
|
+
|
|
|
+import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder
|
|
|
+
|
|
|
+@SuppressWarnings("unused")
|
|
|
+class BE_Order_DeliveryChange_DYLK implements BusinessExecutor<InvokeCallParams, InvokeCallResult> {
|
|
|
+
|
|
|
+ private final Logger logger = LogManager.getLogger(this.getClass().getSimpleName())
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private JsonConvert jsonConvert
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private TableIdService tableIdService
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private StoreService storeService
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private ESClient esClient
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private SupplierInitService supplierInitService
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private OrderStepService orderStepService
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private OrderService orderService
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private OrderSearchService orderSearchService
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private DouyinService douyinService
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private TunaService tunaService
|
|
|
+
|
|
|
+ private OrderDao orderDao
|
|
|
+
|
|
|
+ @Override
|
|
|
+ String scriptName() {
|
|
|
+ return "[抖音来客]订单配送信息修改"
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ ERPModule module() {
|
|
|
+ return ERPModule.ORDER_API
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ RetResult<InvokeCallParams> beforeExecute(InvokeCallParams source) {
|
|
|
+ return super.beforeExecute(source)
|
|
|
+ }
|
|
|
+
|
|
|
+ RetResult<InvokeCallResult> execute(InvokeCallParams source) {
|
|
|
+ //秒级时间戳,groovy里面不让用system
|
|
|
+ long currentTime = LocalDateTime.now().toEpochSecond(ZoneOffset.ofHours(8))
|
|
|
+ String dataSourceId = source.dataSourceId
|
|
|
+ long supplierCode = source.supplierCode
|
|
|
+ ERPTokenUser currentUser = source.currentUser
|
|
|
+
|
|
|
+ def jsonSlurper = new JsonSlurper()
|
|
|
+ def invokeOrder = jsonSlurper.parseText(source.params)
|
|
|
+ String incomePlatformOrderCode = invokeOrder["order_id"] as String
|
|
|
+ String platformStoreId = invokeOrder["poi_id"] as String
|
|
|
+ String platformAccountId = invokeOrder["account_id"] as String
|
|
|
+ ArrayList<Integer> options = invokeOrder["op_list"] as ArrayList
|
|
|
+
|
|
|
+ StorePlatform storePlatform = storeService.getStorePlatformByInvokeInfo(platformStoreId, "DYLK", PlatformType.order.value, supplierCode)
|
|
|
+ if (storePlatform == null) return RetResult.<InvokeCallResult> errorT().retinfo("订单来源平台未知!")
|
|
|
+ if (storePlatform.enableStatue == 0) {
|
|
|
+ logger.info("[" + storePlatform.platformName + "]门店[" + storePlatform.platformStoreId + "]已被禁用,消息已忽略")
|
|
|
+ return RetResult.<InvokeCallResult> errorT().retinfo("[" + storePlatform.platformName + "]门店[" + storePlatform.platformStoreId + "]已被禁用")
|
|
|
+ }
|
|
|
+
|
|
|
+ RMap searchParams = new RMap();
|
|
|
+ searchParams.set("incomePlatformId", storePlatform.id)
|
|
|
+ searchParams.set("incomePlatformOrderCode", incomePlatformOrderCode)
|
|
|
+
|
|
|
+ RetResult<List<BusinessOrder>> orderResult = orderSearchService.searchBusinessOrderList(searchParams, currentUser, dataSourceId, supplierCode, ReadOrderOption.ORDER_DELIVERY)
|
|
|
+ if (!orderResult.isSuccess()) return RetResult.<InvokeCallResult> errorT().retinfo(orderResult.retinfo)
|
|
|
+
|
|
|
+ BusinessOrder esOrder = orderResult.result.get(0) //这么查只可能有一个
|
|
|
+ OrderDeliveryInfo orderDeliveryInfo = esOrder.orderDeliveryInfo
|
|
|
+
|
|
|
+ //这里的逻辑是,抖音给了我订单配送信息改变的通知,然后我再次调用订单查询接口获取最新的订单信息
|
|
|
+ //不得不说略显傻B
|
|
|
+ def postBody = [
|
|
|
+ order_id : incomePlatformOrderCode,
|
|
|
+ account_id: platformAccountId
|
|
|
+ ]
|
|
|
+ String responseStr = douyinService.getInvoker("/goodlife/v1/hermes/trade/order/query/", jsonConvert.convertTo(postBody))
|
|
|
+ def response = jsonSlurper.parseText(responseStr)
|
|
|
+
|
|
|
+ StringBuilder sb = new StringBuilder()
|
|
|
+ if (response["extra"]["error_code"] as Integer == 0) {
|
|
|
+ def dyOrder = (response["data"]["orders"] as ArrayList).get(0)
|
|
|
+
|
|
|
+ if (options.contains(1)) {
|
|
|
+ sb.append("收货人:" + orderDeliveryInfo.receiveMan + "->" + dyOrder["receiver_info"]["receiver_name"] as String)
|
|
|
+ }
|
|
|
+ if (options.contains(2)) {
|
|
|
+ sb.append("收货人电话:" + orderDeliveryInfo.contractPhone + "->" + dyOrder["receiver_info"]["secret_number"] as String + "(" + dyOrder["receiver_info"]["receiver_phone"] as String + ")")
|
|
|
+ }
|
|
|
+
|
|
|
+ String address = "[" + dyOrder["receiver_info"]["city"] + "]" + dyOrder["receiver_info"]["province"] +
|
|
|
+ dyOrder["receiver_info"]["city"] +
|
|
|
+ dyOrder["receiver_info"]["district"] +
|
|
|
+ dyOrder["receiver_info"]["town"] +
|
|
|
+ dyOrder["receiver_info"]["location_address"] +
|
|
|
+ dyOrder["receiver_info"]["location_name"] + "-" +
|
|
|
+ dyOrder["receiver_info"]["receiver_name"]
|
|
|
+
|
|
|
+ if (options.contains(3) ||
|
|
|
+ options.contains(4) ||
|
|
|
+ options.contains(5) ||
|
|
|
+ options.contains(6) ||
|
|
|
+ options.contains(7) ||
|
|
|
+ options.contains(8) ||
|
|
|
+ options.contains(9)) {
|
|
|
+ sb.append("收货地址:" + esOrder.deliverAddressAll + "->" + address)
|
|
|
+ }
|
|
|
+
|
|
|
+ //抖音传过来的格式为起始时间戳(单位秒)- 截止时间戳(单位秒),示例:1669370006-1669371206,目前先取起始时间
|
|
|
+ long suggestTimeLong = (dyOrder["order"]["sys_expect_time"] as String).split("-")[0] as long
|
|
|
+
|
|
|
+ orderDeliveryInfo.setDeliverProvince(dyOrder["receiver_info"]["province"] as String)
|
|
|
+ orderDeliveryInfo.setDeliverCity(dyOrder["receiver_info"]["city"] as String)
|
|
|
+ orderDeliveryInfo.setDeliverDistract(dyOrder["receiver_info"]["district"] as String)
|
|
|
+ orderDeliveryInfo.setDeliverAddress(dyOrder["receiver_info"]["location_address"] as String)
|
|
|
+ orderDeliveryInfo.setGeoPoiLocation(new GeoPoiLocation(dyOrder["receiver_info"]["lng"] as double, dyOrder["receiver_info"]["lat"] as double))
|
|
|
+ orderDeliveryInfo.setGeoLocationSerial(jsonConvert.convertTo(orderDeliveryInfo.geoPoiLocation))
|
|
|
+ orderDeliveryInfo.setReceiveMan(dyOrder["receiver_info"]["receiver_name"] as String)
|
|
|
+ orderDeliveryInfo.setContractPhone(dyOrder["receiver_info"]["secret_number"] as String + "(" + dyOrder["receiver_info"]["receiver_phone"] as String + ")")
|
|
|
+ orderDeliveryInfo.setDeliverTimeliness(dyOrder["order"]["is_book"] as int - 1) //抖音1表示即时单,2表示预订单
|
|
|
+ orderDeliveryInfo.setBookingDeliverTimeLong(suggestTimeLong * 1000)
|
|
|
+ orderDeliveryInfo.setBookingDeliverTime(new Date(orderDeliveryInfo.bookingDeliverTimeLong))
|
|
|
+ orderDeliveryInfo.setCustomerMemo(dyOrder["order"]["remark"] as String)
|
|
|
+
|
|
|
+ BigDecimal deliverFee = new BigDecimal((dyOrder["amount_info"]["freight_pay_amount"] as int) / 100).setScale(2, RoundingMode.CEILING)
|
|
|
+ esOrder.setDeliverFee(deliverFee)
|
|
|
+
|
|
|
+ esOrder.setDeliverAddressAll(address)
|
|
|
+
|
|
|
+ } else {
|
|
|
+ //todo 抖音侧调用失败,怎么处理?
|
|
|
+ logger.error("[抖音来客]获取订单信息接口调用失败:" + response["extra"]["description"] as String)
|
|
|
+ return RetResult.<InvokeCallResult> errorT().retinfo("[抖音来客]获取订单信息接口调用失败:" + response["extra"]["description"] as String)
|
|
|
+ }
|
|
|
+
|
|
|
+ OrderDeliveryInfo.update(orderDeliveryInfo, currentUser.id)
|
|
|
+ BusinessOrder.update(esOrder, currentUser.id)
|
|
|
+
|
|
|
+
|
|
|
+ OrderStep orderStep = orderStepService.parseOrderStep(esOrder.id, "订单修改信息",
|
|
|
+ sb.toString(), currentUser)
|
|
|
+
|
|
|
+
|
|
|
+ //持久化
|
|
|
+ //订单主表
|
|
|
+ BulkRequestBuilder bulkRequest = esClient.getClient().prepareBulk().setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE)
|
|
|
+
|
|
|
+ UpdateRequestBuilder orderRequest = esClient.getClient().prepareUpdate(
|
|
|
+ supplierInitService.getDateYearESIndex(supplierCode, ESKeys.ES_DELIVER_BUSINESS_ORDER_INDEX, 0),
|
|
|
+ ESKeys.ES_DELIVER_BUSINESS_ORDER_TYPE,
|
|
|
+ String.valueOf(esOrder.getId()))
|
|
|
+ .setDoc(jsonBuilder()
|
|
|
+ .startObject()
|
|
|
+ .field("deliverFee", esOrder.deliverFee)
|
|
|
+ .field("deliverAddressAll", esOrder.deliverAddressAll)
|
|
|
+ .field("updateBy", esOrder.getUpdateBy())
|
|
|
+ .field("updateTimeLong", esOrder.getUpdateTimeLong())
|
|
|
+ .endObject())
|
|
|
+ bulkRequest.add(orderRequest)
|
|
|
+
|
|
|
+ //订单配送信息
|
|
|
+ UpdateRequestBuilder deliverRequest = esClient.getClient().prepareUpdate(
|
|
|
+ supplierInitService.getDateYearESIndex(supplierCode, ESKeys.ES_DELIVER_BUSINESS_ORDER_INDEX, 0),
|
|
|
+ ESKeys.ES_DELIVER_ORDER_DELIVERY_INFO_TYPE,
|
|
|
+ String.valueOf(orderDeliveryInfo.getId()))
|
|
|
+ .setRouting(String.valueOf(esOrder.id))
|
|
|
+ .setDoc(jsonBuilder()
|
|
|
+ .startObject()
|
|
|
+ .field("deliverProvince", orderDeliveryInfo.deliverProvince)
|
|
|
+ .field("deliverCity", orderDeliveryInfo.deliverCity)
|
|
|
+ .field("deliverDistract", orderDeliveryInfo.deliverDistract)
|
|
|
+ .field("deliverAddress", orderDeliveryInfo.deliverAddress)
|
|
|
+ .field("geoLocationSerial", orderDeliveryInfo.geoLocationSerial)
|
|
|
+ .field("receiveMan", orderDeliveryInfo.receiveMan)
|
|
|
+ .field("contractPhone", orderDeliveryInfo.contractPhone)
|
|
|
+ .field("deliverTimeliness", orderDeliveryInfo.deliverTimeliness)
|
|
|
+ .field("bookingDeliverTimeLong", orderDeliveryInfo.bookingDeliverTimeLong)
|
|
|
+ .field("updateBy", orderDeliveryInfo.getUpdateBy())
|
|
|
+ .field("updateTimeLong", orderDeliveryInfo.getUpdateTimeLong())
|
|
|
+ .endObject())
|
|
|
+ bulkRequest.add(deliverRequest)
|
|
|
+
|
|
|
+ //订单脚印
|
|
|
+ IndexRequestBuilder orderStepRequest = esClient.getClient()
|
|
|
+ .prepareIndex(supplierInitService.getDateYearESIndex(supplierCode, ESKeys.ES_DELIVER_ORDER_STEP_INDEX, 0),
|
|
|
+ ESKeys.ES_DELIVER_ORDER_STEP_TYPE)
|
|
|
+ .setId(String.valueOf(orderStep.id))
|
|
|
+ .setSource(jsonConvert.convertTo(orderStep), XContentType.JSON)
|
|
|
+ bulkRequest.add(orderStepRequest)
|
|
|
+
|
|
|
+
|
|
|
+ BulkResponse bulkResponse = bulkRequest.get();
|
|
|
+ orderDao = tunaService.generate(OrderDao.class)
|
|
|
+ if (bulkResponse.hasFailures()) {
|
|
|
+ logger.error("订单修改信息出错:" + bulkResponse.buildFailureMessage());
|
|
|
+ return RetResult.<InvokeCallResult> errorT().retinfo("订单修改信息出错:" + esOrder.orderName);
|
|
|
+ } else {
|
|
|
+ logger.info("订单修改信息成功");
|
|
|
+ //写数据库
|
|
|
+ orderDao.updateOrderDelivery(esOrder,
|
|
|
+ orderDeliveryInfo,
|
|
|
+ [orderStep],
|
|
|
+ dataSourceId,
|
|
|
+ String.valueOf(supplierCode))
|
|
|
+
|
|
|
+ return RetResult.<InvokeCallResult> successT()
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+}
|