package com.fs.watch.service.impl;

import com.fs.common.utils.DateUtils;
import com.fs.common.utils.StringUtils;
import com.fs.watch.domain.WatchDeviceInfo;
import com.fs.watch.domain.WatchThirdBkData;
import com.fs.watch.enums.WatchBkStatusEnum;
import com.fs.watch.mapper.WatchDeviceInfoMapper;
import com.fs.watch.mapper.WatchThirdBkDataMapper;
import com.fs.watch.service.WatchThirdBkDataService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
* @author Administrator
* @description 针对表【watch_third_bk_data(设备三方血酮)】的数据库操作Service实现
* @createDate 2024-10-15 11:06:51
*/
@Service
@Slf4j
public class WatchThirdBkDataServiceImpl
implements WatchThirdBkDataService {
    @Autowired
    private WatchThirdBkDataMapper mapper;
    @Autowired
    private WatchDeviceInfoMapper watchDeviceInfoMapper;

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

    @Override
    public Boolean insert(WatchThirdBkData watchThirdBkData) {
        Integer num = mapper.queryByCreateTime(watchThirdBkData.getCreateTime());
        if (num != null && num > 0) {
            return false;
        }
        try {
            mapper.insert(watchThirdBkData);
        } catch (Exception e) {
            log.error("===============watchThirdBkData:insert=========================={}", e.getMessage());
            return false;
        }
        //修改设备健康状态
        WatchDeviceInfo info = new WatchDeviceInfo();
        info.setDeviceNumber(watchThirdBkData.getDeviceId());
        if (watchThirdBkData.getStatus() != null){
            if (!Objects.equals(watchThirdBkData.getStatus(), WatchBkStatusEnum.NORMAL.getValue())) {
                info.setIsNormal(0);
            } else {
                info.setIsNormal(1);
            }
            watchDeviceInfoMapper.updateByDeviceNumber(info);
        }
        return true;
    }


    private Map<String, Object> getThirdBkDataMap(WatchThirdBkData watchThirdBkData) {
        Map<String, Object> params = new HashMap<>();
        params.put("id", watchThirdBkData.getId().toString());
        params.put("deviceId", watchThirdBkData.getDeviceId());
        params.put("val", watchThirdBkData.getVal());
        params.put("status", watchThirdBkData.getStatus());
        params.put("createTime", watchThirdBkData.getCreateTime());
        return params;
    }

    /**
     * 获取血酮状态 0:正常 1:异常
     *
     * @param bloodKetones
     * @return
     */
    @Override
    public Integer getStatus(int bloodKetones) {
        //血酮正常值是0.05-0.34mmol/L。酮体是机体脂肪氧化代谢产生的中间代谢产物，正常值一般在0.05-0.34mmol/L，当糖代谢发生障碍，酮体血浓度超过肾阈值，可产生酮尿。
        //当血酮升高，>1.0mmol/L为高血酮，>3.0mmoL提示可有酸中毒，常见的有糖尿病酮症酸中毒。糖尿病酮症酸中毒时，纠正酮症常比纠正高血糖缓慢
        if (bloodKetones < 0.05 || bloodKetones > 0.34) {
            return 1;
        } else {
            return 0;
        }
    }

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