package com.yihu.jw.util.healthIndex;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.stereotype.Component;

/**
 * Created by Bing on 2021/7/20.
 * 体征数据
 */
@Component
public class HealthIndexUtil {
    // 血糖餐前最小值
    public static final double HEALTH_STANDARD_ST_MIN_BEFORE = 4;
    // 血糖餐前最大值
    public static final double HEALTH_STANDARD_ST_MAX_BEFORE = 7;
    // 血糖餐后最小值
    public static final double HEALTH_STANDARD_ST_MIN_AFTER = 4;
    // 血糖餐后最大值
    public static final double HEALTH_STANDARD_ST_MAX_AFTER = 11.1;

    // 舒张压最小值
    public static final double HEALTH_STANDARD_SZY_MIN = 60;
    // 舒张压最大值
    public static final double HEALTH_STANDARD_SZY_MAX = 90;
    // 收缩压最小值
    public static final double HEALTH_STANDARD_SSY_MIN = 90;
    // 收缩压最大值
    public static final double HEALTH_STANDARD_SSY_MAX = 140;

    //最小值
    public static final double HEALTH_STANDARD_HEART_RATE_MIN = 60;
    //最大值
    public static final double HEALTH_STANDARD_HEART_RATE_MAX = 100;


    /**
     * type:1血糖2血压，3体重/身高/BMI，4腰围,5心率,6体温
     * @return
     */
    public JSONArray verifyHealthIndex(Integer type, String ...values) {
        //血糖校验
        JSONArray result = new JSONArray();
        JSONObject tmp = new JSONObject();
        if (1 ==type) {
            // 血糖记录，查询患者血糖预警值
            Double maxValueBefore = HEALTH_STANDARD_ST_MAX_BEFORE;
            Double minValueBefore = HEALTH_STANDARD_ST_MIN_BEFORE;
            Double maxValueAfter = HEALTH_STANDARD_ST_MAX_AFTER;
            Double minValueAfter = HEALTH_STANDARD_ST_MIN_AFTER;

            int index = Integer.valueOf(values[1]);
            String value1 = values[0];
            // 餐后
            if (StringUtils.isNotBlank(value1)){
                if (index % 2 == 0) {
                    tmp.put("value1",value1);
                    tmp.put("indexName","血糖");
                    tmp.put("error",checkHealthIndex(NumberUtils.toDouble(value1), maxValueAfter, minValueAfter));

                } else { //餐前
                    tmp.put("value1",value1);
                    tmp.put("indexName","血糖");
                    tmp.put("error",checkHealthIndex(NumberUtils.toDouble(value1), maxValueBefore, minValueBefore));
                }
                result.add(tmp);
            }
        }
        //血压校验
        else if (type == 2) {
            String value1 = values[0];//收缩压
            String value2 = values[1];//舒张压
            String value3 = values[2];//脉搏

            // 血压记录，查询患者血压预警值
            Double maxValueSSY = HEALTH_STANDARD_SSY_MAX;
            Double minValueSSY = HEALTH_STANDARD_SSY_MIN;
            Double maxValueSZY = HEALTH_STANDARD_SZY_MAX;
            Double minValueSZY = HEALTH_STANDARD_SZY_MIN;
            // 收缩压/舒张压校验
            if (StringUtils.isNotBlank(value1)){
                tmp.put("value1",value1);
                tmp.put("indexName","收缩压");
                tmp.put("error",checkHealthIndex(NumberUtils.toDouble(value1), maxValueSSY, minValueSSY));
                result.add(tmp);
            }
            //舒张压校验
            if (StringUtils.isNotBlank(value2)){
                tmp = new JSONObject();
                tmp.put("value1",value2);
                tmp.put("indexName","舒张压");
                tmp.put("error",checkHealthIndex(NumberUtils.toDouble(value2), maxValueSZY, minValueSZY));
                result.add(tmp);
                tmp = new JSONObject();
            }
            //脉搏检验
            if (StringUtils.isNotBlank(value3)){
                tmp.put("value1",value3);
                tmp.put("indexName","脉搏");
                tmp.put("error",checkHealthIndex(NumberUtils.toDouble(value3), HEALTH_STANDARD_HEART_RATE_MAX, HEALTH_STANDARD_HEART_RATE_MIN));
                result.add(tmp);
            }
        }
        else if (type == 5) {//心率
            String value1 = values[0];//心率
            if (StringUtils.isNotBlank(value1)){
                tmp.put("value1",value1);
                tmp.put("indexName","心率");
                tmp.put("error",checkHealthIndex(NumberUtils.toDouble(value1), HEALTH_STANDARD_HEART_RATE_MAX, HEALTH_STANDARD_HEART_RATE_MIN));
                result.add(tmp);
            }
        }
        return result;
    }

    /**
     * 判断当前值是否在区间内
     */
    private Integer checkHealthIndex(Double current, Double max, Double min) {
        if (current > max  ) {
            return 1;//指标过高
        }else if(current < min || current < 0) {
            return -1;//指标过低
        }
        else {//正常
            return 0;
        }
    }
}
