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.master.pojo.message.MessageRsp;
import com..master.pojo.storage.bo.req.RackStorageSyncReq;
import com.master.web.request.RestPageRequest;
import com.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());
}**/