package com.fs.watch.scheduled;

import com.fs.his.domain.FsHealthData;
import com.fs.his.service.IFsHealthDataService;
import com.fs.watch.domain.*;
import com.fs.watch.domain.vo.AppWatchSleepDataVo;
import com.fs.watch.domain.vo.WatchDeviceInfoAndUserIdVo;
import com.fs.watch.domain.vo.WatchSportDataVo;
import com.fs.watch.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import java.util.List;

@Component
public class HealthDataTask {
    @Autowired
    private WatchBloodGlucoseDataService watchBloodGlucoseDataService;
    @Autowired
    private WatchBloodPressureDataService watchBloodPressureDataService;
    @Autowired
    private WatchHeartRateDataService watchHeartRateDataService;
    @Autowired
    private WatchTemperatureDataService watchTemperatureDataService;
    @Autowired
    private WatchThirdUaDataService watchThirdUaDataService;
    @Autowired
    private WatchSleepDataService watchSleepDataService;
    @Autowired
    private WatchDeviceInfoService watchDeviceInfoService;
    @Autowired
    private IFsHealthDataService healthDataService;
    @Autowired
    private WatchSportDataService watchSportDataService;


    // 使用cron表达式设置每天凌晨12:00执行任务
    @Scheduled(cron = "0 0 0 * * ?")
    public void pushHealthData() {
       //1.查询腕表用户
        List<WatchDeviceInfoAndUserIdVo> vos = watchDeviceInfoService.selectByUserExist();
        if (vos != null && !vos.isEmpty()) {
            for (WatchDeviceInfoAndUserIdVo vo : vos) {
                if (vo.getUserId()!=null){
                    //2.创建健康数据对象
                    FsHealthData fsHealthData = new FsHealthData();
                    fsHealthData.setUserId(vo.getUserId());
                    String deviceId = vo.getDeviceNumber();
                    if (deviceId != null && !deviceId.isEmpty()) {
                        //2.1体重 不能测量

                        //2.2心率
                        WatchHeartRateData heartRateData = watchHeartRateDataService.getLatest(deviceId);
                        fsHealthData.setHeartRate((heartRateData==null?"":heartRateData.getAvgBpm())+"bpm");
                        //2.3体温
                        WatchTemperatureData temperatureData = watchTemperatureDataService.getLatest(deviceId);
                        fsHealthData.setTemperature((temperatureData==null?"":temperatureData.getEstTemp())+"℃");
                        //2.4血压
                        WatchThirdBpData bpData = watchBloodPressureDataService.getLatest(deviceId);
                        fsHealthData.setBloodPressure(bpData==null?"":("sdp:"+bpData.getSbp()+"mmgh dbp:" + bpData.getDbp()+"mmgh"));
                        //2.5血糖
                        WatchBloodGlucoseData bgData = watchBloodGlucoseDataService.getLatest(deviceId);
                        fsHealthData.setBloodSugar(bgData==null?"":bgData.getBloodGlucose().toString());
                        //2.6尿酸
                        WatchThirdUaData uaData = watchThirdUaDataService.getLatest(deviceId);
                        fsHealthData.setUric(uaData==null?"":uaData.getVal().toString());
                        //2.7睡眠
                        // 创建Calendar实例
                        Calendar calendar = Calendar.getInstance();
                        // 将日期减去1天
                        calendar.add(Calendar.DATE, -1);
                        // 创建SimpleDateFormat实例用于格式化日期
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                        // 格式化前一天的日期
                        String yesterday = sdf.format(calendar.getTime());
                        AppWatchSleepDataVo appWatchSleepDataVo = watchSleepDataService.queryByDateAndDeviceIdAndState(yesterday, deviceId);
                        fsHealthData.setSleep("睡眠评分为:" + (appWatchSleepDataVo==null?"0":appWatchSleepDataVo.getScore()));
                        //2.8步数
                        WatchSportDataVo sportData = watchSportDataService.queryByDateAndDeviceIdAndState(yesterday, deviceId);
                        fsHealthData.setStep(sportData==null?"0":sportData.getStepTotal().toString());
                        //3 保存数据
                        healthDataService.insertFsHealthData(fsHealthData);
                    }

                }
            }
        }

    }
}
