jinpengyong
2024-02-02 28923b3d67c58b032bc4ff128aa1d5e62d73dba1
screen-api/src/main/java/com/moral/api/service/impl/SpecialDeviceServiceImpl.java
@@ -4,20 +4,25 @@
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.moral.api.entity.HistorySecondCruiser;
import com.moral.api.entity.Organization;
import com.moral.api.entity.SpecialDevice;
import com.moral.api.entity.SpecialDeviceHistory;
import com.moral.api.entity.TbDaily;
import com.moral.api.config.Interceptor.UserHelper;
import com.moral.api.entity.*;
import com.moral.api.mapper.DailyMapper;
import com.moral.api.mapper.HistorySecondCruiserMapper;
import com.moral.api.mapper.SpecialDeviceMapper;
import com.moral.api.mapper.SysDictTypeMapper;
import com.moral.api.pojo.bo.ExcelBO;
import com.moral.api.pojo.dto.historySecondCruiser.HistorySecondCruiserListDTO;
import com.moral.api.pojo.dto.historySecondCruiser.HistorySecondCruiserResultCountDTO;
import com.moral.api.pojo.dto.historySecondCruiser.HistorySecondCruiserResultDTO;
import com.moral.api.pojo.enums.SysDictTypeEnum;
import com.moral.api.pojo.vo.device.AppDeviceVo;
import com.moral.api.pojo.vo.excel.DailyVo;
import com.moral.api.pojo.vo.user.QxUser;
import com.moral.api.service.OrganizationService;
import com.moral.api.service.SpecialDeviceHistoryService;
import com.moral.api.service.SpecialDeviceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moral.api.service.SysDictTypeService;
import com.moral.constant.Constants;
import com.moral.util.AmendUtils;
@@ -26,25 +31,21 @@
import com.moral.util.GeodesyUtils;
import com.moral.util.TokenUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.*;
import java.util.stream.Collectors;
/**
@@ -60,6 +61,9 @@
    @Autowired
    private HistorySecondCruiserMapper HistorySecondCruiserMapper;
    @Autowired
    private SysDictTypeService sysDictTypeService;
    @Autowired
    private SpecialDeviceHistoryService specialDeviceHistoryService;
@@ -84,26 +88,41 @@
    private final static Double dis = 50d;
    private final static Double DIS_NEW = 100d;
    @Override
    public List<Map<String, Object>> selectCruisers() {
        //获取当前用户信息
        Map<String, Object> userInfo = (Map<String, Object>) TokenUtils.getUserInfo();
        Map<String, Object> orgInfo = (Map<String, Object>) userInfo.get("organization");
        Integer userId = (Integer) userInfo.get("userId");
        List<Integer> integerList = new ArrayList<>();
        SysDictData listSys = sysDictTypeService.listOne(SysDictTypeEnum.SYS_SECOND_CRUISER_RANGE.getValue(),userId.toString());
        if(listSys.getDataValue().contains(",")){
            List<Integer> resultStr = Arrays.asList(listSys.getDataValue().split(",")).stream().map(Integer::parseInt).collect(Collectors.toList());
            integerList.addAll(resultStr);
        }else {
            Integer dataValue = Objects.nonNull(listSys.getDataValue())?Integer.parseInt(listSys.getDataValue()):0;
            integerList.add(dataValue);
        }
        Integer orgId = (Integer) orgInfo.get("id");
        //获取所有子组织
        List<Organization> organizations = organizationService.getChildrenOrganizationsById(orgId);
        List<Integer> orgIds = organizations.stream().map(Organization::getId).collect(Collectors.toList());
        orgIds.add(orgId);
        integerList.addAll(orgIds);
        QueryWrapper<SpecialDeviceHistory> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("mac", "name")
        queryWrapper.select("DISTINCT mac, name").lambda();
                //.eq("special_type", Constants.SPECIAL_DEVICE_CRUISER)
                .eq("is_delete", Constants.NOT_DELETE)
                .in("organization_id", orgIds);
        queryWrapper.eq("is_delete", Constants.NOT_DELETE)
                .in("organization_id", integerList);
        return specialDeviceHistoryService.listMaps(queryWrapper);
    }
    @Override
    public List<Map<String, Object>> carTrajectory(Map<String, Object> params) {
    public List<Map<String, Object>> carTrajectory(Map<String, Object> params,boolean type) {
        params.put("dateFormat", "%Y-%m-%d %H:%i:%s");
        Map<String, Object> userInfo = (Map<String, Object>) TokenUtils.getUserInfo();
        Map<String, Object> orgInfo = (Map<String, Object>) userInfo.get("organization");
@@ -141,6 +160,76 @@
            map.put("a21004",String.valueOf(Double.parseDouble(a21004) / 10));
        }*/
        return filterData(data);
    }
    @Override
    public List<HistorySecondCruiserResultDTO> carTrajectoryNewAvg(Map<String, Object> params) {
        double range = Double.valueOf(params.get("range").toString());
        String type = params.get("type").toString();
        params.put("dateFormat", "%Y-%m-%d %H:%i:%s");
        Map<String, Object> userInfo = (Map<String, Object>) TokenUtils.getUserInfo();
        Integer userId = (Integer) userInfo.get("userId");
        Map<String, Object> orgInfo = (Map<String, Object>) userInfo.get("organization");
        Integer orgId = (Integer) orgInfo.get("id");
        List<Integer> integerList = new ArrayList<>();
        SysDictData listSys = sysDictTypeService.listOne(SysDictTypeEnum.SYS_SECOND_CRUISER_RANGE.getValue(),userId.toString());
        if(listSys.getDataValue().contains(",")){
            List<Integer> resultStr = Arrays.asList(listSys.getDataValue().split(",")).stream().map(Integer::parseInt).collect(Collectors.toList());
            integerList.addAll(resultStr);
        }else {
            Integer dataValue = Objects.nonNull(listSys.getDataValue())?Integer.parseInt(listSys.getDataValue()):0;
            integerList.add(dataValue);
        }
        integerList.add(orgId);
        params.put("orgIds", integerList);
        params.put("type","$."+type);
        //从秒数据表获取走航车数据
        List<HistorySecondCruiserListDTO> data = HistorySecondCruiserMapper.getCruiserDataNewAvg(params);
        data.removeIf(o->{
            if (ObjectUtils.isEmpty(o.getFlyLon()) || ObjectUtils.isEmpty(o.getFlyLat())) {
                return true;
            }
            double lon = o.getFlyLon();
            double lat = o.getFlyLat();
            if (lon < 70 || lon > 150 || lat < 20 || lat > 60) {
                return true;
            }
            return false;
        });
        if(CollectionUtils.isEmpty(data)){
            return null;
        }
        List<HistorySecondCruiserResultDTO> result = filterDataNew(data,range);
        if(CollectionUtils.isNotEmpty(result)){
            SysDictData list = sysDictTypeService.listOne(SysDictTypeEnum.SYS_SECOND_CRUISER.getValue(),type);
            if(list.getDataValue().contains(",")){
                List<String> resultStr = Arrays.asList(list.getDataValue().split(","));
                if(resultStr.size() % 2 ==0){
                    result.forEach(it->{
                        it.setAvgNum(numAvg(resultStr,it.getAvgNum()));
                    });
                }
            }else {
                BigDecimal dataValue = Objects.nonNull(list.getDataValue())?BigDecimal.valueOf(Double.parseDouble(list.getDataValue())):BigDecimal.ZERO;
                result.forEach(it->{
                    it.setAvgNum(it.getAvgNum().add(dataValue));
                });
            }
        }
        return result;
    }
    private BigDecimal numAvg(List<String> list , BigDecimal num){
        int nums = 1;
        for (int i=0;i<list.size();i=i+2){
            if(num.compareTo(BigDecimal.valueOf(Double.parseDouble(list.get(i))))>= 0 ){
                return num.add(BigDecimal.valueOf(Double.parseDouble(list.get(i+1))));
            }
            nums+=2;
        }
        return num;
    }
    @Override
