package com.prolog.rdc.tms.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.prolog.common.util.DateUtils;
import com.prolog.common.util.ToolKits;
import com.prolog.rdc.tms.g7.G7Conn;
import com.prolog.rdc.tms.model.dto.*;
import com.prolog.rdc.tms.model.dto.g7.CurrentGpsLocationDTO;
import com.prolog.rdc.tms.model.dto.g7.CurrentTemperatureDTO;
import com.prolog.rdc.tms.model.vo.*;
import com.prolog.rdc.tms.model.vo.g7.CurrentTemperatureVO;
import com.prolog.rdc.tms.model.vo.g7.HistoryLocationByGpsnoVO;
import com.prolog.rdc.tms.service.G7Service;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 货主资料接口实现层
 *
 * @author :fanzw
 * @version :1.0.0
 * @className :CargoOwnerServiceImpl
 * @creationTime :2022/11/28
 * @description :
 * @modifier :fanzw
 * @modifyTime :2022/11/28
 * @reviseRemarks:
 */
@Slf4j
@Service
public class G7ServiceImpl implements G7Service {

    @Autowired
    private G7Conn g7Conn;

    /**
     * 车辆当前定位V1.0
     *
     * @param
     * @return
     */
    @Override
    public List<CurrentLocationDTO> currentLocationCall(List<CurrentLocationVO> vos) throws Exception {
        ToolKits.isParamNull(vos, "车辆当前定位V1.0请求参数不为空");
        List<CurrentLocationDTO> list = Lists.newArrayList();
        String path = "/v1/device/truck/current_location";
        for (CurrentLocationVO vo : vos) {
            if (ToolKits.isEmpty(vo.getPlateNum())) {
                continue;
            }
            Map<String, String> querys = new HashMap<String, String>();
            querys.put("plate_num", vo.getPlateNum());
            //querys.put("map", "baidu");
            JSONObject rsData = g7Conn.connect(querys,path);
            if (ToolKits.isEmpty(rsData)) {
                continue;
            }
            if (ToolKits.isEmpty(rsData.get("data"))) {
                continue;
            }
            CurrentLocationDTO locationDTO = JSONObject.parseObject(rsData.get("data").toString(), CurrentLocationDTO.class);
            if (ToolKits.isEmpty(locationDTO)) {
                continue;
            }
            list.add(locationDTO);
        }
        return list;
    }

    /**
     * 设备车辆当前温度查询接口
     *
     * @param
     * @return
     */
    @Override
    public List<CurrentTemperatureDTO> currentTemperatureCall(CurrentTemperatureVO vo) throws Exception{
        ToolKits.isParamNull(vo, "设备车辆当前温度查询请求参数不为空");
        List<CurrentTemperatureDTO> list = Lists.newArrayList();
        String path = "/v1/coldchain/daily/temperatureCurrents";
        Map<String, String> querys = new HashMap<String, String>();
        if (StrUtil.isNotBlank(vo.getPlateNum())) {
            querys.put("carnums", vo.getPlateNum());
        }
        if (StrUtil.isNotBlank(vo.getGpsnos())) {
            querys.put("gpsnos", vo.getGpsnos());
        }
        JSONObject rsData = g7Conn.connect(querys,path);
        if (ToolKits.isEmpty(rsData)) {
            return list;
        }
        if (ToolKits.isEmpty(rsData.get("data"))) {
            return list;
        }
        List<CurrentTemperatureDTO> temperatureDTOS = JSON.parseArray(rsData.get("data").toString(), CurrentTemperatureDTO.class);
        if (ToolKits.isEmpty(temperatureDTOS)) {
            return list;
        }
        list.addAll(temperatureDTOS);
        return list;
    }

    /**
     * 设备历史轨迹查询接口
     *
     * @param
     * @return
     */
    @Override
    public List<HistoryLocationByGpsnoDTO> historyLocationByGpsnoCall(HistoryLocationByGpsnoVO vo) throws Exception {
        ToolKits.isParamNull(vo, "设备历史轨迹查询请求参数不为空");
        ToolKits.isParamNull(vo.getGpsno(), "设备号不为空");

        List<HistoryLocationByGpsnoDTO> list = Lists.newArrayList();
        String path = "/v1/device/truck/history_location_by_gpsno";
        Map<String, String> querys = new HashMap<String, String>();
        querys.put("gpsno", vo.getGpsno());

        if (ToolKits.isEmpty(vo.getFrom()) || ToolKits.isEmpty(vo.getTo())) {
            Date date = DateUtil.date();
            Date preDate = DateUtil.offsetMonth(date, -1);
            querys.put("from",DateUtils.format(preDate,DateUtils.PATTERN_NORMAL));
            querys.put("to",DateUtils.format(date,DateUtils.PATTERN_NORMAL));
        } else {
            querys.put("from",DateUtils.format(vo.getFrom(),DateUtils.PATTERN_NORMAL));
            querys.put("to",DateUtils.format(vo.getTo(),DateUtils.PATTERN_NORMAL));
        }
        JSONObject rsData = g7Conn.connect(querys,path);
        if (ToolKits.isEmpty(rsData)) {
            return list;
        }
        if (ToolKits.isEmpty(rsData.get("data"))) {
            return list;
        }
        List<HistoryLocationByGpsnoDTO> dtos = JSONObject.parseArray(rsData.get("data").toString(), HistoryLocationByGpsnoDTO.class);
        if (ToolKits.isEmpty(dtos)) {
            return list;
        }
        list.addAll(dtos);
        return list;
    }

