package com.fs.watch.service.impl;

import com.fs.common.config.WatchConfig;
import com.fs.common.utils.DateUtils;
import com.fs.common.utils.StringUtils;
import com.fs.watch.domain.WatchEcgData;
import com.fs.watch.mapper.WatchEcgDataMapper;
import com.fs.watch.service.WatchEcgDataService;
import com.fs.watch.utils.MyHttpUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
* @author Administrator
* @description 针对表【watch_ecg_data(设备心电数据，一次测量会分成很多包上传，同一次测量的心电数据包时间是一样的，可以通过时间分辨每一次测量的数据)】的数据库操作Service实现
* @createDate 2024-10-15 11:06:50
*/
@Service
@Slf4j
public class WatchEcgDataServiceImpl implements WatchEcgDataService {
    @Autowired
    private WatchEcgDataMapper mapper;

    @Autowired
    private WatchConfig watchConfig;

    private MyHttpUtils httpUtils = new MyHttpUtils();



    @Override
    public Boolean insert(WatchEcgData watchEcgData) {
        Integer num = mapper.queryByCreateTime(watchEcgData.getCreateTime());
        if (num != null && num > 0) {
            return false;
        }
        try {
            mapper.insert(watchEcgData);
        } catch (Exception e) {
            log.error("===============watchEcgData:insert=========================={}", e.getMessage());
            return false;
        }
        return true;
    }

    @Override
    public Map<String, Object> queryByDateAndDeviceId(String date, String deviceId) {
        //1.查询数据
        List<WatchEcgData> data = this.getByDateAndDeviceId(date, deviceId);
        Map<String, Object> map = new HashMap<>();
        map.put("data", data);
        //2.分析数据得到结果
        //2.1组装分析所需参数
        List<Integer> params = new ArrayList<>();
        for (WatchEcgData ecgData : data) {
            params.addAll(ecgData.getVal());
        }
        HashMap<String, Object> param = new HashMap<>();
        param.put("ecg_list", params);
        param.put("device_id", deviceId);
        param.put("account", watchConfig.getAccount1());
        param.put("password", watchConfig.getPassword1());
        //2.2发送请求
        Map resData = httpUtils.getPost2("https://api1.iwown.com/algoservice", "/calculation/ecg", param);
        if (resData == null) {
            param.put("account", watchConfig.getAccount2());
            param.put("password", watchConfig.getPassword2());
            //2.2发送请求
            resData = httpUtils.getPost2("https://api1.iwown.com/algoservice", "/calculation/ecg", param);
            if (resData == null) {
                param.put("account", watchConfig.getAccount3());
                param.put("password", watchConfig.getPassword3());
                //2.2发送请求
                resData = httpUtils.getPost2("https://api1.iwown.com/algoservice", "/calculation/ecg", param);
            }
        }
        if (resData != null) {
            /**
             * 0 无结果, 有干扰或者数据异常
             * 1 窦性心率
             * 2 窦性心率过缓
             * 3 窦性心率过速
             * 4 心律不齐（早搏）
             * 5 心律不齐（房颤）
             * 6 心率过速（室上性心动过速）
             */
            Integer result = (Integer)resData.get("result");
            String mes="";
            switch (result) {
                case 0:
                    mes = "无结果";
                    break;
                case 1:
                    mes = "窦性心率. 散点图: 基于⾮线性混沌理论,依靠计算机快速运算技术,对⻓程RR间期数据进⾏图形表达,⽤于⼼率变异";
                    break;
                case 2:
                    mes = "窦性心率过缓 散点图: 基于⾮线性混沌理论,依靠计算机快速运算技术,对⻓程RR间期数据进⾏图形表达,⽤于⼼率变异";
                    break;
                case 3:
                    mes = "窦性心率过速 散点图: 基于⾮线性混沌理论,依靠计算机快速运算技术,对⻓程RR间期数据进⾏图形表达,⽤于⼼率变异";
                    break;
                case 4:
                    mes = "心律不齐（早搏）散点图: 基于⾮线性混沌理论,依靠计算机快速运算技术,对⻓程RR间期数据进⾏图形表达,⽤于⼼率变异";
                    break;
                case 5:
                    mes = "心律不齐（房颤）散点图: 基于⾮线性混沌理论,依靠计算机快速运算技术,对⻓程RR间期数据进⾏图形表达,⽤于⼼率变异";
                    break;
                case 6:
                    mes = "心率过速（室上性心动过速）散点图: 基于⾮线性混沌理论,依靠计算机快速运算技术,对⻓程RR间期数据进⾏图形表达,⽤于⼼率变异";
                    break;
            }
            map.put("mes", mes);
            map.put("result", resData);
        }
        return map;
    }

    /**
     * 查询某天某个设备的房颤数据
     * @param date
     * @param deviceId
     * @return
     */
    public List<WatchEcgData> getByDateAndDeviceId(String date, String deviceId) {
        if (StringUtils.isBlank(date)){
            date = DateUtils.dateTimeNow("yyyy-MM-dd");
        }
        return mapper.getByDateAndDeviceId(date, deviceId);
    }
}
