package com.fs.watch.service.impl;

import com.fs.common.utils.DateUtils;
import com.fs.common.utils.StringUtils;
import com.fs.watch.domain.WatchContinuousSpo2Data;
import com.fs.watch.domain.vo.WatchContinuousSpo2DataVo;
import com.fs.watch.domain.vo.WatchPulseRateVo;
import com.fs.watch.domain.vo.WatchSpo2DataVo;
import com.fs.watch.mapper.WatchContinuousSpo2DataMapper;
import com.fs.watch.service.WatchContinuousSpo2DataService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author Administrator
 * @description 针对表【watch_continuous_spo2_data(设备连续的血氧数据)】的数据库操作Service实现
 * @createDate 2024-10-15 11:06:50
 */
@Service
@Slf4j
public class WatchContinuousSpo2DataServiceImpl implements WatchContinuousSpo2DataService {
    @Autowired
    private WatchContinuousSpo2DataMapper mapper;

    @Override
    public Boolean insert(WatchContinuousSpo2Data watchContinuousSpo2Data) {
        Integer num = mapper.queryByCreateTime(watchContinuousSpo2Data.getCreateTime());
        if (num != null && num > 0) {
            return false;
        }
        try {
            mapper.insert(watchContinuousSpo2Data);
        } catch (Exception e) {
            log.error("===============watchContinuousSpo2Data:insert=========================={}", e.getMessage());
            return false;
        }
        return true;
    }

    @Override
    public WatchContinuousSpo2Data getLastByDeviceId(String deviceId) {
        return mapper.getLastByDeviceId(deviceId);
    }

    @Override
    public List<WatchContinuousSpo2Data> queryByDateAndDeviceIdAndState(String date, String deviceId) {
        return mapper.queryByDateAndDeviceIdAndState(date, deviceId);
    }

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

    @Override
    public List<WatchContinuousSpo2Data> querySpPageByDate(String startTime, String endTime, String deviceId, Integer num, Integer size) {
        return mapper.querySpPageByDate(startTime, endTime, deviceId, num, size);

    }

    @Override
    public int countSpPageByDate(String startTime, String endTime, String deviceId) {
        return mapper.countSpPageByDate(startTime,endTime,deviceId);
    }

    @Override
    public List<WatchContinuousSpo2Data> querySpByDate(String startTime, String endTime, String deviceId) {
        return mapper.querySpByDate(startTime, endTime, deviceId);
    }

    @Override
    public List<WatchPulseRateVo> queryOneByDateAndDeviceIdAndState(String date, String deviceId) {
        if (StringUtils.isBlank(date)) {
            date = DateUtils.dateTimeNow("yyyy-MM-dd");
        }
        List<WatchContinuousSpo2Data> spo2Data = mapper.queryOneByDateAndDeviceIdAndState(date,deviceId);
        ArrayList<WatchPulseRateVo> list = new ArrayList<>();
        if (spo2Data != null) {
            for (WatchContinuousSpo2Data data : spo2Data) {
                //处理数据
                List<Integer> hr = data.getHr();
                for (int i = 0; i < hr.size(); i++) {
                    if (hr.get(i) == 0){
                        continue;
                    }
                    WatchPulseRateVo vo = new WatchPulseRateVo();
                    vo.setPulseRate(hr.get(i));
                    // 计算延后一秒的时间
                    vo.setTime(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS,
                            new Date(DateUtils.dateTime(DateUtils.YYYY_MM_DD_HH_MM_SS,
                                    data.getCreateTime()).getTime() + (1000L * (i)))));
                    list.add(vo);
                }
            }

        }

        return list;
    }

}
