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.WatchDeviceInfo;
import com.fs.watch.domain.WatchThirdBpData;
import com.fs.watch.enums.WatchBpStatusEnum;
import com.fs.watch.mapper.WatchBloodPressureDataMapper;
import com.fs.watch.mapper.WatchDeviceInfoMapper;
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.*;

/**
* @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;

    @Autowired
    private WatchDeviceInfoMapper watchDeviceInfoMapper;

    @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;
        }
        //修改设备健康状态
        WatchDeviceInfo info = new WatchDeviceInfo();
        info.setDeviceNumber(watchBloodPressureData.getDeviceId());
        if (watchBloodPressureData.getStatus() != null){
            if (!Objects.equals(watchBloodPressureData.getStatus(), WatchBpStatusEnum.NORMAL.getValue())) {
                info.setIsNormal(0);
            } else {
                info.setIsNormal(1);
            }
            watchDeviceInfoMapper.updateByDeviceNumber(info);
        }
        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;
    }


    /**
     * 统计血压异常次数
     * @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;
        }
    }
}
