package cn.iocoder.yudao.module.system.service.watch;

import cn.iocoder.yudao.framework.common.util.date.DateUtils;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.DeviceProductDO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.ElderlyVitalSignsDO;
import cn.iocoder.yudao.module.system.dal.mysql.biz.DeviceProductMapper;
import cn.iocoder.yudao.module.system.dal.mysql.biz.ElderlyVitalSignsMapper;
import cn.iocoder.yudao.module.system.enums.common.BooleanEnum;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.ZoneId;
import java.util.Date;
import java.util.Objects;

@Service
public class WatchDeviceServiceImpl implements WatchDeviceService {

    @Resource
    private DeviceProductMapper productMapper;

    @Autowired
    private ElderlyVitalSignsMapper elderlyVitalSignsMapper;

    @Async
    @Override
    public void asyncHandleWatchData(JSONObject jsonObject) {
        String imei = (String) jsonObject.get("imei");
        String cmd = (String) jsonObject.get("cmd");
        Long pushTimestamp = (Long) jsonObject.get("pushTimestamp");
        Date date = new Date(pushTimestamp);

        ElderlyVitalSignsDO signsDO = new ElderlyVitalSignsDO();
        signsDO.setDataSourceType(2);
        signsDO.setCreatedTime(date);
        signsDO.setSnCode(imei);
        signsDO.setMeasuringTime(date.toInstant().atZone(ZoneId.systemDefault()).toLocalTime());
        //todo  缺少租户，长者id，长者名称,设备名称
        DeviceProductDO deviceProductDO = productMapper.selectOne(new LambdaQueryWrapperX<DeviceProductDO>().eq(DeviceProductDO::getSnCode, imei));
        if (deviceProductDO != null && Objects.equals(deviceProductDO.getBindingStatus(), BooleanEnum.TRUE.getValue())) {
            //todo 分析绑定的类型
            signsDO.setDeviceName(deviceProductDO.getDeviceName());
            signsDO.setElderName(deviceProductDO.getBindingUserName());
            signsDO.setElderId(deviceProductDO.getBindingUserId());
            signsDO.setTenantId(deviceProductDO.getTenantId());
        }
//    signsDO.setCreatedBy(SecurityFrameworkUtils.getLoginUserNickname());

        switch (cmd) {
            case "CZW01":  //步数
                String step = (String) jsonObject.get("step");
                signsDO.setStep(Integer.parseInt(step));
                elderlyVitalSignsMapper.insert(signsDO);
                break;
            case "CZW02": //血压
                Double dbp = (Double) jsonObject.get("dbp");
                Double sbp = (Double) jsonObject.get("sbp");
                Integer pulse = (Integer) jsonObject.get("pulse");
                Long recordTime = (Long) jsonObject.get("recordTime");

                signsDO.setPulse(pulse.toString());
                signsDO.setDiastolicPressure(dbp.toString());
                signsDO.setSystolicPressure(sbp.toString());
                signsDO.setMeasuringDate(new Date(recordTime));
                elderlyVitalSignsMapper.insert(signsDO);
                break;
            case "CZW03": //心率
                Double heartRate = (Double) jsonObject.get("heartRate");
                signsDO.setHeartRate(heartRate.toString());
                signsDO.setMeasuringDate(new Date((Long) jsonObject.get("recordTime")));
                elderlyVitalSignsMapper.insert(signsDO);
                break;

            case "CZW04": //睡眠
                Integer allSleepTime = (Integer) jsonObject.get("allSleepTime");
                String sleepDate = (String) jsonObject.get("sleepDate");
                Integer lowSleepTime = (Integer) jsonObject.get("lowSleepTime");

                Integer deepSleepTime = (Integer) jsonObject.get("deepSleepTime");
                Integer wakeTime = (Integer) jsonObject.get("wakeTime");
                Integer remSleepTime = (Integer) jsonObject.get("remSleepTime");

                signsDO.setLowSleepTime(String.valueOf(lowSleepTime));
                signsDO.setAllSleepTime(String.valueOf(allSleepTime));
                signsDO.setSleepDate(sleepDate);
                signsDO.setDeepSleepTime(String.valueOf(deepSleepTime));
                signsDO.setWakeTime(String.valueOf(wakeTime));
                signsDO.setRemSleepTime(String.valueOf(remSleepTime));
                elderlyVitalSignsMapper.insert(signsDO);
                break;

            case "CZW05": //血氧
                Double oxygen = (Double) jsonObject.get("oxygen");
                signsDO.setBloodOxygenSaturation(oxygen.toString());
                signsDO.setMeasuringDate(new Date((Long) jsonObject.get("recordTime")));
                elderlyVitalSignsMapper.insert(signsDO);
                break;
            case "CZW06": //体温
                String temperature = (String) jsonObject.get("temperature");
                signsDO.setTemperature(temperature);
                signsDO.setMeasuringDate(new Date((Long) jsonObject.get("recordTime")));
                elderlyVitalSignsMapper.insert(signsDO);
                break;
            case "CZW07": //经纬度定位
                break;
        }
    }
}
