| | |
| | | package com.xiaopeng.pushMessage.webSocket.handler; |
| | | |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.fasterxml.jackson.core.JsonProcessingException; |
| | | import com.fasterxml.jackson.databind.ObjectMapper; |
| | | import com.xiaopeng.api.dto.account.XpOperationsUserPositionDTO; |
| | |
| | | @Slf4j |
| | | public class PositionMsgHandler implements BusinessMsgHandler{ |
| | | |
| | | @Autowired |
| | | private MongoTemplate mongoTemplate; |
| | | |
| | | |
| | | @Override |
| | | public String getMessageType() { |
| | | return "position"; |
| | |
| | | |
| | | @Override |
| | | public void handle(String uid, String msg) { |
| | | XpOperationsUserPositionDTO xpOperationsUserPositionDTO; |
| | | try { |
| | | xpOperationsUserPositionDTO = om.readValue(msg, XpOperationsUserPositionDTO.class); |
| | | } catch (JsonProcessingException e) { |
| | | throw new RuntimeException(e); |
| | | } |
| | | if (null != xpOperationsUserPositionDTO) { |
| | | xpOperationsUserPositionDTO.setId(Long.parseLong(uid)); |
| | | saveToMongo(xpOperationsUserPositionDTO); |
| | | JSONObject jsonObject = JSON.parseObject(msg); |
| | | if (null != jsonObject) { |
| | | saveToMongo(uid, jsonObject); |
| | | } |
| | | } |
| | | @Autowired ObjectMapper om; |
| | | @Autowired MongoTemplate mongoTemplate; |
| | | |
| | | private void saveToMongo(XpOperationsUserPositionDTO dto) { |
| | | XpOperationsUserPositionDTO vehiclePosition = mongoTemplate.findOne(new Query(Criteria.where("id").is(dto.getId())), XpOperationsUserPositionDTO.class); |
| | | private void saveToMongo(String uid, JSONObject jsonObject) { |
| | | Double latitude = jsonObject.getDouble("latitude"); |
| | | Double longitude = jsonObject.getDouble("longitude"); |
| | | XpOperationsUserPositionDTO vehiclePosition = mongoTemplate.findOne(new Query(Criteria.where("id").is(uid)), XpOperationsUserPositionDTO.class); |
| | | if (null == vehiclePosition) { |
| | | GeoJsonPoint location = new GeoJsonPoint(dto.getLongitude(), dto.getLatitude()); |
| | | dto.setLocation(location); |
| | | mongoTemplate.save(dto); |
| | | vehiclePosition = new XpOperationsUserPositionDTO(); |
| | | GeoJsonPoint location = new GeoJsonPoint(longitude, latitude); |
| | | vehiclePosition.setLocation(location); |
| | | vehiclePosition.setId(Long.parseLong(uid)); |
| | | vehiclePosition.setLatitude(latitude); |
| | | vehiclePosition.setLongitude(longitude); |
| | | vehiclePosition.setLocation(location); |
| | | mongoTemplate.save(vehiclePosition); |
| | | }else{ |
| | | Update update = new Update() |
| | | .set("id",dto.getId()) |
| | | .set("latitude", dto.getLatitude()) |
| | | .set("longitude", dto.getLongitude()); |
| | | double[] location = {dto.getLongitude(), dto.getLatitude()}; |
| | | .set("id", Long.parseLong(uid)) |
| | | .set("latitude", latitude) |
| | | .set("longitude", longitude); |
| | | GeoJsonPoint location = new GeoJsonPoint(longitude, latitude); |
| | | update.set("location", location); |
| | | mongoTemplate.updateFirst(new Query(Criteria.where("id").is(dto.getId())), update, XpOperationsUserPositionDTO.class); |
| | | mongoTemplate.updateFirst(new Query(Criteria.where("id").is(Long.parseLong(uid))), update, XpOperationsUserPositionDTO.class); |
| | | } |
| | | log.info("保存位置信息成功:id:{},longitude:{},latitude:{}", dto.getId(), dto.getLongitude(),dto.getLatitude()); |
| | | log.info("保存位置信息成功:id:{},longitude:{},latitude:{}", uid, longitude, latitude); |
| | | } |
| | | |
| | | } |