package com.casic.missiles.modular.system.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.casic.missiles.core.datascope.DataScope;
import com.casic.missiles.model.response.ResponseData;
import com.casic.missiles.modular.system.dao.DeviceTubeMapper;
import com.casic.missiles.modular.system.service.DeviceTubeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
@Slf4j
public class DeviceTubeServiceImpl implements DeviceTubeService {

    private final DeviceTubeMapper deviceTubeMapper;

    public DeviceTubeServiceImpl(DeviceTubeMapper deviceTubeMapper) {
        this.deviceTubeMapper = deviceTubeMapper;
    }

    /**
     * 获取点位信息
     *
     * @return
     */
    public Object deviceList(DataScope dataScope) {
        ResponseData responseData = new ResponseData();
        try {
            List<Map<String, Object>> tubeWellList = deviceTubeMapper.deviceList(dataScope);
            responseData.setCode(ResponseData.DEFAULT_SUCCESS_CODE);
            responseData.setMessage(ResponseData.DEFAULT_SUCCESS_MESSAGE);
            responseData.setData(tubeWellList);
        } catch (DataAccessException sqlex) {
            responseData.setCode(ResponseData.DEFAULT_ERROR_CODE);
            responseData.setMessage("请求数据异常");
            responseData.setSuccess(true);
            log.error("主题:获取点位信息列表出现异常,异常信息为{}", sqlex);
        }
        return responseData;
    }

    /**
     * 获取组列表信息
     *
     * @param dataScope
     * @return
     */
    public Object deviceGroupsList(DataScope dataScope) {
        ResponseData responseData = new ResponseData();
        try {
            List<Map<String, Object>> tubeDeviceList = deviceTubeMapper.deviceGroupsList(dataScope);
            responseData.setCode(ResponseData.DEFAULT_SUCCESS_CODE);
            responseData.setMessage(ResponseData.DEFAULT_SUCCESS_MESSAGE);
            responseData.setData(tubeDeviceList);
        } catch (DataAccessException sqlex) {
            responseData.setCode(ResponseData.DEFAULT_ERROR_CODE);
            responseData.setMessage("请求数据异常");
            responseData.setSuccess(true);
            log.error("主题:数据上报保存出现错误,异常信息为{}", sqlex);
        }
        return responseData;
    }

    /**
     * 分页获取点位信息
     *
     * @param oreder
     * @param page
     * @param wellGroupId
     * @param dataScope
     * @return
     */
    public Object groupDeviceListPage(Integer oreder, Page<Map<String, Object>> page, String wellGroupId, DataScope dataScope) {
        ResponseData responseData = new ResponseData();
        try {
            List<Map<String, Object>> tubeDeviceList = deviceTubeMapper.groupDeviceListPage(page, wellGroupId, dataScope);
            List<Map<String, Object>> tubeDataDeviceList = new ArrayList<>();
            tubeDeviceList.forEach(tubeDevice -> {
                        Map<String, Object> tubeDeviceData = deviceTubeMapper.dataTubeByDevcode(tubeDevice.get("DEVCODE").toString());
                        if (tubeDeviceData == null) {
                            tubeDeviceData = new HashMap<>();
                            initTubeDeviceData(tubeDeviceData, tubeDevice.get("DEVCODE"));
                        }
                        tubeDeviceData.put("deviceName", tubeDevice.get("DEVICE_NAME"));
                        tubeDataDeviceList.add(tubeDeviceData);
                    }
            );
            if (oreder != null && oreder == 0) {
                resvertSortStrength(tubeDataDeviceList);
            } else {
                sortStrength(tubeDataDeviceList);
            }
            responseData.setCode(ResponseData.DEFAULT_SUCCESS_CODE);
            responseData.setMessage(ResponseData.DEFAULT_SUCCESS_MESSAGE);
            responseData.setData(tubeDataDeviceList);
        } catch (DataAccessException sqlex) {
            responseData.setCode(ResponseData.DEFAULT_ERROR_CODE);
            responseData.setMessage("请求数据异常");
            responseData.setSuccess(true);
            log.error("主题:数据上报保存出现错误,异常信息为{}", sqlex);
        }
        return responseData;
    }

