package com.fs.watch.service.impl;

import com.fs.common.utils.DateUtils;
import com.fs.common.utils.StringUtils;
import com.fs.watch.domain.WatchBasicInfo;
import com.fs.watch.domain.WatchHeartRateData;
import com.fs.watch.domain.vo.WatchSleepDataVo;
import com.fs.watch.mapper.WatchBasicInfoMapper;
import com.fs.watch.param.WatchDataQueryParam;
import com.fs.watch.service.*;
import com.fs.watch.utils.MyHttpUtils;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.mybatis.spring.MyBatisSystemException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fs.watch.domain.vo.GnssVo;

import javax.annotation.Resource;
import java.util.*;

/**
* @author Administrator
* @description 针对表【watch_basic_info(设备基础数据)】的数据库操作Service实现
* @createDate 2024-10-15 11:06:50
*/
@Service
@Slf4j
public class WatchBasicInfoServiceImpl implements WatchBasicInfoService {
    @Resource
    private WatchHeartRateDataService watchHeartRateDataService;
    @Autowired
    private WatchRriDataService watchRriDataService;
    @Autowired
    private WatchSleepDataService watchSleepDataService;
    @Autowired
    private WatchBasicInfoMapper watchBasicInfoMapper;

    /**
     * 新增数据
     *
     * @param watchBasicInfo 实例对象
     * @return 实例对象
     */
    @Override
    public Boolean insert(WatchBasicInfo watchBasicInfo) {
        //判断创建时间是否唯一
        Integer num = watchBasicInfoMapper.queryByCreateTime(watchBasicInfo.getCreateTime());
        if (num != null && num > 0) {
            return false;
        }
        try {
            watchBasicInfoMapper.insert(watchBasicInfo);
        } catch (Exception e) {
            log.error("===============watchBasicInfo:insert=========================={}", e.getMessage());
            return false;
        }
        return true;
    }


    /**
     * 通过ID查询单条数据
     *
     * @param deviceId 主键
     * @return 实例对象
     */
    @Override
    public WatchBasicInfo queryByDeviceId(String deviceId) {
//        return watchBasicInfoMapper.queryByDeviceId(deviceId);
        log.info("DeviceId: {}", deviceId);
        if (deviceId == null) {
            log.error("DeviceId is null, cannot proceed with the query.");
        }
        try {
            // 执行查询
            return watchBasicInfoMapper.queryByDeviceId(deviceId);
        } catch (Exception e) {
            log.error("Exception during query execution: ", e);
            throw new MyBatisSystemException(e);
        }


    }

    @Override
    public WatchBasicInfo queryLastByDeviceId(String deviceId) {
        return watchBasicInfoMapper.getLastByDeviceId(deviceId);
    }


    private Map<String, Object> getBasicInfoMap(WatchBasicInfo watchBasicInfo) {
        Map<String, Object> params = new HashMap<>();
        params.put("id", watchBasicInfo.getId().toString());
        params.put("deviceId", watchBasicInfo.getDeviceId());
        params.put("steps", watchBasicInfo.getSteps());
        params.put("distance", watchBasicInfo.getDistance());
        params.put("calorie", watchBasicInfo.getCalorie());
        params.put("battery", watchBasicInfo.getBattery());
        params.put("rssi", watchBasicInfo.getRssi());
        params.put("jsonDataGnssLon", watchBasicInfo.getGnssLon().toString());
        params.put("jsonDataGnssLat", watchBasicInfo.getGnssLat().toString());
        params.put("jsonDataGnssLocType", watchBasicInfo.getGnssLocType().toString());
        params.put("jsonDataCreateTime", watchBasicInfo.getGnssTime().toString());
        params.put("createTime", watchBasicInfo.getCreateTime());
        return params;
    }

    @Override
    public List<GnssVo> queryGnssByDateAndDeviceId(String date, String deviceId) {
        if (StringUtils.isBlank(date)) {
            date = DateUtils.dateTimeNow("yyyy-MM-dd");
        }
        List<WatchBasicInfo> basicInfos = watchBasicInfoMapper.queryGnssByDateAndDeviceId(date,deviceId);
        List<GnssVo> gnssVos = getGnssVos(basicInfos);

        return gnssVos;
    }

    private static @NotNull List<GnssVo> getGnssVos(List<WatchBasicInfo> basicInfos) {
        List<GnssVo> gnssVos = new ArrayList<>();
        for (WatchBasicInfo basicInfo : basicInfos) {
            List<String> gnssTimes = basicInfo.getGnssTime();
            GnssVo gnssVo = new GnssVo();
            if (gnssTimes == null || gnssTimes.isEmpty()) {
                continue;
            }
            int i = gnssTimes.size() - 1;
            gnssVo.setTime(basicInfo.getCreateTime());
            gnssVo.setLng(basicInfo.getGnssLon().get(i));
            gnssVo.setLat(basicInfo.getGnssLat().get(i));
            gnssVo.setLocType(basicInfo.getGnssLocType().get(i));
            gnssVos.add(gnssVo);
        }
        return gnssVos;
    }

    @Override
    public Map<String, Object> queryOverviewsByDateAndDeviceId(String date, String deviceId) {
        Map<String, Object> map = new HashMap<>();
        //心率
        WatchHeartRateData hrData = watchHeartRateDataService.queryByDeviceId(deviceId);
        if (hrData != null) {
            map.put("hr", hrData.getAvgBpm());
        }
        //房颤
        Map<String, Object> afData = this.watchRriDataService.queryByDateAndDeviceId(date, deviceId);
        if (afData != null && afData.get("mes") != null) {
            map.put("af", afData.get("mes").toString().split("\\.")[0]);
        }
        //步数
        //睡眠
        WatchSleepDataVo sleepData = watchSleepDataService.queryByDateAndDeviceIdAndState(date, deviceId);
        if (sleepData != null) {
            map.put("sleep", sleepData.getSleepTime());
        }

        return map;
    }

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

    @Override
    public List<GnssVo> queryGnssByDate(String starTime, String endTime, String deviceId) {
        List<WatchBasicInfo> basicInfos = watchBasicInfoMapper.queryGnssByDate(starTime,endTime,deviceId);
        List<GnssVo> gnssVos = getGnssVos(basicInfos);
        return gnssVos;
    }
}