@@ -182,7 +271,10 @@
        String time2 = params.get("time2").toString();
        String rsTime = getTime(time1, time2);
        list.add(rsTime);
        List<Map<String, Object>> maps = specialDeviceService.carTrajectory(params);
//        List<Map<String, Object>> maps = specialDeviceService.carTrajectory(params,false);
//        List<Map<String, Object>> maps = HistorySecondCruiserMapper.getCruiserData(params);
        List<Map<String, Object>> maps = HistorySecondCruiserMapper.getAllCruiserData(params);
        if (ObjectUtils.isEmpty(maps)){
            return null;
        }
@@ -194,7 +286,8 @@
            map.put("time1",time3);
            map.put("time2",time4);
            map.put("mac",mac);
            List<Map<String, Object>> maps1 = specialDeviceService.carTrajectory(map);
//            List<Map<String, Object>> maps1 = specialDeviceService.carTrajectory(map,false);
            List<Map<String, Object>> maps1 = HistorySecondCruiserMapper.getAllCruiserData(params);
            if (ObjectUtils.isEmpty(maps1)){
                return null;
            }
@@ -210,7 +303,8 @@
            map.put("time1",time5);
            map.put("time2",time6);
            map.put("mac",mac);
            List<Map<String, Object>> maps1 = specialDeviceService.carTrajectory(map);
