|
|
@@ -0,0 +1,213 @@
|
|
|
+import com.alibaba.fastjson2.JSON
|
|
|
+import com.dderp.business.dao.StoreDao
|
|
|
+import com.dderp.common.api.BusinessExecutor
|
|
|
+import com.dderp.common.api.NoSqlKeysService
|
|
|
+import com.dderp.common.api.PlatformService
|
|
|
+import com.dderp.common.api.StoreService
|
|
|
+import com.dderp.common.datas.ERPModule
|
|
|
+import com.dderp.common.datas.ESKeys
|
|
|
+import com.dderp.common.datas.flycat.PlatformType
|
|
|
+import com.dderp.common.entity.base.InvokeCallResult
|
|
|
+import com.dderp.common.entity.base.ProcessStringItem
|
|
|
+import com.dderp.common.entity.inquire.InquireMainInfo
|
|
|
+import com.dderp.common.entity.platform.PlatformInfo
|
|
|
+import com.dderp.common.entity.site.ERPTokenUser
|
|
|
+import com.dderp.common.entity.store.StoreInfo
|
|
|
+import com.dderp.common.entity.store.StorePlatform
|
|
|
+import com.dderp.common.entity.store.StorePlatformRequire
|
|
|
+import com.dderp.common.entity.store.ViewStoreInfo
|
|
|
+import com.dderp.common.http.HttpTools
|
|
|
+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.support.WriteRequest
|
|
|
+import org.elasticsearch.action.update.UpdateRequestBuilder
|
|
|
+import org.elasticsearch.common.xcontent.XContentType
|
|
|
+
|
|
|
+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
|
|
|
+
|
|
|
+import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder
|
|
|
+
|
|
|
+@SuppressWarnings("unused")
|
|
|
+class BE_Store_BindStorePlatform_SFTC implements BusinessExecutor<ProcessStringItem, StorePlatform> {
|
|
|
+
|
|
|
+ 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
|
|
|
+ private JsonConvert jsonConvert
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private TableIdService tableIdService
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private StoreService storeService
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private PlatformService platformService
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private ESClient esClient
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private NoSqlKeysService keysService
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private TunaService tunaService
|
|
|
+
|
|
|
+ private StoreDao storeDao
|
|
|
+
|
|
|
+ @Override
|
|
|
+ String scriptName() {
|
|
|
+ return "顺丰同城授权并绑定门店"
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ ERPModule module() {
|
|
|
+ return ERPModule.STORE_API
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ void start(long supplierCode) {
|
|
|
+ storeDao = tunaService.generate(StoreDao.class)
|
|
|
+ }
|
|
|
+
|
|
|
+ RetResult<StorePlatform> execute(ProcessStringItem source) {
|
|
|
+ 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 invokeContent = jsonSlurper.parseText(source.itemValue)
|
|
|
+
|
|
|
+ long idStore = invokeContent["out_shop_id"] as Long
|
|
|
+ StoreInfo storeInfo = storeService.getStoreInfo(idStore, supplierCode, false, false, false)
|
|
|
+ if (storeInfo == null) return RetResult.<StorePlatform> errorT().retinfo("门店信息不存在")
|
|
|
+
|
|
|
+ PlatformInfo platformInfo = platformService.getPlatformInfoByCode("SFTC", true, supplierCode)
|
|
|
+ if (platformInfo == null) return RetResult.<StorePlatform> errorT().retinfo("系统档案内未找到[顺丰同城]平台")
|
|
|
+
|
|
|
+ //推送过来的数据不足,需要通过 获取门店信息 接口获取
|
|
|
+ def sfStoreRequest = [
|
|
|
+ dev_id : sfAppId,
|
|
|
+ shop_id : invokeContent["shop_id"] as String,
|
|
|
+ push_time : currentTime,
|
|
|
+
|
|
|
+ ]
|
|
|
+ String postData = JSON.toJSONString(sfStoreRequest)
|
|
|
+ String sign = ExpressApiSign.sfGenerateOpenSign(postData, sfAppId, sfAppKey)
|
|
|
+ String url = sfApiUrl + "getshopinfo?sign=" + sign
|
|
|
+
|
|
|
+ CompletableFuture<String> apiResult = HttpTools.postHttpContentAsync(url,
|
|
|
+ 5000,
|
|
|
+ StandardCharsets.UTF_8,
|
|
|
+ postData,
|
|
|
+ ["Content-Type": "application/json;charset=utf-8"])
|
|
|
+ try {
|
|
|
+ String sfStoreResult = apiResult.get(6000, TimeUnit.SECONDS)
|
|
|
+ logger.info(sfStoreResult)
|
|
|
+ def sfStoreJson = jsonSlurper.parseText(sfStoreResult)
|
|
|
+
|
|
|
+ if (sfStoreJson["error_code"] as int == 0) {
|
|
|
+ //查询之前绑定过这个门店没有
|
|
|
+ StorePlatform esStorePlatform = storeService.getStorePlatformByInvokeInfo(invokeContent["shop_id"] as String,
|
|
|
+ "SFTC", PlatformType.delivery.value, supplierCode)
|
|
|
+
|
|
|
+ if (esStorePlatform != null) {
|
|
|
+ //之前已绑定过,重新启用即可
|
|
|
+ logger.info("[" + platformInfo.platformName + "]门店已绑定过[" + invokeContent["shop_id"] as String + "],重新启用")
|
|
|
+
|
|
|
+ esStorePlatform.enableStatue = 1
|
|
|
+ StorePlatform.update(esStorePlatform, currentUser.id)
|
|
|
+ try {
|
|
|
+ esClient.getClient().prepareUpdate(keysService.getESKey(ESKeys.ES_DELIVER_STORE_INFO_INDEX, supplierCode), ESKeys.ES_DELIVER_STORE_PLATFORM_TYPE,
|
|
|
+ String.valueOf(esStorePlatform.getId()))
|
|
|
+ .setDoc(jsonBuilder()
|
|
|
+ .startObject()
|
|
|
+ .field("enableStatue", esStorePlatform.enableStatue)
|
|
|
+ .field("updateBy", esStorePlatform.getUpdateBy())
|
|
|
+ .field("updateTimeLong", esStorePlatform.getUpdateTimeLong())
|
|
|
+ .endObject())
|
|
|
+ .get()
|
|
|
+
|
|
|
+
|
|
|
+ storeDao.enableStorePlatform(esStorePlatform, dataSourceId, supplierCode)
|
|
|
+ return RetResult.<StorePlatform>successT().result(esStorePlatform)
|
|
|
+ } catch (IOException e) {
|
|
|
+ logger.error("绑定门店重新启用出错:" + e.getMessage(), e)
|
|
|
+ return RetResult.<StorePlatform> errorT().retcode(-1).retinfo("绑定门店重新启用出错:" + e.getMessage())
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ //之前未绑定过,需要新开
|
|
|
+ StorePlatform storePlatform = new StorePlatform(
|
|
|
+ id: tableIdService.getTableIdMulti("tbStorePlatform.id", 1, dataSourceId, String.valueOf(supplierCode)),
|
|
|
+ idStore: idStore,
|
|
|
+ platformStoreId: invokeContent["shop_id"] as String,
|
|
|
+ platformShopName: sfStoreJson["result"]["shop_info"]["shop_name"],
|
|
|
+ platformShopAddress: sfStoreJson["result"]["shop_info"]["path_name"] + sfStoreJson["result"]["shop_info"]["shop_address"],
|
|
|
+ platformShopPhone: sfStoreJson["result"]["shop_info"]["shop_contact_phone"],
|
|
|
+ platformShopGeoLat: sfStoreJson["result"]["shop_info"]["latitude"],
|
|
|
+ platformShopGeoLng: sfStoreJson["result"]["shop_info"]["longitude"],
|
|
|
+ idPlatformInfo: platformInfo.id,
|
|
|
+ platformName: platformInfo.platformName,
|
|
|
+ platformCode: platformInfo.platformCode,
|
|
|
+ platformType: platformInfo.platformType,
|
|
|
+ enableStatue: 1
|
|
|
+ )
|
|
|
+ StorePlatform.create(storePlatform, currentUser.id)
|
|
|
+
|
|
|
+ BulkRequestBuilder bulkRequest = esClient.getClient().prepareBulk().setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE)
|
|
|
+
|
|
|
+ bulkRequest.add(esClient.getClient().prepareIndex(
|
|
|
+ keysService.getESKey(ESKeys.ES_DELIVER_STORE_INFO_INDEX, supplierCode), ESKeys.ES_DELIVER_STORE_PLATFORM_TYPE)
|
|
|
+ .setId(String.valueOf(storePlatform.getId()))
|
|
|
+ .setParent(String.valueOf(storePlatform.idStore))
|
|
|
+ .setSource(source.getConvert("storePlatformWriteConvert").convertTo(storePlatform), XContentType.JSON))
|
|
|
+
|
|
|
+ BulkResponse bulkResponse = bulkRequest.get()
|
|
|
+
|
|
|
+ if (bulkResponse.hasFailures()) {
|
|
|
+ logger.error("门店开通平台出错:" + bulkResponse.buildFailureMessage())
|
|
|
+ return RetResult.<StorePlatform> errorT().retinfo("门店开通平台出错:" + storePlatform.getPlatformName())
|
|
|
+ } else {
|
|
|
+ logger.info("门店开通平台成功")
|
|
|
+ //写数据库
|
|
|
+ storeDao.addStorePlatform(storePlatform, storePlatform.getRequireList(), dataSourceId, supplierCode)
|
|
|
+ return RetResult.<StorePlatform>successT().result(storePlatform)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ return RetResult.<StorePlatform> errorT().retinfo("无法获取顺丰同城店铺信息")
|
|
|
+ }
|
|
|
+ } catch (InterruptedException | ExecutionException | TimeoutException e) {
|
|
|
+ logger.error(e.getMessage(), e)
|
|
|
+
|
|
|
+ return RetResult.<StorePlatform> errorT().retinfo(e.getMessage())
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+}
|