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

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.sooka.sponest.monitor.meiqiguan.gps.domain.GasTankDevices;
import com.sooka.sponest.monitor.meiqiguan.gps.domain.dto.GasTankDevicesDTO;
import com.sooka.sponest.monitor.meiqiguan.gps.domain.vo.GasTankDevicesVo;
import com.sooka.sponest.monitor.meiqiguan.gps.mapper.GasTankDevicesMapper;
import com.sooka.sponest.monitor.meiqiguan.gps.mapper.GasTankHistoricalTrajectoryMapper;
import com.sooka.sponest.monitor.meiqiguan.gps.service.IGasTankDevicesService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author ruoyi
 * @date 2023-12-09
 */
@Slf4j
@Service
public class GasTankDevicesServiceImpl implements IGasTankDevicesService {

    @Autowired
    private GasTankDevicesMapper gasTankDevicesMapper;
    @Autowired
    private GasTankHistoricalTrajectoryMapper gasTankHistoricalTrajectoryMapper;

    /**
     * 查询煤气罐设备信息
     *
     * @param id 煤气罐设备信息主键
     * @return 煤气罐设备信息
     */
    @Override
    public GasTankDevices selectGasTankDevicesById(Long id) {
        return gasTankDevicesMapper.selectGasTankDevicesById(id);
    }

    /**
     * 查询煤气罐设备信息列表
     *
     * @param gasTankDevices 煤气罐设备信息
     * @return 煤气罐设备信息
     */
    @Override
    public List<GasTankDevices> selectGasTankDevicesList(GasTankDevices gasTankDevices) {
        return gasTankDevicesMapper.selectGasTankDevicesList(gasTankDevices);
    }


    @Override
    public List<GasTankDevices> selectGasTankDevicesListDpet(List<String> gps) {
        return gasTankDevicesMapper.selectGasTankDevicesListDept(gps);
    }

    /**
     * 新增煤气罐设备信息
     *
     * @param gasTankDevices 煤气罐设备信息
     * @return 结果
     */
    @Override
    public int insertGasTankDevices(GasTankDevices gasTankDevices) {
        return gasTankDevicesMapper.insertGasTankDevices(gasTankDevices);
    }

    /**
     * 修改煤气罐设备信息
     *
     * @param gasTankDevices 煤气罐设备信息
     * @return 结果
     */
    @Override
    public int updateGasTankDevices(GasTankDevices gasTankDevices) {
        return gasTankDevicesMapper.updateGasTankDevices(gasTankDevices);
    }

    /**
     * 批量删除煤气罐设备信息
     *
     * @param ids 需要删除的煤气罐设备信息主键
     * @return 结果
     */
    @Override
    public int deleteGasTankDevicesByIds(Long[] ids) {
        return gasTankDevicesMapper.deleteGasTankDevicesByIds(ids);
    }

    /**
     * 删除煤气罐设备信息信息
     *
     * @param id 煤气罐设备信息主键
     * @return 结果
     */
    @Override
    public int deleteGasTankDevicesById(Long id) {
        return gasTankDevicesMapper.deleteGasTankDevicesById(id);
    }

    /**
     * 查询煤气罐设备列表与轨迹
     * @param gasTankDevices
     * @return
     */
    @Override
    public List<GasTankDevicesVo> selectGasTankDevicesListAll(GasTankDevices gasTankDevices) {
        List<GasTankDevicesVo> devicesList = gasTankDevicesMapper.selectGasTankDevicesListAll(gasTankDevices);
        for (GasTankDevicesVo device : devicesList) {
            if (StringUtils.isEmpty(gasTankDevices.getDeviceid())) {
                //不传deviceid，返回每个设备的最新一条轨迹
                device.setTrajectory(gasTankHistoricalTrajectoryMapper.selectHistoricalTrajectoryListOneByDeviceId(device.getDeviceid()));
            } else {
                //传deviceid，返回单个设备的所有轨迹
                device.setTrajectory(gasTankHistoricalTrajectoryMapper.selectHistoricalTrajectoryListByDeviceId(device.getDeviceid()));
            }
        }
        return devicesList;
    }

    @Override
    public List<GasTankDevices> selectGasTankDevicesNewEndTime() {
        return gasTankDevicesMapper.selectGasTankDevicesNewEndTime();
    }


}
