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.WatchDeviceInfo;
import com.fs.watch.enums.WatchBgStatusEnum;
import com.fs.watch.mapper.WatchBloodGlucoseDataMapper;
import com.fs.watch.mapper.WatchDeviceInfoMapper;
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.*;

/**
* @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;
    @Autowired
    private WatchDeviceInfoMapper watchDeviceInfoMapper;


    @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;
        }
        //修改设备健康状态
        WatchDeviceInfo info = new WatchDeviceInfo();
        info.setDeviceNumber(watchBloodGlucoseData.getDeviceId());
        if (watchBloodGlucoseData.getStatus() != null){
            if (!Objects.equals(watchBloodGlucoseData.getStatus(), WatchBgStatusEnum.NORMAL.getValue())) {

                info.setIsNormal(0);
            } else {
                info.setIsNormal(1);
            }
            watchDeviceInfoMapper.updateByDeviceNumber(info);
        }

        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;
    }


    @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;
        }
    }
}