//            List<Map<String, Object>> maps1 = specialDeviceService.carTrajectory(map,false);
            List<Map<String, Object>> maps1 = HistorySecondCruiserMapper.getAllCruiserData(params);
            if (ObjectUtils.isEmpty(maps1)){
                return null;
            }
@@ -416,6 +510,20 @@
        return excelBOS;
    }
    @Override
    public List<AppDeviceVo> selectSpecialDevice() {
        QxUser user = UserHelper.getCurrentUser();
        Integer organizationId = user.getOrganizationId();
//        Integer organizationId=71;
        List<AppDeviceVo> appDeviceVos;
        if (organizationId!=24){
            appDeviceVos = specialDeviceMapper.selectSpecialDevice(organizationId);
        }else {
            appDeviceVos = specialDeviceMapper.selectSpecialDevice(null);
        }
        return appDeviceVos;
    }
    //计算平均值和最大值
    private HashMap<String, Object> getStringObjectHashMap(List<Map<String, Object>> maps) {
        HashMap<String, Object> rsMap = new HashMap<>();
@@ -430,20 +538,28 @@
        for (Map<String, Object> map : maps) {
//            String flylon = map.get("flylon").toString();
//            String flylat = map.get("flylat").toString();
//            String s = flylon + "-" + flylat;
//            if (Double.parseDouble(map.get("a34004").toString())>200){
//
//            }
            pm25List.add(Double.parseDouble(map.get("a34004").toString()));
            pm10List.add(Double.parseDouble(map.get("a34002").toString()));
            COList.add(Double.parseDouble(map.get("a21005").toString()));
            SO2List.add(Double.parseDouble(map.get("a21026").toString()));
            NO2List.add(Double.parseDouble(map.get("a21004").toString()));
            O3List.add(Double.parseDouble(map.get("a05024").toString()));
            VOCList.add(Double.parseDouble(map.get("a99054").toString()));
            Map value = JSON.parseObject(map.get("value").toString(), Map.class);
            if (!ObjectUtils.isEmpty(value.get("a34004"))){
                pm25List.add(Double.parseDouble(value.get("a34004").toString()));
            }
            if (!ObjectUtils.isEmpty(value.get("a34002"))){
                pm10List.add(Double.parseDouble(value.get("a34002").toString()));
            }
            if (!ObjectUtils.isEmpty(value.get("a21005"))){
                COList.add(Double.parseDouble(value.get("a21005").toString()));
            }
            if (!ObjectUtils.isEmpty(value.get("a21026"))){
                SO2List.add(Double.parseDouble(value.get("a21026").toString()));
            }
            if (!ObjectUtils.isEmpty(value.get("a21004"))){
                NO2List.add(Double.parseDouble(value.get("a21004").toString()));
            }
            if (!ObjectUtils.isEmpty(value.get("a05024"))){
                O3List.add(Double.parseDouble(value.get("a05024").toString()));
            }
            if (!ObjectUtils.isEmpty(value.get("a99054"))){
                VOCList.add(Double.parseDouble(value.get("a99054").toString()));
            }
        }
        //获取平均值
@@ -495,6 +611,174 @@
                result.add(map);
            }
        }
        return result;
    }
    //范围取均值
    private List<Map<String, Object>> filterData(List<Map<String, Object>> data, List<Map<String, Object>> result) {
        List<Map<String, Object>> list = new ArrayList<>();
        Map<String,List<Object>> map = new HashMap<>();
        result.forEach(it->map.put(it.get(Constants.SENSOR_CODE_LON).toString()+ "_"+Constants.SENSOR_CODE_LAT.toString(),Arrays.asList(it)));
        // 坐标在50 以内分组
        for (Map<String, Object> mapData : data) {
            for (Map.Entry<String, List<Object>> entry : map.entrySet()) {
                double lng1 = Double.parseDouble(mapData.get(Constants.SENSOR_CODE_LON).toString());
                double lat1 = Double.parseDouble(mapData.get(Constants.SENSOR_CODE_LAT).toString());
                String[] mapKey = entry.getKey().split("_");
                List<Object> mapValue = entry.getValue();
                double lng2 = Double.parseDouble(mapKey[0]);
                double lat2 = Double.parseDouble(mapKey[1]);
                double distance = GeodesyUtils.getDistance(lat1, lng1, lat2, lng2);
                if (distance < dis) {
                    mapValue.add(mapData);
                }
            }
        }
        return list;
    }
    //根据距离筛选数据
    private List<HistorySecondCruiserResultDTO> filterDataNew(List<HistorySecondCruiserListDTO> data,double range) {
        List<HistorySecondCruiserListDTO> list = new ArrayList<>();
        Map<Integer, HistorySecondCruiserResultCountDTO> map = new HashMap<>();
        Map<String, BigDecimal> mapTest = new HashMap<>();
        Integer integer = 1;
        HistorySecondCruiserResultCountDTO countDTO  = new HistorySecondCruiserResultCountDTO();
        countDTO.setSumNum(data.get(0).getNum());
        countDTO.setContNum(1);
        map.put(integer,countDTO);
        list.add(0,data.remove(0));
        mapTest.put(data.get(0).getFlyLon()+"_"+data.get(0).getFlyLat(),data.get(0).getNum());
        int k =1;
        for (HistorySecondCruiserListDTO d : data) {
            boolean flag = true;
            for (HistorySecondCruiserListDTO l : list) {
                double distance = GeodesyUtils.getDistance(d.getFlyLat(), d.getFlyLon(), l.getFlyLat(), l.getFlyLon());
                if (distance < range) {
                    flag = false;
                    if(integer.equals(list.size())&& !mapTest.containsKey(d.getFlyLon()+"_"+d.getFlyLat())){
                        HistorySecondCruiserResultCountDTO countResultDTO = Objects.isNull(map.get(integer))?new HistorySecondCruiserResultCountDTO():map.get(integer);
                        countResultDTO.setContNum(countResultDTO.getContNum()+1);
                        countResultDTO.setSumNum(countResultDTO.getSumNum().add(d.getNum()));
                        map.put(integer,countResultDTO);
                        mapTest.put(d.getFlyLon()+"_"+d.getFlyLat(),d.getNum());
                        k++;
                    }
                }
            }
            if (flag) {
                if(!list.contains(d)){
                    list.add(d);
                    integer++;
                    HistorySecondCruiserResultCountDTO countResultDTO = Objects.isNull(map.get(integer))?new HistorySecondCruiserResultCountDTO():map.get(integer);
                    countResultDTO.setSumNum(d.getNum());
                    countResultDTO.setContNum(1);
                    map.put(integer,countResultDTO);
                    mapTest.put(d.getFlyLon()+"_"+d.getFlyLat(),d.getNum());
                }
            }
        }
        if(!list.contains(data.get(data.size()-1))){
            HistorySecondCruiserListDTO result = data.get(data.size()-1);
            list.add(result);
            integer++;
            HistorySecondCruiserResultCountDTO countResultDTO = new HistorySecondCruiserResultCountDTO();
            countResultDTO.setContNum(1);
            countResultDTO.setSumNum(result.getNum());
            map.put(integer,countResultDTO);
        }
        return filterDataNe(list,map);
    }
    private List<HistorySecondCruiserResultDTO> filterDataNe(List<HistorySecondCruiserListDTO> list,Map<Integer, HistorySecondCruiserResultCountDTO> map){
        List<HistorySecondCruiserResultDTO> listResult = new ArrayList<>();
        for(Map.Entry<Integer, HistorySecondCruiserResultCountDTO> entry : map.entrySet()){
            Integer mapKey = entry.getKey();
            HistorySecondCruiserResultCountDTO mapValue = entry.getValue();
            BigDecimal avgNum = mapValue.getSumNum().divide(BigDecimal.valueOf(mapValue.getContNum()),2,BigDecimal.ROUND_HALF_UP);
            HistorySecondCruiserResultDTO resultDTO = new HistorySecondCruiserResultDTO();
            resultDTO.setTime(list.get(mapKey-1).getTime());
            resultDTO.setAvgNum(avgNum);
            resultDTO.setFlyLon(list.get(mapKey-1).getFlyLon());
            resultDTO.setFlyLat(list.get(mapKey-1).getFlyLat());
            listResult.add(resultDTO);
        }
    return listResult;
    }
    private List<HistorySecondCruiserResultDTO> filterDataNew(List<HistorySecondCruiserListDTO> data, List<HistorySecondCruiserListDTO> list,double range) {
        List<HistorySecondCruiserResultDTO> listResult = new ArrayList<>();
        List<HistorySecondCruiserListDTO> filter = new ArrayList<>();
        for (HistorySecondCruiserListDTO d : data) {
            for (HistorySecondCruiserListDTO l : list) {
                double distance = GeodesyUtils.getDistance(d.getFlyLat(), d.getFlyLon(), l.getFlyLat(), l.getFlyLon());
                if (filter.contains(d)) {
                    break;
                }
                if (distance < range ) {
                    List<BigDecimal> numList = new ArrayList<>(CollectionUtils.isEmpty(l.getNumList()) ? Arrays.asList(l.getNum()) : l.getNumList());
                    numList.add(d.getNum());
                    l.setNumList(numList);
                    filter.add(d);
                    break;
                }
            }
        }
        int k = 0;
        for(int j =0;j<list.size(); j++){
            HistorySecondCruiserListDTO l = list.get(j);
            HistorySecondCruiserResultDTO resultDTO = new HistorySecondCruiserResultDTO();
            resultDTO.setFlyLat(l.getFlyLat());
            resultDTO.setTime(l.getTime());
            resultDTO.setFlyLon(l.getFlyLon());
            BigDecimal num = BigDecimal.ZERO;
            int count = l.getNumList().size();
            for(int i = 0;i < count; i++) {
                num = num.add(l.getNumList().get(i));
            }
            k = k+count;
            resultDTO.setAvgNum(num.divide(BigDecimal.valueOf(count),2,BigDecimal.ROUND_HALF_UP));
            listResult.add(resultDTO);
        }
        System.out.println("sss>>>>:"+ (k++));
        System.out.println("sss>>>>:"+ (filter.size())+">>>>:"+data.size());
        int z = 0 ;
        return listResult;
    }
    /**
     *
     */
      private double getDivisor(String code,double num){
          double rs = 0.0;
        if (code.contains("-")){
            String data = code.substring(1);
            rs = num - Double.parseDouble(data);
        } else if (code.contains("*")){
            String data = code.substring(1);
            rs = num * Double.parseDouble(data);
        } else if (code.contains("/")){
            String data = code.substring(1);
            rs = num / Double.parseDouble(data);
        } else if (code.contains(",")){
            String[] split = code.split(",");
            for (String s : split) {
                String[] split1 = s.split("<");
                double sp1 = Double.parseDouble(split1[0]);
                double sp2 = Double.parseDouble(split1[1]);
                double sp3 = Double.parseDouble(split1[2]);
                if (num>=sp1 && num<=sp2){
                    rs = num + sp3;
                }
            }
        }else {
            rs = num + Double.parseDouble(code);
        }
        return rs;
      }
}