package com.fs.watch.service.impl;

import com.fs.common.utils.DateUtils;
import com.fs.common.utils.StringUtils;
import com.fs.watch.domain.WatchBloodPressureData;
import com.fs.watch.domain.WatchThirdBpData;
import com.fs.watch.mapper.WatchBloodPressureDataMapper;
import com.fs.watch.service.WatchBloodPressureDataService;
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_pressure_data(设备血压数据)】的数据库操作Service实现
* @createDate 2024-10-15 11:06:50
*/
@Service
@Slf4j
public class WatchBloodPressureDataServiceImpl implements WatchBloodPressureDataService {
    @Autowired
    private WatchBloodPressureDataMapper mapper;

    @Autowired
    private WatchThirdBpDataService watchThirdBpDataService;

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

    private Map<String, Object> getBloodPressureDataMap(WatchBloodPressureData watchBloodPressureData) {
        Map<String, Object> params = new HashMap<>();
        params.put("id", watchBloodPressureData.getId().toString());
        params.put("deviceId", watchBloodPressureData.getDeviceId());
        params.put("sbp", watchBloodPressureData.getSbp());
        params.put("dbp", watchBloodPressureData.getDbp());
        params.put("status", watchBloodPressureData.getStatus());
        params.put("createTime", watchBloodPressureData.getCreateTime());
        return params;
    }

    @Override
    public List<?> queryByDateAndDeviceId(String date, String deviceId) {

        //优先查询血压计数据(三方数据)
        List<WatchThirdBpData> watchThirdBpDatas = watchThirdBpDataService.queryByDateAndDeviceId(date, deviceId);
        if (watchThirdBpDatas.size()>0){
            return watchThirdBpDatas;
        } else {
            if (StringUtils.isBlank(date)) {
                date = DateUtils.dateTimeNow("yyyy-MM-dd");
            }
            return mapper.queryByDateAndDeviceId(date,deviceId);
        }
    }

    @Override
    public List<?> queryBpByDate(Date startTime, Date endTime, String deviceId,Integer status) {
        if (StringUtils.isNotBlank(deviceId)) {
            //优先查询血压计数据(三方数据)
            List<WatchThirdBpData> watchThirdBpDatas = watchThirdBpDataService.queryBpByDate(
                    DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, startTime),
                    DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, endTime),
                    deviceId,
                    status);
            if (!watchThirdBpDatas.isEmpty()){
                return watchThirdBpDatas;
            } else {
                return mapper.queryBpByDate(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, startTime),
                        DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, endTime),
                        deviceId,
                        status);
            }
        }
        return null;
    }

    /**
     * 重度: 收缩压 > 180 mmHg 或 舒张压 > 110 mmHg
     * 中度: 收缩压在 160-179 mmHg 之间 或 舒张压在 100-109 mmHg 之间
     * 轻度: 收缩压在 140-159 mmHg 之间 或 舒张压在 90-99 mmHg 之间
     * 正常高值: 收缩压在 130-139 mmHg 之间 或 舒张压在 85-89 mmHg 之间
     * 偏低: 收缩压 < 90 mmHg 或 舒张压 < 60 mmHg
     * 正常: 收缩压在 90-139 mmHg 之间 且 舒张压在 60-89 mmHg 之间
     * @param sbp
     * @param dbp
     * @return
     */
    @Override
    public Integer getStatus(int sbp, int dbp) {
        int status;
        if (sbp > 180 || dbp > 110) {
            status = 1; // 重度
        } else if (sbp >= 160 || dbp >= 100) {
            status = 2; // 中度
        } else if (sbp >= 140 || dbp >= 90) {
            status = 3; // 轻度
        } else if (sbp >= 130 || dbp >= 85) {
            status = 4; // 正常高值
        } else if (sbp < 90 || dbp < 60) {
            status = 5; // 偏低
        } else {
            status = 6; // 正常
        }
        return status;
    }

    /**
     * 统计血压异常次数
     * @param startTime
     * @param endTime
     * @param deviceId
     * @return
     */
    @Override
    public List<Map<String, Object>> countBpByDate(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 WatchThirdBpData getLatest(String deviceId) {
        //优先查询血压计数据(三方数据)
        WatchThirdBpData watchThirdBpData = watchThirdBpDataService.getLatest(deviceId);
//        WatchBloodPressureData watchBloodPressureData = mapper.getLatest(deviceId);
//        WatchBloodPressureData watchBloodPressureData = mapper.getLatest(deviceId);
//        if (watchThirdBpData!=null && watchBloodPressureData!=null) {
//            if (DateUtils.parseDate(watchThirdBpData.getCreateTime()).before(DateUtils.parseDate(watchBloodPressureData.getCreateTime()))) {
//            } else {
//                BeanUtils.copyProperties(watchBloodPressureData,watchThirdBpData);
//            }
//            return watchBloodPressureData;
//        }
        return watchThirdBpData;

    }

    @Override
    public int countBpPageByDate(Date startTime, Date endTime, String deviceId,Integer status) {
        return mapper.countBpPageByDate(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<WatchBloodPressureData> 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;
        }
    }
}
