package com.fs.watch.service.impl;

import com.fs.common.utils.DateUtils;
import com.fs.common.utils.StringUtils;
import com.fs.watch.domain.WatchTemperatureData;
import com.fs.watch.mapper.WatchTemperatureDataMapper;
import com.fs.watch.param.WatchDataQueryParam;
import com.fs.watch.service.WatchTemperatureDataService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
* @author Administrator
* @description 针对表【watch_temperature_data(设备温度数据)】的数据库操作Service实现
* @createDate 2024-10-15 11:06:51
*/
@Service
@Slf4j
public class WatchTemperatureDataServiceImpl implements WatchTemperatureDataService {
    @Autowired
    private WatchTemperatureDataMapper mapper;


    @Override
    public Boolean insert(WatchTemperatureData watchTemperatureData) {
        //判断创建时间是否唯一
        Integer num = mapper.queryByCreateTime(watchTemperatureData.getCreateTime());
        if (num != null && num > 0) {
            return false;
        }
        try {
            mapper.insert(watchTemperatureData);
        } catch (Exception e) {
            log.error("===============watchTemperatureData:insert=========================={}", e.getMessage());
            return false;
        }
        return true;
    }

    private Map<String, Object> getTemperatureDataMap(WatchTemperatureData watchTemperatureData) {
        Map<String, Object> params = new HashMap<>();
        params.put("id", watchTemperatureData.getId().toString());
        params.put("deviceId", watchTemperatureData.getDeviceId());
        params.put("envTemp", watchTemperatureData.getEnvTemp());
        params.put("estTemp", watchTemperatureData.getEstTemp());
        params.put("axillaryTemp", watchTemperatureData.getAxillaryTemp());
        params.put("shellTemp", watchTemperatureData.getShellTemp());
        params.put("createTime", watchTemperatureData.getCreateTime());
        return params;
    }

    /**
     * 查询某天某个设备的温度数据
     *
     * @param date
     * @param deviceId
     * @return
     */
    @Override
    public List<WatchTemperatureData> queryByDateAndDeviceId(String date, String deviceId) {
        if (StringUtils.isBlank(date)) {
            date = DateUtils.dateTimeNow("yyyy-MM-dd");
        }
        return mapper.queryByDateAndDeviceId(date,deviceId);
    }

    @Override
    public WatchTemperatureData getLatest(String deviceId) {
        return mapper.getLatest(deviceId);

    }

    @Override
    public List<WatchTemperatureData> page(WatchDataQueryParam param) {
        return mapper.page(param);
    }

    @Override
    public List<WatchTemperatureData> list(Date startTime, Date endTime, String deviceId) {
        return mapper.list(startTime,endTime,deviceId);
    }
}
