package com.ruoyi.system.timing;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.TShouhuanAlarmList;
import com.ruoyi.system.domain.TShouhuanInfo;
import com.ruoyi.system.domain.dto.SettingDto;
import com.ruoyi.system.mapper.TDeviceListMapper;
import com.ruoyi.system.mapper.TShouhuanAlarmListMapper;
import com.ruoyi.system.mapper.TShouhuanInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component("callPoliceTiming")
public class CallPoliceTiming {

    @Autowired
    private TDeviceListMapper tDeviceListMapper;

    @Autowired
    private TShouhuanInfoMapper tShouhuanInfoMapper;

    @Autowired
    private TShouhuanAlarmListMapper tShouhuanAlarmListMapper;

    //终端主动发
    Integer alarmFromType = 1;
    //平台记录
    Integer alarmFromTypelog = 0;
    //心率异常报警
    Integer heartalarmType = 22;
    //血压异常报警
    Integer bloodalarmType = 23;
    //体温异常报警
    Integer tempalarmType = 24;
    //血氧异常报警
    Integer oxygenalarmType = 25;

    public void checkCallPolice(){

        List<TShouhuanInfo> tShouhuanInfos = tShouhuanInfoMapper.selectTShouhuanInfo();
        String shouhuan_body_max = tDeviceListMapper.selectshouhuan("shouhuan_body_max");
        String shouhuan_blood_high_max = tDeviceListMapper.selectshouhuan("shouhuan_blood_high_max");
        String shouhuan_blood_high_min = tDeviceListMapper.selectshouhuan("shouhuan_blood_high_min");
        String shouhuan_heart_max = tDeviceListMapper.selectshouhuan("shouhuan_heart_max");
        String shouhuan_oxygen_max = tDeviceListMapper.selectshouhuan("shouhuan_oxygen_max");

        for (TShouhuanInfo info : tShouhuanInfos){
            if (info.getTemp()>Integer.parseInt(shouhuan_body_max)){
                tShouhuanAlarmListMapper.insertTShouhuanAlarmList(saveInfo(info,tempalarmType,alarmFromType));
            }
            if(info.getBloodHeightPressure()<Integer.parseInt(shouhuan_blood_high_max)){
                tShouhuanAlarmListMapper.insertTShouhuanAlarmList(saveInfo(info,bloodalarmType,alarmFromType));
            }
            if(info.getBloodLowPressure()>Integer.parseInt(shouhuan_blood_high_min)){
                tShouhuanAlarmListMapper.insertTShouhuanAlarmList(saveInfo(info,bloodalarmType,alarmFromType));
            }
            if(info.getHeartRate()>Integer.parseInt(shouhuan_heart_max)){
                tShouhuanAlarmListMapper.insertTShouhuanAlarmList(saveInfo(info,heartalarmType,alarmFromType));
            }
            if(info.getOxygen()>Integer.parseInt(shouhuan_oxygen_max)){
                tShouhuanAlarmListMapper.insertTShouhuanAlarmList(saveInfo(info,oxygenalarmType,alarmFromType));
            }
        }
    }

    public TShouhuanAlarmList saveInfo(TShouhuanInfo info, Integer heartalarmType, Integer alarmFromType){
        TShouhuanAlarmList tShouhuanAlarmList = new TShouhuanAlarmList();
        if(StringUtils.isNotNull(info.getFacilityId())){
            tShouhuanAlarmList.setFacilityId(info.getFacilityId());
        }
        if(StringUtils.isNotNull(info.getId())){
            tShouhuanAlarmList.setShinfoId(info.getId());
        }
        if(StringUtils.isNotNull(info.getDeviceIdCode())){
            tShouhuanAlarmList.setDeviceIdCode(info.getDeviceIdCode());
        }
        if(StringUtils.isNotNull(info.getGpsLong())){
            tShouhuanAlarmList.setGpsLong(info.getGpsLong());
        }
        if(StringUtils.isNotNull(info.getGpsLat())){
            tShouhuanAlarmList.setGpsLat(info.getGpsLat());
        }
        if(StringUtils.isNotNull(info.getSpeed())){
            tShouhuanAlarmList.setSpeed(info.getSpeed());
        }
        if(StringUtils.isNotNull(info.getDirection())){
            tShouhuanAlarmList.setDirection(info.getDirection());
        }
        if(StringUtils.isNotNull(info.getPoster())){
            tShouhuanAlarmList.setPoster(info.getPoster());
        }
        if(StringUtils.isNotNull(info.getElectricQuantity())){
            tShouhuanAlarmList.setElectricQuantity(info.getElectricQuantity());
        }
        if(StringUtils.isNotNull(info.getStepNumber())){
            tShouhuanAlarmList.setStepNumber(info.getStepNumber());
        }
        if(StringUtils.isNotNull(info.getRollNumber())){
            tShouhuanAlarmList.setRollNumber(info.getRollNumber());
        }
        if(StringUtils.isNotNull(heartalarmType)){
            tShouhuanAlarmList.setAlarmType(heartalarmType);
        }
        if(StringUtils.isNotNull(info.getTerminalStatus())){
            tShouhuanAlarmList.setTerminalStatus(info.getTerminalStatus());
        }
        tShouhuanAlarmList.setCreateTime(DateUtils.getNowDate());
        if(StringUtils.isNotNull(alarmFromType)){
            tShouhuanAlarmList.setAlarmFromType(alarmFromType);
        }
        return tShouhuanAlarmList;
    }
}
