package com.fs.watch.service.impl;

import com.fs.common.utils.DateUtils;
import com.fs.common.utils.StringUtils;
import com.fs.watch.domain.WatchHeartRateData;
import com.fs.watch.domain.vo.WatchHeartRateDataVo;
import com.fs.watch.mapper.WatchHeartRateDataMapper;
import com.fs.watch.service.WatchHeartRateDataService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author Administrator
 * @description 针对表【watch_heart_rate_data(设备心率数据)】的数据库操作Service实现
 * @createDate 2024-10-15 11:06:50
 */
@Service
@Slf4j
public class WatchHeartRateDataServiceImpl implements WatchHeartRateDataService {
    @Autowired
    private WatchHeartRateDataMapper mapper;

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

    @Override
    public WatchHeartRateData queryByDeviceId(String deviceId) {
        return mapper.queryByDeviceId(deviceId);
    }

    @Override
    public WatchHeartRateDataVo queryByDate(String date, String deviceId) {
        if (StringUtils.isBlank(date)) {
            date = DateUtils.dateTimeNow("yyyy-MM-dd");
        }
        List<WatchHeartRateData> list = mapper.queryByDate(date, deviceId);
        return getWatchHeartRateDataVo(list);
    }

    private static WatchHeartRateDataVo getWatchHeartRateDataVo(List<WatchHeartRateData> list) {
        WatchHeartRateDataVo vo = new WatchHeartRateDataVo();
        vo.setList(list);
        Integer max = 0;
        Integer min = 0;
        Integer avg = 0;
        if (list.size() > 0) {
            max = list.stream().max(Comparator.comparingInt(WatchHeartRateData::getMaxBpm)).get().getMaxBpm();
            min = list.stream().min(Comparator.comparingInt(WatchHeartRateData::getMinBpm)).get().getMinBpm();
            //计算平均值
            OptionalDouble averageValue = list.stream().mapToInt(WatchHeartRateData::getAvgBpm).average();
            // 如果平均值存在，将其转换为 int 类型并输出
            if (averageValue.isPresent()) {
                avg = (int) averageValue.getAsDouble();  // 将 double 转换为 int
            }
        }

        vo.setAvg(avg);
        //最大值
        vo.setMax(max);
        //最小值
        vo.setMin(min);
        return vo;
    }

    @Override
    public WatchHeartRateDataVo list(Date startTime, Date endTime, String deviceId) {
        if (StringUtils.isNotBlank(deviceId)) {
            List<WatchHeartRateData> data = mapper.list(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, startTime),
                    DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, endTime),
                    deviceId);
            return getWatchHeartRateDataVo(data);
        }
        return null;
    }

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

    @Override
    public int countHeartRateByDate(Date startTime, Date endTime, String deviceId) {
        return mapper.countHeartRateByDate(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, startTime),
                DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, endTime),
                deviceId);
    }


    @Override
    public List<WatchHeartRateData> queryHeartPageByDate(Date startTime, Date endTime, String deviceId, Integer pageNum, Integer pageSize) {
        if (StringUtils.isNotBlank(deviceId)) {
            return mapper.queryHeartPageByDate(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, startTime),
                    DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, endTime),
                    deviceId, pageNum, pageSize);
        } else {
            return null;
        }
    }

    @Override
    public Integer getStatus(int maxBpm, int minBpm) {
        if (maxBpm > 100) {
            return 2;
        } else if (minBpm < 60) {
            return 1;
        } else {
            return 0;
        }
    }

    @Override
    public List<Map<String, Object>> countHeartByDate(Date startTime, Date endTime, String deviceId) {
        if (StringUtils.isBlank(deviceId)) {
            return null;
        }
        //返回对象
        List<Map<String, Object>> res = null;
        res = mapper.countBpByDate(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, startTime),
                DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, endTime), deviceId);

        return res;
    }
}
