Commit fefbae53 by yinyong

shopify平台费用半成品代码提交

parent 617b8c70
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>data-base</artifactId>
<groupId>com.bailuntec</groupId>
<version>1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>base-sync-shopify</artifactId>
<name>base-sync-shopify</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>com.bailuntec</groupId>
<artifactId>data-common</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<archive>
<manifest>
<mainClass>com.bailuntec.Application</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<appendAssemblyId>false</appendAssemblyId>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
<!--默认是true-->
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
</resource>
</resources>
</build>
</project>
package com.bailuntec;
import com.alibaba.druid.pool.DruidDataSource;
import com.bailuntec.job.ShopifySyncJob;
import com.bailuntec.listener.ShopifySyncJobListener;
import com.bailuntec.utils.PropertiesUtil;
import com.dangdang.ddframe.job.config.JobCoreConfiguration;
import com.dangdang.ddframe.job.config.simple.SimpleJobConfiguration;
import com.dangdang.ddframe.job.event.JobEventConfiguration;
import com.dangdang.ddframe.job.event.rdb.JobEventRdbConfiguration;
import com.dangdang.ddframe.job.lite.api.JobScheduler;
import com.dangdang.ddframe.job.lite.config.LiteJobConfiguration;
import com.dangdang.ddframe.job.reg.base.CoordinatorRegistryCenter;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperConfiguration;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter;
import lombok.extern.slf4j.Slf4j;
import javax.sql.DataSource;
@Slf4j
public class Application {
private static PropertiesUtil propertiesUtil = PropertiesUtil.getInstance("job");
private static final String EVENT_RDB_STORAGE_DRIVER = propertiesUtil.getPropertyAsString("EVENT_RDB_STORAGE_DRIVER");
private static final String EVENT_RDB_STORAGE_URL = propertiesUtil.getPropertyAsString("EVENT_RDB_STORAGE_URL");
private static final String EVENT_RDB_STORAGE_USERNAME = propertiesUtil.getPropertyAsString("EVENT_RDB_STORAGE_USERNAME");
private static final String EVENT_RDB_STORAGE_PASSWORD = propertiesUtil.getPropertyAsString("EVENT_RDB_STORAGE_PASSWORD");
public static void main(String[] args) {
new JobScheduler(createRegistryCenter(), createJobConfiguration(),createJobEventConfiguration(), new ShopifySyncJobListener()).init();
}
private static CoordinatorRegistryCenter createRegistryCenter() {
CoordinatorRegistryCenter regCenter = new ZookeeperRegistryCenter(new ZookeeperConfiguration(propertiesUtil.getPropertyAsString("ZOOKEEPER_SERVER"), propertiesUtil.getPropertyAsString("NAME_SPACE")));
regCenter.init();
return regCenter;
}
private static LiteJobConfiguration createJobConfiguration() {
JobCoreConfiguration simpleCoreConfig = JobCoreConfiguration.newBuilder(propertiesUtil.getPropertyAsString("JOB_NAME"), propertiesUtil.getPropertyAsString("JOB_CRON"), propertiesUtil.getPropertyAsInt("SHARDING_TOTAL_COUNT")).build();
SimpleJobConfiguration simpleJobConfig = new SimpleJobConfiguration(simpleCoreConfig, ShopifySyncJob.class.getCanonicalName());
LiteJobConfiguration simpleJobRootConfig = LiteJobConfiguration.newBuilder(simpleJobConfig).build();
return simpleJobRootConfig;
}
private static JobEventConfiguration createJobEventConfiguration() {
JobEventConfiguration jobEventRdbConfig = new JobEventRdbConfiguration(setUpEventTraceDataSource());
return jobEventRdbConfig;
}
private static DataSource setUpEventTraceDataSource() {
DruidDataSource result = new DruidDataSource();
result.setDriverClassName(EVENT_RDB_STORAGE_DRIVER);
result.setUrl(EVENT_RDB_STORAGE_URL);
result.setUsername(EVENT_RDB_STORAGE_USERNAME);
result.setPassword(EVENT_RDB_STORAGE_PASSWORD);
return result;
}
}
package com.bailuntec.domain;
import lombok.Data;
@Data
public class ShopifyAuth {
private String PrivateAppName;
private String APIKey;
private String Password;
private String Hostname;
private String Version;
private String SharedSecret;
private String StorefrontAccessToken;
}
package com.bailuntec.job;
import com.alibaba.fastjson.JSON;
import com.bailuntec.domain.ShopifyAuth;
import com.bailuntec.domain.entity.DcBaseCompanyAccount;
import com.bailuntec.domain.entity.JobAccountLog;
import com.bailuntec.domain.entity.JobPointLog;
import com.bailuntec.domain.enumerate.AccountPlatformType;
import com.bailuntec.domain.example.DcBaseCompanyAccountExample;
import com.bailuntec.domain.example.JobAccountLogExample;
import com.bailuntec.mapper.DcBaseCompanyAccountMapper;
import com.bailuntec.mapper.JobAccountLogMapper;
import com.bailuntec.support.PointJob;
import com.bailuntec.utils.OkHttpUtil;
import com.bailuntec.utils.PropertiesUtil;
import com.bailuntec.utils.SessionUtil;
import com.dangdang.ddframe.job.api.ShardingContext;
import lombok.extern.slf4j.Slf4j;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.apache.commons.beanutils.BeanUtils;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.LinkedHashMap;
import java.util.List;
@Slf4j
public class ShopifySyncJob extends PointJob {
private OkHttpClient okHttpClient = OkHttpUtil.getInstance();
private PropertiesUtil propertiesUtil = PropertiesUtil.getInstance("const");
@Override
public void executeJob(ShardingContext shardingContext, JobPointLog jobPointLog) {
int totalPage = getCount();
try{
JobAccountLogMapper jobAccountLogMapper = SessionUtil.getSession().getMapper(JobAccountLogMapper.class);
int pageSize = totalPage % shardingContext.getShardingTotalCount() == 0 ? totalPage / shardingContext.getShardingTotalCount() : totalPage / shardingContext.getShardingTotalCount() + 1;
List<JobAccountLog> listByPage = jobAccountLogMapper.getListByPage(AccountPlatformType.Shopify.code(),pageSize * shardingContext.getShardingItem(), pageSize);
if(listByPage != null && listByPage.size() > 0) {
for (JobAccountLog jobAccountLog : listByPage) {
LocalDateTime localDateTime = jobAccountLog.getStartTime();
do{
if (jobAccountLog.getId() == null) {
BeanUtils.copyProperties(jobAccountLog, jobPointLog);
jobAccountLog.setId(null);
}
handleCampaign(jobAccountLog);
localDateTime = localDateTime.plusDays(1L);
}while (!localDateTime.isAfter(jobAccountLog.getEndTime()));
jobAccountLog.setStartTime(jobAccountLog.getEndTime());
jobAccountLog.setEndTime(jobAccountLog.getStartTime().plusDays(jobAccountLog.getIntervalTime()).isAfter(LocalDateTime.now()) ? LocalDateTime.now() : jobAccountLog.getEndTime().plusDays(jobAccountLog.getIntervalTime()));
jobAccountLogMapper = SessionUtil.getSession().getMapper(JobAccountLogMapper.class);
if (jobAccountLog.getId() == null) {
jobAccountLogMapper.insertSelective(jobAccountLog);
} else {
jobAccountLog.setBjModified(LocalDateTime.now());
jobAccountLogMapper.updateByExampleSelective(jobAccountLog, JobAccountLogExample.newAndCreateCriteria().andAccountIdEqualTo(jobAccountLog.getAccountId()).andJobNameEqualTo(jobAccountLog.getJobName()).example());
}
}
}
}catch (Exception e){
e.printStackTrace();
log.warn("Shio获取广告费接口错误", e);
}
}
private void handleCampaign(JobAccountLog jobAccountLog) {
LinkedHashMap<String, String> map = new LinkedHashMap<>();
DcBaseCompanyAccount dcBaseCompanyAccount = getToken(jobAccountLog);
ShopifyAuth shopifyAuth = JSON.parseObject(dcBaseCompanyAccount.getAuthJson(), ShopifyAuth.class);
String endUrl = propertiesUtil.getPropertyAsString("SHOPIFY_URL");
String str = OkHttpUtil.attachHttpGetParams("https://"+shopifyAuth.getAPIKey()+"/"+shopifyAuth.getPassword()+"/"+shopifyAuth.getHostname()+"/"+ endUrl, map);
String urlPath = "https://"+shopifyAuth.getAPIKey()+"/"+shopifyAuth.getPassword()+"/"+shopifyAuth.getHostname()+ endUrl;
Response response = null;
String palResultStr = null;
try {
Request request = new Request.Builder()
.get()
.url(urlPath)
.addHeader("Content-Type", "application/json")
.build();
response = okHttpClient.newCall(request).execute();
palResultStr = response.body().string();
} catch (IOException e) {
throw new RuntimeException(urlPath + "请求wish接口失败" + response, e);
} finally {
if (response != null) {
response.close();
}
}
System.out.println("======"+ palResultStr);
}
private DcBaseCompanyAccount getToken(JobAccountLog jobAccountLog) {
DcBaseCompanyAccountMapper mapper = SessionUtil.getSession().getMapper(DcBaseCompanyAccountMapper.class);
return mapper.selectOneByExample(DcBaseCompanyAccountExample.newAndCreateCriteria().andCompanyIdEqualTo(jobAccountLog.getCompanyId()).andAccountIdEqualTo(jobAccountLog.getAccountId()).example());
}
private int getCount() {
int i = 0;
try {
DcBaseCompanyAccountMapper accountMapper = SessionUtil.getSession().getMapper(DcBaseCompanyAccountMapper.class);
i = (int)accountMapper.countByExample(DcBaseCompanyAccountExample.newAndCreateCriteria().andPlatformIdEqualTo(AccountPlatformType.Shopify.code()).example());
} catch (Exception e) {
e.printStackTrace();
} finally {
SessionUtil.closeSession();
}
return i;
}
}
package com.bailuntec.listener;
import com.dangdang.ddframe.job.executor.ShardingContexts;
import com.dangdang.ddframe.job.lite.api.listener.ElasticJobListener;
public class ShopifySyncJobListener implements ElasticJobListener {
@Override
public void beforeJobExecuted(ShardingContexts shardingContexts) {
}
@Override
public void afterJobExecuted(ShardingContexts shardingContexts) {
}
}
SHOPIFY_URL=/admin/api/2019-10/shopify_payments/payouts.json
ZOOKEEPER_SERVER=94.191.111.219:2181
EVENT_RDB_STORAGE_DRIVER=com.mysql.jdbc.Driver
EVENT_RDB_STORAGE_URL=jdbc:mysql://gz-cdb-kp7s5i79.sql.tencentcdb.com:61691/bailun_datacenter?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull
EVENT_RDB_STORAGE_USERNAME=root
EVENT_RDB_STORAGE_PASSWORD=#7kfnymAM$Y9-Ntf
#EVENT_RDB_STORAGE_DRIVER=com.mysql.jdbc.Driver
#EVENT_RDB_STORAGE_URL=jdbc:mysql://10.0.8.15:3306/bailun_datacenter?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8
#EVENT_RDB_STORAGE_USERNAME=root
#EVENT_RDB_STORAGE_PASSWORD=#7kfnymAM$Y9-Ntf
#ZOOKEEPER_SERVER=172.31.255.120:2181
NAME_SPACE=data-center
JOB_NAME=base-sync-shopify
JOB_CRON=0/1 * * * * ? *
SHARDING_TOTAL_COUNT=1
\ No newline at end of file
package com.bailuntec;
import com.bailuntec.domain.entity.JobAccountLog;
import com.bailuntec.domain.entity.JobPointLog;
import com.bailuntec.domain.enumerate.AccountPlatformType;
import com.bailuntec.domain.example.JobAccountLogExample;
import com.bailuntec.job.ShopifySyncJob;
import com.bailuntec.mapper.JobAccountLogMapper;
import com.bailuntec.utils.SessionUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.BeanUtils;
import org.junit.jupiter.api.Test;
import java.lang.reflect.Method;
import java.time.LocalDateTime;
import java.util.List;
@Slf4j
public class ApplicationTest {
@Test
public void test() {
JobAccountLogMapper jobAccountLogMapper = SessionUtil.getSession().getMapper(JobAccountLogMapper.class);
JobPointLog jobPointLog = new JobPointLog("base-sync-shopify", 1, 100, 1, 1, LocalDateTime.of(2019, 10, 01, 00, 00, 00), LocalDateTime.of(2019, 11, 28, 00, 00, 00));
List<JobAccountLog> listByPage = jobAccountLogMapper.getListByPage(AccountPlatformType.Shopify.code(),0, 100);
try{
Class clazz = Class.forName("com.bailuntec.job.ShopifySyncJob");
Method generateReportId = clazz.getDeclaredMethod("handleCampaign", JobAccountLog.class);
generateReportId.setAccessible(true);
if(listByPage != null && listByPage.size() > 0) {
for (JobAccountLog jobAccountLog : listByPage) {
LocalDateTime localDateTime = jobAccountLog.getStartTime();
do{
if (jobAccountLog.getId() == null) {
BeanUtils.copyProperties(jobAccountLog, jobPointLog);
jobAccountLog.setId(null);
}
generateReportId.invoke(clazz.newInstance(), jobAccountLog);
localDateTime = localDateTime.plusDays(1L);
}while (!localDateTime.isAfter(jobAccountLog.getEndTime()));
jobAccountLog.setStartTime(jobAccountLog.getEndTime());
jobAccountLog.setEndTime(jobAccountLog.getStartTime().plusDays(jobAccountLog.getIntervalTime()).isAfter(LocalDateTime.now()) ? LocalDateTime.now() : jobAccountLog.getEndTime().plusDays(jobAccountLog.getIntervalTime()));
jobAccountLogMapper = SessionUtil.getSession().getMapper(JobAccountLogMapper.class);
if (jobAccountLog.getId() == null) {
jobAccountLogMapper.insertSelective(jobAccountLog);
} else {
jobAccountLog.setBjModified(LocalDateTime.now());
jobAccountLogMapper.updateByExampleSelective(jobAccountLog, JobAccountLogExample.newAndCreateCriteria().andAccountIdEqualTo(jobAccountLog.getAccountId()).andJobNameEqualTo(jobAccountLog.getJobName()).example());
}
}
}
}catch (Exception e){
e.printStackTrace();
}
}
@Test
public void test1() {
System.out.println(restart(1));
}
public long restart(int restartNum) {
try{
if(restartNum == 2) {
return 10;
}
int i = 10/0;
return 20;
}catch (Exception e){
if(restartNum == 5) {
System.out.println("结束了。。。。");
return restartNum;
}else{
restart(restartNum + 1);
System.out.println("重跑第:"+restartNum+"次");
return restartNum;
}
}
}
}
...@@ -31,6 +31,7 @@ ...@@ -31,6 +31,7 @@
<module>base-sync-finance-wish</module> <module>base-sync-finance-wish</module>
<module>base-sync-cashier</module> <module>base-sync-cashier</module>
<module>base-sync-semi-order</module> <module>base-sync-semi-order</module>
<module>base-sync-shopify</module>
</modules> </modules>
<packaging>pom</packaging> <packaging>pom</packaging>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment