//package com.yida.data.server.service.impl;
//
//import cc.mrbird.febs.common.redis.service.RedisService;
//import cn.hutool.core.collection.CollUtil;
//import cn.hutool.core.lang.Snowflake;
//import cn.hutool.core.util.StrUtil;
//import cn.hutool.json.JSONObject;
//import com.yida.data.common.core.entity.card.EduStudentCardLocation;
//import com.yida.data.common.core.entity.constant.CachePrefixConstant;
//import com.yida.data.common.core.utils.GeoUtil;
//import com.yida.data.common.core.utils.LocationUtil;
//import com.yida.data.device.vo.studentCard.LocationVO;
//import com.yida.data.rabbit.constant.RabbitConstant;
//import com.yida.data.rabbit.util.RabbitUtil;
//import com.yida.data.server.dto.DeviceReportDTO;
//import com.yida.data.server.dto.FenceAlarmDTO;
//import com.yida.data.server.entity.BtsLocation;
//import com.yida.data.server.entity.WifimacLocation;
//import com.yida.data.server.repository.BtsLocationRespository;
//import com.yida.data.server.repository.EduStudentCardLocationRespository;
//import com.yida.data.server.repository.WifimacLocationRespository;
//import com.yida.data.server.service.DataService;
//import com.yida.data.server.service.OperationService;
//import java.time.LocalDateTime;
//import java.time.format.DateTimeFormatter;
//import java.util.List;
//import lombok.RequiredArgsConstructor;
//import org.springframework.beans.BeanUtils;
//import org.springframework.stereotype.Service;
//
//@Service
//@RequiredArgsConstructor
//public class DataServiceImpl implements DataService {
//
//    private final EduStudentCardLocationRespository eduStudentCardLocationRespository;
//    private final BtsLocationRespository btsLocationRespository;
//    private final WifimacLocationRespository wifimacLocationRespository;
//    private final OperationService operationService;
//    private final RedisService redisService;
//    private final RabbitUtil rabbitUtil;
//    private final static Snowflake snowFlake = new Snowflake(1L, 1L);
//
//    /**
//     * 处理设备V2数据
//     */
//    public void dealDeviceV2(String[] datas) {
//        EduStudentCardLocation position = new EduStudentCardLocation();
//        position.setId(snowFlake.nextId());
//        position.setCreateDate(LocalDateTime.now());
//        position.setType(0);
//        position.setLat(positionTrans(datas[9].equals("N") ? 1 : -1, datas[8]));
//        position.setLng(positionTrans(datas[11].equals("E") ? 1 : -1, datas[10]));
//        position.setImei(datas[1]);
//        position.setVendor(datas[0]);
//        position.setBat(Integer.valueOf(datas[6]));
////        position.setSv(Integer.valueOf(datas[5]));
////        position.setSignal(Integer.valueOf(datas[4]));
////        position.setSpeed(Double.valueOf(datas[12]));
////        position.setPdop(Double.valueOf(datas[13]));
//        position.setDeviceTime(dealDateTime(datas[14], datas[3]));
//        position.setStatus(datas[15]);
//        // gps数据需要转换为高德坐标系
//        List<Double> togcj02 = GeoUtil.wgs84togcj02(position.getLng(), position.getLat());
//        position.setLng(togcj02.get(0));
//        position.setLat(togcj02.get(1));
//        afterLocationDeal(position);
//    }
//
//    @Override
//    public void dealDeviceV3(String[] datas) {
//        EduStudentCardLocation position = new EduStudentCardLocation();
//        position.setType(3);
//
//        position.setImei(datas[1]);
//        position.setVendor(datas[0]);
//
//        position.setStatus(datas[datas.length - 1]);
//        int locationNum = Integer.valueOf(datas[4]);
//        // 保存其它点
//        for (int i = 0; i < locationNum; i++) {
//            // 设备定位点间隔20s
//            position.setDeviceTime(i == 0 ? dealDateTime(datas[6], datas[3]) : position.getDeviceTime().plusSeconds(20L));
//            String lngStr = datas[8 + i * 2];
//            String latStr = datas[7 + i * 2];
//            position.setLng(StrUtil.isNotEmpty(lngStr) ? positionTrans(1, lngStr) : position.getLng());
//            position.setLat(StrUtil.isNotEmpty(latStr) ? positionTrans(1, latStr) : position.getLat());
//            // gps数据需要转换为高德坐标系
//            if (StrUtil.isNotBlank(lngStr)) {
//                List<Double> togcj02 = GeoUtil.wgs84togcj02(position.getLng(), position.getLat());
//                position.setLng(togcj02.get(0));
//                position.setLat(togcj02.get(1));
//            }
//            position.setId(snowFlake.nextId());
//            position.setCreateDate(LocalDateTime.now());
//            // 保存数据
//            eduStudentCardLocationRespository.save(position);
//        }
////        afterLocationDeal(position);
//    }
//
//    @Override
//    public void dealFenceAlarm(String[] datas, Integer type) {
//        FenceAlarmDTO dto = new FenceAlarmDTO();
//        dto.setImei(datas[1]);
//        dto.setLng(positionTrans(datas[9].equals("N") ? 1 : -1, datas[8]));
//        dto.setLat(positionTrans(datas[7].equals("E") ? 1 : -1, datas[6]));
//        dto.setId(Integer.valueOf(datas[4]));
//        dto.setTime(dealDateTime(datas[12], datas[3]));
//        // TODO: 2021/11/25 保存告警信息
//    }
//
//    @Override
//    public void dealStep(String[] datas) {
//        DeviceReportDTO reportMsg = new DeviceReportDTO();
//        reportMsg.setImei(datas[1]);
//        reportMsg.setStep(Integer.valueOf(datas[2]));
//        reportMsg.setDeviceTime(dealDateTime(datas[5], datas[3]));
//        rabbitUtil.convertAndSendMsg(RabbitConstant.CARD_EXCHANGE, RabbitConstant.CARD_LOCATION_KEY, reportMsg);
//    }
//
//    @Override
//    public void dealLocation(String[] datas) {
//        EduStudentCardLocation position = new EduStudentCardLocation();
//        position.setId(snowFlake.nextId());
//        position.setCreateDate(LocalDateTime.now());
//        position.setVendor(datas[0]);
//        position.setImei(datas[1]);
//        position.setType(1);
//        position.setSignal(Integer.valueOf(datas[20]));
//        position.setSv(Integer.valueOf(datas[21]));
//        position.setBat(Integer.valueOf(datas[22]));
//        position.setDeviceTime(dealDateTime(datas[datas.length - 2], datas[5]));
//        position.setStatus(datas[datas.length - 1]);
//        // 查询经纬度数据
//        BtsLocation btsLocation = null;
//        // 基站数据是否缓存
//        Boolean locationCache = redisService
//            .hasKey(CachePrefixConstant.LOCATION_BTS + StrUtil.join(",", datas[13], datas[14], datas[17], datas[18]));
//        if (locationCache) {
//            btsLocation = (BtsLocation) redisService
//                .get(CachePrefixConstant.LOCATION_BTS + StrUtil.join(",", datas[13], datas[14], datas[17], datas[18]));
//        } else {
//            btsLocation = btsLocationRespository
//                .findByMccAndMncAndLacAndCid(datas[13], datas[14], datas[17], datas[18]);
//            // 未保存该数据 从高德查询
//            if (btsLocation == null) {
//                JSONObject locationJson = LocationUtil
//                    .translateBts(datas[13], datas[14], datas[17], datas[18], Integer.valueOf(datas[21]) * 2 - 113);
//                String[] location = locationJson.getStr("location").split(",");
//                btsLocation = new BtsLocation(Double.valueOf(location[0]), Double.valueOf(location[1]), datas[13],
//                    datas[14], datas[17], datas[18], Integer.valueOf(datas[21]) * 2 - 113);
//                btsLocation.setAdcode(locationJson.getStr("adcode"));
//                btsLocationRespository.insert(btsLocation);
//            }
//            // 缓存两小时基站数据
//            redisService.set(
//                CachePrefixConstant.LOCATION_BTS + StrUtil.join(",", datas[13], datas[14], datas[17], datas[18]),
//                btsLocation, 7200L);
//        }
//        position.setLng(btsLocation.getLng());
//        position.setLat(btsLocation.getLat());
//        if (!redisService.hasKey(CachePrefixConstant.DEVICE_CARD_WEATHER + position.getImei())) {
//            // 天气数据12小时有效
//            operationService.weather(position.getImei(), btsLocation.getAdcode());
//            redisService.set(CachePrefixConstant.DEVICE_CARD_WEATHER + position.getImei(), 43200L);
//        }
//        afterLocationDeal(position);
//    }
//
//    public void dealDeviceKa(String[] datas) {
//        EduStudentCardLocation position = new EduStudentCardLocation();
//        position.setId(snowFlake.nextId());
//        position.setCreateDate(LocalDateTime.now());
//        position.setVendor(datas[0]);
//        position.setImei(datas[1]);
//        position.setType(1);
//        position.setSignal(Integer.valueOf(datas[4]));
//        position.setSv(Integer.valueOf(datas[5]));
//        position.setBat(Integer.valueOf(datas[6]));
//        position.setDeviceTime(dealDateTime(datas[datas.length - 2], datas[3]));
//        position.setStatus(datas[datas.length - 1]);
//        // 查询经纬度数据
//        BtsLocation btsLocation = null;
//        // 基站数据是否缓存
//        Boolean locationCache = redisService
//            .hasKey(CachePrefixConstant.LOCATION_BTS + StrUtil.join(",", datas[7], datas[8], datas[11], datas[12]));
//        if (locationCache) {
//            btsLocation = (BtsLocation) redisService
//                .get(CachePrefixConstant.LOCATION_BTS + StrUtil.join(",", datas[7], datas[8], datas[11], datas[12]));
//        } else {
//            btsLocation = btsLocationRespository
//                .findByMccAndMncAndLacAndCid(datas[7], datas[8], datas[11], datas[12]);
//            // 未保存该数据 从高德查询
//            if (btsLocation == null) {
//                JSONObject locationJson = LocationUtil
//                    .translateBts(datas[7], datas[8], datas[11], datas[12], Integer.valueOf(datas[13]) * 2 - 113);
//                String[] location = locationJson.getStr("location").split(",");
//                btsLocation = new BtsLocation(Double.valueOf(location[0]), Double.valueOf(location[1]), datas[7],
//                    datas[8], datas[11], datas[12], Integer.valueOf(datas[13]) * 2 - 113);
//                btsLocation.setAdcode(locationJson.getStr("adcode"));
//                btsLocationRespository.insert(btsLocation);
//            }
//            // 缓存两小时基站数据
//            redisService.set(
//                CachePrefixConstant.LOCATION_BTS + StrUtil.join(",", datas[7], datas[8], datas[11], datas[12]),
//                btsLocation, 7200L);
//        }
//        position.setLng(btsLocation.getLng());
//        position.setLat(btsLocation.getLat());
//        if (!redisService.hasKey(CachePrefixConstant.DEVICE_CARD_WEATHER + position.getImei())) {
//            // 天气数据12小时有效
//            operationService.weather(position.getImei(), btsLocation.getAdcode());
//            redisService.set(CachePrefixConstant.DEVICE_CARD_WEATHER + position.getImei(), 43200L);
//        }
//        afterLocationDeal(position);
//    }
//
//    @Override
//    public void dealDeviceWifi(String[] datas) {
//        EduStudentCardLocation position = new EduStudentCardLocation();
//        position.setId(snowFlake.nextId());
//        position.setCreateDate(LocalDateTime.now());
//        position.setVendor(datas[0]);
//        position.setImei(datas[1]);
//        position.setType(2);
//        position.setSignal(Integer.valueOf(datas[4]));
//        position.setSv(Integer.valueOf(datas[5]));
//        position.setBat(Integer.valueOf(datas[6]));
//        position.setDeviceTime(dealDateTime(datas[datas.length - 2], datas[3]));
//        position.setStatus(datas[datas.length - 1]);
//        // 查询经纬度数据
//        WifimacLocation wifimacLocation = null;
//        Integer wifiNum = Integer.valueOf(datas[7]);
//        List<String> mac = CollUtil.newArrayList("", "", "", "", "", "");
//        List<String> rssi = CollUtil.newArrayList("", "", "", "", "", "");
//        StringBuilder sb = new StringBuilder();
//        for (int i = 0; i < wifiNum; i++) {
//            mac.set(i, datas[8 + 2 * i]);
//            rssi.set(i, datas[9 + 2 * i]);
//            sb.append(mac.get(i))
//                .append(",")
//                .append(rssi.get(i))
//                .append("|");
//        }
//        wifimacLocation = wifimacLocationRespository
//            .findByMac1InAndMac2InAndMac3InAndMac4InAndMac5InAndMac6In(mac, mac, mac, mac, mac, mac);
//        // 未保存该数据 从高德查询
//        if (wifimacLocation == null) {
//            JSONObject locationJson = LocationUtil.translateWifimac(sb.substring(0, sb.length() - 1));
//            String[] location = locationJson.getStr("location").split(",");
//            // 存入mongo
//            wifimacLocation = new WifimacLocation(Double.valueOf(location[0]), Double.valueOf(location[1]), mac.get(0),
//                mac.get(1), mac.get(2), mac.get(3), mac.get(4), mac.get(5));
//            wifimacLocationRespository.insert(wifimacLocation);
//        }
//        position.setLng(wifimacLocation.getLng());
//        position.setLat(wifimacLocation.getLat());
//        afterLocationDeal(position);
//    }
//
//    private void afterLocationDeal(EduStudentCardLocation location) {
//        // 存入es
//        eduStudentCardLocationRespository.save(location);
//        // 缓存定位数据
//        LocationVO locationVO = new LocationVO();
//        BeanUtils.copyProperties(location, locationVO);
//        redisService.hset(CachePrefixConstant.DEVICE_CARD_LATEST_LOCATION, location.getImei(), locationVO);
//        DeviceReportDTO reportMsg = new DeviceReportDTO();
//        BeanUtils.copyProperties(location, reportMsg);
//        // 消息通知
//        rabbitUtil.convertAndSendMsg(RabbitConstant.CARD_EXCHANGE, RabbitConstant.CARD_LOCATION_KEY, reportMsg);
//    }
//
//    /**
//     * @param type 1-东经/北纬，-1-西经/南纬
//     * @param data
//     * @return
//     */
//    private static Double positionTrans(Integer type, String data) {
//        int base = Integer.parseInt(data.substring(0, data.indexOf(".") - 2));
//        double v = Double.valueOf(data.substring(data.indexOf(".") - 2)) / 60;
//        return (base + v) * type;
//    }
//
//    private static LocalDateTime dealDateTime(String date, String time) {
//        if (date.length() == 5) {
//            date = "0" + date;
//        }
//        if (time.length() == 5) {
//            time = "0" + time;
//        }
//        date = date.substring(0, 4) + "20" + date.substring(4);
//        return LocalDateTime.parse(date + time, DateTimeFormatter.ofPattern("ddMMyyyyHHmmss"));
//    }
//}
