添加ec标签同步完整代码
This commit is contained in:
@@ -1,43 +0,0 @@
|
||||
package com.cool.store.job;
|
||||
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import com.cool.store.utils.StringUtil;
|
||||
import com.xxl.job.core.context.XxlJobHelper;
|
||||
import com.xxl.job.core.handler.annotation.XxlJob;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* @author hxd
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
public class EcSyncLabelJob {
|
||||
|
||||
@Value("${ec.baseUrl:null}")
|
||||
private String baseUrl;
|
||||
|
||||
@XxlJob("SyncAppletToEcJob")
|
||||
public void syncAppletToEcJob() {
|
||||
XxlJobHelper.log("-------------------------------定时同步小程序数据到ec开始-------------------------------");
|
||||
syncEcLabelExecute();
|
||||
XxlJobHelper.log("-------------------------------定时同步小程序数据到ec结束-------------------------------");
|
||||
XxlJobHelper.handleSuccess();
|
||||
}
|
||||
|
||||
|
||||
//定时同步小程序数据到ec
|
||||
private void syncEcLabelExecute() {
|
||||
// 获取参数
|
||||
String param = XxlJobHelper.getJobParam();
|
||||
String startTime = "";
|
||||
if (StringUtil.isNotEmpty(param)) {
|
||||
XxlJobHelper.log("输入参数为:" + param);
|
||||
startTime = param;
|
||||
}
|
||||
String endTime = DateUtil.now();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
package com.cool.store.job;
|
||||
|
||||
import cn.hutool.core.bean.BeanUtil;
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import com.cool.store.entity.HyPartnerBaseInfoDO;
|
||||
import com.cool.store.entity.SyncEcCustomerDO;
|
||||
import com.cool.store.entity.SyncEcCustomerLabelDO;
|
||||
import com.cool.store.mapper.HyPartnerBaseInfoMapper;
|
||||
import com.cool.store.sdk.ec.EcClient;
|
||||
import com.cool.store.sdk.ec.request.SyncEcCustomerLabelRequest;
|
||||
import com.cool.store.sdk.ec.request.SyncEcCustomerRequest;
|
||||
import com.cool.store.sdk.ec.response.SyncEcCustomerLabelResponse;
|
||||
import com.cool.store.service.HyPartnerBaseInfoService;
|
||||
import com.cool.store.utils.MybatisBatchUtils;
|
||||
import com.cool.store.utils.StringUtil;
|
||||
import com.xxl.job.core.context.XxlJobHelper;
|
||||
import com.xxl.job.core.handler.annotation.XxlJob;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* @author hxd
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
public class SyncEcCustomerLabelJob {
|
||||
|
||||
@Value("${ec.baseUrl:null}")
|
||||
private String baseUrl;
|
||||
|
||||
@Resource
|
||||
private HyPartnerBaseInfoMapper hyPartnerBaseInfoMapper;
|
||||
|
||||
@XxlJob("SyncEcCustomerLabelJob")
|
||||
public void execute() {
|
||||
XxlJobHelper.log("-------------------------------同步ec客户标签数据到小程序开始-------------------------------");
|
||||
syncEcLabelExecute();
|
||||
XxlJobHelper.log("-------------------------------同步ec标签数据到小程序结束-------------------------------");
|
||||
XxlJobHelper.handleSuccess();
|
||||
}
|
||||
|
||||
@Autowired
|
||||
private MybatisBatchUtils mybatisBatchUtils;
|
||||
|
||||
private final Integer count = 10;
|
||||
|
||||
/**
|
||||
* 同步ec标签到招商数据库中
|
||||
*/
|
||||
private void syncEcLabelExecute() {
|
||||
//获取总数
|
||||
int size = hyPartnerBaseInfoMapper.selectAllList();
|
||||
//执行数
|
||||
int counts = size / count;
|
||||
//取余,如果大于1,就再加一
|
||||
int yu = size % count;
|
||||
if (yu > 0) {
|
||||
counts += 1;
|
||||
}
|
||||
for (int i = 1; i <= counts; i++) {
|
||||
XxlJobHelper.log("执行limit1:" + count * (i - 1) + ",执行limit2:" + count);
|
||||
List<SyncEcCustomerLabelDO> list = hyPartnerBaseInfoMapper.selectListByLimit(count * (i - 1), count);
|
||||
SyncEcCustomerLabelRequest syncEcCustomerLabelRequest = new SyncEcCustomerLabelRequest();
|
||||
syncEcCustomerLabelRequest.setParameter(list);
|
||||
EcClient ecClient = new EcClient();
|
||||
SyncEcCustomerLabelResponse exec = ecClient.exec(baseUrl, syncEcCustomerLabelRequest);
|
||||
if (ObjectUtil.isNull(exec)) {
|
||||
continue;
|
||||
}
|
||||
List<SyncEcCustomerLabelDO> execList = exec.getData();
|
||||
if (CollectionUtils.isEmpty(execList)) {
|
||||
continue;
|
||||
}
|
||||
List<HyPartnerBaseInfoDO> collect = execList.stream().map((item) -> {
|
||||
HyPartnerBaseInfoDO hyPartnerBaseInfoDO = new HyPartnerBaseInfoDO();
|
||||
BeanUtil.copyProperties(item, hyPartnerBaseInfoDO);
|
||||
return hyPartnerBaseInfoDO;
|
||||
}).collect(Collectors.toList());
|
||||
mybatisBatchUtils.batchInsertOrUpdate(collect, HyPartnerBaseInfoMapper.class, (record, mapper) -> mapper.updateByMobile(record));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.cool.store.sdk.ec.request;
|
||||
|
||||
import com.cool.store.sdk.ec.core.EcRequest;
|
||||
import com.cool.store.sdk.ec.response.SyncEcCustomerLabelResponse;
|
||||
|
||||
/**
|
||||
* @author hxd
|
||||
*/
|
||||
public class SyncEcCustomerLabelRequest extends EcRequest<SyncEcCustomerLabelResponse> {
|
||||
|
||||
@Override
|
||||
public String getApiUrl() {
|
||||
return "/ec/appletToEcLabel";
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Class<SyncEcCustomerLabelResponse> getResponseClass() {
|
||||
return SyncEcCustomerLabelResponse.class;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.cool.store.sdk.ec.response;
|
||||
|
||||
|
||||
import com.cool.store.entity.SyncEcCustomerLabelDO;
|
||||
import com.cool.store.sdk.ec.core.EcResponse;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author hxd
|
||||
*/
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@Data
|
||||
@Accessors(chain = true)
|
||||
public class SyncEcCustomerLabelResponse extends EcResponse {
|
||||
|
||||
private List<SyncEcCustomerLabelDO> data;
|
||||
|
||||
}
|
||||
@@ -111,7 +111,8 @@ public class LabelServiceImpl implements LabelService {
|
||||
|
||||
@Override
|
||||
public void addEcLabel(HyPartnerLabelDO hyPartnerLabelDO) {
|
||||
hyPartnerLabelDO.setEditDate(new Date()).setCreateTime(new Date()).setEditUserId(createUserId).setCreateUserId(createUserId);
|
||||
hyPartnerLabelDO.setEditDate(new Date()).setCreateTime(new Date()).setEditUserId(createUserId).setCreateUserId(createUserId)
|
||||
.setUpdateUserId(createUserId);
|
||||
labelMapper.insertSelective(hyPartnerLabelDO);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
package com.cool.store.utils;
|
||||
|
||||
import org.apache.ibatis.session.ExecutorType;
|
||||
import org.apache.ibatis.session.SqlSession;
|
||||
import org.apache.ibatis.session.SqlSessionFactory;
|
||||
import org.mybatis.spring.SqlSessionFactoryBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.List;
|
||||
import java.util.function.BiConsumer;
|
||||
|
||||
@Component
|
||||
@ConditionalOnClass({SqlSessionFactory.class, SqlSessionFactoryBean.class})
|
||||
public class MybatisBatchUtils {
|
||||
|
||||
/**
|
||||
* 每次处理500条
|
||||
*/
|
||||
private static final int BATCH_SIZE = 500;
|
||||
|
||||
@Resource
|
||||
private SqlSessionFactory sqlSessionFactory;
|
||||
|
||||
/**
|
||||
* 批量处理修改或者插入
|
||||
*
|
||||
* @param data 需要被处理的数据
|
||||
* @param mapperClass Mybatis的Mapper类
|
||||
* @param consumer 自定义处理逻辑
|
||||
* @return int 影响的总行数
|
||||
* @author renwd
|
||||
*/
|
||||
public <T, U> int batchInsertOrUpdate(List<T> data, Class<U> mapperClass, BiConsumer<T, U> consumer) {
|
||||
int i = 1;
|
||||
SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH);
|
||||
try {
|
||||
U mapper = sqlSession.getMapper(mapperClass);
|
||||
int size = data.size();
|
||||
for (T element : data) {
|
||||
consumer.accept(element, mapper);
|
||||
if ((i % BATCH_SIZE == 0) || i == size) {
|
||||
sqlSession.flushStatements();
|
||||
}
|
||||
i++;
|
||||
}
|
||||
sqlSession.commit();
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
} finally {
|
||||
sqlSession.close();
|
||||
}
|
||||
return i - 1;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user