基于mybatisplus的数据同步

发布时间:2024年01月16日

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.genepoint.master.pojo.message.MessageRsp;
import com.genepoint.master.pojo.storage.bo.req.RackStorageSyncReq;
import com.genepoint.master.web.request.RestPageRequest;
import com.genepoint.master.web.result.RestPageResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
public abstract class DataCheckService<M extends BaseMapper<T>, T,K> extends ServiceImpl<M, T> {
public abstract void delSyncData(String onlineEqCode, String userName);

public abstract MessageRsp<RestPageResponse<K>> getSyncPage(String eqCode, String userName, RestPageRequest<?> req);


public abstract LambdaQueryWrapper<T> getUpdateWrapper(T data);

public abstract LambdaQueryWrapper<T> getSelectListWrapper(List<K> list, String eqCode);

public abstract String getKey(T data);

public abstract T getNewData();

public abstract String getSkadiKey(K data);

public void checkSyncData(String onlineEqCode, String userName) {
try {
RestPageRequest<RackStorageSyncReq> req = new RestPageRequest<RackStorageSyncReq>();
int index = 1;
req.setIndex(index);
req.setSize(2000);
req.setCondition(new RackStorageSyncReq());
MessageRsp<RestPageResponse<K>> syncPage = getSyncPage(onlineEqCode, userName, req);
RestPageResponse<K> result = syncPage.getResult();
findDiffData(onlineEqCode, result.getRecords());
index++;
for (; index <= result.getPages(); index++) {
req.setIndex(index);
syncPage = getSyncPage(onlineEqCode, userName, req);
result = syncPage.getResult();
findDiffData(onlineEqCode, result.getRecords());
}
} catch (Exception e) {
log.error("", e);
}
}

private void findDiffData(String eqCode, List<K> list) {
try {
List<T> addData = new ArrayList<>();
List<T> updateData = new ArrayList<>();
List<T> delData = new ArrayList<>();
List<T> dbList = this.list(getSelectListWrapper(list, eqCode));
Map<String, T> dbMap = new HashMap<>();
if (CollectionUtil.isNotEmpty(dbList)) {
for (T data : dbList) {
dbMap.put(getKey(data), data);
}
}
if (CollectionUtil.isNotEmpty(list)) {

for ( K k: list) {
T data = dbMap.get(getSkadiKey(k));
if (data == null) {
data = getNewData();
BeanUtils.copyProperties(k, data);
addData.add(data);
} else {
T target = getNewData();
BeanUtils.copyProperties(k, target);
if (!target.equals(data)) {
updateData.add(target);
}
}
dbMap.remove(getKey(data));
}
for (Map.Entry<String , T> entry : dbMap.entrySet()) {
delData.add(entry.getValue());
}
}
dealDiffData(addData,updateData,delData);
}catch (Exception e){
log.error("",e);
}

}

private void dealDiffData(List<T> addData, List<T> updateData, List<T> delData) {
if (CollectionUtil.isNotEmpty(addData)) {
this.saveBatch(addData);
}
if (CollectionUtil.isNotEmpty(updateData)) {
for (T data : updateData) {
this.update(data, getUpdateWrapper(data));
}
}
if (CollectionUtil.isNotEmpty(delData)) {
for (T data : delData) {
this.remove(getUpdateWrapper(data));
}
}
}


}

/**

@Resource
private List<DataCheckService> checkServices;

for (DataCheckService checkService : checkServices) {
checkService.delSyncData(onlineEqCode, user.getName());
checkService.checkSyncData(onlineEqCode, user.getName());
}**/

文章来源:https://blog.csdn.net/qq_15875773/article/details/135627000
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。