import java.util.List; import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.google.common.collect.Lists; @Service public class AuthUserService extends MpServiceImpl<AuthUserMapper, AuthUser> @Autowired private OldAuthUserMapper oldAuthUserMapper; // 返回变化的 public Integer sync() Wrapper<OldAuthUser> queryWrapper = Wrappers.query(); List<OldAuthUser> oldList = oldAuthUserMapper.selectList(queryWrapper); AuthUser query = new AuthUser(); query.setIsDeleted(YnEnum.NO.getCode()); List<AuthUser> dbList = super .list(query); Map<Integer, AuthUser> dbUserMap = ListKit.listToMap(dbList, AuthUser::getOldId); List<AuthUser> changedList = Lists.newArrayList(); for (OldAuthUser oldAuthUser : oldList) try
one(dbUserMap, changedList, oldAuthUser); catch (Exception e) log.error( "AuthUserService sync error" , e);
return changedList.size();
private void one(Map<Integer, AuthUser> dbUserMap, List<AuthUser> newList, OldAuthUser oldAuthUser) Integer oldId = oldAuthUser.getId(); AuthUser dbUser = dbUserMap.get(oldId); if (dbUser != null ) boolean changed = changed(oldAuthUser, dbUser); if (changed) AuthUser newDbUser = newDbUser(oldAuthUser, dbUser); newList.add(newDbUser); super .updateById(newDbUser);
else
AuthUser saveDbUser = saveDbUser(oldAuthUser); newList.add(saveDbUser); super .save(saveDbUser);
private AuthUser newDbUser(OldAuthUser oldAuthUser, AuthUser dbUser) AuthUser newDbUser = BeanKit.build(oldAuthUser, AuthUser. class ); newDbUser.setId(dbUser.getId()); newDbUser.setUpdateTime(DateKit.now()); return newDbUser;
private static boolean changed(OldAuthUser oldAuthUser, AuthUser dbUser) // str if (StringKit.notEquals(oldAuthUser.getUsername(), dbUser.getUsername())) return true ;
if (StringKit.notEquals(oldAuthUser.getPassword(), dbUser.getPassword())) return true ;
if (StringKit.notEquals(oldAuthUser.getLastName(), dbUser.getLastName())) return true ;
if (StringKit.notEquals(oldAuthUser.getFirstName(), dbUser.getFirstName())) return true ;
if (StringKit.notEquals(oldAuthUser.getEmail(), dbUser.getEmail())) return true ;
// int if (EqualsKit.notEquals(oldAuthUser.getIsActive(), dbUser.getIsActive())) return true ;
if (EqualsKit.notEquals(oldAuthUser.getIsStaff(), dbUser.getIsStaff())) return true ;
if (EqualsKit.notEquals(oldAuthUser.getIsSuperuser(), dbUser.getIsSuperuser())) return true ;
// date if (EqualsKit.notEquals(oldAuthUser.getDateJoined(), dbUser.getDateJoined())) return true ;
if (EqualsKit.notEquals(oldAuthUser.getLastLogin(), dbUser.getLastLogin())) return true ;
return false ;
private AuthUser saveDbUser(OldAuthUser oldAuthUser) AuthUser authUser = BeanKit.build(oldAuthUser, AuthUser. class ); authUser.setId( null ); authUser.setOldId(oldAuthUser.getId()); authUser.setCreateTime(DateKit.now()); authUser.setUpdateTime(DateKit.now()); authUser.setIsDeleted(YnEnum.NO.getCode()); return authUser;
|