@Override
public void modifyOrCreateLocationInfo(Query query, Update update) throws DataAccessException {
this.mongoTemplate.upsert(query, update, LocationInfo.class);
}
@Override
public void modifyOrCreateLocationInfo(LocationInfo locationInfo) throws DataAccessException {
Update update = new Update();
Assert.hasText(locationInfo.getPushChannelId(), "用户App的channelId为空");
Assert.notNull(locationInfo.getActor(), "修改地理位置上传参数角色为空");
update.set("pushChannelId", locationInfo.getPushChannelId());
if (!Strings.isNullOrEmpty(locationInfo.getUserId())) {
update.set("userId", locationInfo.getUserId());
}
if (!Strings.isNullOrEmpty(locationInfo.getPushUserId())) {
update.set("pushUserId", locationInfo.getPushUserId());
}
if (null != locationInfo.getCoordinate()) {
update.set("coordinate", locationInfo.getCoordinate());
}
if (null != locationInfo.getActor()) {
update.set("actor", locationInfo.getActor());
}
if (null != locationInfo.getUserStatus()) {
update.set("userStatus", locationInfo.getUserStatus());
}
if (null != locationInfo.getOnlineState()) {
update.set("onlineState", locationInfo.getOnlineState());
}
if (null == locationInfo.getDistance()) {
update.setOnInsert("distance", 50);
} else {
update.set("distance", locationInfo.getDistance());
}
if (null != locationInfo.getDeviceType()) {
update.set("deviceType", locationInfo.getDeviceType());
}
Criteria criteria = Criteria.where("pushChannelId")
.is(locationInfo.getPushChannelId())
.and("actor").is(locationInfo.getActor());
Query query = new Query(criteria);
logger.debug("增加或修改一条位置记录到数据库 {} {}", query.toString(), update.toString());
this.modifyOrCreateLocationInfo(query, update);
}
转载于:https://my.oschina.net/linwenbin/blog/420062