|  |  |  | 
|---|
|  |  |  | package com.moral.api.service.impl; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import com.alibaba.fastjson.JSONObject; | 
|---|
|  |  |  | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; | 
|---|
|  |  |  | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; | 
|---|
|  |  |  | import com.moral.api.entity.HistorySecondCruiser; | 
|---|
|  |  |  | import com.moral.api.entity.ManageCoordinateDetail; | 
|---|
|  |  |  | import com.moral.api.entity.Sensor; | 
|---|
|  |  |  | import com.moral.api.entity.SpecialDevice; | 
|---|
|  |  |  | import com.moral.api.mapper.HistorySecondCruiserMapper; | 
|---|
|  |  |  | import com.moral.api.service.DeviceService; | 
|---|
|  |  |  | import com.moral.api.mapper.ManageCoordinateDetailMapper; | 
|---|
|  |  |  | import com.moral.api.mapper.SpecialDeviceMapper; | 
|---|
|  |  |  | import com.moral.api.pojo.dto.cruiser.CruiserDTO; | 
|---|
|  |  |  | import com.moral.api.pojo.dto.cruiser.CruiserListDTO; | 
|---|
|  |  |  | import com.moral.api.service.HistorySecondCruiserService; | 
|---|
|  |  |  | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; | 
|---|
|  |  |  | import com.moral.api.service.SpecialDeviceService; | 
|---|
|  |  |  | import com.moral.api.util.RoadUtils; | 
|---|
|  |  |  | import com.moral.constant.Constants; | 
|---|
|  |  |  | import com.moral.constant.RedisConstants; | 
|---|
|  |  |  | import com.moral.util.DateUtils; | 
|---|
|  |  |  | import lombok.extern.slf4j.Slf4j; | 
|---|
|  |  |  | import org.springframework.beans.factory.annotation.Autowired; | 
|---|
|  |  |  | import org.springframework.data.redis.core.RedisTemplate; | 
|---|
|  |  |  | import org.springframework.stereotype.Service; | 
|---|
|  |  |  | import org.springframework.util.ObjectUtils; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import java.util.Date; | 
|---|
|  |  |  | import java.util.List; | 
|---|
|  |  |  | import java.util.Map; | 
|---|
|  |  |  | import java.util.*; | 
|---|
|  |  |  | import java.util.concurrent.TimeUnit; | 
|---|
|  |  |  | import java.util.regex.Matcher; | 
|---|
|  |  |  | import java.util.regex.Pattern; | 
|---|
|  |  |  | import java.util.stream.Collectors; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | 
|---|
|  |  |  | * @author moral | 
|---|
|  |  |  | * @since 2021-09-02 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | @Slf4j | 
|---|
|  |  |  | @Service | 
|---|
|  |  |  | public class HistorySecondCruiserServiceImpl extends ServiceImpl<HistorySecondCruiserMapper, HistorySecondCruiser> implements HistorySecondCruiserService { | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | private HistorySecondCruiserMapper historySecondCruiserMapper; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private DeviceService deviceService; | 
|---|
|  |  |  | private SpecialDeviceService specialDeviceService; | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private SpecialDeviceMapper specialDeviceMapper; | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private ManageCoordinateDetailMapper manageCoordinateDetailMapper; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private RedisTemplate redisTemplate; | 
|---|
|  |  |  | 
|---|
|  |  |  | }).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //数据校准 | 
|---|
|  |  |  | data = deviceService.adjustSpecialDeviceData(data); | 
|---|
|  |  |  | data = specialDeviceService.adjustSpecialDeviceData(data); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | HistorySecondCruiser historySecondCruiser = new HistorySecondCruiser(); | 
|---|
|  |  |  | historySecondCruiser.setMac(mac); | 
|---|
|  |  |  | 
|---|
|  |  |  | historySecondCruiser.setBatch(batchTime); | 
|---|
|  |  |  | historySecondCruiserMapper.insert(historySecondCruiser); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 获取所有走航车信息 | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public List<SpecialDevice> selectCruisers() { | 
|---|
|  |  |  | QueryWrapper<SpecialDevice> wrapper = new QueryWrapper<>(); | 
|---|
|  |  |  | wrapper.select("name","mac"); | 
|---|
|  |  |  | wrapper.eq("is_delete",Constants.NOT_DELETE); | 
|---|
|  |  |  | List<SpecialDevice> specialDevices = specialDeviceMapper.selectList(wrapper); | 
|---|
|  |  |  | return specialDevices; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 查看走航车轨迹 | 
|---|
|  |  |  | * @param params | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public Map<String,Object> getCruiserInFo(Map<String, Object> params) { | 
|---|
|  |  |  | HashMap<String, Object> map = new HashMap<>(); | 
|---|
|  |  |  | String mac = params.get("mac").toString(); | 
|---|
|  |  |  | String time1 = params.get("time1").toString(); | 
|---|
|  |  |  | params.put("dateFormat", "%Y-%m-%d %H:%i:%s"); | 
|---|
|  |  |  | ArrayList<Object> list = new ArrayList<>(); | 
|---|
|  |  |  | List<CruiserDTO> data = (List<CruiserDTO>) redisTemplate.opsForHash().get(RedisConstants.DATE_COORDINATE, mac + time1); | 
|---|
|  |  |  | //        List<CruiserDTO> data = (List<CruiserDTO>) redisTemplate.opsForValue().get(mac + time1); | 
|---|
|  |  |  | if (ObjectUtils.isEmpty(data)){ | 
|---|
|  |  |  | data = historySecondCruiserMapper.getCruiserInfo(params); | 
|---|
|  |  |  | data = data.stream().distinct().collect(Collectors.toList()); | 
|---|
|  |  |  | data.removeIf(o->{ | 
|---|
|  |  |  | if (ObjectUtils.isEmpty(o.getFlyLon()) || ObjectUtils.isEmpty(o.getFlyLat())) { | 
|---|
|  |  |  | return true; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | double lon = o.getFlyLon(); | 
|---|
|  |  |  | double lat = o.getFlyLat(); | 
|---|
|  |  |  | double[] doubles = RoadUtils.transformWGS84ToBD09(lon, lat); | 
|---|
|  |  |  | Matcher matcher = Pattern.compile("\\d*\\.\\d{8}").matcher(""+doubles[0]); | 
|---|
|  |  |  | matcher.find(); | 
|---|
|  |  |  | String s = matcher.group(); | 
|---|
|  |  |  | Matcher matcher1 = Pattern.compile("\\d*\\.\\d{8}").matcher(""+doubles[1]); | 
|---|
|  |  |  | matcher1.find(); | 
|---|
|  |  |  | String s1 = matcher1.group(); | 
|---|
|  |  |  | o.setFlyLon(Double.parseDouble(s)); | 
|---|
|  |  |  | o.setFlyLat(Double.parseDouble(s1)); | 
|---|
|  |  |  | o.setData(lon+"_"+lat); | 
|---|
|  |  |  | //            o.setFlyLon(doubles[0]); | 
|---|
|  |  |  | //            o.setFlyLat(doubles[1]); | 
|---|
|  |  |  | if (lon < 70 || lon > 150 || lat < 20 || lat > 60) { | 
|---|
|  |  |  | return true; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return false; | 
|---|
|  |  |  | }); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | redisTemplate.opsForHash().put(RedisConstants.DATE_COORDINATE,mac+time1,data); | 
|---|
|  |  |  | //设置过期时间 | 
|---|
|  |  |  | redisTemplate.opsForHash().getOperations().expire(RedisConstants.DATE_COORDINATE,600, TimeUnit.SECONDS); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //        redisTemplate.opsForValue().set(mac+time1,rsCruiser,600, TimeUnit.SECONDS); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | map.put("rsData",data); | 
|---|
|  |  |  | map.put("data",list); | 
|---|
|  |  |  | log.info(data.size()+""); | 
|---|
|  |  |  | log.info(data.size()+""); | 
|---|
|  |  |  | return map; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public Map<String, Object> getCruiserCompare(Map<String, Object> params) { | 
|---|
|  |  |  | HashMap<String,Object> rsMap = new HashMap<>(); | 
|---|
|  |  |  | List<CruiserListDTO> list = historySecondCruiserMapper.cruiserList(params); | 
|---|
|  |  |  | List<CruiserDTO> cruiserInfo = new ArrayList<>(); | 
|---|
|  |  |  | ArrayList<ManageCoordinateDetail> rsList1 = new ArrayList<>(); | 
|---|
|  |  |  | for(CruiserListDTO c : list){ | 
|---|
|  |  |  | if (ObjectUtils.isEmpty(c.getFlyLon()) || ObjectUtils.isEmpty(c.getFlyLat())) { | 
|---|
|  |  |  | continue; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | double[] doubles = RoadUtils.transformWGS84ToBD09(c.getFlyLon(), c.getFlyLat()); | 
|---|
|  |  |  | if(doubles.length !=2){ | 
|---|
|  |  |  | continue; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | String date = new StringBuilder().append(c.getFlyLon()).append("_").append(c.getFlyLat()).toString(); | 
|---|
|  |  |  | if(c.getState().equals(0)){ | 
|---|
|  |  |  | CruiserDTO cruiserDTO = new CruiserDTO(); | 
|---|
|  |  |  | cruiserDTO.setTime(c.getTime()); | 
|---|
|  |  |  | cruiserDTO.setFlyLat(doubles[1]); | 
|---|
|  |  |  | cruiserDTO.setFlyLon(doubles[0]); | 
|---|
|  |  |  | cruiserDTO.setData(date); | 
|---|
|  |  |  | cruiserDTO.setState("1"); | 
|---|
|  |  |  | cruiserInfo.add(cruiserDTO); | 
|---|
|  |  |  | }else { | 
|---|
|  |  |  | ManageCoordinateDetail detail = new ManageCoordinateDetail(); | 
|---|
|  |  |  | detail.setId(c.getId()); | 
|---|
|  |  |  | detail.setCoordinateId(c.getCoordinateId()); | 
|---|
|  |  |  | detail.setLongitude(doubles[0]); | 
|---|
|  |  |  | detail.setLatitude(doubles[1]); | 
|---|
|  |  |  | detail.setState(c.getState().toString()); | 
|---|
|  |  |  | detail.setCode(date); | 
|---|
|  |  |  | rsList1.add(detail); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | rsMap.put("rsData",cruiserInfo); | 
|---|
|  |  |  | rsMap.put("data",rsList1); | 
|---|
|  |  |  | rsMap.put("message","有"+cruiserInfo.size()+"个不在里面,"+"一共有"+(cruiserInfo.size()+rsList1.size())+"个"); | 
|---|
|  |  |  | return rsMap; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|