package com.ruoyi.system.timing;

import com.ruoyi.system.domain.*;
import com.ruoyi.system.domain.dto.*;
import com.ruoyi.system.mapper.TDeviceListMapper;
import com.ruoyi.system.mapper.TShouhuanAlarmListMapper;
import com.ruoyi.system.service.ITEarlyWarningService;
import com.ruoyi.system.service.ITUserProfileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component("callPoliceTiming")
public class CallPoliceTiming {

    @Autowired
    private TDeviceListMapper tDeviceListMapper;

    @Autowired
    private TShouhuanAlarmListMapper tShouhuanAlarmListMapper;

    @Autowired
    private ITEarlyWarningService itEarlyWarningService;
    @Autowired
    private ITUserProfileService itUserProfileService;

    public void checkCallPolice() {
        //获取手环预警信息
        List<AlarmStatusListDto> alarmStatusListDtos = tShouhuanAlarmListMapper.selectAlarmStatusListDto();
        //遍历是否进行预警
        for (AlarmStatusListDto info : alarmStatusListDtos) {
            //判断是否为空
            if (info != null) {
                //已经处理过的不处理
                List<TEarlyWarning> tEarlyWarnings = itEarlyWarningService.selectTEarlyWarningByAlarmId(info.getId());
                if (tEarlyWarnings != null && !tEarlyWarnings.isEmpty()) {
                    continue;
                }
                TEarlyWarning earlyWarning = new TEarlyWarning();
                //查找对应的手环绑定用户
                List<TDeviceList> deviceList = tDeviceListMapper.selectTDeviceByDeviceIdCode(info.getDevice_id_code());
                //绑定用户ID
                if (deviceList != null && !deviceList.isEmpty()) {
                    earlyWarning.setUserId(deviceList.get(0).getUserid().longValue());
                }
                //状态：0-未处理；1-处理中；2-已处理
                earlyWarning.setState(0);
                // 类型：0-其他；1-SOS；2-跌倒；3-围栏；4-健康
                earlyWarning.setType(info.getAlarm_type());
                //告警内容
                Map<String, String> map = getContentByType(info.getAlarm_type(), info.getCreatetime(), earlyWarning.getUserId());
                earlyWarning.setTypeDesc(map.get("type_desc"));
                earlyWarning.setContent(map.get("content"));
                //事件记录
                earlyWarning.setEventRecord("");
                //预警定位
                earlyWarning.setLocation(info.getLocation());
                //经度
                earlyWarning.setLng(info.getUse_gps_long());
                //维度
                earlyWarning.setLat(info.getUse_gps_lat());
                //设备ID
                earlyWarning.setFacilityId(info.getFacilityId().longValue());
                //设置id编码
                earlyWarning.setDeviceIdCode(info.getDevice_id_code());
                //预警表ID
                earlyWarning.setAlarmId(info.getId());
                itEarlyWarningService.insertTEarlyWarning(earlyWarning);
            }
        }
    }

    /**
     * 根据类型获取内容
     *
     * @param alarmType
     * @param createtime
     * @param userId
     * @return
     */
    private Map<String, String> getContentByType(Integer alarmType, Date createtime, Long userId) {
        //健康告警求助：成浪平于2023-09-27 15:17:02发起健康告警求助
        //设备对应的用户
        TUserProfile tUserProfile = null;
        if (userId != null && userId > 0) {
            tUserProfile = itUserProfileService.selectTUserProfileById(userId.intValue());
        }
        // 创建一个SimpleDateFormat对象，指定日期格式
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 使用SimpleDateFormat对象将日期格式化为指定格式的字符串
        String time = dateFormat.format(createtime);
        StringBuilder stringBuilder = new StringBuilder();
        String typeDesc = "";
        if (alarmType != null) {
            if (alarmType == 0) {
                typeDesc = "无效报警";
                stringBuilder.append(typeDesc).append("!");
            } else if (alarmType == 16) {
                //16:SOS报警;
                typeDesc = "SOS报警";
                stringBuilder.append(typeDesc).append("求助：");
                if (tUserProfile != null) {
                    stringBuilder.append(tUserProfile.getName());
                } else {
                    stringBuilder.append("设备");
                }
                stringBuilder.append("于").append(time).append("发起SOS报警求助！");
            } else if (alarmType == 17) {
                //17:低电报警;
                typeDesc = "低电报警";
                stringBuilder.append(typeDesc).append("：");
                if (tUserProfile != null) {
                    stringBuilder.append(tUserProfile.getName());
                } else {
                    stringBuilder.append("设备");
                }
                stringBuilder.append("于").append(time).append("发起低电报警！");
            } else if (alarmType == 20) {
                //20:手环拆除报警;
                typeDesc = "手环拆除报警";
                stringBuilder.append(typeDesc).append("：");
                if (tUserProfile != null) {
                    stringBuilder.append(tUserProfile.getName());
                } else {
                    stringBuilder.append("设备");
                }
                stringBuilder.append("于").append(time).append("发起手环拆除报警！");
            } else if (alarmType == 21) {
                //21:跌倒报警;
                typeDesc = "跌倒报警";
                stringBuilder.append(typeDesc).append("：");
                if (tUserProfile != null) {
                    stringBuilder.append(tUserProfile.getName());
                } else {
                    stringBuilder.append("设备");
                }
                stringBuilder.append("于").append(time).append("发起跌倒报警求助！");
            } else if (alarmType == 22) {
                //22:心率异常报警,
                typeDesc = "心率异常报警";
                stringBuilder.append(typeDesc).append("：");
                if (tUserProfile != null) {
                    stringBuilder.append(tUserProfile.getName());
                } else {
                    stringBuilder.append("设备");
                }
                stringBuilder.append("于").append(time).append("发起心率异常报警求助！");
            } else if (alarmType == 23) {
                //23:血压异常报警，
                typeDesc = "血压异常报警";
                stringBuilder.append(typeDesc).append("：");
                if (tUserProfile != null) {
                    stringBuilder.append(tUserProfile.getName());
                } else {
                    stringBuilder.append("设备");
                }
                stringBuilder.append("于").append(time).append("发起血压异常报警求助！");
            } else if (alarmType == 24) {
                //24:体温异常报警
                typeDesc = "体温异常报警";
                stringBuilder.append(typeDesc).append("：");
                if (tUserProfile != null) {
                    stringBuilder.append(tUserProfile.getName());
                } else {
                    stringBuilder.append("设备");
                }
                stringBuilder.append("于").append(time).append("发起体温异常报警求助！");
            } else if (alarmType == 25) {
                //25:血氧异常报警
                typeDesc = "血氧异常报警";
                stringBuilder.append(typeDesc).append("：");
                if (tUserProfile != null) {
                    stringBuilder.append(tUserProfile.getName());
                } else {
                    stringBuilder.append("设备");
                }
                stringBuilder.append("于").append(time).append("发起血氧异常报警求助！");
            } else {
                typeDesc = "无效报警";
                stringBuilder.append(typeDesc).append("!");
            }
        } else {
            typeDesc = "无效报警";
            stringBuilder.append(typeDesc).append("!");
        }
        Map<String, String> map = new HashMap<>();
        map.put("content", stringBuilder.toString());
        map.put("type_desc", typeDesc);
        return map;
    }

}
