package com.fs.watch.service.impl;

import com.fs.common.utils.DateUtils;
import com.fs.common.utils.StringUtils;
import com.fs.watch.domain.WatchBloodGlucoseData;
import com.fs.watch.domain.WatchThirdBpData;
import com.fs.watch.mapper.WatchBloodGlucoseDataMapper;
import com.fs.watch.service.WatchBloodGlucoseDataService;
import com.fs.watch.service.WatchThirdBpDataService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
* @author Administrator
* @description 针对表【watch_blood_glucose_data(血糖数据)】的数据库操作Service实现
* @createDate 2024-10-15 11:06:50
*/
@Service
@Slf4j
public class WatchBloodGlucoseDataServiceImpl implements WatchBloodGlucoseDataService {
    @Autowired
    private WatchBloodGlucoseDataMapper mapper;

    @Autowired
    private WatchThirdBpDataService watchThirdBpDataService;


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

    private Map<String, Object> getBloodGlucoseDataMap(WatchBloodGlucoseData watchBloodGlucoseData) {
        Map<String, Object> params = new HashMap<>();
        params.put("id", watchBloodGlucoseData.getId().toString());
        params.put("deviceId", watchBloodGlucoseData.getDeviceId());
        params.put("bloodGlucose", watchBloodGlucoseData.getBloodGlucose());
        params.put("status", watchBloodGlucoseData.getStatus());
        params.put("createTime", watchBloodGlucoseData.getCreateTime());
        return params;
    }

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

    @Override
    public List<WatchBloodGlucoseData> queryBgByDate(Date startTime, Date endTime, String deviceId, Integer status) {
        if (StringUtils.isNotBlank(deviceId)) {
            List<WatchBloodGlucoseData> data = mapper.queryBgByDate(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, startTime),
                    DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, endTime),
                    deviceId,
                    status);
            return data;
        }
        return null;
    }

    /**
     * 高风险：血糖值大于 7.0 mmol/L，返回 1。
     * 一般：血糖值在 6.0 mmol/L 和 7.0 mmol/L 之间，返回 3。
     * 良好：血糖值在 5.0 mmol/L 和 6.0 mmol/L 之间，返回 4。
     * 正常：血糖值在 3.9 mmol/L和 4.0 mmol/L 之间，，返回 5。
     * 不良：血糖值小于 3.9 mmol/L 之间，返回 2。
     * 获得血糖状态
     *
     * @param glu
     * @return 状态码
     */
    @Override
    public Integer getStatus(int glu) {
        if (glu > 7.0) {
            return 1; // 高风险
        } else if (glu > 6.0) {
            return 3; // 一般
        } else if (glu >= 5.0) {
            return 4; // 良好
        } else if (glu >= 3.9) {
            return 5; // 正常
        } else {
            return 2;
        }

    }

    @Override
    public List<Map<String, Object>> countBgByDate(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;
    }

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

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

    @Override
    public List<WatchBloodGlucoseData> queryBgPageByDate(Date startTime, Date endTime, String deviceId, Integer status, Integer num, Integer size) {
        if (StringUtils.isNotBlank(deviceId)) {
            return mapper.queryBgPageByDate(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, startTime),
                    DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, endTime),
                    deviceId, status, num, size);
        } else {
            return null;
        }
    }
}