    /**
     * 车辆历史轨迹查询接口
     *
     * @param
     * @return
     */
    @Override
    public List<HistoryLocationByGpsnoDTO> historyLocationByVehCall(HistoryLocationByGpsnoVO vo) throws Exception {
        ToolKits.isParamNull(vo, "车辆历史轨迹查询请求参数不为空");
        ToolKits.isParamNull(vo.getPlateNum(), "车辆号不为空");

        List<HistoryLocationByGpsnoDTO> list = Lists.newArrayList();
        String path = "/v1/device/truck/history_location";
        Map<String, String> querys = new HashMap<String, String>();
        querys.put("plate_num", vo.getPlateNum());

        if (ToolKits.isEmpty(vo.getFrom()) || ToolKits.isEmpty(vo.getTo())) {
            Date date = DateUtil.date();
            Date preDate = DateUtil.offsetMonth(date, -1);
            querys.put("from",DateUtils.format(preDate,DateUtils.PATTERN_NORMAL));
            querys.put("to",DateUtils.format(date,DateUtils.PATTERN_NORMAL));
        } else {
            querys.put("from",DateUtils.format(vo.getFrom(),DateUtils.PATTERN_NORMAL));
            querys.put("to",DateUtils.format(vo.getTo(),DateUtils.PATTERN_NORMAL));
        }
        JSONObject rsData = g7Conn.connect(querys,path);
        if (ToolKits.isEmpty(rsData)) {
            return list;
        }
        if (ToolKits.isEmpty(rsData.get("data"))) {
            return list;
        }
        List<HistoryLocationByGpsnoDTO> dtos = JSONObject.parseArray(rsData.get("data").toString(), HistoryLocationByGpsnoDTO.class);
        if (ToolKits.isEmpty(dtos)) {
            return list;
        }
        list.addAll(dtos);
        return list;
    }

    /**
     * 查询车辆事件报表接口
     *
     * @param
     * @return
     */
    public List<TruckEventDTO> truckEventCall(TruckEventVO vo) throws Exception {
        ToolKits.isParamNull(vo, "查询车辆事件报表请求参数不为空");
        ToolKits.isParamNull(vo.getCarnums(), "车牌号不为空");

        List<TruckEventDTO> list = Lists.newArrayList();
        String path = "/v1/ems-openapi/ems/truck_event";
        Map<String, String> querys = new HashMap<String, String>();
        querys.put("carnums", vo.getCarnums());
        querys.put("alarmtypes", "6,7,21,22,26,27,28,29,30,601,41,45,");

        if (ToolKits.isEmpty(vo.getFrom()) || ToolKits.isEmpty(vo.getTo())) {
            Date date = DateUtil.date();
            Date preDate = DateUtil.offsetMonth(date, -1);
            querys.put("from",DateUtils.format(preDate,DateUtils.PATTERN_NORMAL));
            querys.put("to",DateUtils.format(date,DateUtils.PATTERN_NORMAL));
        } else {
            querys.put("from",DateUtils.format(vo.getFrom(),DateUtils.PATTERN_NORMAL));
            querys.put("to",DateUtils.format(vo.getTo(),DateUtils.PATTERN_NORMAL));
        }
        JSONObject rsData = g7Conn.connect(querys,path);
        if (ToolKits.isEmpty(rsData)) {
            return list;
        }
        if (ToolKits.isEmpty(rsData.get("data"))) {
            return list;
        }
        List<TruckEventDTO> dtos = JSONObject.parseArray(rsData.get("data").toString(), TruckEventDTO.class);
        if (ToolKits.isEmpty(dtos)) {
            return list;
        }
        list.addAll(dtos);
        return list;
    }

    /**
     * 冷链报警列表接口
     *
     * @param
     * @return
     */
    @Override
    public List<ColdchainAlarmDTO> coldchainAlarmCall(ColdchainAlarmVO vo) throws Exception {
        ToolKits.isParamNull(vo, "查询冷链报警列表请求参数不为空");
        ToolKits.isParamNull(vo.getCarnum(), "车牌号不为空");

        List<ColdchainAlarmDTO> list = Lists.newArrayList();
        String path = "/v1/coldchain/alarm/getAlarms";
        Map<String, String> querys = new HashMap<String, String>();

        List<String> carnums = Arrays.stream(vo.getCarnum().split(",")).collect(Collectors.toList());
        querys.put("carnumIn", JSONObject.toJSONString(carnums));
        if (StrUtil.isNotBlank(vo.getStarttime())) {
            querys.put("starttime", vo.getStarttime());
        }

        JSONObject rsData = g7Conn.connect(querys,path);
        if (ToolKits.isEmpty(rsData)) {
            return list;
        }
        if (ToolKits.isEmpty(rsData.get("data"))) {
            return list;
        }
        JSONObject results = JSONObject.parseObject(rsData.get("data").toString());
        if (ToolKits.isEmpty(results)) {
            return list;
        }
        List<ColdchainAlarmDTO> dtos = JSONObject.parseArray(results.get("result").toString(), ColdchainAlarmDTO.class);
        if (ToolKits.isEmpty(dtos)) {
            return list;
        }
        list.addAll(dtos);
        return list;
    }

    @Override
    public List<CurrentGpsLocationDTO> simpleCurrentsByGpsnos(CurrentTemperatureVO vo) throws Exception {
        List<CurrentGpsLocationDTO> list = Lists.newArrayList();
        String path = "/v1/device/equipment/simple_currents_by_gpsnos";
        Map<String, String> querys = new HashMap<String, String>();
        querys.put("gpsnos", vo.getGpsnos());
        JSONObject rsData = g7Conn.post(querys,path);
        if (ToolKits.isEmpty(rsData)) {
            return list;
        }
        if (ToolKits.isEmpty(rsData.get("data"))) {
            return list;
        }
        return JSONObject.parseArray(rsData.get("data").toString(), CurrentGpsLocationDTO.class);
    }
}