    /**
     * 获取历史记录信息
     *
     * @param page
     * @param devcode
     * @param beginTime
     * @param endTime
     * @return
     */
    public Object tubeDataHistory(Page<Map<String, Object>> page, String devcode, String beginTime, String endTime) {
        ResponseData responseData = new ResponseData();
        try {
            List<Map<String, Object>> tubeDeviceList = deviceTubeMapper.tubeDataHistory(page, devcode, beginTime, endTime);
            responseData.setCode(ResponseData.DEFAULT_SUCCESS_CODE);
            responseData.setMessage(ResponseData.DEFAULT_SUCCESS_MESSAGE);
            responseData.setData(tubeDeviceList);
        } catch (DataAccessException sqlex) {
            responseData.setCode(ResponseData.DEFAULT_ERROR_CODE);
            responseData.setMessage("请求数据异常");
            responseData.setSuccess(true);
            log.error("主题:数据上报保存出现错误,异常信息为{}", sqlex);
        }
        return responseData;
    }

    /**
     * 获取设备列表的信息
     *
     * @param wellGroupId
     * @return
     */
    public Object groupDeviceList(String wellGroupId, DataScope dataScope) {
        ResponseData responseData = new ResponseData();
        try {
            List<Map<String, Object>> tubeDeviceList = deviceTubeMapper.groupDeviceList(wellGroupId, dataScope);
            responseData.setCode(ResponseData.DEFAULT_SUCCESS_CODE);
            responseData.setMessage(ResponseData.DEFAULT_SUCCESS_MESSAGE);
            responseData.setData(tubeDeviceList);
        } catch (DataAccessException sqlex) {
            responseData.setCode(ResponseData.DEFAULT_ERROR_CODE);
            responseData.setMessage("请求数据异常");
            responseData.setSuccess(true);
            log.error("主题:数据上报保存出现错误,异常信息为{}", sqlex);
        }
        return responseData;
    }

    /**
     * 获取详情信息
     */
    public Object deviceDetail(String id) {
        ResponseData responseData = new ResponseData();
        try {
            Map<String, Object> tubeDeviceData = deviceTubeMapper.deviceDetail(Long.valueOf(id));
            responseData.setCode(ResponseData.DEFAULT_SUCCESS_CODE);
            responseData.setMessage(ResponseData.DEFAULT_SUCCESS_MESSAGE);
            responseData.setData(tubeDeviceData);
        } catch (DataAccessException sqlex) {
            responseData.setCode(ResponseData.DEFAULT_ERROR_CODE);
            responseData.setMessage("请求数据异常");
            responseData.setSuccess(true);
            log.error("主题:数据上报保存出现错误,异常信息为{}", sqlex);
        }
        return responseData;
    }

    private void initTubeDeviceData(Map<String, Object> tubeDeviceData, Object devcode) {
        tubeDeviceData.put("deviceCode", devcode);
        tubeDeviceData.put("strength", -1);
        tubeDeviceData.put("uptime", new Date());
        tubeDeviceData.put("strength", -1);
        tubeDeviceData.put("pci", "");
        tubeDeviceData.put("rsrp", "");
        tubeDeviceData.put("snr", "");
        tubeDeviceData.put("cell", "");
        tubeDeviceData.put("id", "");
    }

    private void sortStrength(List<Map<String, Object>> lstMap) {
        Collections.sort(lstMap, new Comparator<Map<String, Object>>() {
            public int compare(Map<String, Object> o1, Map<String, Object> o2) {
                return o1.get("strength").toString().compareTo(o2.get("strength").toString());
            }
        });
    }

    private void resvertSortStrength(List<Map<String, Object>> lstMap) {
        Collections.sort(lstMap, new Comparator<Map<String, Object>>() {
            public int compare(Map<String, Object> o1, Map<String, Object> o2) {
                return o2.get("strength").toString().compareTo(o1.get("strength").toString());
            }
        });
    }

}
