package com.fs.watch.service.impl;

import com.fs.common.utils.DateUtils;
import com.fs.common.utils.StringUtils;
import com.fs.watch.domain.WatchThirdBpData;
import com.fs.watch.mapper.WatchThirdBpDataMapper;
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_third_bp_data(设备三方血压)】的数据库操作Service实现
* @createDate 2024-10-15 11:06:51
*/
@Service
@Slf4j
public class WatchThirdBpDataServiceImpl implements WatchThirdBpDataService {
    @Autowired
    private WatchThirdBpDataMapper mapper;

    /**
     * 新增数据
     *
     * @param watchThirdBpData 实例对象
     * @return 实例对象
     */
    @Override
    public Boolean insert(WatchThirdBpData watchThirdBpData) {
        //判断创建时间是否唯一
        Integer num = mapper.queryByCreateTime(watchThirdBpData.getCreateTime());
        if (num != null && num > 0) {
            return false;
        }
        try {
            mapper.insert(watchThirdBpData);
        } catch (Exception e) {
            log.error("===============watchThirdBpData:insert=========================={}", e.getMessage());
            return false;
        }
        return true;
    }

    private Map<String, Object> getThirdBpDataMap(WatchThirdBpData watchThirdBpData) {
        Map<String, Object> params = new HashMap<>();
        params.put("id", watchThirdBpData.getId().toString());
        params.put("deviceId", watchThirdBpData.getDeviceId());
        params.put("sbp", watchThirdBpData.getSbp());
        params.put("dbp", watchThirdBpData.getDbp());
        params.put("hr", watchThirdBpData.getHr());
        params.put("pulse", watchThirdBpData.getPulse());
        params.put("mode", watchThirdBpData.getMode());
        params.put("createTime", watchThirdBpData.getCreateTime());
        return params;
    }

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

    @Override
    public List<WatchThirdBpData> queryBpByDate(String startTime, String endTime, String deviceId, Integer status) {
        return mapper.queryBpByDate(startTime,endTime,deviceId,status);
    }



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

    /**
     * /**
     *      * 重度: 收缩压 > 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
     * @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;
    }

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

}
