package com.sooka.sponest.monitor.meiqiguan.gps.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.shaded.com.google.common.collect.Lists;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.sooka.sponest.monitor.meiqiguan.gps.domain.GasTankHistoricalTrajectory;
import com.sooka.sponest.monitor.meiqiguan.gps.domain.GasTankTravelMileage;
import com.sooka.sponest.monitor.meiqiguan.gps.mapper.GasTankHistoricalTrajectoryMapper;
import com.sooka.sponest.monitor.meiqiguan.gps.mapper.GasTankTravelMileageMapper;
import com.sooka.sponest.monitor.meiqiguan.gps.service.IGasTankHistoricalTrajectoryService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.apache.commons.lang3.ObjectUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 历史轨迹Service业务层处理
 *
 * @author ruoyi
 * @date 2024-09-09
 */
@Service
public class GasTankHistoricalTrajectoryServiceImpl implements IGasTankHistoricalTrajectoryService {
    @Autowired
    private GasTankHistoricalTrajectoryMapper gasTankHistoricalTrajectoryMapper;
    @Autowired
    private GasTankTravelMileageMapper gasTankTravelMileageMapper;

    /**
     * 查询历史轨迹
     *
     * @param id 历史轨迹主键
     * @return 历史轨迹
     */
    @Override
    public GasTankHistoricalTrajectory selectGasTankHistoricalTrajectoryById(Long id) {
        return gasTankHistoricalTrajectoryMapper.selectGasTankHistoricalTrajectoryById(id);
    }

    /**
     * 查询历史轨迹列表
     *
     * @param gasTankHistoricalTrajectory 历史轨迹
     * @return 历史轨迹
     */
    @Override
    public List<GasTankHistoricalTrajectory> selectGasTankHistoricalTrajectoryList(GasTankHistoricalTrajectory gasTankHistoricalTrajectory) {
        return gasTankHistoricalTrajectoryMapper.selectGasTankHistoricalTrajectoryList(gasTankHistoricalTrajectory);
    }

    /**
     * 新增历史轨迹
     *
     * @param gasTankHistoricalTrajectory 历史轨迹
     * @return 结果
     */
    @Override
    public int insertGasTankHistoricalTrajectory(GasTankHistoricalTrajectory gasTankHistoricalTrajectory) {
        return gasTankHistoricalTrajectoryMapper.insertGasTankHistoricalTrajectory(gasTankHistoricalTrajectory);
    }

    /**
     * 修改历史轨迹
     *
     * @param gasTankHistoricalTrajectory 历史轨迹
     * @return 结果
     */
    @Override
    public int updateGasTankHistoricalTrajectory(GasTankHistoricalTrajectory gasTankHistoricalTrajectory) {
        return gasTankHistoricalTrajectoryMapper.updateGasTankHistoricalTrajectory(gasTankHistoricalTrajectory);
    }

    /**
     * 批量删除历史轨迹
     *
     * @param ids 需要删除的历史轨迹主键
     * @return 结果
     */
    @Override
    public int deleteGasTankHistoricalTrajectoryByIds(Long[] ids) {
        return gasTankHistoricalTrajectoryMapper.deleteGasTankHistoricalTrajectoryByIds(ids);
    }

    /**
     * 删除历史轨迹信息
     *
     * @param id 历史轨迹主键
     * @return 结果
     */
    @Override
    public int deleteGasTankHistoricalTrajectoryById(Long id) {
        return gasTankHistoricalTrajectoryMapper.deleteGasTankHistoricalTrajectoryById(id);
    }

    @Override
    public Map<String, List> selectGasTankHistoricalTrajectoryListByDeviceid(GasTankHistoricalTrajectory gasTankHistoricalTrajectory) {
        Map<String, List> map = new HashMap<>();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        // 获取当前日期
        LocalDate currentDate = LocalDate.now();
        // 00:00:00 的时间戳
        LocalDateTime startOfDay = currentDate.atStartOfDay();
        long startTimestamp = startOfDay.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
        // 23:59:59 的时间戳
        LocalDateTime endOfDay = currentDate.atTime(23, 59, 59);
        long endTimestamp = endOfDay.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
        Map<String, Object> params = gasTankHistoricalTrajectory.getParams();
        if (ObjectUtils.isEmpty(params)) {
            params.put("beginTime", startTimestamp);
            params.put("endTime", endTimestamp);
            gasTankHistoricalTrajectory.setParams(params);
        } else {
            LocalDateTime beginTime = LocalDateTime.parse(params.get("beginTime").toString().split(" ")[0] + " 00:00:00", formatter);
            LocalDateTime endTime = LocalDateTime.parse(params.get("endTime").toString().split(" ")[0] + " 23:59:59", formatter);
            params.put("beginTime", beginTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli());
            params.put("endTime", endTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli());
        }
        //历史轨迹
        List<GasTankHistoricalTrajectory> gasTankHistoricalTrajectories = gasTankHistoricalTrajectoryMapper.selectGasTankHistoricalTrajectoryListByDeviceid(gasTankHistoricalTrajectory);
        //行程
        List<GasTankTravelMileage> gasTankTravelMileages = gasTankTravelMileageMapper.selectGasTankTravelMileageListByDeviceid(gasTankHistoricalTrajectory);
        map.put("historicalTrajectories", gasTankHistoricalTrajectories);
        map.put("travelMileages", gasTankTravelMileages);
        return map;
    }

}
